1
0

cmGlobalXCodeGenerator.cxx 132 KB

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