cmGlobalXCodeGenerator.cxx 104 KB

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