cmGlobalXCodeGenerator.cxx 133 KB

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