cmGlobalXCodeGenerator.cxx 102 KB

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