cmGlobalXCodeGenerator.cxx 133 KB

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