cmGlobalXCodeGenerator.cxx 135 KB

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