cmGlobalXCodeGenerator.cxx 128 KB

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