cmGlobalXCodeGenerator.cxx 108 KB

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