cmGlobalXCodeGenerator.cxx 134 KB

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