cmGlobalXCodeGenerator.cxx 133 KB

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