cmGlobalXCodeGenerator.cxx 112 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmGlobalXCodeGenerator.h"
  11. #include "cmLocalXCodeGenerator.h"
  12. #include "cmMakefile.h"
  13. #include "cmXCodeObject.h"
  14. #include "cmXCode21Object.h"
  15. #include "cmake.h"
  16. #include "cmGeneratedFileStream.h"
  17. #include "cmComputeLinkInformation.h"
  18. #include "cmSourceFile.h"
  19. #include <cmsys/auto_ptr.hxx>
  20. //----------------------------------------------------------------------------
  21. #if defined(CMAKE_BUILD_WITH_CMAKE)
  22. #include "cmXMLParser.h"
  23. // parse the xml file storing the installed version of Xcode on
  24. // the machine
  25. class cmXcodeVersionParser : public cmXMLParser
  26. {
  27. public:
  28. cmXcodeVersionParser(): Version("1.5") {}
  29. void StartElement(const char* , const char** )
  30. {
  31. this->Data = "";
  32. }
  33. void EndElement(const char* name)
  34. {
  35. if(strcmp(name, "key") == 0)
  36. {
  37. this->Key = this->Data;
  38. }
  39. else if(strcmp(name, "string") == 0)
  40. {
  41. if(this->Key == "CFBundleShortVersionString")
  42. {
  43. this->Version = this->Data;
  44. }
  45. }
  46. }
  47. void CharacterDataHandler(const char* data, int length)
  48. {
  49. this->Data.append(data, length);
  50. }
  51. std::string Version;
  52. std::string Key;
  53. std::string Data;
  54. };
  55. #endif
  56. // Builds either an object list or a space-separated string from the
  57. // given inputs.
  58. class cmGlobalXCodeGenerator::BuildObjectListOrString
  59. {
  60. cmGlobalXCodeGenerator *Generator;
  61. cmXCodeObject *Group;
  62. bool Empty;
  63. std::string String;
  64. public:
  65. BuildObjectListOrString(cmGlobalXCodeGenerator *gen, bool buildObjectList)
  66. : Generator(gen), Group(0), Empty(true)
  67. {
  68. if (buildObjectList)
  69. {
  70. this->Group = this->Generator->CreateObject(cmXCodeObject::OBJECT_LIST);
  71. }
  72. }
  73. bool IsEmpty() const { return this->Empty; }
  74. void Add(const char *newString)
  75. {
  76. this->Empty = false;
  77. if (this->Group)
  78. {
  79. this->Group->AddObject(this->Generator->CreateString(newString));
  80. }
  81. else
  82. {
  83. this->String += newString;
  84. this->String += ' ';
  85. }
  86. }
  87. const std::string &GetString() const { return this->String; }
  88. cmXCodeObject *CreateList()
  89. {
  90. if (this->Group)
  91. {
  92. return this->Group;
  93. }
  94. else
  95. {
  96. return this->Generator->CreateString(this->String.c_str());
  97. }
  98. }
  99. };
  100. //----------------------------------------------------------------------------
  101. cmGlobalXCodeGenerator::cmGlobalXCodeGenerator(std::string const& version)
  102. {
  103. this->VersionString = version;
  104. // Compute an integer form of the version number.
  105. unsigned int v[2] = {0,0};
  106. sscanf(this->VersionString.c_str(), "%u.%u", &v[0], &v[1]);
  107. this->XcodeVersion = 10*v[0] + v[1];
  108. this->FindMakeProgramFile = "CMakeFindXCode.cmake";
  109. this->RootObject = 0;
  110. this->MainGroupChildren = 0;
  111. this->SourcesGroupChildren = 0;
  112. this->ResourcesGroupChildren = 0;
  113. this->CurrentMakefile = 0;
  114. this->CurrentLocalGenerator = 0;
  115. }
  116. //----------------------------------------------------------------------------
  117. cmGlobalGenerator* cmGlobalXCodeGenerator::New()
  118. {
  119. #if defined(CMAKE_BUILD_WITH_CMAKE)
  120. cmXcodeVersionParser parser;
  121. parser.ParseFile
  122. ("/Developer/Applications/Xcode.app/Contents/version.plist");
  123. cmsys::auto_ptr<cmGlobalXCodeGenerator>
  124. gg(new cmGlobalXCodeGenerator(parser.Version));
  125. if (gg->XcodeVersion == 20)
  126. {
  127. cmSystemTools::Message("Xcode 2.0 not really supported by cmake, "
  128. "using Xcode 15 generator\n");
  129. gg->XcodeVersion = 15;
  130. }
  131. return gg.release();
  132. #else
  133. std::cerr << "CMake should be built with cmake to use XCode, "
  134. "default to Xcode 1.5\n";
  135. return new cmGlobalXCodeGenerator;
  136. #endif
  137. }
  138. //----------------------------------------------------------------------------
  139. void cmGlobalXCodeGenerator::EnableLanguage(std::vector<std::string>const&
  140. lang,
  141. cmMakefile * mf, bool optional)
  142. {
  143. mf->AddDefinition("XCODE","1");
  144. mf->AddDefinition("XCODE_VERSION", this->VersionString.c_str());
  145. if(this->XcodeVersion == 15)
  146. {
  147. }
  148. else
  149. {
  150. mf->AddCacheDefinition(
  151. "CMAKE_CONFIGURATION_TYPES",
  152. "Debug;Release;MinSizeRel;RelWithDebInfo",
  153. "Semicolon separated list of supported configuration types, "
  154. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  155. "anything else will be ignored.",
  156. cmCacheManager::STRING);
  157. }
  158. mf->AddDefinition("CMAKE_GENERATOR_CC", "gcc");
  159. mf->AddDefinition("CMAKE_GENERATOR_CXX", "g++");
  160. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  161. // initialize Architectures so it can be used by
  162. // GetTargetObjectFileDirectories
  163. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  164. const char* osxArch =
  165. mf->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  166. const char* sysroot =
  167. mf->GetDefinition("CMAKE_OSX_SYSROOT");
  168. if(osxArch && sysroot)
  169. {
  170. this->Architectures.clear();
  171. cmSystemTools::ExpandListArgument(std::string(osxArch),
  172. this->Architectures);
  173. }
  174. }
  175. //----------------------------------------------------------------------------
  176. std::string cmGlobalXCodeGenerator
  177. ::GenerateBuildCommand(const char* makeProgram,
  178. const char *projectName,
  179. const char* additionalOptions,
  180. const char *targetName,
  181. const char* config,
  182. bool ignoreErrors,
  183. bool)
  184. {
  185. // Config is not used yet
  186. (void) ignoreErrors;
  187. // now build the test
  188. if(makeProgram == 0 || !strlen(makeProgram))
  189. {
  190. cmSystemTools::Error(
  191. "Generator cannot find the appropriate make command.");
  192. return "";
  193. }
  194. std::string makeCommand =
  195. cmSystemTools::ConvertToOutputPath(makeProgram);
  196. std::string lowerCaseCommand = makeCommand;
  197. cmSystemTools::LowerCase(lowerCaseCommand);
  198. makeCommand += " -project ";
  199. makeCommand += projectName;
  200. makeCommand += ".xcode";
  201. if(this->XcodeVersion > 20)
  202. {
  203. makeCommand += "proj";
  204. }
  205. bool clean = false;
  206. if ( targetName && strcmp(targetName, "clean") == 0 )
  207. {
  208. clean = true;
  209. targetName = "ALL_BUILD";
  210. }
  211. if(clean)
  212. {
  213. makeCommand += " clean";
  214. }
  215. else
  216. {
  217. makeCommand += " build";
  218. }
  219. makeCommand += " -target ";
  220. // if it is a null string for config don't use it
  221. if(config && *config == 0)
  222. {
  223. config = 0;
  224. }
  225. if (targetName && strlen(targetName))
  226. {
  227. makeCommand += targetName;
  228. }
  229. else
  230. {
  231. makeCommand += "ALL_BUILD";
  232. }
  233. if(this->XcodeVersion == 15)
  234. {
  235. makeCommand += " -buildstyle Development ";
  236. }
  237. else
  238. {
  239. makeCommand += " -configuration ";
  240. makeCommand += config?config:"Debug";
  241. }
  242. if ( additionalOptions )
  243. {
  244. makeCommand += " ";
  245. makeCommand += additionalOptions;
  246. }
  247. return makeCommand;
  248. }
  249. //----------------------------------------------------------------------------
  250. ///! Create a local generator appropriate to this Global Generator
  251. cmLocalGenerator *cmGlobalXCodeGenerator::CreateLocalGenerator()
  252. {
  253. cmLocalGenerator *lg = new cmLocalXCodeGenerator;
  254. lg->SetGlobalGenerator(this);
  255. return lg;
  256. }
  257. //----------------------------------------------------------------------------
  258. void cmGlobalXCodeGenerator::Generate()
  259. {
  260. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  261. // make sure extra targets are added before calling
  262. // the parent generate which will call trace depends
  263. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  264. {
  265. cmLocalGenerator* root = it->second[0];
  266. this->SetGenerationRoot(root);
  267. // add ALL_BUILD, INSTALL, etc
  268. this->AddExtraTargets(root, it->second);
  269. }
  270. this->ForceLinkerLanguages();
  271. this->cmGlobalGenerator::Generate();
  272. for(it = this->ProjectMap.begin(); it!= this->ProjectMap.end(); ++it)
  273. {
  274. cmLocalGenerator* root = it->second[0];
  275. this->SetGenerationRoot(root);
  276. // now create the project
  277. this->OutputXCodeProject(root, it->second);
  278. }
  279. }
  280. //----------------------------------------------------------------------------
  281. void cmGlobalXCodeGenerator::SetGenerationRoot(cmLocalGenerator* root)
  282. {
  283. this->CurrentProject = root->GetMakefile()->GetProjectName();
  284. this->SetCurrentLocalGenerator(root);
  285. cmSystemTools::SplitPath(this->CurrentMakefile->GetCurrentDirectory(),
  286. this->ProjectSourceDirectoryComponents);
  287. cmSystemTools::SplitPath(this->CurrentMakefile->GetCurrentOutputDirectory(),
  288. this->ProjectOutputDirectoryComponents);
  289. this->CurrentXCodeHackMakefile =
  290. root->GetMakefile()->GetCurrentOutputDirectory();
  291. this->CurrentXCodeHackMakefile += "/CMakeScripts";
  292. cmSystemTools::MakeDirectory(this->CurrentXCodeHackMakefile.c_str());
  293. this->CurrentXCodeHackMakefile += "/XCODE_DEPEND_HELPER.make";
  294. }
  295. //----------------------------------------------------------------------------
  296. void
  297. cmGlobalXCodeGenerator::AddExtraTargets(cmLocalGenerator* root,
  298. std::vector<cmLocalGenerator*>& gens)
  299. {
  300. cmMakefile* mf = root->GetMakefile();
  301. // Add ALL_BUILD
  302. const char* no_working_directory = 0;
  303. std::vector<std::string> no_depends;
  304. mf->AddUtilityCommand("ALL_BUILD", true, no_depends,
  305. no_working_directory,
  306. "echo", "Build all projects");
  307. cmTarget* allbuild = mf->FindTarget("ALL_BUILD");
  308. // Refer to the main build configuration file for easy editing.
  309. std::string listfile = mf->GetStartDirectory();
  310. listfile += "/";
  311. listfile += "CMakeLists.txt";
  312. allbuild->AddSource(listfile.c_str());
  313. // Add XCODE depend helper
  314. std::string dir = mf->GetCurrentOutputDirectory();
  315. cmCustomCommandLine makecommand;
  316. makecommand.push_back("make");
  317. makecommand.push_back("-C");
  318. makecommand.push_back(dir.c_str());
  319. makecommand.push_back("-f");
  320. makecommand.push_back(this->CurrentXCodeHackMakefile.c_str());
  321. if(this->XcodeVersion > 20)
  322. {
  323. makecommand.push_back("all.$(CONFIGURATION)");
  324. }
  325. cmCustomCommandLines commandLines;
  326. commandLines.push_back(makecommand);
  327. // Add Re-Run CMake rules
  328. this->CreateReRunCMakeFile(root);
  329. // now make the allbuild depend on all the non-utility targets
  330. // in the project
  331. for(std::vector<cmLocalGenerator*>::iterator i = gens.begin();
  332. i != gens.end(); ++i)
  333. {
  334. cmLocalGenerator* lg = *i;
  335. if(this->IsExcluded(root, *i))
  336. {
  337. continue;
  338. }
  339. cmTargets& tgts = lg->GetMakefile()->GetTargets();
  340. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  341. {
  342. cmTarget& target = l->second;
  343. // make all exe, shared libs and modules
  344. // run the depend check makefile as a post build rule
  345. // this will make sure that when the next target is built
  346. // things are up-to-date
  347. if((target.GetType() == cmTarget::EXECUTABLE ||
  348. target.GetType() == cmTarget::STATIC_LIBRARY ||
  349. target.GetType() == cmTarget::SHARED_LIBRARY ||
  350. target.GetType() == cmTarget::MODULE_LIBRARY))
  351. {
  352. lg->GetMakefile()->AddCustomCommandToTarget(target.GetName(),
  353. no_depends,
  354. commandLines,
  355. cmTarget::POST_BUILD,
  356. "Depend check for xcode",
  357. dir.c_str());
  358. }
  359. if(!target.GetPropertyAsBool("EXCLUDE_FROM_ALL"))
  360. {
  361. allbuild->AddUtility(target.GetName());
  362. }
  363. // Refer to the build configuration file for easy editing.
  364. listfile = lg->GetMakefile()->GetStartDirectory();
  365. listfile += "/";
  366. listfile += "CMakeLists.txt";
  367. target.AddSource(listfile.c_str());
  368. }
  369. }
  370. }
  371. //----------------------------------------------------------------------------
  372. void cmGlobalXCodeGenerator::CreateReRunCMakeFile(cmLocalGenerator* root)
  373. {
  374. cmMakefile* mf = root->GetMakefile();
  375. std::vector<std::string> lfiles = mf->GetListFiles();
  376. // sort the array
  377. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  378. std::vector<std::string>::iterator new_end =
  379. std::unique(lfiles.begin(), lfiles.end());
  380. lfiles.erase(new_end, lfiles.end());
  381. std::string dir = mf->GetHomeOutputDirectory();
  382. this->CurrentReRunCMakeMakefile = dir;
  383. this->CurrentReRunCMakeMakefile += "/CMakeScripts";
  384. cmSystemTools::MakeDirectory(this->CurrentReRunCMakeMakefile.c_str());
  385. this->CurrentReRunCMakeMakefile += "/ReRunCMake.make";
  386. cmGeneratedFileStream makefileStream
  387. (this->CurrentReRunCMakeMakefile.c_str());
  388. makefileStream.SetCopyIfDifferent(true);
  389. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  390. makefileStream << cmake::GetCMakeFilesDirectoryPostSlash();
  391. makefileStream << "cmake.check_cache: ";
  392. for(std::vector<std::string>::const_iterator i = lfiles.begin();
  393. i != lfiles.end(); ++i)
  394. {
  395. makefileStream << "\\\n" << this->ConvertToRelativeForMake(i->c_str());
  396. }
  397. std::string cmake = mf->GetRequiredDefinition("CMAKE_COMMAND");
  398. makefileStream << "\n\t" << this->ConvertToRelativeForMake(cmake.c_str())
  399. << " -H" << this->ConvertToRelativeForMake(
  400. mf->GetHomeDirectory())
  401. << " -B" << this->ConvertToRelativeForMake(
  402. mf->GetHomeOutputDirectory()) << "\n";
  403. }
  404. //----------------------------------------------------------------------------
  405. void cmGlobalXCodeGenerator::ClearXCodeObjects()
  406. {
  407. this->TargetDoneSet.clear();
  408. for(unsigned int i = 0; i < this->XCodeObjects.size(); ++i)
  409. {
  410. delete this->XCodeObjects[i];
  411. }
  412. this->XCodeObjects.clear();
  413. this->GroupMap.clear();
  414. this->GroupNameMap.clear();
  415. this->TargetGroup.clear();
  416. this->FileRefs.clear();
  417. }
  418. //----------------------------------------------------------------------------
  419. cmXCodeObject*
  420. cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::PBXType ptype)
  421. {
  422. cmXCodeObject* obj;
  423. if(this->XcodeVersion == 15)
  424. {
  425. obj = new cmXCodeObject(ptype, cmXCodeObject::OBJECT);
  426. }
  427. else
  428. {
  429. obj = new cmXCode21Object(ptype, cmXCodeObject::OBJECT);
  430. }
  431. this->XCodeObjects.push_back(obj);
  432. return obj;
  433. }
  434. //----------------------------------------------------------------------------
  435. cmXCodeObject*
  436. cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
  437. {
  438. cmXCodeObject* obj = new cmXCodeObject(cmXCodeObject::None, type);
  439. this->XCodeObjects.push_back(obj);
  440. return obj;
  441. }
  442. //----------------------------------------------------------------------------
  443. cmXCodeObject*
  444. cmGlobalXCodeGenerator::CreateString(const char* s)
  445. {
  446. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::STRING);
  447. obj->SetString(s);
  448. return obj;
  449. }
  450. //----------------------------------------------------------------------------
  451. cmXCodeObject* cmGlobalXCodeGenerator
  452. ::CreateObjectReference(cmXCodeObject* ref)
  453. {
  454. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::OBJECT_REF);
  455. obj->SetObject(ref);
  456. return obj;
  457. }
  458. //----------------------------------------------------------------------------
  459. cmStdString GetGroupMapKey(cmTarget& cmtarget, cmSourceFile* sf)
  460. {
  461. cmStdString key(cmtarget.GetName());
  462. key += "-";
  463. key += sf->GetFullPath();
  464. return key;
  465. }
  466. //----------------------------------------------------------------------------
  467. cmXCodeObject*
  468. cmGlobalXCodeGenerator::CreateXCodeSourceFile(cmLocalGenerator* lg,
  469. cmSourceFile* sf,
  470. cmTarget& cmtarget)
  471. {
  472. // Add flags from target and source file properties.
  473. std::string flags;
  474. if(cmtarget.GetProperty("COMPILE_FLAGS"))
  475. {
  476. lg->AppendFlags(flags, cmtarget.GetProperty("COMPILE_FLAGS"));
  477. }
  478. lg->AppendFlags(flags, sf->GetProperty("COMPILE_FLAGS"));
  479. // Add per-source definitions.
  480. BuildObjectListOrString flagsBuild(this, false);
  481. this->AppendDefines(flagsBuild,
  482. sf->GetProperty("COMPILE_DEFINITIONS"), true);
  483. if (!flagsBuild.IsEmpty())
  484. {
  485. if (flags.size())
  486. {
  487. flags += ' ';
  488. }
  489. flags += flagsBuild.GetString();
  490. }
  491. // Using a map and the full path guarantees that we will always get the same
  492. // fileRef object for any given full path.
  493. //
  494. cmXCodeObject* fileRef = this->CreateXCodeFileReference(sf, cmtarget);
  495. cmXCodeObject* buildFile = this->CreateObject(cmXCodeObject::PBXBuildFile);
  496. buildFile->SetComment(fileRef->GetComment());
  497. buildFile->AddAttribute("fileRef", this->CreateObjectReference(fileRef));
  498. cmXCodeObject* settings =
  499. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  500. settings->AddAttribute("COMPILER_FLAGS", this->CreateString(flags.c_str()));
  501. // Is this a resource file in this target? Add it to the resources group...
  502. //
  503. cmTarget::SourceFileFlags tsFlags = cmtarget.GetTargetSourceFileFlags(sf);
  504. bool isResource = (tsFlags.Type == cmTarget::SourceFileTypeResource);
  505. // Is this a "private" or "public" framework header file?
  506. // Set the ATTRIBUTES attribute appropriately...
  507. //
  508. if(cmtarget.IsFrameworkOnApple())
  509. {
  510. if(tsFlags.Type == cmTarget::SourceFileTypePrivateHeader)
  511. {
  512. cmXCodeObject* attrs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  513. attrs->AddObject(this->CreateString("Private"));
  514. settings->AddAttribute("ATTRIBUTES", attrs);
  515. isResource = true;
  516. }
  517. else if(tsFlags.Type == cmTarget::SourceFileTypePublicHeader)
  518. {
  519. cmXCodeObject* attrs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  520. attrs->AddObject(this->CreateString("Public"));
  521. settings->AddAttribute("ATTRIBUTES", attrs);
  522. isResource = true;
  523. }
  524. }
  525. // Add the fileRef to the top level Resources group/folder if it is not
  526. // already there.
  527. //
  528. if(isResource && this->ResourcesGroupChildren &&
  529. !this->ResourcesGroupChildren->HasObject(fileRef))
  530. {
  531. this->ResourcesGroupChildren->AddObject(fileRef);
  532. }
  533. buildFile->AddAttribute("settings", settings);
  534. return buildFile;
  535. }
  536. //----------------------------------------------------------------------------
  537. cmXCodeObject*
  538. cmGlobalXCodeGenerator::CreateXCodeFileReference(cmSourceFile* sf,
  539. cmTarget& cmtarget)
  540. {
  541. std::string fname = sf->GetFullPath();
  542. cmXCodeObject* fileRef = this->FileRefs[fname];
  543. if(!fileRef)
  544. {
  545. fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  546. std::string comment = fname;
  547. //comment += " in ";
  548. //std::string gname = group->GetObject("name")->GetString();
  549. //comment += gname.substr(1, gname.size()-2);
  550. fileRef->SetComment(fname.c_str());
  551. this->FileRefs[fname] = fileRef;
  552. }
  553. cmStdString key = GetGroupMapKey(cmtarget, sf);
  554. cmXCodeObject* group = this->GroupMap[key];
  555. cmXCodeObject* children = group->GetObject("children");
  556. if (!children->HasObject(fileRef))
  557. {
  558. children->AddObject(fileRef);
  559. }
  560. fileRef->AddAttribute("fileEncoding", this->CreateString("4"));
  561. const char* lang =
  562. this->CurrentLocalGenerator->GetSourceFileLanguage(*sf);
  563. std::string sourcecode = "sourcecode";
  564. std::string ext = sf->GetExtension();
  565. ext = cmSystemTools::LowerCase(ext);
  566. if(ext == "o")
  567. {
  568. sourcecode = "compiled.mach-o.objfile";
  569. }
  570. else if(ext == "xib")
  571. {
  572. sourcecode = "file.xib";
  573. }
  574. else if(ext == "mm")
  575. {
  576. sourcecode += ".cpp.objcpp";
  577. }
  578. else if(ext == "m")
  579. {
  580. sourcecode += ".c.objc";
  581. }
  582. else if(ext == "xib")
  583. {
  584. sourcecode += ".file.xib";
  585. }
  586. else if(ext == "plist")
  587. {
  588. sourcecode += ".text.plist";
  589. }
  590. else if(ext == "h")
  591. {
  592. sourcecode += ".c.h";
  593. }
  594. else if(ext == "hxx" || ext == "hpp" || ext == "txx"
  595. || ext == "pch")
  596. {
  597. sourcecode += ".cpp.h";
  598. }
  599. else if(lang && strcmp(lang, "CXX") == 0)
  600. {
  601. sourcecode += ".cpp.cpp";
  602. }
  603. else if(lang && strcmp(lang, "C") == 0)
  604. {
  605. sourcecode += ".c.c";
  606. }
  607. else if(lang && strcmp(lang, "Fortran") == 0)
  608. {
  609. sourcecode += ".fortran.f90";
  610. }
  611. else if(ext == "png" || ext == "gif" || ext == "jpg")
  612. {
  613. sourcecode = "image";
  614. }
  615. else if(ext == "txt")
  616. {
  617. sourcecode += ".text";
  618. }
  619. //else
  620. // {
  621. // // Already specialized above or we leave sourcecode == "sourcecode"
  622. // // which is probably the most correct choice. Extensionless headers,
  623. // // for example... Or file types unknown to Xcode that do not map to a
  624. // // valid lastKnownFileType value.
  625. // }
  626. fileRef->AddAttribute("lastKnownFileType",
  627. this->CreateString(sourcecode.c_str()));
  628. // Store the file path relative to the top of the source tree.
  629. std::string path = this->RelativeToSource(sf->GetFullPath().c_str());
  630. std::string name = cmSystemTools::GetFilenameName(path.c_str());
  631. const char* sourceTree = (cmSystemTools::FileIsFullPath(path.c_str())?
  632. "<absolute>" : "SOURCE_ROOT");
  633. fileRef->AddAttribute("name", this->CreateString(name.c_str()));
  634. fileRef->AddAttribute("path", this->CreateString(path.c_str()));
  635. fileRef->AddAttribute("sourceTree", this->CreateString(sourceTree));
  636. if(this->XcodeVersion == 15)
  637. {
  638. fileRef->AddAttribute("refType", this->CreateString("4"));
  639. }
  640. return fileRef;
  641. }
  642. //----------------------------------------------------------------------------
  643. bool cmGlobalXCodeGenerator::SpecialTargetEmitted(std::string const& tname)
  644. {
  645. if(tname == "ALL_BUILD" || tname == "XCODE_DEPEND_HELPER" ||
  646. tname == "install" || tname == "package" || tname == "RUN_TESTS" )
  647. {
  648. if(this->TargetDoneSet.find(tname) != this->TargetDoneSet.end())
  649. {
  650. return true;
  651. }
  652. this->TargetDoneSet.insert(tname);
  653. return false;
  654. }
  655. return false;
  656. }
  657. //----------------------------------------------------------------------------
  658. void cmGlobalXCodeGenerator::SetCurrentLocalGenerator(cmLocalGenerator* gen)
  659. {
  660. this->CurrentLocalGenerator = gen;
  661. this->CurrentMakefile = gen->GetMakefile();
  662. std::string outdir =
  663. cmSystemTools::CollapseFullPath(this->CurrentMakefile->
  664. GetCurrentOutputDirectory());
  665. cmSystemTools::SplitPath(outdir.c_str(),
  666. this->CurrentOutputDirectoryComponents);
  667. // Select the current set of configuration types.
  668. this->CurrentConfigurationTypes.clear();
  669. if(this->XcodeVersion > 20)
  670. {
  671. if(const char* types =
  672. this->CurrentMakefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  673. {
  674. cmSystemTools::ExpandListArgument(types,
  675. this->CurrentConfigurationTypes);
  676. }
  677. }
  678. if(this->CurrentConfigurationTypes.empty())
  679. {
  680. if(const char* buildType =
  681. this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE"))
  682. {
  683. this->CurrentConfigurationTypes.push_back(buildType);
  684. }
  685. else
  686. {
  687. this->CurrentConfigurationTypes.push_back("");
  688. }
  689. }
  690. }
  691. //----------------------------------------------------------------------------
  692. void
  693. cmGlobalXCodeGenerator::CreateXCodeTargets(cmLocalGenerator* gen,
  694. std::vector<cmXCodeObject*>&
  695. targets)
  696. {
  697. this->SetCurrentLocalGenerator(gen);
  698. cmTargets &tgts = this->CurrentMakefile->GetTargets();
  699. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  700. {
  701. cmTarget& cmtarget = l->second;
  702. // make sure ALL_BUILD, INSTALL, etc are only done once
  703. if(this->SpecialTargetEmitted(l->first.c_str()))
  704. {
  705. continue;
  706. }
  707. if(cmtarget.GetType() == cmTarget::UTILITY ||
  708. cmtarget.GetType() == cmTarget::GLOBAL_TARGET)
  709. {
  710. targets.push_back(this->CreateUtilityTarget(cmtarget));
  711. continue;
  712. }
  713. // organize the sources
  714. std::vector<cmSourceFile*> const &classes = cmtarget.GetSourceFiles();
  715. std::vector<cmXCodeObject*> externalObjFiles;
  716. std::vector<cmXCodeObject*> headerFiles;
  717. std::vector<cmXCodeObject*> resourceFiles;
  718. std::vector<cmXCodeObject*> sourceFiles;
  719. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  720. i != classes.end(); ++i)
  721. {
  722. cmXCodeObject* xsf =
  723. this->CreateXCodeSourceFile(this->CurrentLocalGenerator,
  724. *i, cmtarget);
  725. cmXCodeObject* fr = xsf->GetObject("fileRef");
  726. cmXCodeObject* filetype =
  727. fr->GetObject()->GetObject("lastKnownFileType");
  728. cmTarget::SourceFileFlags tsFlags =
  729. cmtarget.GetTargetSourceFileFlags(*i);
  730. if(strcmp(filetype->GetString(), "compiled.mach-o.objfile") == 0)
  731. {
  732. externalObjFiles.push_back(xsf);
  733. }
  734. else if(this->IsHeaderFile(*i) ||
  735. (tsFlags.Type == cmTarget::SourceFileTypePrivateHeader) ||
  736. (tsFlags.Type == cmTarget::SourceFileTypePublicHeader))
  737. {
  738. headerFiles.push_back(xsf);
  739. }
  740. else if(tsFlags.Type == cmTarget::SourceFileTypeResource)
  741. {
  742. resourceFiles.push_back(xsf);
  743. }
  744. else if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  745. {
  746. // Include this file in the build if it has a known language
  747. // and has not been listed as an ignored extension for this
  748. // generator.
  749. if(this->CurrentLocalGenerator->GetSourceFileLanguage(**i) &&
  750. !this->IgnoreFile((*i)->GetExtension().c_str()))
  751. {
  752. sourceFiles.push_back(xsf);
  753. }
  754. }
  755. }
  756. // some build phases only apply to bundles and/or frameworks
  757. bool isFrameworkTarget = cmtarget.IsFrameworkOnApple();
  758. bool isBundleTarget = cmtarget.GetPropertyAsBool("MACOSX_BUNDLE");
  759. cmXCodeObject* buildFiles = 0;
  760. // create source build phase
  761. cmXCodeObject* sourceBuildPhase = 0;
  762. if (!sourceFiles.empty())
  763. {
  764. sourceBuildPhase =
  765. this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  766. sourceBuildPhase->SetComment("Sources");
  767. sourceBuildPhase->AddAttribute("buildActionMask",
  768. this->CreateString("2147483647"));
  769. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  770. for(std::vector<cmXCodeObject*>::iterator i = sourceFiles.begin();
  771. i != sourceFiles.end(); ++i)
  772. {
  773. buildFiles->AddObject(*i);
  774. }
  775. sourceBuildPhase->AddAttribute("files", buildFiles);
  776. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  777. this->CreateString("0"));
  778. }
  779. // create header build phase - only for framework targets
  780. cmXCodeObject* headerBuildPhase = 0;
  781. if (!headerFiles.empty() && isFrameworkTarget)
  782. {
  783. headerBuildPhase =
  784. this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  785. headerBuildPhase->SetComment("Headers");
  786. headerBuildPhase->AddAttribute("buildActionMask",
  787. this->CreateString("2147483647"));
  788. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  789. for(std::vector<cmXCodeObject*>::iterator i = headerFiles.begin();
  790. i != headerFiles.end(); ++i)
  791. {
  792. buildFiles->AddObject(*i);
  793. }
  794. headerBuildPhase->AddAttribute("files", buildFiles);
  795. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  796. this->CreateString("0"));
  797. }
  798. // create resource build phase - only for framework or bundle targets
  799. cmXCodeObject* resourceBuildPhase = 0;
  800. if (!resourceFiles.empty() && (isFrameworkTarget || isBundleTarget))
  801. {
  802. resourceBuildPhase =
  803. this->CreateObject(cmXCodeObject::PBXResourcesBuildPhase);
  804. resourceBuildPhase->SetComment("Resources");
  805. resourceBuildPhase->AddAttribute("buildActionMask",
  806. this->CreateString("2147483647"));
  807. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  808. for(std::vector<cmXCodeObject*>::iterator i = resourceFiles.begin();
  809. i != resourceFiles.end(); ++i)
  810. {
  811. buildFiles->AddObject(*i);
  812. }
  813. resourceBuildPhase->AddAttribute("files", buildFiles);
  814. resourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  815. this->CreateString("0"));
  816. }
  817. // create vector of "non-resource content file" build phases - only for
  818. // framework or bundle targets
  819. std::vector<cmXCodeObject*> contentBuildPhases;
  820. if (isFrameworkTarget || isBundleTarget)
  821. {
  822. typedef std::map<cmStdString, std::vector<cmSourceFile*> >
  823. mapOfVectorOfSourceFiles;
  824. mapOfVectorOfSourceFiles bundleFiles;
  825. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  826. i != classes.end(); ++i)
  827. {
  828. cmTarget::SourceFileFlags tsFlags =
  829. cmtarget.GetTargetSourceFileFlags(*i);
  830. if(tsFlags.Type == cmTarget::SourceFileTypeMacContent)
  831. {
  832. bundleFiles[tsFlags.MacFolder].push_back(*i);
  833. }
  834. }
  835. mapOfVectorOfSourceFiles::iterator mit;
  836. for ( mit = bundleFiles.begin(); mit != bundleFiles.end(); ++ mit )
  837. {
  838. cmXCodeObject* copyFilesBuildPhase =
  839. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  840. copyFilesBuildPhase->SetComment("Copy files");
  841. copyFilesBuildPhase->AddAttribute("buildActionMask",
  842. this->CreateString("2147483647"));
  843. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  844. this->CreateString("6"));
  845. cmOStringStream ostr;
  846. if (cmtarget.IsFrameworkOnApple())
  847. {
  848. // dstPath in frameworks is relative to Versions/<version>
  849. ostr << mit->first;
  850. }
  851. else if ( mit->first != "MacOS" )
  852. {
  853. // dstPath in bundles is relative to Contents/MacOS
  854. ostr << "../" << mit->first.c_str();
  855. }
  856. copyFilesBuildPhase->AddAttribute("dstPath",
  857. this->CreateString(ostr.str().c_str()));
  858. copyFilesBuildPhase->AddAttribute(
  859. "runOnlyForDeploymentPostprocessing", this->CreateString("0"));
  860. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  861. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  862. std::vector<cmSourceFile*>::iterator sfIt;
  863. for ( sfIt = mit->second.begin(); sfIt != mit->second.end(); ++ sfIt )
  864. {
  865. cmXCodeObject* xsf =
  866. this->CreateXCodeSourceFile(this->CurrentLocalGenerator,
  867. *sfIt, cmtarget);
  868. buildFiles->AddObject(xsf);
  869. }
  870. contentBuildPhases.push_back(copyFilesBuildPhase);
  871. }
  872. }
  873. // create framework build phase
  874. cmXCodeObject* frameworkBuildPhase = 0;
  875. if (!externalObjFiles.empty())
  876. {
  877. frameworkBuildPhase =
  878. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  879. frameworkBuildPhase->SetComment("Frameworks");
  880. frameworkBuildPhase->AddAttribute("buildActionMask",
  881. this->CreateString("2147483647"));
  882. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  883. frameworkBuildPhase->AddAttribute("files", buildFiles);
  884. for(std::vector<cmXCodeObject*>::iterator i = externalObjFiles.begin();
  885. i != externalObjFiles.end(); ++i)
  886. {
  887. buildFiles->AddObject(*i);
  888. }
  889. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  890. this->CreateString("0"));
  891. }
  892. // create list of build phases and create the XCode target
  893. cmXCodeObject* buildPhases =
  894. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  895. this->CreateCustomCommands(buildPhases, sourceBuildPhase,
  896. headerBuildPhase, resourceBuildPhase,
  897. contentBuildPhases,
  898. frameworkBuildPhase, cmtarget);
  899. targets.push_back(this->CreateXCodeTarget(cmtarget, buildPhases));
  900. }
  901. }
  902. //----------------------------------------------------------------------------
  903. void cmGlobalXCodeGenerator::ForceLinkerLanguages()
  904. {
  905. // This makes sure all targets link using the proper language.
  906. for(std::map<cmStdString, cmTarget*>::const_iterator
  907. ti = this->TotalTargets.begin(); ti != this->TotalTargets.end(); ++ti)
  908. {
  909. this->ForceLinkerLanguage(*ti->second);
  910. }
  911. }
  912. //----------------------------------------------------------------------------
  913. void cmGlobalXCodeGenerator::ForceLinkerLanguage(cmTarget& cmtarget)
  914. {
  915. // This matters only for targets that link.
  916. if(cmtarget.GetType() != cmTarget::EXECUTABLE &&
  917. cmtarget.GetType() != cmTarget::SHARED_LIBRARY &&
  918. cmtarget.GetType() != cmTarget::MODULE_LIBRARY)
  919. {
  920. return;
  921. }
  922. const char* llang = cmtarget.GetLinkerLanguage("NOCONFIG");
  923. if(!llang) { return; }
  924. // If the language is compiled as a source trust Xcode to link with it.
  925. cmTarget::LinkImplementation const* impl =
  926. cmtarget.GetLinkImplementation("NOCONFIG");
  927. for(std::vector<std::string>::const_iterator li = impl->Languages.begin();
  928. li != impl->Languages.end(); ++li)
  929. {
  930. if(*li == llang) { return; }
  931. }
  932. // Add an empty source file to the target that compiles with the
  933. // linker language. This should convince Xcode to choose the proper
  934. // language.
  935. cmMakefile* mf = cmtarget.GetMakefile();
  936. std::string fname = mf->GetCurrentOutputDirectory();
  937. fname += cmake::GetCMakeFilesDirectory();
  938. fname += "/";
  939. fname += cmtarget.GetName();
  940. fname += "-CMakeForceLinker";
  941. fname += ".";
  942. fname += cmSystemTools::LowerCase(llang);
  943. {
  944. cmGeneratedFileStream fout(fname.c_str());
  945. fout << "\n";
  946. }
  947. if(cmSourceFile* sf = mf->GetOrCreateSource(fname.c_str()))
  948. {
  949. sf->SetProperty("LANGUAGE", llang);
  950. cmtarget.AddSourceFile(sf);
  951. }
  952. }
  953. //----------------------------------------------------------------------------
  954. bool cmGlobalXCodeGenerator::IsHeaderFile(cmSourceFile* sf)
  955. {
  956. const std::vector<std::string>& hdrExts =
  957. this->CurrentMakefile->GetHeaderExtensions();
  958. return (std::find(hdrExts.begin(), hdrExts.end(), sf->GetExtension()) !=
  959. hdrExts.end());
  960. }
  961. //----------------------------------------------------------------------------
  962. cmXCodeObject*
  963. cmGlobalXCodeGenerator::CreateBuildPhase(const char* name,
  964. const char* name2,
  965. cmTarget& cmtarget,
  966. const std::vector<cmCustomCommand>&
  967. commands)
  968. {
  969. if(commands.size() == 0 && strcmp(name, "CMake ReRun") != 0)
  970. {
  971. return 0;
  972. }
  973. cmXCodeObject* buildPhase =
  974. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  975. buildPhase->AddAttribute("buildActionMask",
  976. this->CreateString("2147483647"));
  977. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  978. buildPhase->AddAttribute("files", buildFiles);
  979. buildPhase->AddAttribute("name",
  980. this->CreateString(name));
  981. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  982. this->CreateString("0"));
  983. buildPhase->AddAttribute("shellPath",
  984. this->CreateString("/bin/sh"));
  985. this->AddCommandsToBuildPhase(buildPhase, cmtarget, commands,
  986. name2);
  987. return buildPhase;
  988. }
  989. //----------------------------------------------------------------------------
  990. void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
  991. cmXCodeObject*
  992. sourceBuildPhase,
  993. cmXCodeObject*
  994. headerBuildPhase,
  995. cmXCodeObject*
  996. resourceBuildPhase,
  997. std::vector<cmXCodeObject*>
  998. contentBuildPhases,
  999. cmXCodeObject*
  1000. frameworkBuildPhase,
  1001. cmTarget& cmtarget)
  1002. {
  1003. std::vector<cmCustomCommand> const & prebuild
  1004. = cmtarget.GetPreBuildCommands();
  1005. std::vector<cmCustomCommand> const & prelink
  1006. = cmtarget.GetPreLinkCommands();
  1007. std::vector<cmCustomCommand> const & postbuild
  1008. = cmtarget.GetPostBuildCommands();
  1009. std::vector<cmSourceFile*>const &classes = cmtarget.GetSourceFiles();
  1010. // add all the sources
  1011. std::vector<cmCustomCommand> commands;
  1012. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  1013. i != classes.end(); ++i)
  1014. {
  1015. if((*i)->GetCustomCommand())
  1016. {
  1017. commands.push_back(*(*i)->GetCustomCommand());
  1018. }
  1019. }
  1020. std::vector<cmCustomCommand> reruncom;
  1021. cmXCodeObject* cmakeReRunPhase =
  1022. this->CreateBuildPhase("CMake ReRun", "cmakeReRunPhase",
  1023. cmtarget, reruncom);
  1024. buildPhases->AddObject(cmakeReRunPhase);
  1025. // create prebuild phase
  1026. cmXCodeObject* cmakeRulesBuildPhase =
  1027. this->CreateBuildPhase("CMake Rules",
  1028. "cmakeRulesBuildPhase",
  1029. cmtarget, commands);
  1030. // create prebuild phase
  1031. cmXCodeObject* preBuildPhase =
  1032. this->CreateBuildPhase("CMake PreBuild Rules", "preBuildCommands",
  1033. cmtarget, prebuild);
  1034. // create prelink phase
  1035. cmXCodeObject* preLinkPhase =
  1036. this->CreateBuildPhase("CMake PreLink Rules", "preLinkCommands",
  1037. cmtarget, prelink);
  1038. // create postbuild phase
  1039. cmXCodeObject* postBuildPhase =
  1040. this->CreateBuildPhase("CMake PostBuild Rules", "postBuildPhase",
  1041. cmtarget, postbuild);
  1042. // The order here is the order they will be built in.
  1043. // The order "headers, resources, sources" mimics a native project generated
  1044. // from an xcode template...
  1045. //
  1046. if(preBuildPhase)
  1047. {
  1048. buildPhases->AddObject(preBuildPhase);
  1049. }
  1050. if(cmakeRulesBuildPhase)
  1051. {
  1052. buildPhases->AddObject(cmakeRulesBuildPhase);
  1053. }
  1054. if(headerBuildPhase)
  1055. {
  1056. buildPhases->AddObject(headerBuildPhase);
  1057. }
  1058. if(resourceBuildPhase)
  1059. {
  1060. buildPhases->AddObject(resourceBuildPhase);
  1061. }
  1062. std::vector<cmXCodeObject*>::iterator cit;
  1063. for (cit = contentBuildPhases.begin(); cit != contentBuildPhases.end();
  1064. ++cit)
  1065. {
  1066. buildPhases->AddObject(*cit);
  1067. }
  1068. if(sourceBuildPhase)
  1069. {
  1070. buildPhases->AddObject(sourceBuildPhase);
  1071. }
  1072. if(preLinkPhase)
  1073. {
  1074. buildPhases->AddObject(preLinkPhase);
  1075. }
  1076. if(frameworkBuildPhase)
  1077. {
  1078. buildPhases->AddObject(frameworkBuildPhase);
  1079. }
  1080. if(postBuildPhase)
  1081. {
  1082. buildPhases->AddObject(postBuildPhase);
  1083. }
  1084. }
  1085. //----------------------------------------------------------------------------
  1086. std::string cmGlobalXCodeGenerator::ExtractFlag(const char* flag,
  1087. std::string& flags)
  1088. {
  1089. std::string retFlag;
  1090. std::string::size_type pos = flags.find(flag);
  1091. if(pos != flags.npos && (pos ==0 || flags[pos-1]==' '))
  1092. {
  1093. while(pos < flags.size() && flags[pos] != ' ')
  1094. {
  1095. retFlag += flags[pos];
  1096. flags[pos] = ' ';
  1097. pos++;
  1098. }
  1099. }
  1100. return retFlag;
  1101. }
  1102. //----------------------------------------------------------------------------
  1103. void
  1104. cmGlobalXCodeGenerator::AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  1105. cmTarget& target,
  1106. std::vector<cmCustomCommand>
  1107. const & commands,
  1108. const char* name)
  1109. {
  1110. if(strcmp(name, "cmakeReRunPhase") == 0)
  1111. {
  1112. std::string cdir = this->CurrentMakefile->GetHomeOutputDirectory();
  1113. cdir = this->ConvertToRelativeForMake(cdir.c_str());
  1114. std::string makecmd = "make -C ";
  1115. makecmd += cdir;
  1116. makecmd += " -f ";
  1117. makecmd +=
  1118. this->ConvertToRelativeForMake(this->CurrentReRunCMakeMakefile.c_str());
  1119. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  1120. buildphase->AddAttribute("shellScript",
  1121. this->CreateString(makecmd.c_str()));
  1122. return;
  1123. }
  1124. // collect multiple outputs of custom commands into a set
  1125. // which will be used for every configuration
  1126. std::map<cmStdString, cmStdString> multipleOutputPairs;
  1127. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1128. i != commands.end(); ++i)
  1129. {
  1130. cmCustomCommand const& cc = *i;
  1131. if(!cc.GetCommandLines().empty())
  1132. {
  1133. const std::vector<std::string>& outputs = cc.GetOutputs();
  1134. if(!outputs.empty())
  1135. {
  1136. // If there are more than one outputs treat the
  1137. // first as the primary output and make the rest depend on it.
  1138. std::vector<std::string>::const_iterator o = outputs.begin();
  1139. std::string primaryOutput = this->ConvertToRelativeForMake(o->c_str());
  1140. for(++o; o != outputs.end(); ++o)
  1141. {
  1142. std::string currentOutput=this->ConvertToRelativeForMake(o->c_str());
  1143. multipleOutputPairs[currentOutput] = primaryOutput;
  1144. }
  1145. }
  1146. }
  1147. }
  1148. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1149. dir += "/CMakeScripts";
  1150. cmSystemTools::MakeDirectory(dir.c_str());
  1151. std::string makefile = dir;
  1152. makefile += "/";
  1153. makefile += target.GetName();
  1154. makefile += "_";
  1155. makefile += name;
  1156. makefile += ".make";
  1157. for (std::vector<std::string>::const_iterator currentConfig=
  1158. this->CurrentConfigurationTypes.begin();
  1159. currentConfig!=this->CurrentConfigurationTypes.end();
  1160. currentConfig++ )
  1161. {
  1162. this->CreateCustomRulesMakefile(makefile.c_str(),
  1163. target,
  1164. commands,
  1165. currentConfig->c_str(),
  1166. multipleOutputPairs);
  1167. }
  1168. std::string cdir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1169. cdir = this->ConvertToRelativeForXCode(cdir.c_str());
  1170. std::string makecmd = "make -C ";
  1171. makecmd += cdir;
  1172. makecmd += " -f ";
  1173. makecmd += this->ConvertToRelativeForMake(
  1174. (makefile+"$CONFIGURATION").c_str());
  1175. if(!multipleOutputPairs.empty())
  1176. {
  1177. makecmd += " cmake_check_multiple_outputs";
  1178. }
  1179. makecmd += " all";
  1180. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  1181. buildphase->AddAttribute("shellScript",
  1182. this->CreateString(makecmd.c_str()));
  1183. }
  1184. //----------------------------------------------------------------------------
  1185. void cmGlobalXCodeGenerator
  1186. ::CreateCustomRulesMakefile(const char* makefileBasename,
  1187. cmTarget& target,
  1188. std::vector<cmCustomCommand>
  1189. const & commands,
  1190. const char* configName,
  1191. const std::map<cmStdString,
  1192. cmStdString>& multipleOutputPairs
  1193. )
  1194. {
  1195. std::string makefileName=makefileBasename;
  1196. if(this->XcodeVersion > 20)
  1197. {
  1198. makefileName+=configName;
  1199. }
  1200. cmGeneratedFileStream makefileStream(makefileName.c_str());
  1201. if(!makefileStream)
  1202. {
  1203. return;
  1204. }
  1205. makefileStream.SetCopyIfDifferent(true);
  1206. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  1207. makefileStream << "# Custom rules for " << target.GetName() << "\n";
  1208. // have all depend on all outputs
  1209. makefileStream << "all: ";
  1210. std::map<const cmCustomCommand*, cmStdString> tname;
  1211. int count = 0;
  1212. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1213. i != commands.end(); ++i)
  1214. {
  1215. cmCustomCommand const& cc = *i;
  1216. if(!cc.GetCommandLines().empty())
  1217. {
  1218. const std::vector<std::string>& outputs = cc.GetOutputs();
  1219. if(!outputs.empty())
  1220. {
  1221. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1222. o != outputs.end(); ++o)
  1223. {
  1224. makefileStream
  1225. << "\\\n\t" << this->ConvertToRelativeForMake(o->c_str());
  1226. }
  1227. }
  1228. else
  1229. {
  1230. cmOStringStream str;
  1231. str << "_buildpart_" << count++ ;
  1232. tname[&cc] = std::string(target.GetName()) + str.str();
  1233. makefileStream << "\\\n\t" << tname[&cc];
  1234. }
  1235. }
  1236. }
  1237. makefileStream << "\n\n";
  1238. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1239. i != commands.end(); ++i)
  1240. {
  1241. cmCustomCommand const& cc = *i;
  1242. if(!cc.GetCommandLines().empty())
  1243. {
  1244. bool escapeOldStyle = cc.GetEscapeOldStyle();
  1245. bool escapeAllowMakeVars = cc.GetEscapeAllowMakeVars();
  1246. makefileStream << "\n";
  1247. const std::vector<std::string>& outputs = cc.GetOutputs();
  1248. if(!outputs.empty())
  1249. {
  1250. // There is at least one output, start the rule for it
  1251. std::string primary_output =
  1252. this->ConvertToRelativeForMake(outputs.begin()->c_str());
  1253. makefileStream << primary_output << ": ";
  1254. }
  1255. else
  1256. {
  1257. // There are no outputs. Use the generated force rule name.
  1258. makefileStream << tname[&cc] << ": ";
  1259. }
  1260. for(std::vector<std::string>::const_iterator d =
  1261. cc.GetDepends().begin();
  1262. d != cc.GetDepends().end(); ++d)
  1263. {
  1264. std::string dep =
  1265. this->CurrentLocalGenerator->GetRealDependency(d->c_str(),
  1266. configName);
  1267. makefileStream << "\\\n" << this
  1268. ->ConvertToRelativeForMake(dep.c_str());
  1269. }
  1270. makefileStream << "\n";
  1271. if(const char* comment = cc.GetComment())
  1272. {
  1273. std::string echo_cmd = "echo ";
  1274. echo_cmd += (this->CurrentLocalGenerator->
  1275. EscapeForShell(comment, escapeAllowMakeVars));
  1276. makefileStream << "\t" << echo_cmd.c_str() << "\n";
  1277. }
  1278. // Add each command line to the set of commands.
  1279. for(cmCustomCommandLines::const_iterator cl =
  1280. cc.GetCommandLines().begin();
  1281. cl != cc.GetCommandLines().end(); ++cl)
  1282. {
  1283. // Build the command line in a single string.
  1284. const cmCustomCommandLine& commandLine = *cl;
  1285. std::string cmd2 = this->CurrentLocalGenerator
  1286. ->GetRealLocation(commandLine[0].c_str(), configName);
  1287. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  1288. cmd2 = this->ConvertToRelativeForMake(cmd2.c_str());
  1289. std::string cmd;
  1290. if(cc.GetWorkingDirectory())
  1291. {
  1292. cmd += "cd ";
  1293. cmd += this->ConvertToRelativeForMake(cc.GetWorkingDirectory());
  1294. cmd += " && ";
  1295. }
  1296. cmd += cmd2;
  1297. for(unsigned int j=1; j < commandLine.size(); ++j)
  1298. {
  1299. cmd += " ";
  1300. if(escapeOldStyle)
  1301. {
  1302. cmd += (this->CurrentLocalGenerator
  1303. ->EscapeForShellOldStyle(commandLine[j].c_str()));
  1304. }
  1305. else
  1306. {
  1307. cmd += (this->CurrentLocalGenerator->
  1308. EscapeForShell(commandLine[j].c_str(),
  1309. escapeAllowMakeVars));
  1310. }
  1311. }
  1312. makefileStream << "\t" << cmd.c_str() << "\n";
  1313. }
  1314. }
  1315. }
  1316. // Add rules to deal with multiple outputs of custom commands.
  1317. if(!multipleOutputPairs.empty())
  1318. {
  1319. makefileStream <<
  1320. "\n# Dependencies of multiple outputs to their primary outputs \n";
  1321. for(std::map<cmStdString, cmStdString>::const_iterator o =
  1322. multipleOutputPairs.begin(); o != multipleOutputPairs.end(); ++o)
  1323. {
  1324. makefileStream << o->first << ": " << o->second << "\n";
  1325. }
  1326. makefileStream <<
  1327. "\n"
  1328. "cmake_check_multiple_outputs:\n";
  1329. for(std::map<cmStdString, cmStdString>::const_iterator o =
  1330. multipleOutputPairs.begin(); o != multipleOutputPairs.end(); ++o)
  1331. {
  1332. makefileStream << "\t@if [ ! -f "
  1333. << o->first << " ]; then rm -f "
  1334. << o->second << "; fi\n";
  1335. }
  1336. }
  1337. }
  1338. //----------------------------------------------------------------------------
  1339. void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
  1340. cmXCodeObject* buildSettings,
  1341. const char* configName)
  1342. {
  1343. std::string flags;
  1344. std::string defFlags;
  1345. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  1346. (target.GetType() == cmTarget::MODULE_LIBRARY));
  1347. const char* lang = target.GetLinkerLanguage(configName);
  1348. std::string cflags;
  1349. if(lang)
  1350. {
  1351. // for c++ projects get the c flags as well
  1352. if(strcmp(lang, "CXX") == 0)
  1353. {
  1354. this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
  1355. this->CurrentLocalGenerator->AddSharedFlags(cflags, lang, shared);
  1356. }
  1357. // Add language-specific flags.
  1358. this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
  1359. // Add shared-library flags if needed.
  1360. this->CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
  1361. }
  1362. // Add define flags
  1363. this->CurrentLocalGenerator->
  1364. AppendFlags(defFlags,
  1365. this->CurrentMakefile->GetDefineFlags());
  1366. // Add preprocessor definitions for this target and configuration.
  1367. BuildObjectListOrString ppDefs(this, this->XcodeVersion >= 30);
  1368. if(this->XcodeVersion > 15)
  1369. {
  1370. this->AppendDefines(ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)\"");
  1371. }
  1372. if(const char* exportMacro = target.GetExportMacro())
  1373. {
  1374. // Add the export symbol definition for shared library objects.
  1375. this->AppendDefines(ppDefs, exportMacro);
  1376. }
  1377. this->AppendDefines
  1378. (ppDefs, this->CurrentMakefile->GetProperty("COMPILE_DEFINITIONS"));
  1379. this->AppendDefines(ppDefs, target.GetProperty("COMPILE_DEFINITIONS"));
  1380. if(configName)
  1381. {
  1382. std::string defVarName = "COMPILE_DEFINITIONS_";
  1383. defVarName += cmSystemTools::UpperCase(configName);
  1384. this->AppendDefines
  1385. (ppDefs, this->CurrentMakefile->GetProperty(defVarName.c_str()));
  1386. this->AppendDefines(ppDefs, target.GetProperty(defVarName.c_str()));
  1387. }
  1388. buildSettings->AddAttribute
  1389. ("GCC_PREPROCESSOR_DEFINITIONS", ppDefs.CreateList());
  1390. std::string extraLinkOptions;
  1391. if(target.GetType() == cmTarget::EXECUTABLE)
  1392. {
  1393. extraLinkOptions =
  1394. this->CurrentMakefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1395. std::string var = "CMAKE_EXE_LINKER_FLAGS_";
  1396. var += cmSystemTools::UpperCase(configName);
  1397. std::string val =
  1398. this->CurrentMakefile->GetSafeDefinition(var.c_str());
  1399. if(val.size())
  1400. {
  1401. extraLinkOptions += " ";
  1402. extraLinkOptions += val;
  1403. }
  1404. }
  1405. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1406. {
  1407. extraLinkOptions = this->CurrentMakefile->
  1408. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1409. }
  1410. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1411. {
  1412. extraLinkOptions = this->CurrentMakefile->
  1413. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1414. }
  1415. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1416. if(targetLinkFlags)
  1417. {
  1418. extraLinkOptions += " ";
  1419. extraLinkOptions += targetLinkFlags;
  1420. }
  1421. if(configName && *configName)
  1422. {
  1423. std::string linkFlagsVar = "LINK_FLAGS_";
  1424. linkFlagsVar += cmSystemTools::UpperCase(configName);
  1425. if(const char* linkFlags = target.GetProperty(linkFlagsVar.c_str()))
  1426. {
  1427. extraLinkOptions += " ";
  1428. extraLinkOptions += linkFlags;
  1429. }
  1430. }
  1431. // Set target-specific architectures.
  1432. std::vector<std::string> archs;
  1433. target.GetAppleArchs(configName, archs);
  1434. if(!archs.empty())
  1435. {
  1436. // Enable ARCHS attribute.
  1437. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH",
  1438. this->CreateString("NO"));
  1439. // Store ARCHS value.
  1440. if(archs.size() == 1)
  1441. {
  1442. buildSettings->AddAttribute("ARCHS",
  1443. this->CreateString(archs[0].c_str()));
  1444. }
  1445. else
  1446. {
  1447. cmXCodeObject* archObjects =
  1448. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1449. for(std::vector<std::string>::iterator i = archs.begin();
  1450. i != archs.end(); i++)
  1451. {
  1452. archObjects->AddObject(this->CreateString((*i).c_str()));
  1453. }
  1454. buildSettings->AddAttribute("ARCHS", archObjects);
  1455. }
  1456. }
  1457. // Get the product name components.
  1458. std::string pnprefix;
  1459. std::string pnbase;
  1460. std::string pnsuffix;
  1461. target.GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  1462. // Store the product name for all target types.
  1463. buildSettings->AddAttribute("PRODUCT_NAME",
  1464. this->CreateString(pnbase.c_str()));
  1465. // Set attributes to specify the proper name for the target.
  1466. std::string pndir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1467. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1468. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1469. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1470. target.GetType() == cmTarget::EXECUTABLE)
  1471. {
  1472. if(this->XcodeVersion >= 21)
  1473. {
  1474. std::string pncdir = target.GetDirectory(configName);
  1475. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  1476. this->CreateString(pncdir.c_str()));
  1477. }
  1478. else
  1479. {
  1480. buildSettings->AddAttribute("OBJROOT",
  1481. this->CreateString(pndir.c_str()));
  1482. pndir = target.GetDirectory(configName);
  1483. }
  1484. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1485. this->CreateString(pnprefix.c_str()));
  1486. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1487. this->CreateString(pnsuffix.c_str()));
  1488. }
  1489. buildSettings->AddAttribute("SYMROOT",
  1490. this->CreateString(pndir.c_str()));
  1491. // Handle settings for each target type.
  1492. switch(target.GetType())
  1493. {
  1494. case cmTarget::STATIC_LIBRARY:
  1495. {
  1496. buildSettings->AddAttribute("LIBRARY_STYLE",
  1497. this->CreateString("STATIC"));
  1498. break;
  1499. }
  1500. case cmTarget::MODULE_LIBRARY:
  1501. {
  1502. buildSettings->AddAttribute("LIBRARY_STYLE",
  1503. this->CreateString("BUNDLE"));
  1504. if(this->XcodeVersion >= 22)
  1505. {
  1506. buildSettings->AddAttribute("MACH_O_TYPE",
  1507. this->CreateString("mh_bundle"));
  1508. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1509. this->CreateString("NO"));
  1510. // Add the flags to create an executable.
  1511. std::string createFlags =
  1512. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1513. if(!createFlags.empty())
  1514. {
  1515. extraLinkOptions += " ";
  1516. extraLinkOptions += createFlags;
  1517. }
  1518. }
  1519. else
  1520. {
  1521. // Add the flags to create a module.
  1522. std::string createFlags =
  1523. this->LookupFlags("CMAKE_SHARED_MODULE_CREATE_", lang, "_FLAGS",
  1524. "-bundle");
  1525. if(!createFlags.empty())
  1526. {
  1527. extraLinkOptions += " ";
  1528. extraLinkOptions += createFlags;
  1529. }
  1530. }
  1531. break;
  1532. }
  1533. case cmTarget::SHARED_LIBRARY:
  1534. {
  1535. if(target.GetPropertyAsBool("FRAMEWORK"))
  1536. {
  1537. std::string version = target.GetFrameworkVersion();
  1538. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1539. this->CreateString(version.c_str()));
  1540. std::string plist = this->ComputeInfoPListLocation(target);
  1541. // Xcode will create the final version of Info.plist at build time,
  1542. // so let it replace the framework name. This avoids creating
  1543. // a per-configuration Info.plist file.
  1544. this->CurrentLocalGenerator
  1545. ->GenerateFrameworkInfoPList(&target, "$(EXECUTABLE_NAME)",
  1546. plist.c_str());
  1547. std::string path =
  1548. this->ConvertToRelativeForXCode(plist.c_str());
  1549. buildSettings->AddAttribute("INFOPLIST_FILE",
  1550. this->CreateString(path.c_str()));
  1551. }
  1552. else
  1553. {
  1554. // Add the flags to create a shared library.
  1555. std::string createFlags =
  1556. this->LookupFlags("CMAKE_SHARED_LIBRARY_CREATE_", lang, "_FLAGS",
  1557. "-dynamiclib");
  1558. if(!createFlags.empty())
  1559. {
  1560. extraLinkOptions += " ";
  1561. extraLinkOptions += createFlags;
  1562. }
  1563. }
  1564. buildSettings->AddAttribute("LIBRARY_STYLE",
  1565. this->CreateString("DYNAMIC"));
  1566. break;
  1567. }
  1568. case cmTarget::EXECUTABLE:
  1569. {
  1570. // Add the flags to create an executable.
  1571. std::string createFlags =
  1572. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1573. if(!createFlags.empty())
  1574. {
  1575. extraLinkOptions += " ";
  1576. extraLinkOptions += createFlags;
  1577. }
  1578. // Handle bundles and normal executables separately.
  1579. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1580. {
  1581. std::string plist = this->ComputeInfoPListLocation(target);
  1582. // Xcode will create the final version of Info.plist at build time,
  1583. // so let it replace the executable name. This avoids creating
  1584. // a per-configuration Info.plist file.
  1585. this->CurrentLocalGenerator
  1586. ->GenerateAppleInfoPList(&target, "$(EXECUTABLE_NAME)",
  1587. plist.c_str());
  1588. std::string path =
  1589. this->ConvertToRelativeForXCode(plist.c_str());
  1590. buildSettings->AddAttribute("INFOPLIST_FILE",
  1591. this->CreateString(path.c_str()));
  1592. }
  1593. }
  1594. break;
  1595. default:
  1596. break;
  1597. }
  1598. if(this->XcodeVersion >= 22)
  1599. {
  1600. buildSettings->AddAttribute("PREBINDING",
  1601. this->CreateString("NO"));
  1602. }
  1603. BuildObjectListOrString dirs(this, this->XcodeVersion >= 30);
  1604. BuildObjectListOrString fdirs(this, this->XcodeVersion >= 30);
  1605. std::vector<std::string> includes;
  1606. this->CurrentLocalGenerator->GetIncludeDirectories(includes);
  1607. std::set<cmStdString> emitted;
  1608. emitted.insert("/System/Library/Frameworks");
  1609. for(std::vector<std::string>::iterator i = includes.begin();
  1610. i != includes.end(); ++i)
  1611. {
  1612. if(this->NameResolvesToFramework(i->c_str()))
  1613. {
  1614. std::string frameworkDir = *i;
  1615. frameworkDir += "/../";
  1616. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1617. if(emitted.insert(frameworkDir).second)
  1618. {
  1619. fdirs.Add(this->XCodeEscapePath(frameworkDir.c_str()).c_str());
  1620. }
  1621. }
  1622. else
  1623. {
  1624. std::string incpath =
  1625. this->XCodeEscapePath(i->c_str());
  1626. dirs.Add(incpath.c_str());
  1627. }
  1628. }
  1629. std::vector<std::string>& frameworks = target.GetFrameworks();
  1630. if(frameworks.size())
  1631. {
  1632. for(std::vector<std::string>::iterator fmIt = frameworks.begin();
  1633. fmIt != frameworks.end(); ++fmIt)
  1634. {
  1635. if(emitted.insert(*fmIt).second)
  1636. {
  1637. fdirs.Add(this->XCodeEscapePath(fmIt->c_str()).c_str());
  1638. }
  1639. }
  1640. }
  1641. if(!fdirs.IsEmpty())
  1642. {
  1643. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS",
  1644. fdirs.CreateList());
  1645. }
  1646. if(!dirs.IsEmpty())
  1647. {
  1648. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  1649. dirs.CreateList());
  1650. }
  1651. std::string oflagc = this->ExtractFlag("-O", cflags);
  1652. char optLevel[2];
  1653. optLevel[0] = '0';
  1654. optLevel[1] = 0;
  1655. if(oflagc.size() == 3)
  1656. {
  1657. optLevel[0] = oflagc[2];
  1658. }
  1659. if(oflagc.size() == 2)
  1660. {
  1661. optLevel[0] = '1';
  1662. }
  1663. std::string oflag = this->ExtractFlag("-O", flags);
  1664. if(oflag.size() == 3)
  1665. {
  1666. optLevel[0] = oflag[2];
  1667. }
  1668. if(oflag.size() == 2)
  1669. {
  1670. optLevel[0] = '1';
  1671. }
  1672. std::string gflagc = this->ExtractFlag("-g", cflags);
  1673. // put back gdwarf-2 if used since there is no way
  1674. // to represent it in the gui, but we still want debug yes
  1675. if(gflagc == "-gdwarf-2")
  1676. {
  1677. cflags += " ";
  1678. cflags += gflagc;
  1679. }
  1680. std::string gflag = this->ExtractFlag("-g", flags);
  1681. if(gflag == "-gdwarf-2")
  1682. {
  1683. flags += " ";
  1684. flags += gflag;
  1685. }
  1686. const char* debugStr = "YES";
  1687. if(gflagc.size() ==0 && gflag.size() == 0)
  1688. {
  1689. debugStr = "NO";
  1690. }
  1691. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1692. this->CreateString(debugStr));
  1693. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1694. this->CreateString(optLevel));
  1695. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1696. this->CreateString("NO"));
  1697. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1698. this->CreateString("NO"));
  1699. if(lang && strcmp(lang, "CXX") == 0)
  1700. {
  1701. flags += " ";
  1702. flags += defFlags;
  1703. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1704. this->CreateString(flags.c_str()));
  1705. cflags += " ";
  1706. cflags += defFlags;
  1707. buildSettings->AddAttribute("OTHER_CFLAGS",
  1708. this->CreateString(cflags.c_str()));
  1709. }
  1710. else
  1711. {
  1712. flags += " ";
  1713. flags += defFlags;
  1714. buildSettings->AddAttribute("OTHER_CFLAGS",
  1715. this->CreateString(flags.c_str()));
  1716. }
  1717. // Create the INSTALL_PATH attribute.
  1718. std::string install_name_dir;
  1719. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1720. {
  1721. // Get the install_name directory for the build tree.
  1722. install_name_dir = target.GetInstallNameDirForBuildTree(configName, true);
  1723. if(install_name_dir.empty())
  1724. {
  1725. // Xcode will not pass the -install_name option at all if INSTALL_PATH
  1726. // is not given or is empty. We must explicitly put the flag in the
  1727. // link flags to create an install_name with just the library soname.
  1728. extraLinkOptions += " -install_name ";
  1729. extraLinkOptions += target.GetFullName(configName);
  1730. }
  1731. else
  1732. {
  1733. // Convert to a path for the native build tool.
  1734. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1735. // do not escape spaces on this since it is only a single path
  1736. }
  1737. }
  1738. buildSettings->AddAttribute("INSTALL_PATH",
  1739. this->CreateString(install_name_dir.c_str()));
  1740. buildSettings->AddAttribute("OTHER_LDFLAGS",
  1741. this->CreateString(extraLinkOptions.c_str()));
  1742. buildSettings->AddAttribute("OTHER_REZFLAGS",
  1743. this->CreateString(""));
  1744. buildSettings->AddAttribute("SECTORDER_FLAGS",
  1745. this->CreateString(""));
  1746. buildSettings->AddAttribute("USE_HEADERMAP",
  1747. this->CreateString("NO"));
  1748. if (this->XcodeVersion >= 30)
  1749. {
  1750. cmXCodeObject *group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1751. group->AddObject(this->CreateString("-Wmost"));
  1752. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  1753. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  1754. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  1755. }
  1756. else
  1757. {
  1758. buildSettings->AddAttribute("WARNING_CFLAGS",
  1759. this->CreateString(
  1760. "-Wmost -Wno-four-char-constants"
  1761. " -Wno-unknown-pragmas"));
  1762. }
  1763. // Runtime version information.
  1764. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1765. {
  1766. int major;
  1767. int minor;
  1768. int patch;
  1769. // VERSION -> current_version
  1770. target.GetTargetVersion(false, major, minor, patch);
  1771. cmOStringStream v;
  1772. // Xcode always wants at least 1.0.0 or nothing
  1773. if(!(major == 0 && minor == 0 && patch == 0))
  1774. {
  1775. v << major << "." << minor << "." << patch;
  1776. }
  1777. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1778. this->CreateString(v.str().c_str()));
  1779. // SOVERSION -> compatibility_version
  1780. target.GetTargetVersion(true, major, minor, patch);
  1781. cmOStringStream vso;
  1782. // Xcode always wants at least 1.0.0 or nothing
  1783. if(!(major == 0 && minor == 0 && patch == 0))
  1784. {
  1785. vso << major << "." << minor << "." << patch;
  1786. }
  1787. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1788. this->CreateString(vso.str().c_str()));
  1789. }
  1790. // put this last so it can override existing settings
  1791. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  1792. {
  1793. cmPropertyMap const& props = target.GetProperties();
  1794. for(cmPropertyMap::const_iterator i = props.begin();
  1795. i != props.end(); ++i)
  1796. {
  1797. if(i->first.find("XCODE_ATTRIBUTE_") == 0)
  1798. {
  1799. buildSettings->AddAttribute(i->first.substr(16).c_str(),
  1800. this->CreateString(i->second.GetValue()));
  1801. }
  1802. }
  1803. }
  1804. }
  1805. //----------------------------------------------------------------------------
  1806. cmXCodeObject*
  1807. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  1808. {
  1809. cmXCodeObject* shellBuildPhase =
  1810. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1811. shellBuildPhase->AddAttribute("buildActionMask",
  1812. this->CreateString("2147483647"));
  1813. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1814. shellBuildPhase->AddAttribute("files", buildFiles);
  1815. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1816. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1817. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1818. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1819. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1820. this->CreateString("0"));
  1821. shellBuildPhase->AddAttribute("shellPath",
  1822. this->CreateString("/bin/sh"));
  1823. shellBuildPhase->AddAttribute("shellScript",
  1824. this->CreateString(
  1825. "# shell script goes here\nexit 0"));
  1826. cmXCodeObject* target =
  1827. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1828. target->SetComment(cmtarget.GetName());
  1829. cmXCodeObject* buildPhases =
  1830. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1831. std::vector<cmXCodeObject*> emptyContentVector;
  1832. this->CreateCustomCommands(buildPhases, 0, 0, 0, emptyContentVector, 0,
  1833. cmtarget);
  1834. target->AddAttribute("buildPhases", buildPhases);
  1835. if(this->XcodeVersion > 20)
  1836. {
  1837. this->AddConfigurations(target, cmtarget);
  1838. }
  1839. else
  1840. {
  1841. const char* theConfig =
  1842. this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1843. cmXCodeObject* buildSettings =
  1844. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1845. this->CreateBuildSettings(cmtarget, buildSettings, theConfig);
  1846. target->AddAttribute("buildSettings", buildSettings);
  1847. }
  1848. cmXCodeObject* dependencies =
  1849. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1850. target->AddAttribute("dependencies", dependencies);
  1851. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1852. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1853. target->SetTarget(&cmtarget);
  1854. // Add source files without build rules for editing convenience.
  1855. if(cmtarget.GetType() == cmTarget::UTILITY)
  1856. {
  1857. std::vector<cmSourceFile*> const& sources = cmtarget.GetSourceFiles();
  1858. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1859. i != sources.end(); ++i)
  1860. {
  1861. if(!(*i)->GetPropertyAsBool("GENERATED"))
  1862. {
  1863. this->CreateXCodeFileReference(*i, cmtarget);
  1864. }
  1865. }
  1866. }
  1867. return target;
  1868. }
  1869. //----------------------------------------------------------------------------
  1870. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  1871. cmTarget& cmtarget)
  1872. {
  1873. std::string configTypes =
  1874. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  1875. std::vector<std::string> configVectorIn;
  1876. std::vector<std::string> configVector;
  1877. configVectorIn.push_back(configTypes);
  1878. cmSystemTools::ExpandList(configVectorIn, configVector);
  1879. cmXCodeObject* configlist =
  1880. this->CreateObject(cmXCodeObject::XCConfigurationList);
  1881. cmXCodeObject* buildConfigurations =
  1882. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1883. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  1884. std::string comment = "Build configuration list for ";
  1885. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  1886. comment += " \"";
  1887. comment += cmtarget.GetName();
  1888. comment += "\"";
  1889. configlist->SetComment(comment.c_str());
  1890. target->AddAttribute("buildConfigurationList",
  1891. this->CreateObjectReference(configlist));
  1892. for(unsigned int i = 0; i < configVector.size(); ++i)
  1893. {
  1894. cmXCodeObject* config =
  1895. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  1896. buildConfigurations->AddObject(config);
  1897. cmXCodeObject* buildSettings =
  1898. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1899. this->CreateBuildSettings(cmtarget, buildSettings,
  1900. configVector[i].c_str());
  1901. config->AddAttribute("name", this->CreateString(configVector[i].c_str()));
  1902. config->SetComment(configVector[i].c_str());
  1903. config->AddAttribute("buildSettings", buildSettings);
  1904. }
  1905. if(configVector.size())
  1906. {
  1907. configlist->AddAttribute("defaultConfigurationName",
  1908. this->CreateString(configVector[0].c_str()));
  1909. configlist->AddAttribute("defaultConfigurationIsVisible",
  1910. this->CreateString("0"));
  1911. return configVector[0];
  1912. }
  1913. return "";
  1914. }
  1915. //----------------------------------------------------------------------------
  1916. const char* cmGlobalXCodeGenerator::GetTargetFileType(cmTarget& cmtarget)
  1917. {
  1918. switch(cmtarget.GetType())
  1919. {
  1920. case cmTarget::STATIC_LIBRARY:
  1921. return "archive.ar";
  1922. case cmTarget::MODULE_LIBRARY:
  1923. return ((this->XcodeVersion >= 22)?
  1924. "compiled.mach-o.executable" : "compiled.mach-o.dylib");
  1925. case cmTarget::SHARED_LIBRARY:
  1926. return (cmtarget.GetPropertyAsBool("FRAMEWORK")?
  1927. "wrapper.framework" : "compiled.mach-o.dylib");
  1928. case cmTarget::EXECUTABLE:
  1929. return "compiled.mach-o.executable";
  1930. default: break;
  1931. }
  1932. return 0;
  1933. }
  1934. //----------------------------------------------------------------------------
  1935. const char* cmGlobalXCodeGenerator::GetTargetProductType(cmTarget& cmtarget)
  1936. {
  1937. switch(cmtarget.GetType())
  1938. {
  1939. case cmTarget::STATIC_LIBRARY:
  1940. return "com.apple.product-type.library.static";
  1941. case cmTarget::MODULE_LIBRARY:
  1942. return ((this->XcodeVersion >= 22)? "com.apple.product-type.tool" :
  1943. "com.apple.product-type.library.dynamic");
  1944. case cmTarget::SHARED_LIBRARY:
  1945. return (cmtarget.GetPropertyAsBool("FRAMEWORK")?
  1946. "com.apple.product-type.framework" :
  1947. "com.apple.product-type.library.dynamic");
  1948. case cmTarget::EXECUTABLE:
  1949. return (cmtarget.GetPropertyAsBool("MACOSX_BUNDLE")?
  1950. "com.apple.product-type.application" :
  1951. "com.apple.product-type.tool");
  1952. default: break;
  1953. }
  1954. return 0;
  1955. }
  1956. //----------------------------------------------------------------------------
  1957. cmXCodeObject*
  1958. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  1959. cmXCodeObject* buildPhases)
  1960. {
  1961. cmXCodeObject* target =
  1962. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  1963. target->AddAttribute("buildPhases", buildPhases);
  1964. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1965. target->AddAttribute("buildRules", buildRules);
  1966. std::string defConfig;
  1967. if(this->XcodeVersion > 20)
  1968. {
  1969. defConfig = this->AddConfigurations(target, cmtarget);
  1970. }
  1971. else
  1972. {
  1973. cmXCodeObject* buildSettings =
  1974. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1975. defConfig = this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1976. this->CreateBuildSettings(cmtarget, buildSettings, defConfig.c_str());
  1977. target->AddAttribute("buildSettings", buildSettings);
  1978. }
  1979. cmXCodeObject* dependencies =
  1980. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1981. target->AddAttribute("dependencies", dependencies);
  1982. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1983. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1984. cmXCodeObject* fileRef =
  1985. this->CreateObject(cmXCodeObject::PBXFileReference);
  1986. if(const char* fileType = this->GetTargetFileType(cmtarget))
  1987. {
  1988. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  1989. }
  1990. std::string fullName = cmtarget.GetFullName(defConfig.c_str());
  1991. fileRef->AddAttribute("path", this->CreateString(fullName.c_str()));
  1992. fileRef->AddAttribute("refType", this->CreateString("0"));
  1993. fileRef->AddAttribute("sourceTree",
  1994. this->CreateString("BUILT_PRODUCTS_DIR"));
  1995. fileRef->SetComment(cmtarget.GetName());
  1996. target->AddAttribute("productReference",
  1997. this->CreateObjectReference(fileRef));
  1998. if(const char* productType = this->GetTargetProductType(cmtarget))
  1999. {
  2000. target->AddAttribute("productType", this->CreateString(productType));
  2001. }
  2002. target->SetTarget(&cmtarget);
  2003. return target;
  2004. }
  2005. //----------------------------------------------------------------------------
  2006. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  2007. {
  2008. if(!t)
  2009. {
  2010. return 0;
  2011. }
  2012. for(std::vector<cmXCodeObject*>::iterator i = this->XCodeObjects.begin();
  2013. i != this->XCodeObjects.end(); ++i)
  2014. {
  2015. cmXCodeObject* o = *i;
  2016. if(o->GetTarget() == t)
  2017. {
  2018. return o;
  2019. }
  2020. }
  2021. return 0;
  2022. }
  2023. //----------------------------------------------------------------------------
  2024. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2025. cmXCodeObject* dependTarget)
  2026. {
  2027. // make sure a target does not depend on itself
  2028. if(target == dependTarget)
  2029. {
  2030. return;
  2031. }
  2032. // now avoid circular references if dependTarget already
  2033. // depends on target then skip it. Circular references crashes
  2034. // xcode
  2035. cmXCodeObject* dependTargetDepends =
  2036. dependTarget->GetObject("dependencies");
  2037. if(dependTargetDepends)
  2038. {
  2039. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  2040. {
  2041. return;
  2042. }
  2043. }
  2044. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  2045. if(!targetdep)
  2046. {
  2047. cmXCodeObject* container =
  2048. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2049. container->SetComment("PBXContainerItemProxy");
  2050. container->AddAttribute("containerPortal",
  2051. this->CreateObjectReference(this->RootObject));
  2052. container->AddAttribute("proxyType", this->CreateString("1"));
  2053. container->AddAttribute("remoteGlobalIDString",
  2054. this->CreateObjectReference(dependTarget));
  2055. container->AddAttribute("remoteInfo",
  2056. this->CreateString(
  2057. dependTarget->GetTarget()->GetName()));
  2058. targetdep =
  2059. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2060. targetdep->SetComment("PBXTargetDependency");
  2061. targetdep->AddAttribute("target",
  2062. this->CreateObjectReference(dependTarget));
  2063. targetdep->AddAttribute("targetProxy",
  2064. this->CreateObjectReference(container));
  2065. dependTarget->SetPBXTargetDependency(targetdep);
  2066. }
  2067. cmXCodeObject* depends = target->GetObject("dependencies");
  2068. if(!depends)
  2069. {
  2070. cmSystemTools::
  2071. Error("target does not have dependencies attribute error..");
  2072. }
  2073. else
  2074. {
  2075. depends->AddUniqueObject(targetdep);
  2076. }
  2077. }
  2078. //----------------------------------------------------------------------------
  2079. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2080. const char* attribute,
  2081. const char* value)
  2082. {
  2083. if(settings)
  2084. {
  2085. cmXCodeObject* attr = settings->GetObject(attribute);
  2086. if(!attr)
  2087. {
  2088. settings->AddAttribute(attribute, this->CreateString(value));
  2089. }
  2090. else
  2091. {
  2092. std::string oldValue = attr->GetString();
  2093. oldValue += " ";
  2094. oldValue += value;
  2095. attr->SetString(oldValue.c_str());
  2096. }
  2097. }
  2098. }
  2099. //----------------------------------------------------------------------------
  2100. void cmGlobalXCodeGenerator
  2101. ::AppendBuildSettingAttribute(cmXCodeObject* target,
  2102. const char* attribute,
  2103. const char* value,
  2104. const char* configName)
  2105. {
  2106. if(this->XcodeVersion < 21)
  2107. {
  2108. // There is only one configuration. Add the setting to the buildSettings
  2109. // of the target.
  2110. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  2111. attribute, value);
  2112. }
  2113. else
  2114. {
  2115. // There are multiple configurations. Add the setting to the
  2116. // buildSettings of the configuration name given.
  2117. cmXCodeObject* configurationList =
  2118. target->GetObject("buildConfigurationList")->GetObject();
  2119. cmXCodeObject* buildConfigs =
  2120. configurationList->GetObject("buildConfigurations");
  2121. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  2122. // each configuration and the target itself has a buildSettings in it
  2123. //list.push_back(target);
  2124. for(std::vector<cmXCodeObject*>::iterator i = list.begin();
  2125. i != list.end(); ++i)
  2126. {
  2127. if(configName)
  2128. {
  2129. if(strcmp((*i)->GetObject("name")->GetString(), configName) == 0)
  2130. {
  2131. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2132. this->AppendOrAddBuildSetting(settings, attribute, value);
  2133. }
  2134. }
  2135. else
  2136. {
  2137. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2138. this->AppendOrAddBuildSetting(settings, attribute, value);
  2139. }
  2140. }
  2141. }
  2142. }
  2143. //----------------------------------------------------------------------------
  2144. void cmGlobalXCodeGenerator
  2145. ::AddDependAndLinkInformation(cmXCodeObject* target)
  2146. {
  2147. cmTarget* cmtarget = target->GetTarget();
  2148. if(!cmtarget)
  2149. {
  2150. cmSystemTools::Error("Error no target on xobject\n");
  2151. return;
  2152. }
  2153. // Add dependencies on other CMake targets.
  2154. TargetDependSet const& deps = this->GetTargetDirectDepends(*cmtarget);
  2155. for(TargetDependSet::const_iterator i = deps.begin(); i != deps.end(); ++i)
  2156. {
  2157. if(cmXCodeObject* dptarget = this->FindXCodeTarget(*i))
  2158. {
  2159. this->AddDependTarget(target, dptarget);
  2160. }
  2161. }
  2162. // Skip link information for static libraries.
  2163. if(cmtarget->GetType() == cmTarget::STATIC_LIBRARY)
  2164. {
  2165. return;
  2166. }
  2167. // Loop over configuration types and set per-configuration info.
  2168. for(std::vector<std::string>::iterator i =
  2169. this->CurrentConfigurationTypes.begin();
  2170. i != this->CurrentConfigurationTypes.end(); ++i)
  2171. {
  2172. // Get the current configuration name.
  2173. const char* configName = i->c_str();
  2174. if(!*configName)
  2175. {
  2176. configName = 0;
  2177. }
  2178. // Compute the link library and directory information.
  2179. cmComputeLinkInformation* pcli = cmtarget->GetLinkInformation(configName);
  2180. if(!pcli)
  2181. {
  2182. continue;
  2183. }
  2184. cmComputeLinkInformation& cli = *pcli;
  2185. // Add dependencies directly on library files.
  2186. {
  2187. std::vector<std::string> const& libDeps = cli.GetDepends();
  2188. for(std::vector<std::string>::const_iterator j = libDeps.begin();
  2189. j != libDeps.end(); ++j)
  2190. {
  2191. target->AddDependLibrary(configName, j->c_str());
  2192. }
  2193. }
  2194. // add the library search paths
  2195. {
  2196. std::vector<std::string> const& libDirs = cli.GetDirectories();
  2197. std::string linkDirs;
  2198. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  2199. libDir != libDirs.end(); ++libDir)
  2200. {
  2201. if(libDir->size() && *libDir != "/usr/lib")
  2202. {
  2203. if(this->XcodeVersion > 15)
  2204. {
  2205. // now add the same one but append $(CONFIGURATION) to it:
  2206. linkDirs += " ";
  2207. linkDirs += this->XCodeEscapePath(
  2208. (*libDir + "/$(CONFIGURATION)").c_str());
  2209. }
  2210. linkDirs += " ";
  2211. linkDirs += this->XCodeEscapePath(libDir->c_str());
  2212. }
  2213. }
  2214. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2215. linkDirs.c_str(), configName);
  2216. }
  2217. // add the framework search paths
  2218. {
  2219. const char* sep = "";
  2220. std::string fdirs;
  2221. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  2222. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  2223. fdi != fwDirs.end(); ++fdi)
  2224. {
  2225. fdirs += sep;
  2226. sep = " ";
  2227. fdirs += this->XCodeEscapePath(fdi->c_str());
  2228. }
  2229. if(!fdirs.empty())
  2230. {
  2231. this->AppendBuildSettingAttribute(target, "FRAMEWORK_SEARCH_PATHS",
  2232. fdirs.c_str(), configName);
  2233. }
  2234. }
  2235. // now add the link libraries
  2236. {
  2237. std::string linkLibs;
  2238. const char* sep = "";
  2239. typedef cmComputeLinkInformation::ItemVector ItemVector;
  2240. ItemVector const& libNames = cli.GetItems();
  2241. for(ItemVector::const_iterator li = libNames.begin();
  2242. li != libNames.end(); ++li)
  2243. {
  2244. linkLibs += sep;
  2245. sep = " ";
  2246. if(li->IsPath)
  2247. {
  2248. linkLibs += this->XCodeEscapePath(li->Value.c_str());
  2249. }
  2250. else
  2251. {
  2252. linkLibs += li->Value;
  2253. }
  2254. }
  2255. this->AppendBuildSettingAttribute(target, "OTHER_LDFLAGS",
  2256. linkLibs.c_str(), configName);
  2257. }
  2258. }
  2259. }
  2260. //----------------------------------------------------------------------------
  2261. void cmGlobalXCodeGenerator::CreateGroups(cmLocalGenerator* root,
  2262. std::vector<cmLocalGenerator*>&
  2263. generators)
  2264. {
  2265. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2266. i != generators.end(); ++i)
  2267. {
  2268. if(this->IsExcluded(root, *i))
  2269. {
  2270. continue;
  2271. }
  2272. cmMakefile* mf = (*i)->GetMakefile();
  2273. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2274. cmTargets &tgts = mf->GetTargets();
  2275. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  2276. {
  2277. cmTarget& cmtarget = l->second;
  2278. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2279. // end up with (empty anyhow) ALL_BUILD and XCODE_DEPEND_HELPER source
  2280. // groups:
  2281. //
  2282. if(cmtarget.GetType() == cmTarget::GLOBAL_TARGET)
  2283. {
  2284. continue;
  2285. }
  2286. // add the soon to be generated Info.plist file as a source for a
  2287. // MACOSX_BUNDLE file
  2288. if(cmtarget.GetPropertyAsBool("MACOSX_BUNDLE"))
  2289. {
  2290. std::string plist = this->ComputeInfoPListLocation(cmtarget);
  2291. cmSourceFile* sf = mf->GetOrCreateSource(plist.c_str(), true);
  2292. cmtarget.AddSourceFile(sf);
  2293. }
  2294. std::vector<cmSourceFile*> classes = cmtarget.GetSourceFiles();
  2295. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  2296. s != classes.end(); s++)
  2297. {
  2298. cmSourceFile* sf = *s;
  2299. // Add the file to the list of sources.
  2300. std::string const& source = sf->GetFullPath();
  2301. cmSourceGroup& sourceGroup =
  2302. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2303. cmXCodeObject* pbxgroup =
  2304. this->CreateOrGetPBXGroup(cmtarget, &sourceGroup);
  2305. cmStdString key = GetGroupMapKey(cmtarget, sf);
  2306. this->GroupMap[key] = pbxgroup;
  2307. }
  2308. }
  2309. }
  2310. }
  2311. //----------------------------------------------------------------------------
  2312. cmXCodeObject* cmGlobalXCodeGenerator
  2313. ::CreateOrGetPBXGroup(cmTarget& cmtarget, cmSourceGroup* sg)
  2314. {
  2315. cmStdString s = cmtarget.GetName();
  2316. s += "/";
  2317. s += sg->GetName();
  2318. std::map<cmStdString, cmXCodeObject* >::iterator i =
  2319. this->GroupNameMap.find(s);
  2320. if(i != this->GroupNameMap.end())
  2321. {
  2322. return i->second;
  2323. }
  2324. i = this->TargetGroup.find(cmtarget.GetName());
  2325. cmXCodeObject* tgroup = 0;
  2326. if(i != this->TargetGroup.end())
  2327. {
  2328. tgroup = i->second;
  2329. }
  2330. else
  2331. {
  2332. tgroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2333. this->TargetGroup[cmtarget.GetName()] = tgroup;
  2334. cmXCodeObject* tgroupChildren =
  2335. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2336. tgroup->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  2337. tgroup->AddAttribute("children", tgroupChildren);
  2338. if(this->XcodeVersion == 15)
  2339. {
  2340. tgroup->AddAttribute("refType", this->CreateString("4"));
  2341. }
  2342. tgroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2343. this->SourcesGroupChildren->AddObject(tgroup);
  2344. }
  2345. // If it's the default source group (empty name) then put the source file
  2346. // directly in the tgroup...
  2347. //
  2348. if (cmStdString(sg->GetName()) == "")
  2349. {
  2350. this->GroupNameMap[s] = tgroup;
  2351. return tgroup;
  2352. }
  2353. cmXCodeObject* tgroupChildren = tgroup->GetObject("children");
  2354. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2355. cmXCodeObject* groupChildren =
  2356. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2357. group->AddAttribute("name", this->CreateString(sg->GetName()));
  2358. group->AddAttribute("children", groupChildren);
  2359. if(this->XcodeVersion == 15)
  2360. {
  2361. group->AddAttribute("refType", this->CreateString("4"));
  2362. }
  2363. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2364. tgroupChildren->AddObject(group);
  2365. this->GroupNameMap[s] = group;
  2366. return group;
  2367. }
  2368. //----------------------------------------------------------------------------
  2369. void cmGlobalXCodeGenerator
  2370. ::CreateXCodeObjects(cmLocalGenerator* root,
  2371. std::vector<cmLocalGenerator*>&
  2372. generators)
  2373. {
  2374. this->ClearXCodeObjects();
  2375. this->RootObject = 0;
  2376. this->SourcesGroupChildren = 0;
  2377. this->ResourcesGroupChildren = 0;
  2378. this->MainGroupChildren = 0;
  2379. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2380. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2381. cmXCodeObject* developBuildStyle =
  2382. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2383. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2384. if(this->XcodeVersion == 15)
  2385. {
  2386. developBuildStyle->AddAttribute("name",
  2387. this->CreateString("Development"));
  2388. developBuildStyle->AddAttribute("buildSettings", group);
  2389. listObjs->AddObject(developBuildStyle);
  2390. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2391. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  2392. cmXCodeObject* deployBuildStyle =
  2393. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2394. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  2395. deployBuildStyle->AddAttribute("buildSettings", group);
  2396. listObjs->AddObject(deployBuildStyle);
  2397. }
  2398. else
  2399. {
  2400. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2401. {
  2402. cmXCodeObject* buildStyle =
  2403. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2404. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2405. buildStyle->AddAttribute("name", this->CreateString(name));
  2406. buildStyle->SetComment(name);
  2407. cmXCodeObject* sgroup =
  2408. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2409. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2410. buildStyle->AddAttribute("buildSettings", sgroup);
  2411. listObjs->AddObject(buildStyle);
  2412. }
  2413. }
  2414. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2415. this->MainGroupChildren =
  2416. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2417. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2418. if(this->XcodeVersion == 15)
  2419. {
  2420. mainGroup->AddAttribute("refType", this->CreateString("4"));
  2421. }
  2422. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2423. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2424. this->SourcesGroupChildren =
  2425. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2426. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  2427. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  2428. if(this->XcodeVersion == 15)
  2429. {
  2430. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2431. }
  2432. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2433. this->MainGroupChildren->AddObject(sourcesGroup);
  2434. cmXCodeObject* resourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2435. this->ResourcesGroupChildren =
  2436. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2437. resourcesGroup->AddAttribute("name", this->CreateString("Resources"));
  2438. resourcesGroup->AddAttribute("children", this->ResourcesGroupChildren);
  2439. if(this->XcodeVersion == 15)
  2440. {
  2441. resourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2442. }
  2443. resourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2444. this->MainGroupChildren->AddObject(resourcesGroup);
  2445. // now create the cmake groups
  2446. this->CreateGroups(root, generators);
  2447. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2448. productGroup->AddAttribute("name", this->CreateString("Products"));
  2449. if(this->XcodeVersion == 15)
  2450. {
  2451. productGroup->AddAttribute("refType", this->CreateString("4"));
  2452. }
  2453. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2454. cmXCodeObject* productGroupChildren =
  2455. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2456. productGroup->AddAttribute("children", productGroupChildren);
  2457. this->MainGroupChildren->AddObject(productGroup);
  2458. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2459. this->RootObject->SetComment("Project object");
  2460. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2461. this->RootObject->AddAttribute("mainGroup",
  2462. this->CreateObjectReference(mainGroup));
  2463. this->RootObject->AddAttribute("buildSettings", group);
  2464. this->RootObject->AddAttribute("buildStyles", listObjs);
  2465. this->RootObject->AddAttribute("hasScannedForEncodings",
  2466. this->CreateString("0"));
  2467. if (this->XcodeVersion >= 30)
  2468. {
  2469. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2470. group->AddAttribute("BuildIndependentTargetsInParallel",
  2471. this->CreateString("YES"));
  2472. this->RootObject->AddAttribute("attributes", group);
  2473. if (this->XcodeVersion >= 31)
  2474. this->RootObject->AddAttribute("compatibilityVersion",
  2475. this->CreateString("Xcode 3.1"));
  2476. else
  2477. this->RootObject->AddAttribute("compatibilityVersion",
  2478. this->CreateString("Xcode 3.0"));
  2479. }
  2480. // Point Xcode at the top of the source tree.
  2481. {
  2482. std::string pdir =
  2483. this->RelativeToBinary(root->GetMakefile()->GetCurrentDirectory());
  2484. this->RootObject->AddAttribute("projectDirPath",
  2485. this->CreateString(pdir.c_str()));
  2486. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  2487. }
  2488. cmXCodeObject* configlist =
  2489. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2490. cmXCodeObject* buildConfigurations =
  2491. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2492. std::vector<cmXCodeObject*> configs;
  2493. if(this->XcodeVersion == 15)
  2494. {
  2495. cmXCodeObject* configDebug =
  2496. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2497. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2498. configs.push_back(configDebug);
  2499. cmXCodeObject* configRelease =
  2500. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2501. configRelease->AddAttribute("name", this->CreateString("Release"));
  2502. configs.push_back(configRelease);
  2503. }
  2504. else
  2505. {
  2506. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2507. {
  2508. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2509. cmXCodeObject* config =
  2510. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2511. config->AddAttribute("name", this->CreateString(name));
  2512. configs.push_back(config);
  2513. }
  2514. }
  2515. for(std::vector<cmXCodeObject*>::iterator c = configs.begin();
  2516. c != configs.end(); ++c)
  2517. {
  2518. buildConfigurations->AddObject(*c);
  2519. }
  2520. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2521. std::string comment = "Build configuration list for PBXProject ";
  2522. comment += " \"";
  2523. comment += this->CurrentProject;
  2524. comment += "\"";
  2525. configlist->SetComment(comment.c_str());
  2526. configlist->AddAttribute("defaultConfigurationIsVisible",
  2527. this->CreateString("0"));
  2528. configlist->AddAttribute("defaultConfigurationName",
  2529. this->CreateString("Debug"));
  2530. cmXCodeObject* buildSettings =
  2531. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2532. const char* osxArch =
  2533. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2534. if(strlen(osxArch) == 0)
  2535. {
  2536. if(this->XcodeVersion >= 32)
  2537. {
  2538. osxArch = "$(ARCHS_STANDARD_32_64_BIT)";
  2539. }
  2540. else if(this->XcodeVersion == 31)
  2541. {
  2542. osxArch = "$(ARCHS_STANDARD_32_BIT)";
  2543. }
  2544. else if(this->XcodeVersion <= 30)
  2545. {
  2546. #ifdef __ppc__
  2547. osxArch = "ppc";
  2548. #endif
  2549. #ifdef __i386
  2550. osxArch = "i386";
  2551. #endif
  2552. }
  2553. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH",
  2554. this->CreateString("YES"));
  2555. }
  2556. const char* sysroot =
  2557. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2558. const char* sysrootDefault =
  2559. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT_DEFAULT");
  2560. const char* deploymentTarget =
  2561. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2562. if(osxArch && sysroot)
  2563. {
  2564. bool flagsUsed = false;
  2565. // recompute this as it may have been changed since enable language
  2566. this->Architectures.clear();
  2567. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2568. this->Architectures);
  2569. flagsUsed = true;
  2570. buildSettings->AddAttribute("SDKROOT",
  2571. this->CreateString(sysroot));
  2572. std::string archString;
  2573. for( std::vector<std::string>::iterator i =
  2574. this->Architectures.begin();
  2575. i != this->Architectures.end(); ++i)
  2576. {
  2577. archString += *i;
  2578. archString += " ";
  2579. }
  2580. buildSettings->AddAttribute("ARCHS",
  2581. this->CreateString(archString.c_str()));
  2582. if(!flagsUsed && sysrootDefault &&
  2583. strcmp(sysroot, sysrootDefault) != 0)
  2584. {
  2585. buildSettings->AddAttribute("SDKROOT",
  2586. this->CreateString(sysroot));
  2587. }
  2588. }
  2589. if(deploymentTarget && *deploymentTarget)
  2590. {
  2591. buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
  2592. this->CreateString(deploymentTarget));
  2593. }
  2594. // Put this last so it can override existing settings
  2595. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  2596. {
  2597. std::vector<std::string> vars = this->CurrentMakefile->GetDefinitions();
  2598. for(std::vector<std::string>::const_iterator i = vars.begin();
  2599. i != vars.end(); ++i)
  2600. {
  2601. if(i->find("CMAKE_XCODE_ATTRIBUTE_") == 0)
  2602. {
  2603. buildSettings->AddAttribute(i->substr(22).c_str(),
  2604. this->CreateString(
  2605. this->CurrentMakefile->GetDefinition(i->c_str())));
  2606. }
  2607. }
  2608. }
  2609. std::string symroot = root->GetMakefile()->GetCurrentOutputDirectory();
  2610. symroot += "/build";
  2611. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot.c_str()));
  2612. for( std::vector<cmXCodeObject*>::iterator i = configs.begin();
  2613. i != configs.end(); ++i)
  2614. {
  2615. (*i)->AddAttribute("buildSettings", buildSettings);
  2616. }
  2617. this->RootObject->AddAttribute("buildConfigurationList",
  2618. this->CreateObjectReference(configlist));
  2619. std::vector<cmXCodeObject*> targets;
  2620. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2621. i != generators.end(); ++i)
  2622. {
  2623. if(!this->IsExcluded(root, *i))
  2624. {
  2625. this->CreateXCodeTargets(*i, targets);
  2626. }
  2627. }
  2628. // loop over all targets and add link and depend info
  2629. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2630. i != targets.end(); ++i)
  2631. {
  2632. cmXCodeObject* t = *i;
  2633. this->AddDependAndLinkInformation(t);
  2634. }
  2635. // now create xcode depend hack makefile
  2636. this->CreateXCodeDependHackTarget(targets);
  2637. // now add all targets to the root object
  2638. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2639. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2640. i != targets.end(); ++i)
  2641. {
  2642. cmXCodeObject* t = *i;
  2643. allTargets->AddObject(t);
  2644. cmXCodeObject* productRef = t->GetObject("productReference");
  2645. if(productRef)
  2646. {
  2647. productGroupChildren->AddObject(productRef->GetObject());
  2648. }
  2649. }
  2650. this->RootObject->AddAttribute("targets", allTargets);
  2651. }
  2652. //----------------------------------------------------------------------------
  2653. void
  2654. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2655. std::vector<cmXCodeObject*>& targets)
  2656. {
  2657. cmGeneratedFileStream
  2658. makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2659. if(!makefileStream)
  2660. {
  2661. cmSystemTools::Error("Could not create",
  2662. this->CurrentXCodeHackMakefile.c_str());
  2663. return;
  2664. }
  2665. makefileStream.SetCopyIfDifferent(true);
  2666. // one more pass for external depend information not handled
  2667. // correctly by xcode
  2668. makefileStream << "# DO NOT EDIT\n";
  2669. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2670. makefileStream << "# up-to-date with anything they link to, avoiding a "
  2671. "bug in XCode 1.5\n";
  2672. for(std::vector<std::string>::const_iterator
  2673. ct = this->CurrentConfigurationTypes.begin();
  2674. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2675. {
  2676. if(this->XcodeVersion < 21 || ct->empty())
  2677. {
  2678. makefileStream << "all: ";
  2679. }
  2680. else
  2681. {
  2682. makefileStream << "all." << *ct << ": ";
  2683. }
  2684. const char* configName = 0;
  2685. if(!ct->empty())
  2686. {
  2687. configName = ct->c_str();
  2688. }
  2689. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2690. i != targets.end(); ++i)
  2691. {
  2692. cmXCodeObject* target = *i;
  2693. cmTarget* t =target->GetTarget();
  2694. if(t->GetType() == cmTarget::EXECUTABLE ||
  2695. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2696. t->GetType() == cmTarget::MODULE_LIBRARY)
  2697. {
  2698. std::string tfull = t->GetFullPath(configName);
  2699. makefileStream << "\\\n\t" <<
  2700. this->ConvertToRelativeForMake(tfull.c_str());
  2701. }
  2702. }
  2703. makefileStream << "\n\n";
  2704. }
  2705. makefileStream
  2706. << "# For each target create a dummy rule "
  2707. "so the target does not have to exist\n";
  2708. std::set<cmStdString> emitted;
  2709. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2710. i != targets.end(); ++i)
  2711. {
  2712. cmXCodeObject* target = *i;
  2713. std::map<cmStdString, cmXCodeObject::StringVec> const& deplibs =
  2714. target->GetDependLibraries();
  2715. for(std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator ci
  2716. = deplibs.begin(); ci != deplibs.end(); ++ci)
  2717. {
  2718. for(cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2719. d != ci->second.end(); ++d)
  2720. {
  2721. if(emitted.insert(*d).second)
  2722. {
  2723. makefileStream <<
  2724. this->ConvertToRelativeForMake(d->c_str()) << ":\n";
  2725. }
  2726. }
  2727. }
  2728. }
  2729. makefileStream << "\n\n";
  2730. // Write rules to help Xcode relink things at the right time.
  2731. makefileStream <<
  2732. "# Rules to remove targets that are older than anything to which they\n"
  2733. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2734. "# does not seem to check these dependencies itself.\n";
  2735. for(std::vector<std::string>::const_iterator
  2736. ct = this->CurrentConfigurationTypes.begin();
  2737. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2738. {
  2739. const char* configName = 0;
  2740. if(!ct->empty())
  2741. {
  2742. configName = ct->c_str();
  2743. }
  2744. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2745. i != targets.end(); ++i)
  2746. {
  2747. cmXCodeObject* target = *i;
  2748. cmTarget* t =target->GetTarget();
  2749. if(t->GetType() == cmTarget::EXECUTABLE ||
  2750. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2751. t->GetType() == cmTarget::MODULE_LIBRARY)
  2752. {
  2753. // Create a rule for this target.
  2754. std::string tfull = t->GetFullPath(configName);
  2755. makefileStream << this->ConvertToRelativeForMake(tfull.c_str())
  2756. << ":";
  2757. // List dependencies if any exist.
  2758. std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator
  2759. x = target->GetDependLibraries().find(*ct);
  2760. if(x != target->GetDependLibraries().end())
  2761. {
  2762. std::vector<cmStdString> const& deplibs = x->second;
  2763. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  2764. d != deplibs.end(); ++d)
  2765. {
  2766. makefileStream << "\\\n\t" <<
  2767. this->ConvertToRelativeForMake(d->c_str());
  2768. }
  2769. }
  2770. // Write the action to remove the target if it is out of date.
  2771. makefileStream << "\n";
  2772. makefileStream << "\t/bin/rm -f "
  2773. << this->ConvertToRelativeForMake(tfull.c_str())
  2774. << "\n";
  2775. // if building for more than one architecture
  2776. // then remove those exectuables as well
  2777. if(this->Architectures.size() > 1)
  2778. {
  2779. std::string universal =
  2780. t->GetMakefile()->GetCurrentOutputDirectory();
  2781. universal += "/";
  2782. universal += this->CurrentProject;
  2783. universal += ".build/";
  2784. universal += configName;
  2785. universal += "/";
  2786. universal += t->GetName();
  2787. universal += ".build/Objects-normal/";
  2788. for( std::vector<std::string>::iterator arch =
  2789. this->Architectures.begin();
  2790. arch != this->Architectures.end(); ++arch)
  2791. {
  2792. std::string universalFile = universal;
  2793. universalFile += *arch;
  2794. universalFile += "/";
  2795. universalFile += t->GetFullName(configName);
  2796. makefileStream << "\t/bin/rm -f "
  2797. <<
  2798. this->ConvertToRelativeForMake(universalFile.c_str())
  2799. << "\n";
  2800. }
  2801. }
  2802. makefileStream << "\n\n";
  2803. }
  2804. }
  2805. }
  2806. }
  2807. //----------------------------------------------------------------------------
  2808. void
  2809. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  2810. std::vector<cmLocalGenerator*>&
  2811. generators)
  2812. {
  2813. if(generators.size() == 0)
  2814. {
  2815. return;
  2816. }
  2817. // Skip local generators that are excluded from this project.
  2818. for(std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2819. g != generators.end(); ++g)
  2820. {
  2821. if(this->IsExcluded(root, *g))
  2822. {
  2823. continue;
  2824. }
  2825. }
  2826. this->CreateXCodeObjects(root,
  2827. generators);
  2828. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  2829. xcodeDir += "/";
  2830. xcodeDir += root->GetMakefile()->GetProjectName();
  2831. xcodeDir += ".xcode";
  2832. if(this->XcodeVersion > 20)
  2833. {
  2834. xcodeDir += "proj";
  2835. }
  2836. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2837. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2838. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2839. fout.SetCopyIfDifferent(true);
  2840. if(!fout)
  2841. {
  2842. return;
  2843. }
  2844. this->WriteXCodePBXProj(fout, root, generators);
  2845. this->ClearXCodeObjects();
  2846. }
  2847. //----------------------------------------------------------------------------
  2848. void
  2849. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  2850. cmLocalGenerator* ,
  2851. std::vector<cmLocalGenerator*>& )
  2852. {
  2853. fout << "// !$*UTF8*$!\n";
  2854. fout << "{\n";
  2855. cmXCodeObject::Indent(1, fout);
  2856. fout << "archiveVersion = 1;\n";
  2857. cmXCodeObject::Indent(1, fout);
  2858. fout << "classes = {\n";
  2859. cmXCodeObject::Indent(1, fout);
  2860. fout << "};\n";
  2861. cmXCodeObject::Indent(1, fout);
  2862. if(this->XcodeVersion >= 21)
  2863. {
  2864. if (this->XcodeVersion >= 31)
  2865. fout << "objectVersion = 45;\n";
  2866. else if (this->XcodeVersion >= 30)
  2867. fout << "objectVersion = 44;\n";
  2868. else
  2869. fout << "objectVersion = 42;\n";
  2870. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  2871. }
  2872. else
  2873. {
  2874. fout << "objectVersion = 39;\n";
  2875. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  2876. }
  2877. cmXCodeObject::Indent(1, fout);
  2878. fout << "rootObject = " << this->RootObject->GetId() << ";\n";
  2879. fout << "}\n";
  2880. }
  2881. //----------------------------------------------------------------------------
  2882. const char* cmGlobalXCodeGenerator::GetCMakeCFGInitDirectory()
  2883. {
  2884. return this->XcodeVersion >= 21? "$(CONFIGURATION)" : ".";
  2885. }
  2886. //----------------------------------------------------------------------------
  2887. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  2888. const
  2889. {
  2890. entry.Name = this->GetName();
  2891. entry.Brief = "Generate XCode project files.";
  2892. entry.Full = "";
  2893. }
  2894. //----------------------------------------------------------------------------
  2895. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  2896. {
  2897. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2898. {
  2899. return cmSystemTools::ConvertToOutputPath(p);
  2900. }
  2901. else
  2902. {
  2903. std::string ret =
  2904. this->CurrentLocalGenerator->
  2905. ConvertToRelativePath(this->CurrentOutputDirectoryComponents, p);
  2906. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2907. }
  2908. }
  2909. //----------------------------------------------------------------------------
  2910. std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
  2911. {
  2912. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2913. {
  2914. return cmSystemTools::ConvertToOutputPath(p);
  2915. }
  2916. else
  2917. {
  2918. std::string ret =
  2919. this->CurrentLocalGenerator->
  2920. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2921. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2922. }
  2923. }
  2924. //----------------------------------------------------------------------------
  2925. std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
  2926. {
  2927. // We force conversion because Xcode breakpoints do not work unless
  2928. // they are in a file named relative to the source tree.
  2929. return this->CurrentLocalGenerator->
  2930. ConvertToRelativePath(this->ProjectSourceDirectoryComponents, p, true);
  2931. }
  2932. //----------------------------------------------------------------------------
  2933. std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
  2934. {
  2935. return this->CurrentLocalGenerator->
  2936. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2937. }
  2938. //----------------------------------------------------------------------------
  2939. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
  2940. {
  2941. std::string ret = p;
  2942. if(ret.find(' ') != ret.npos)
  2943. {
  2944. std::string t = ret;
  2945. ret = "\"";
  2946. ret += t;
  2947. ret += "\"";
  2948. }
  2949. return ret;
  2950. }
  2951. //----------------------------------------------------------------------------
  2952. void cmGlobalXCodeGenerator::
  2953. GetTargetObjectFileDirectories(cmTarget* target,
  2954. std::vector<std::string>&
  2955. dirs)
  2956. {
  2957. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  2958. dir += "/";
  2959. dir += this->CurrentMakefile->GetProjectName();
  2960. dir += ".build/";
  2961. dir += this->GetCMakeCFGInitDirectory();
  2962. dir += "/";
  2963. dir += target->GetName();
  2964. dir += ".build/Objects-normal/";
  2965. std::string dirsave = dir;
  2966. if(this->Architectures.size())
  2967. {
  2968. for(std::vector<std::string>::iterator i = this->Architectures.begin();
  2969. i != this->Architectures.end(); ++i)
  2970. {
  2971. dir += *i;
  2972. dirs.push_back(dir);
  2973. dir = dirsave;
  2974. }
  2975. }
  2976. else
  2977. {
  2978. dirs.push_back(dir);
  2979. }
  2980. }
  2981. //----------------------------------------------------------------------------
  2982. void
  2983. cmGlobalXCodeGenerator
  2984. ::AppendDirectoryForConfig(const char* prefix,
  2985. const char* config,
  2986. const char* suffix,
  2987. std::string& dir)
  2988. {
  2989. if(this->XcodeVersion > 20)
  2990. {
  2991. if(config)
  2992. {
  2993. dir += prefix;
  2994. dir += config;
  2995. dir += suffix;
  2996. }
  2997. }
  2998. }
  2999. //----------------------------------------------------------------------------
  3000. std::string cmGlobalXCodeGenerator::LookupFlags(const char* varNamePrefix,
  3001. const char* varNameLang,
  3002. const char* varNameSuffix,
  3003. const char* default_flags)
  3004. {
  3005. if(varNameLang)
  3006. {
  3007. std::string varName = varNamePrefix;
  3008. varName += varNameLang;
  3009. varName += varNameSuffix;
  3010. if(const char* varValue =
  3011. this->CurrentMakefile->GetDefinition(varName.c_str()))
  3012. {
  3013. if(*varValue)
  3014. {
  3015. return varValue;
  3016. }
  3017. }
  3018. }
  3019. return default_flags;
  3020. }
  3021. //----------------------------------------------------------------------------
  3022. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3023. const char* defines_list,
  3024. bool dflag)
  3025. {
  3026. // Skip this if there are no definitions.
  3027. if(!defines_list)
  3028. {
  3029. return;
  3030. }
  3031. // Expand the list of definitions.
  3032. std::vector<std::string> defines;
  3033. cmSystemTools::ExpandListArgument(defines_list, defines);
  3034. // Store the definitions in the string.
  3035. this->AppendDefines(defs, defines, dflag);
  3036. }
  3037. //----------------------------------------------------------------------------
  3038. void
  3039. cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3040. std::vector<std::string> const& defines,
  3041. bool dflag)
  3042. {
  3043. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  3044. std::string def;
  3045. for(std::vector<std::string>::const_iterator di = defines.begin();
  3046. di != defines.end(); ++di)
  3047. {
  3048. // Start with -D if requested.
  3049. def = dflag? "-D": "";
  3050. def += *di;
  3051. // Append the flag with needed escapes.
  3052. std::string tmp;
  3053. this->AppendFlag(tmp, def);
  3054. defs.Add(tmp.c_str());
  3055. }
  3056. }
  3057. //----------------------------------------------------------------------------
  3058. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  3059. std::string const& flag)
  3060. {
  3061. // Short-circuit for an empty flag.
  3062. if(flag.empty())
  3063. {
  3064. return;
  3065. }
  3066. // Separate from previous flags.
  3067. if(!flags.empty())
  3068. {
  3069. flags += " ";
  3070. }
  3071. // Check if the flag needs quoting.
  3072. bool quoteFlag =
  3073. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != flag.npos;
  3074. // We escape a flag as follows:
  3075. // - Place each flag in single quotes ''
  3076. // - Escape a single quote as \\'
  3077. // - Escape a backslash as \\\\ since it itself is an escape
  3078. // Note that in the code below we need one more level of escapes for
  3079. // C string syntax in this source file.
  3080. //
  3081. // The final level of escaping is done when the string is stored
  3082. // into the project file by cmXCodeObject::PrintString.
  3083. if(quoteFlag)
  3084. {
  3085. // Open single quote.
  3086. flags += "'";
  3087. }
  3088. // Flag value with escaped quotes and backslashes.
  3089. for(const char* c = flag.c_str(); *c; ++c)
  3090. {
  3091. if(*c == '\'')
  3092. {
  3093. flags += "\\\\'";
  3094. }
  3095. else if(*c == '\\')
  3096. {
  3097. flags += "\\\\\\\\";
  3098. }
  3099. else
  3100. {
  3101. flags += *c;
  3102. }
  3103. }
  3104. if(quoteFlag)
  3105. {
  3106. // Close single quote.
  3107. flags += "'";
  3108. }
  3109. }
  3110. //----------------------------------------------------------------------------
  3111. std::string
  3112. cmGlobalXCodeGenerator::ComputeInfoPListLocation(cmTarget& target)
  3113. {
  3114. std::string plist = target.GetMakefile()->GetCurrentOutputDirectory();
  3115. plist += cmake::GetCMakeFilesDirectory();
  3116. plist += "/";
  3117. plist += target.GetName();
  3118. plist += ".dir/Info.plist";
  3119. return plist;
  3120. }
  3121. //----------------------------------------------------------------------------
  3122. // Return true if the generated build tree may contain multiple builds.
  3123. // i.e. "Can I build Debug and Release in the same tree?"
  3124. bool cmGlobalXCodeGenerator::IsMultiConfig()
  3125. {
  3126. // Old Xcode 1.5 is single config:
  3127. if(this->XcodeVersion == 15)
  3128. {
  3129. return false;
  3130. }
  3131. // Newer Xcode versions are multi config:
  3132. return true;
  3133. }