cmGlobalXCodeGenerator.cxx 101 KB

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