cmGlobalXCodeGenerator.cxx 134 KB

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