cmGlobalXCodeGenerator.cxx 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629
  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 <algorithm>
  5. #include <cassert>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <iomanip>
  9. #include <sstream>
  10. #include <unordered_set>
  11. #include <utility>
  12. #include <cm/memory>
  13. #include <cmext/algorithm>
  14. #include <cmext/string_view>
  15. #include "cmsys/RegularExpression.hxx"
  16. #include "cmComputeLinkInformation.h"
  17. #include "cmCryptoHash.h"
  18. #include "cmCustomCommand.h"
  19. #include "cmCustomCommandGenerator.h"
  20. #include "cmCustomCommandLines.h"
  21. #include "cmDocumentationEntry.h"
  22. #include "cmGeneratedFileStream.h"
  23. #include "cmGeneratorExpression.h"
  24. #include "cmGeneratorTarget.h"
  25. #include "cmGlobalGeneratorFactory.h"
  26. #include "cmLocalGenerator.h"
  27. #include "cmLocalXCodeGenerator.h"
  28. #include "cmMakefile.h"
  29. #include "cmMessageType.h"
  30. #include "cmOutputConverter.h"
  31. #include "cmSourceFile.h"
  32. #include "cmSourceGroup.h"
  33. #include "cmState.h"
  34. #include "cmStateTypes.h"
  35. #include "cmStringAlgorithms.h"
  36. #include "cmSystemTools.h"
  37. #include "cmTarget.h"
  38. #include "cmXCode21Object.h"
  39. #include "cmXCodeObject.h"
  40. #include "cmXCodeScheme.h"
  41. #include "cmake.h"
  42. struct cmLinkImplementation;
  43. #if !defined(CMAKE_BOOTSTRAP) && defined(__APPLE__)
  44. # define HAVE_APPLICATION_SERVICES
  45. # include <ApplicationServices/ApplicationServices.h>
  46. #endif
  47. #if !defined(CMAKE_BOOTSTRAP)
  48. # include "cmXMLParser.h"
  49. // parse the xml file storing the installed version of Xcode on
  50. // the machine
  51. class cmXcodeVersionParser : public cmXMLParser
  52. {
  53. public:
  54. cmXcodeVersionParser()
  55. : Version("1.5")
  56. {
  57. }
  58. void StartElement(const std::string&, const char**) override
  59. {
  60. this->Data = "";
  61. }
  62. void EndElement(const std::string& name) override
  63. {
  64. if (name == "key") {
  65. this->Key = this->Data;
  66. } else if (name == "string") {
  67. if (this->Key == "CFBundleShortVersionString") {
  68. this->Version = this->Data;
  69. }
  70. }
  71. }
  72. void CharacterDataHandler(const char* data, int length) override
  73. {
  74. this->Data.append(data, length);
  75. }
  76. std::string Version;
  77. std::string Key;
  78. std::string Data;
  79. };
  80. #endif
  81. // Builds either an object list or a space-separated string from the
  82. // given inputs.
  83. class cmGlobalXCodeGenerator::BuildObjectListOrString
  84. {
  85. cmGlobalXCodeGenerator* Generator;
  86. cmXCodeObject* Group;
  87. bool Empty;
  88. std::string String;
  89. public:
  90. BuildObjectListOrString(cmGlobalXCodeGenerator* gen, bool buildObjectList)
  91. : Generator(gen)
  92. , Group(nullptr)
  93. , Empty(true)
  94. {
  95. if (buildObjectList) {
  96. this->Group = this->Generator->CreateObject(cmXCodeObject::OBJECT_LIST);
  97. }
  98. }
  99. bool IsEmpty() const { return this->Empty; }
  100. void Add(const std::string& newString)
  101. {
  102. this->Empty = false;
  103. if (this->Group) {
  104. this->Group->AddObject(this->Generator->CreateString(newString));
  105. } else {
  106. this->String += newString;
  107. this->String += ' ';
  108. }
  109. }
  110. const std::string& GetString() const { return this->String; }
  111. cmXCodeObject* CreateList()
  112. {
  113. if (this->Group) {
  114. return this->Group;
  115. }
  116. return this->Generator->CreateString(this->String);
  117. }
  118. };
  119. class cmGlobalXCodeGenerator::Factory : public cmGlobalGeneratorFactory
  120. {
  121. public:
  122. std::unique_ptr<cmGlobalGenerator> CreateGlobalGenerator(
  123. const std::string& name, bool allowArch, cmake* cm) const override;
  124. void GetDocumentation(cmDocumentationEntry& entry) const override
  125. {
  126. cmGlobalXCodeGenerator::GetDocumentation(entry);
  127. }
  128. std::vector<std::string> GetGeneratorNames() const override
  129. {
  130. std::vector<std::string> names;
  131. names.push_back(cmGlobalXCodeGenerator::GetActualName());
  132. return names;
  133. }
  134. std::vector<std::string> GetGeneratorNamesWithPlatform() const override
  135. {
  136. return std::vector<std::string>();
  137. }
  138. bool SupportsToolset() const override { return true; }
  139. bool SupportsPlatform() const override { return false; }
  140. std::vector<std::string> GetKnownPlatforms() const override
  141. {
  142. return std::vector<std::string>();
  143. }
  144. std::string GetDefaultPlatformName() const override { return std::string(); }
  145. };
  146. cmGlobalXCodeGenerator::cmGlobalXCodeGenerator(
  147. cmake* cm, std::string const& version_string, unsigned int version_number)
  148. : cmGlobalGenerator(cm)
  149. {
  150. this->VersionString = version_string;
  151. this->XcodeVersion = version_number;
  152. if (this->XcodeVersion >= 120) {
  153. this->XcodeBuildSystem = BuildSystem::Twelve;
  154. } else {
  155. this->XcodeBuildSystem = BuildSystem::One;
  156. }
  157. this->RootObject = nullptr;
  158. this->MainGroupChildren = nullptr;
  159. this->FrameworkGroup = nullptr;
  160. this->CurrentMakefile = nullptr;
  161. this->CurrentLocalGenerator = nullptr;
  162. this->XcodeBuildCommandInitialized = false;
  163. this->ObjectDirArchDefault = "$(CURRENT_ARCH)";
  164. this->ObjectDirArch = this->ObjectDirArchDefault;
  165. cm->GetState()->SetIsGeneratorMultiConfig(true);
  166. }
  167. std::unique_ptr<cmGlobalGeneratorFactory> cmGlobalXCodeGenerator::NewFactory()
  168. {
  169. return std::unique_ptr<cmGlobalGeneratorFactory>(new Factory);
  170. }
  171. std::unique_ptr<cmGlobalGenerator>
  172. cmGlobalXCodeGenerator::Factory::CreateGlobalGenerator(const std::string& name,
  173. bool /*allowArch*/,
  174. cmake* cm) const
  175. {
  176. if (name != GetActualName()) {
  177. return std::unique_ptr<cmGlobalGenerator>();
  178. }
  179. #if !defined(CMAKE_BOOTSTRAP)
  180. cmXcodeVersionParser parser;
  181. std::string versionFile;
  182. {
  183. std::string out;
  184. bool commandResult = cmSystemTools::RunSingleCommand(
  185. "xcode-select --print-path", &out, nullptr, nullptr, nullptr,
  186. cmSystemTools::OUTPUT_NONE);
  187. if (commandResult) {
  188. std::string::size_type pos = out.find(".app/");
  189. if (pos != std::string::npos) {
  190. versionFile = out.substr(0, pos + 5) + "Contents/version.plist";
  191. }
  192. }
  193. }
  194. if (!versionFile.empty() && cmSystemTools::FileExists(versionFile)) {
  195. parser.ParseFile(versionFile.c_str());
  196. } else if (cmSystemTools::FileExists(
  197. "/Applications/Xcode.app/Contents/version.plist")) {
  198. parser.ParseFile("/Applications/Xcode.app/Contents/version.plist");
  199. } else {
  200. parser.ParseFile(
  201. "/Developer/Applications/Xcode.app/Contents/version.plist");
  202. }
  203. std::string const& version_string = parser.Version;
  204. // Compute an integer form of the version number.
  205. unsigned int v[2] = { 0, 0 };
  206. sscanf(version_string.c_str(), "%u.%u", &v[0], &v[1]);
  207. unsigned int version_number = 10 * v[0] + v[1];
  208. if (version_number < 50) {
  209. cm->IssueMessage(MessageType::FATAL_ERROR,
  210. "Xcode " + version_string + " not supported.");
  211. return std::unique_ptr<cmGlobalGenerator>();
  212. }
  213. return std::unique_ptr<cmGlobalGenerator>(
  214. cm::make_unique<cmGlobalXCodeGenerator>(cm, version_string,
  215. version_number));
  216. #else
  217. std::cerr << "CMake should be built with cmake to use Xcode, "
  218. "default to Xcode 1.5\n";
  219. return std::unique_ptr<cmGlobalGenerator>(
  220. cm::make_unique<cmGlobalXCodeGenerator>(cm));
  221. #endif
  222. }
  223. bool cmGlobalXCodeGenerator::FindMakeProgram(cmMakefile* mf)
  224. {
  225. // The Xcode generator knows how to lookup its build tool
  226. // directly instead of needing a helper module to do it, so we
  227. // do not actually need to put CMAKE_MAKE_PROGRAM into the cache.
  228. if (cmIsOff(mf->GetDefinition("CMAKE_MAKE_PROGRAM"))) {
  229. mf->AddDefinition("CMAKE_MAKE_PROGRAM", this->GetXcodeBuildCommand());
  230. }
  231. return true;
  232. }
  233. std::string const& cmGlobalXCodeGenerator::GetXcodeBuildCommand()
  234. {
  235. if (!this->XcodeBuildCommandInitialized) {
  236. this->XcodeBuildCommandInitialized = true;
  237. this->XcodeBuildCommand = this->FindXcodeBuildCommand();
  238. }
  239. return this->XcodeBuildCommand;
  240. }
  241. std::string cmGlobalXCodeGenerator::FindXcodeBuildCommand()
  242. {
  243. std::string makeProgram = cmSystemTools::FindProgram("xcodebuild");
  244. if (makeProgram.empty()) {
  245. makeProgram = "xcodebuild";
  246. }
  247. return makeProgram;
  248. }
  249. bool cmGlobalXCodeGenerator::SetSystemName(std::string const& s,
  250. cmMakefile* mf)
  251. {
  252. this->SystemName = s;
  253. return this->cmGlobalGenerator::SetSystemName(s, mf);
  254. }
  255. namespace {
  256. cm::string_view cmXcodeBuildSystemString(cmGlobalXCodeGenerator::BuildSystem b)
  257. {
  258. switch (b) {
  259. case cmGlobalXCodeGenerator::BuildSystem::One:
  260. return "1"_s;
  261. case cmGlobalXCodeGenerator::BuildSystem::Twelve:
  262. return "12"_s;
  263. }
  264. return {};
  265. }
  266. }
  267. bool cmGlobalXCodeGenerator::SetGeneratorToolset(std::string const& ts,
  268. bool build, cmMakefile* mf)
  269. {
  270. if (!this->ParseGeneratorToolset(ts, mf)) {
  271. return false;
  272. }
  273. if (build) {
  274. return true;
  275. }
  276. if (!this->GeneratorToolset.empty()) {
  277. mf->AddDefinition("CMAKE_XCODE_PLATFORM_TOOLSET", this->GeneratorToolset);
  278. }
  279. mf->AddDefinition("CMAKE_XCODE_BUILD_SYSTEM",
  280. cmXcodeBuildSystemString(this->XcodeBuildSystem));
  281. return true;
  282. }
  283. bool cmGlobalXCodeGenerator::ParseGeneratorToolset(std::string const& ts,
  284. cmMakefile* mf)
  285. {
  286. std::vector<std::string> const fields = cmTokenize(ts, ",");
  287. auto fi = fields.cbegin();
  288. if (fi == fields.cend()) {
  289. return true;
  290. }
  291. // The first field may be the Xcode GCC_VERSION.
  292. if (fi->find('=') == fi->npos) {
  293. this->GeneratorToolset = *fi;
  294. ++fi;
  295. }
  296. std::unordered_set<std::string> handled;
  297. // The rest of the fields must be key=value pairs.
  298. for (; fi != fields.cend(); ++fi) {
  299. std::string::size_type pos = fi->find('=');
  300. if (pos == fi->npos) {
  301. /* clang-format off */
  302. std::string const& e = cmStrCat(
  303. "Generator\n"
  304. " ", this->GetName(), "\n"
  305. "given toolset specification\n"
  306. " ", ts, "\n"
  307. "that contains a field after the first ',' with no '='."
  308. );
  309. /* clang-format on */
  310. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  311. return false;
  312. }
  313. std::string const key = fi->substr(0, pos);
  314. std::string const value = fi->substr(pos + 1);
  315. if (!handled.insert(key).second) {
  316. /* clang-format off */
  317. std::string const& e = cmStrCat(
  318. "Generator\n"
  319. " ", this->GetName(), "\n"
  320. "given toolset specification\n"
  321. " ", ts, "\n"
  322. "that contains duplicate field key '", key, "'."
  323. );
  324. /* clang-format on */
  325. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  326. return false;
  327. }
  328. if (!this->ProcessGeneratorToolsetField(key, value, mf)) {
  329. return false;
  330. }
  331. }
  332. return true;
  333. }
  334. bool cmGlobalXCodeGenerator::ProcessGeneratorToolsetField(
  335. std::string const& key, std::string const& value, cmMakefile* mf)
  336. {
  337. if (key == "buildsystem") {
  338. if (value == "1"_s) {
  339. this->XcodeBuildSystem = BuildSystem::One;
  340. } else if (value == "12"_s) {
  341. this->XcodeBuildSystem = BuildSystem::Twelve;
  342. } else {
  343. /* clang-format off */
  344. std::string const& e = cmStrCat(
  345. "Generator\n"
  346. " ", this->GetName(), "\n"
  347. "toolset specification field\n"
  348. " buildsystem=", value, "\n"
  349. "value is unkonwn. It must be '1' or '12'."
  350. );
  351. /* clang-format on */
  352. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  353. return false;
  354. }
  355. if (this->XcodeBuildSystem == BuildSystem::Twelve &&
  356. this->XcodeVersion < 120) {
  357. /* clang-format off */
  358. std::string const& e = cmStrCat(
  359. "Generator\n"
  360. " ", this->GetName(), "\n"
  361. "toolset specification field\n"
  362. " buildsystem=", value, "\n"
  363. "is not allowed with Xcode ", this->VersionString, '.'
  364. );
  365. /* clang-format on */
  366. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  367. return false;
  368. }
  369. return true;
  370. }
  371. /* clang-format off */
  372. std::string const& e = cmStrCat(
  373. "Generator\n"
  374. " ", this->GetName(), "\n"
  375. "given toolset specification that contains invalid field '", key, "'."
  376. );
  377. /* clang-format on */
  378. mf->IssueMessage(MessageType::FATAL_ERROR, e);
  379. return false;
  380. }
  381. void cmGlobalXCodeGenerator::EnableLanguage(
  382. std::vector<std::string> const& lang, cmMakefile* mf, bool optional)
  383. {
  384. mf->AddDefinition("XCODE", "1");
  385. mf->AddDefinition("XCODE_VERSION", this->VersionString);
  386. if (!mf->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
  387. mf->AddCacheDefinition(
  388. "CMAKE_CONFIGURATION_TYPES", "Debug;Release;MinSizeRel;RelWithDebInfo",
  389. "Semicolon separated list of supported configuration types, "
  390. "only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
  391. "anything else will be ignored.",
  392. cmStateEnums::STRING);
  393. }
  394. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  395. this->cmGlobalGenerator::EnableLanguage(lang, mf, optional);
  396. this->ComputeArchitectures(mf);
  397. }
  398. bool cmGlobalXCodeGenerator::Open(const std::string& bindir,
  399. const std::string& projectName, bool dryRun)
  400. {
  401. bool ret = false;
  402. #ifdef HAVE_APPLICATION_SERVICES
  403. std::string url = bindir + "/" + projectName + ".xcodeproj";
  404. if (dryRun) {
  405. return cmSystemTools::FileExists(url, false);
  406. }
  407. CFStringRef cfStr = CFStringCreateWithCString(
  408. kCFAllocatorDefault, url.c_str(), kCFStringEncodingUTF8);
  409. if (cfStr) {
  410. CFURLRef cfUrl = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfStr,
  411. kCFURLPOSIXPathStyle, true);
  412. if (cfUrl) {
  413. OSStatus err = LSOpenCFURLRef(cfUrl, nullptr);
  414. ret = err == noErr;
  415. CFRelease(cfUrl);
  416. }
  417. CFRelease(cfStr);
  418. }
  419. #endif
  420. return ret;
  421. }
  422. std::vector<cmGlobalGenerator::GeneratedMakeCommand>
  423. cmGlobalXCodeGenerator::GenerateBuildCommand(
  424. const std::string& makeProgram, const std::string& projectName,
  425. const std::string& /*projectDir*/,
  426. std::vector<std::string> const& targetNames, const std::string& config,
  427. bool /*fast*/, int jobs, bool /*verbose*/,
  428. std::vector<std::string> const& makeOptions)
  429. {
  430. GeneratedMakeCommand makeCommand;
  431. // now build the test
  432. makeCommand.Add(
  433. this->SelectMakeProgram(makeProgram, this->GetXcodeBuildCommand()));
  434. if (!projectName.empty()) {
  435. makeCommand.Add("-project");
  436. std::string projectArg = cmStrCat(projectName, ".xcodeproj");
  437. makeCommand.Add(projectArg);
  438. }
  439. if (cm::contains(targetNames, "clean")) {
  440. makeCommand.Add("clean");
  441. makeCommand.Add("-target", "ALL_BUILD");
  442. } else {
  443. makeCommand.Add("build");
  444. if (targetNames.empty() ||
  445. ((targetNames.size() == 1) && targetNames.front().empty())) {
  446. makeCommand.Add("-target", "ALL_BUILD");
  447. } else {
  448. for (const auto& tname : targetNames) {
  449. if (!tname.empty()) {
  450. makeCommand.Add("-target", tname);
  451. }
  452. }
  453. }
  454. }
  455. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  456. makeCommand.Add("-parallelizeTargets");
  457. }
  458. makeCommand.Add("-configuration", (config.empty() ? "Debug" : config));
  459. if (jobs != cmake::NO_BUILD_PARALLEL_LEVEL) {
  460. makeCommand.Add("-jobs");
  461. if (jobs != cmake::DEFAULT_BUILD_PARALLEL_LEVEL) {
  462. makeCommand.Add(std::to_string(jobs));
  463. }
  464. }
  465. if (this->XcodeVersion >= 70) {
  466. makeCommand.Add("-hideShellScriptEnvironment");
  467. }
  468. makeCommand.Add(makeOptions.begin(), makeOptions.end());
  469. return { std::move(makeCommand) };
  470. }
  471. //! Create a local generator appropriate to this Global Generator
  472. std::unique_ptr<cmLocalGenerator> cmGlobalXCodeGenerator::CreateLocalGenerator(
  473. cmMakefile* mf)
  474. {
  475. std::unique_ptr<cmLocalGenerator> lg(
  476. cm::make_unique<cmLocalXCodeGenerator>(this, mf));
  477. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  478. // For this build system variant we generate custom commands as
  479. // shell scripts directly rather than inside Makefiles.
  480. // FIXME: Rename or refactor this option for clarity.
  481. lg->SetLinkScriptShell(true);
  482. }
  483. return lg;
  484. }
  485. void cmGlobalXCodeGenerator::AddExtraIDETargets()
  486. {
  487. // make sure extra targets are added before calling
  488. // the parent generate which will call trace depends
  489. for (auto keyVal : this->ProjectMap) {
  490. cmLocalGenerator* root = keyVal.second[0];
  491. this->SetGenerationRoot(root);
  492. // add ALL_BUILD, INSTALL, etc
  493. this->AddExtraTargets(root, keyVal.second);
  494. }
  495. }
  496. void cmGlobalXCodeGenerator::Generate()
  497. {
  498. this->cmGlobalGenerator::Generate();
  499. if (cmSystemTools::GetErrorOccuredFlag()) {
  500. return;
  501. }
  502. for (auto keyVal : this->ProjectMap) {
  503. cmLocalGenerator* root = keyVal.second[0];
  504. bool generateTopLevelProjectOnly =
  505. root->GetMakefile()->IsOn("CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY");
  506. if (generateTopLevelProjectOnly) {
  507. cmStateSnapshot snp = root->GetStateSnapshot();
  508. if (snp.GetBuildsystemDirectoryParent().IsValid()) {
  509. continue;
  510. }
  511. }
  512. // cache the enabled languages for source file type queries
  513. this->GetEnabledLanguages(this->EnabledLangs);
  514. this->SetGenerationRoot(root);
  515. // now create the project
  516. this->OutputXCodeProject(root, keyVal.second);
  517. }
  518. }
  519. void cmGlobalXCodeGenerator::SetGenerationRoot(cmLocalGenerator* root)
  520. {
  521. this->CurrentProject = root->GetProjectName();
  522. this->SetCurrentLocalGenerator(root);
  523. cmSystemTools::SplitPath(
  524. this->CurrentLocalGenerator->GetCurrentSourceDirectory(),
  525. this->ProjectSourceDirectoryComponents);
  526. cmSystemTools::SplitPath(
  527. this->CurrentLocalGenerator->GetCurrentBinaryDirectory(),
  528. this->ProjectOutputDirectoryComponents);
  529. this->CurrentXCodeHackMakefile =
  530. cmStrCat(root->GetCurrentBinaryDirectory(), "/CMakeScripts");
  531. cmSystemTools::MakeDirectory(this->CurrentXCodeHackMakefile);
  532. this->CurrentXCodeHackMakefile += "/XCODE_DEPEND_HELPER.make";
  533. }
  534. std::string cmGlobalXCodeGenerator::PostBuildMakeTarget(
  535. std::string const& tName, std::string const& configName)
  536. {
  537. std::string target = tName;
  538. std::replace(target.begin(), target.end(), ' ', '_');
  539. std::string out = cmStrCat("PostBuild.", target, '.', configName);
  540. return out;
  541. }
  542. #define CMAKE_CHECK_BUILD_SYSTEM_TARGET "ZERO_CHECK"
  543. #define OBJECT_LIBRARY_ARTIFACT_DIR std::string()
  544. void cmGlobalXCodeGenerator::AddExtraTargets(
  545. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& gens)
  546. {
  547. const char* no_working_directory = nullptr;
  548. std::vector<std::string> no_byproducts;
  549. std::vector<std::string> no_depends;
  550. // Add ALL_BUILD
  551. cmTarget* allbuild = root->AddUtilityCommand(
  552. "ALL_BUILD", true, no_working_directory, no_byproducts, no_depends,
  553. cmMakeSingleCommandLine({ "echo", "Build all projects" }));
  554. root->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(allbuild, root));
  555. // Add XCODE depend helper
  556. std::string legacyDependHelperDir = root->GetCurrentBinaryDirectory();
  557. cmCustomCommandLines legacyDependHelperCommandLines;
  558. if (this->XcodeBuildSystem == BuildSystem::One) {
  559. legacyDependHelperCommandLines = cmMakeSingleCommandLine(
  560. { "make", "-C", legacyDependHelperDir, "-f",
  561. this->CurrentXCodeHackMakefile, "OBJDIR=$(OBJDIR)",
  562. /* placeholder, see below */ "" });
  563. }
  564. // Add ZERO_CHECK
  565. bool regenerate = !this->GlobalSettingIsOn("CMAKE_SUPPRESS_REGENERATION");
  566. bool generateTopLevelProjectOnly =
  567. root->GetMakefile()->IsOn("CMAKE_XCODE_GENERATE_TOP_LEVEL_PROJECT_ONLY");
  568. bool isTopLevel =
  569. !root->GetStateSnapshot().GetBuildsystemDirectoryParent().IsValid();
  570. bool isGenerateProject = isTopLevel || !generateTopLevelProjectOnly;
  571. if (regenerate && isGenerateProject) {
  572. this->CreateReRunCMakeFile(root, gens);
  573. std::string file =
  574. this->ConvertToRelativeForMake(this->CurrentReRunCMakeMakefile);
  575. cmSystemTools::ReplaceString(file, "\\ ", " ");
  576. cmTarget* check =
  577. root->AddUtilityCommand(CMAKE_CHECK_BUILD_SYSTEM_TARGET, true,
  578. no_working_directory, no_byproducts, no_depends,
  579. cmMakeSingleCommandLine({ "make", "-f", file }));
  580. root->AddGeneratorTarget(cm::make_unique<cmGeneratorTarget>(check, root));
  581. }
  582. // now make the allbuild depend on all the non-utility targets
  583. // in the project
  584. for (auto& gen : gens) {
  585. for (const auto& target : gen->GetGeneratorTargets()) {
  586. if (target->GetType() == cmStateEnums::GLOBAL_TARGET) {
  587. continue;
  588. }
  589. if (regenerate &&
  590. (target->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET)) {
  591. target->Target->AddUtility(CMAKE_CHECK_BUILD_SYSTEM_TARGET, false);
  592. }
  593. // make all exe, shared libs and modules
  594. // run the depend check makefile as a post build rule
  595. // this will make sure that when the next target is built
  596. // things are up-to-date
  597. if (this->XcodeBuildSystem == BuildSystem::One && isGenerateProject &&
  598. target->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  599. legacyDependHelperCommandLines.front().back() = // fill placeholder
  600. this->PostBuildMakeTarget(target->GetName(), "$(CONFIGURATION)");
  601. gen->AddCustomCommandToTarget(
  602. target->GetName(), no_byproducts, no_depends,
  603. legacyDependHelperCommandLines, cmCustomCommandType::POST_BUILD,
  604. "Depend check for xcode", legacyDependHelperDir.c_str(), true, false,
  605. "", "", false, cmObjectLibraryCommands::Accept);
  606. }
  607. if (!this->IsExcluded(gens[0], target.get())) {
  608. allbuild->AddUtility(target->GetName(), false);
  609. }
  610. }
  611. }
  612. }
  613. void cmGlobalXCodeGenerator::CreateReRunCMakeFile(
  614. cmLocalGenerator* root, std::vector<cmLocalGenerator*> const& gens)
  615. {
  616. std::vector<std::string> lfiles;
  617. for (auto gen : gens) {
  618. cm::append(lfiles, gen->GetMakefile()->GetListFiles());
  619. }
  620. // sort the array
  621. std::sort(lfiles.begin(), lfiles.end());
  622. lfiles.erase(std::unique(lfiles.begin(), lfiles.end()), lfiles.end());
  623. cmake* cm = this->GetCMakeInstance();
  624. if (cm->DoWriteGlobVerifyTarget()) {
  625. lfiles.emplace_back(cm->GetGlobVerifyStamp());
  626. }
  627. this->CurrentReRunCMakeMakefile =
  628. cmStrCat(root->GetCurrentBinaryDirectory(), "/CMakeScripts");
  629. cmSystemTools::MakeDirectory(this->CurrentReRunCMakeMakefile);
  630. this->CurrentReRunCMakeMakefile += "/ReRunCMake.make";
  631. cmGeneratedFileStream makefileStream(this->CurrentReRunCMakeMakefile);
  632. makefileStream.SetCopyIfDifferent(true);
  633. makefileStream << "# Generated by CMake, DO NOT EDIT\n\n";
  634. makefileStream << "TARGETS:= \n";
  635. makefileStream << "empty:= \n";
  636. makefileStream << "space:= $(empty) $(empty)\n";
  637. makefileStream << "spaceplus:= $(empty)\\ $(empty)\n\n";
  638. for (const auto& lfile : lfiles) {
  639. makefileStream << "TARGETS += $(subst $(space),$(spaceplus),$(wildcard "
  640. << this->ConvertToRelativeForMake(lfile) << "))\n";
  641. }
  642. makefileStream << "\n";
  643. std::string checkCache =
  644. cmStrCat(root->GetBinaryDirectory(), "/CMakeFiles/cmake.check_cache");
  645. if (cm->DoWriteGlobVerifyTarget()) {
  646. makefileStream << ".NOTPARALLEL:\n\n";
  647. makefileStream << ".PHONY: all VERIFY_GLOBS\n\n";
  648. makefileStream << "all: VERIFY_GLOBS "
  649. << this->ConvertToRelativeForMake(checkCache) << "\n\n";
  650. makefileStream << "VERIFY_GLOBS:\n";
  651. makefileStream << "\t"
  652. << this->ConvertToRelativeForMake(
  653. cmSystemTools::GetCMakeCommand())
  654. << " -P "
  655. << this->ConvertToRelativeForMake(cm->GetGlobVerifyScript())
  656. << "\n\n";
  657. }
  658. makefileStream << this->ConvertToRelativeForMake(checkCache)
  659. << ": $(TARGETS)\n";
  660. makefileStream << "\t"
  661. << this->ConvertToRelativeForMake(
  662. cmSystemTools::GetCMakeCommand())
  663. << " -H"
  664. << this->ConvertToRelativeForMake(root->GetSourceDirectory())
  665. << " -B"
  666. << this->ConvertToRelativeForMake(root->GetBinaryDirectory())
  667. << "\n";
  668. }
  669. static bool objectIdLessThan(const std::unique_ptr<cmXCodeObject>& l,
  670. const std::unique_ptr<cmXCodeObject>& r)
  671. {
  672. return l->GetId() < r->GetId();
  673. }
  674. void cmGlobalXCodeGenerator::SortXCodeObjects()
  675. {
  676. std::sort(this->XCodeObjects.begin(), this->XCodeObjects.end(),
  677. objectIdLessThan);
  678. }
  679. void cmGlobalXCodeGenerator::ClearXCodeObjects()
  680. {
  681. this->TargetDoneSet.clear();
  682. this->XCodeObjects.clear();
  683. this->XCodeObjectIDs.clear();
  684. this->XCodeObjectMap.clear();
  685. this->GroupMap.clear();
  686. this->GroupNameMap.clear();
  687. this->TargetGroup.clear();
  688. this->FileRefs.clear();
  689. this->ExternalLibRefs.clear();
  690. this->FileRefToBuildFileMap.clear();
  691. this->CommandsVisited.clear();
  692. }
  693. void cmGlobalXCodeGenerator::addObject(std::unique_ptr<cmXCodeObject> obj)
  694. {
  695. if (obj->GetType() == cmXCodeObject::OBJECT) {
  696. const std::string& id = obj->GetId();
  697. // If this is a duplicate id, it's an error:
  698. //
  699. if (this->XCodeObjectIDs.count(id)) {
  700. cmSystemTools::Error(
  701. "Xcode generator: duplicate object ids not allowed");
  702. }
  703. this->XCodeObjectIDs.insert(id);
  704. }
  705. this->XCodeObjects.push_back(std::move(obj));
  706. }
  707. cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(
  708. cmXCodeObject::PBXType ptype, cm::string_view key)
  709. {
  710. auto obj = cm::make_unique<cmXCode21Object>(ptype, cmXCodeObject::OBJECT,
  711. this->GetObjectId(ptype, key));
  712. auto ptr = obj.get();
  713. this->addObject(std::move(obj));
  714. return ptr;
  715. }
  716. cmXCodeObject* cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
  717. {
  718. auto obj = cm::make_unique<cmXCodeObject>(
  719. cmXCodeObject::None, type,
  720. "Temporary cmake object, should not be referred to in Xcode file");
  721. auto ptr = obj.get();
  722. this->addObject(std::move(obj));
  723. return ptr;
  724. }
  725. cmXCodeObject* cmGlobalXCodeGenerator::CreateString(const std::string& s)
  726. {
  727. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::STRING);
  728. obj->SetString(s);
  729. return obj;
  730. }
  731. cmXCodeObject* cmGlobalXCodeGenerator::CreateObjectReference(
  732. cmXCodeObject* ref)
  733. {
  734. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::OBJECT_REF);
  735. obj->SetObject(ref);
  736. return obj;
  737. }
  738. cmXCodeObject* cmGlobalXCodeGenerator::CreateFlatClone(cmXCodeObject* orig)
  739. {
  740. cmXCodeObject* obj = this->CreateObject(orig->GetType());
  741. obj->CopyAttributes(orig);
  742. return obj;
  743. }
  744. std::string GetGroupMapKeyFromPath(cmGeneratorTarget* target,
  745. const std::string& fullpath)
  746. {
  747. std::string key(target->GetName());
  748. key += "-";
  749. key += fullpath;
  750. return key;
  751. }
  752. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeBuildFileFromPath(
  753. const std::string& fullpath, cmGeneratorTarget* target,
  754. const std::string& lang, cmSourceFile* sf)
  755. {
  756. // Using a map and the full path guarantees that we will always get the same
  757. // fileRef object for any given full path. Same goes for the buildFile
  758. // object.
  759. cmXCodeObject* fileRef =
  760. this->CreateXCodeFileReferenceFromPath(fullpath, target, lang, sf);
  761. if (fileRef) {
  762. auto it = this->FileRefToBuildFileMap.find(fileRef);
  763. if (it == this->FileRefToBuildFileMap.end()) {
  764. cmXCodeObject* buildFile =
  765. this->CreateObject(cmXCodeObject::PBXBuildFile);
  766. buildFile->SetComment(fileRef->GetComment());
  767. buildFile->AddAttribute("fileRef", this->CreateObjectReference(fileRef));
  768. this->FileRefToBuildFileMap[fileRef] = buildFile;
  769. return buildFile;
  770. }
  771. return it->second;
  772. }
  773. return nullptr;
  774. }
  775. class XCodeGeneratorExpressionInterpreter
  776. : public cmGeneratorExpressionInterpreter
  777. {
  778. public:
  779. XCodeGeneratorExpressionInterpreter(cmSourceFile* sourceFile,
  780. cmLocalGenerator* localGenerator,
  781. cmGeneratorTarget* headTarget,
  782. const std::string& lang)
  783. : cmGeneratorExpressionInterpreter(
  784. localGenerator, "NO-PER-CONFIG-SUPPORT-IN-XCODE", headTarget, lang)
  785. , SourceFile(sourceFile)
  786. {
  787. }
  788. XCodeGeneratorExpressionInterpreter(
  789. XCodeGeneratorExpressionInterpreter const&) = delete;
  790. XCodeGeneratorExpressionInterpreter& operator=(
  791. XCodeGeneratorExpressionInterpreter const&) = delete;
  792. const std::string& Evaluate(const char* expression,
  793. const std::string& property)
  794. {
  795. return this->Evaluate(std::string(expression ? expression : ""), property);
  796. }
  797. const std::string& Evaluate(const std::string& expression,
  798. const std::string& property)
  799. {
  800. const std::string& processed =
  801. this->cmGeneratorExpressionInterpreter::Evaluate(expression, property);
  802. if (this->CompiledGeneratorExpression->GetHadContextSensitiveCondition()) {
  803. std::ostringstream e;
  804. /* clang-format off */
  805. e <<
  806. "Xcode does not support per-config per-source " << property << ":\n"
  807. " " << expression << "\n"
  808. "specified for source:\n"
  809. " " << this->SourceFile->ResolveFullPath() << "\n";
  810. /* clang-format on */
  811. this->LocalGenerator->IssueMessage(MessageType::FATAL_ERROR, e.str());
  812. }
  813. return processed;
  814. }
  815. private:
  816. cmSourceFile* SourceFile = nullptr;
  817. };
  818. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeSourceFile(
  819. cmLocalGenerator* lg, cmSourceFile* sf, cmGeneratorTarget* gtgt)
  820. {
  821. std::string lang = this->CurrentLocalGenerator->GetSourceFileLanguage(*sf);
  822. XCodeGeneratorExpressionInterpreter genexInterpreter(sf, lg, gtgt, lang);
  823. // Add flags from target and source file properties.
  824. std::string flags;
  825. std::string const& srcfmt = sf->GetSafeProperty("Fortran_FORMAT");
  826. switch (cmOutputConverter::GetFortranFormat(srcfmt)) {
  827. case cmOutputConverter::FortranFormatFixed:
  828. flags = "-fixed " + flags;
  829. break;
  830. case cmOutputConverter::FortranFormatFree:
  831. flags = "-free " + flags;
  832. break;
  833. default:
  834. break;
  835. }
  836. const std::string COMPILE_FLAGS("COMPILE_FLAGS");
  837. if (cmProp cflags = sf->GetProperty(COMPILE_FLAGS)) {
  838. lg->AppendFlags(flags, genexInterpreter.Evaluate(*cflags, COMPILE_FLAGS));
  839. }
  840. const std::string COMPILE_OPTIONS("COMPILE_OPTIONS");
  841. if (cmProp coptions = sf->GetProperty(COMPILE_OPTIONS)) {
  842. lg->AppendCompileOptions(
  843. flags, genexInterpreter.Evaluate(*coptions, COMPILE_OPTIONS));
  844. }
  845. // Add per-source definitions.
  846. BuildObjectListOrString flagsBuild(this, false);
  847. const std::string COMPILE_DEFINITIONS("COMPILE_DEFINITIONS");
  848. if (cmProp compile_defs = sf->GetProperty(COMPILE_DEFINITIONS)) {
  849. this->AppendDefines(
  850. flagsBuild,
  851. genexInterpreter.Evaluate(*compile_defs, COMPILE_DEFINITIONS).c_str(),
  852. true);
  853. }
  854. if (sf->GetPropertyAsBool("SKIP_PRECOMPILE_HEADERS")) {
  855. this->AppendDefines(flagsBuild, "CMAKE_SKIP_PRECOMPILE_HEADERS", true);
  856. }
  857. if (!flagsBuild.IsEmpty()) {
  858. if (!flags.empty()) {
  859. flags += ' ';
  860. }
  861. flags += flagsBuild.GetString();
  862. }
  863. // Add per-source include directories.
  864. std::vector<std::string> includes;
  865. const std::string INCLUDE_DIRECTORIES("INCLUDE_DIRECTORIES");
  866. if (cmProp cincludes = sf->GetProperty(INCLUDE_DIRECTORIES)) {
  867. lg->AppendIncludeDirectories(
  868. includes, genexInterpreter.Evaluate(*cincludes, INCLUDE_DIRECTORIES),
  869. *sf);
  870. }
  871. lg->AppendFlags(flags, lg->GetIncludeFlags(includes, gtgt, lang, true));
  872. cmXCodeObject* buildFile =
  873. this->CreateXCodeBuildFileFromPath(sf->ResolveFullPath(), gtgt, lang, sf);
  874. cmXCodeObject* settings = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  875. settings->AddAttributeIfNotEmpty("COMPILER_FLAGS",
  876. this->CreateString(flags));
  877. cmGeneratorTarget::SourceFileFlags tsFlags =
  878. gtgt->GetTargetSourceFileFlags(sf);
  879. cmXCodeObject* attrs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  880. // Is this a "private" or "public" framework header file?
  881. // Set the ATTRIBUTES attribute appropriately...
  882. //
  883. if (gtgt->IsFrameworkOnApple()) {
  884. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypePrivateHeader) {
  885. attrs->AddObject(this->CreateString("Private"));
  886. } else if (tsFlags.Type == cmGeneratorTarget::SourceFileTypePublicHeader) {
  887. attrs->AddObject(this->CreateString("Public"));
  888. }
  889. }
  890. // Add user-specified file attributes.
  891. cmProp extraFileAttributes = sf->GetProperty("XCODE_FILE_ATTRIBUTES");
  892. if (extraFileAttributes) {
  893. // Expand the list of attributes.
  894. std::vector<std::string> attributes = cmExpandedList(*extraFileAttributes);
  895. // Store the attributes.
  896. for (const auto& attribute : attributes) {
  897. attrs->AddObject(this->CreateString(attribute));
  898. }
  899. }
  900. settings->AddAttributeIfNotEmpty("ATTRIBUTES", attrs);
  901. if (buildFile) {
  902. buildFile->AddAttributeIfNotEmpty("settings", settings);
  903. }
  904. return buildFile;
  905. }
  906. void cmGlobalXCodeGenerator::AddXCodeProjBuildRule(
  907. cmGeneratorTarget* target, std::vector<cmSourceFile*>& sources) const
  908. {
  909. std::string listfile =
  910. cmStrCat(target->GetLocalGenerator()->GetCurrentSourceDirectory(),
  911. "/CMakeLists.txt");
  912. cmSourceFile* srcCMakeLists = target->Makefile->GetOrCreateSource(
  913. listfile, false, cmSourceFileLocationKind::Known);
  914. if (!cm::contains(sources, srcCMakeLists)) {
  915. sources.push_back(srcCMakeLists);
  916. }
  917. }
  918. namespace {
  919. bool IsLinkPhaseLibraryExtension(const std::string& fileExt)
  920. {
  921. // Empty file extension is a special case for paths to framework's
  922. // internal binary which could be MyFw.framework/Versions/*/MyFw
  923. return (fileExt == ".framework" || fileExt == ".a" || fileExt == ".o" ||
  924. fileExt == ".dylib" || fileExt == ".tbd" || fileExt.empty());
  925. }
  926. bool IsLibraryType(const std::string& fileType)
  927. {
  928. return (fileType == "wrapper.framework" || fileType == "archive.ar" ||
  929. fileType == "compiled.mach-o.objfile" ||
  930. fileType == "compiled.mach-o.dylib" ||
  931. fileType == "compiled.mach-o.executable" ||
  932. fileType == "sourcecode.text-based-dylib-definition");
  933. }
  934. std::string GetDirectoryValueFromFileExtension(const std::string& dirExt)
  935. {
  936. std::string ext = cmSystemTools::LowerCase(dirExt);
  937. if (ext == "framework") {
  938. return "wrapper.framework";
  939. }
  940. if (ext == "xcassets") {
  941. return "folder.assetcatalog";
  942. }
  943. return "folder";
  944. }
  945. std::string GetSourcecodeValueFromFileExtension(
  946. const std::string& _ext, const std::string& lang,
  947. bool& keepLastKnownFileType, const std::vector<std::string>& enabled_langs)
  948. {
  949. std::string ext = cmSystemTools::LowerCase(_ext);
  950. std::string sourcecode = "sourcecode";
  951. if (ext == "o") {
  952. keepLastKnownFileType = true;
  953. sourcecode = "compiled.mach-o.objfile";
  954. } else if (ext == "xctest") {
  955. sourcecode = "wrapper.cfbundle";
  956. } else if (ext == "xib") {
  957. keepLastKnownFileType = true;
  958. sourcecode = "file.xib";
  959. } else if (ext == "storyboard") {
  960. keepLastKnownFileType = true;
  961. sourcecode = "file.storyboard";
  962. } else if (ext == "mm" && !cm::contains(enabled_langs, "OBJCXX")) {
  963. sourcecode += ".cpp.objcpp";
  964. } else if (ext == "m" && !cm::contains(enabled_langs, "OBJC")) {
  965. sourcecode += ".c.objc";
  966. } else if (ext == "swift") {
  967. sourcecode += ".swift";
  968. } else if (ext == "plist") {
  969. sourcecode += ".text.plist";
  970. } else if (ext == "h") {
  971. sourcecode += ".c.h";
  972. } else if (ext == "hxx" || ext == "hpp" || ext == "txx" || ext == "pch" ||
  973. ext == "hh") {
  974. sourcecode += ".cpp.h";
  975. } else if (ext == "png" || ext == "gif" || ext == "jpg") {
  976. keepLastKnownFileType = true;
  977. sourcecode = "image";
  978. } else if (ext == "txt") {
  979. sourcecode += ".text";
  980. } else if (lang == "CXX") {
  981. sourcecode += ".cpp.cpp";
  982. } else if (lang == "C") {
  983. sourcecode += ".c.c";
  984. } else if (lang == "OBJCXX") {
  985. sourcecode += ".cpp.objcpp";
  986. } else if (lang == "OBJC") {
  987. sourcecode += ".c.objc";
  988. } else if (lang == "Fortran") {
  989. sourcecode += ".fortran.f90";
  990. } else if (lang == "ASM") {
  991. sourcecode += ".asm";
  992. } else if (ext == "metal") {
  993. sourcecode += ".metal";
  994. } else if (ext == "mig") {
  995. sourcecode += ".mig";
  996. } else if (ext == "tbd") {
  997. sourcecode += ".text-based-dylib-definition";
  998. } else if (ext == "a") {
  999. keepLastKnownFileType = true;
  1000. sourcecode = "archive.ar";
  1001. } else if (ext == "dylib") {
  1002. keepLastKnownFileType = true;
  1003. sourcecode = "compiled.mach-o.dylib";
  1004. } else if (ext == "framework") {
  1005. keepLastKnownFileType = true;
  1006. sourcecode = "wrapper.framework";
  1007. } else if (ext == "xcassets") {
  1008. keepLastKnownFileType = true;
  1009. sourcecode = "folder.assetcatalog";
  1010. }
  1011. // else
  1012. // {
  1013. // // Already specialized above or we leave sourcecode == "sourcecode"
  1014. // // which is probably the most correct choice. Extensionless headers,
  1015. // // for example... Or file types unknown to Xcode that do not map to a
  1016. // // valid explicitFileType value.
  1017. // }
  1018. return sourcecode;
  1019. }
  1020. // If the file has no extension it's either a raw executable or might
  1021. // be a direct reference to a binary within a framework (bad practice!).
  1022. // This is where we change the path to point to the framework directory.
  1023. // .tbd files also can be located in SDK frameworks (they are
  1024. // placeholders for actual libraries shipped with the OS)
  1025. std::string GetLibraryOrFrameworkPath(const std::string& path)
  1026. {
  1027. auto ext = cmSystemTools::GetFilenameLastExtension(path);
  1028. if (ext.empty() || ext == ".tbd") {
  1029. auto name = cmSystemTools::GetFilenameWithoutExtension(path);
  1030. // Check for iOS framework structure:
  1031. // FwName.framework/FwName (and also on macOS where FwName lib is a
  1032. // symlink)
  1033. auto parentDir = cmSystemTools::GetParentDirectory(path);
  1034. auto parentName = cmSystemTools::GetFilenameWithoutExtension(parentDir);
  1035. ext = cmSystemTools::GetFilenameLastExtension(parentDir);
  1036. if (ext == ".framework" && name == parentName) {
  1037. return parentDir;
  1038. }
  1039. // Check for macOS framework structure:
  1040. // FwName.framework/Versions/*/FwName
  1041. std::vector<std::string> components;
  1042. cmSystemTools::SplitPath(path, components);
  1043. if (components.size() > 3 &&
  1044. components[components.size() - 3] == "Versions") {
  1045. ext = cmSystemTools::GetFilenameLastExtension(
  1046. components[components.size() - 4]);
  1047. parentName = cmSystemTools::GetFilenameWithoutExtension(
  1048. components[components.size() - 4]);
  1049. if (ext == ".framework" && name == parentName) {
  1050. components.erase(components.begin() + components.size() - 3,
  1051. components.end());
  1052. return cmSystemTools::JoinPath(components);
  1053. }
  1054. }
  1055. }
  1056. return path;
  1057. }
  1058. } // anonymous
  1059. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeFileReferenceFromPath(
  1060. const std::string& fullpath, cmGeneratorTarget* target,
  1061. const std::string& lang, cmSourceFile* sf)
  1062. {
  1063. bool useLastKnownFileType = false;
  1064. std::string fileType;
  1065. if (sf) {
  1066. if (cmProp e = sf->GetProperty("XCODE_EXPLICIT_FILE_TYPE")) {
  1067. fileType = *e;
  1068. } else if (cmProp l = sf->GetProperty("XCODE_LAST_KNOWN_FILE_TYPE")) {
  1069. useLastKnownFileType = true;
  1070. fileType = *l;
  1071. }
  1072. }
  1073. // Make a copy so that we can override it later
  1074. std::string path = fullpath;
  1075. // Compute the extension without leading '.'.
  1076. std::string ext = cmSystemTools::GetFilenameLastExtension(path);
  1077. if (!ext.empty()) {
  1078. ext = ext.substr(1);
  1079. }
  1080. if (fileType.empty()) {
  1081. path = GetLibraryOrFrameworkPath(path);
  1082. ext = cmSystemTools::GetFilenameLastExtension(path);
  1083. if (!ext.empty()) {
  1084. ext = ext.substr(1);
  1085. }
  1086. // If fullpath references a directory, then we need to specify
  1087. // lastKnownFileType as folder in order for Xcode to be able to
  1088. // open the contents of the folder.
  1089. // (Xcode 4.6 does not like explicitFileType=folder).
  1090. if (cmSystemTools::FileIsDirectory(path)) {
  1091. fileType = GetDirectoryValueFromFileExtension(ext);
  1092. useLastKnownFileType = true;
  1093. } else {
  1094. if (ext.empty() && !sf) {
  1095. // Special case for executable or library without extension
  1096. // that is not a source file. We can't tell which without reading
  1097. // its Mach-O header, but the file might not exist yet, so we
  1098. // have to pick one here.
  1099. useLastKnownFileType = true;
  1100. fileType = "compiled.mach-o.executable";
  1101. } else {
  1102. fileType = GetSourcecodeValueFromFileExtension(
  1103. ext, lang, useLastKnownFileType, this->EnabledLangs);
  1104. }
  1105. }
  1106. }
  1107. std::string key = GetGroupMapKeyFromPath(target, path);
  1108. cmXCodeObject* fileRef = this->FileRefs[key];
  1109. if (!fileRef) {
  1110. fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  1111. fileRef->SetComment(path);
  1112. this->FileRefs[key] = fileRef;
  1113. }
  1114. fileRef->AddAttribute("fileEncoding", this->CreateString("4"));
  1115. fileRef->AddAttribute(useLastKnownFileType ? "lastKnownFileType"
  1116. : "explicitFileType",
  1117. this->CreateString(fileType));
  1118. // Store the file path relative to the top of the source tree.
  1119. if (!IsLibraryType(fileType)) {
  1120. path = this->RelativeToSource(path);
  1121. }
  1122. std::string name = cmSystemTools::GetFilenameName(path);
  1123. const char* sourceTree =
  1124. cmSystemTools::FileIsFullPath(path) ? "<absolute>" : "SOURCE_ROOT";
  1125. fileRef->AddAttribute("name", this->CreateString(name));
  1126. fileRef->AddAttribute("path", this->CreateString(path));
  1127. fileRef->AddAttribute("sourceTree", this->CreateString(sourceTree));
  1128. cmXCodeObject* group = this->GroupMap[key];
  1129. if (!group && IsLibraryType(fileType)) {
  1130. group = this->FrameworkGroup;
  1131. this->GroupMap[key] = group;
  1132. }
  1133. if (!group) {
  1134. cmSystemTools::Error("Could not find a PBX group for " + key);
  1135. return nullptr;
  1136. }
  1137. cmXCodeObject* children = group->GetAttribute("children");
  1138. if (!children->HasObject(fileRef)) {
  1139. children->AddObject(fileRef);
  1140. }
  1141. return fileRef;
  1142. }
  1143. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeFileReference(
  1144. cmSourceFile* sf, cmGeneratorTarget* target)
  1145. {
  1146. std::string lang = this->CurrentLocalGenerator->GetSourceFileLanguage(*sf);
  1147. return this->CreateXCodeFileReferenceFromPath(sf->ResolveFullPath(), target,
  1148. lang, sf);
  1149. }
  1150. bool cmGlobalXCodeGenerator::SpecialTargetEmitted(std::string const& tname)
  1151. {
  1152. if (tname == "ALL_BUILD" || tname == "install" || tname == "package" ||
  1153. tname == "RUN_TESTS" || tname == CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  1154. if (this->TargetDoneSet.find(tname) != this->TargetDoneSet.end()) {
  1155. return true;
  1156. }
  1157. this->TargetDoneSet.insert(tname);
  1158. return false;
  1159. }
  1160. return false;
  1161. }
  1162. void cmGlobalXCodeGenerator::SetCurrentLocalGenerator(cmLocalGenerator* gen)
  1163. {
  1164. this->CurrentLocalGenerator = gen;
  1165. this->CurrentMakefile = gen->GetMakefile();
  1166. // Select the current set of configuration types.
  1167. this->CurrentConfigurationTypes =
  1168. this->CurrentMakefile->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
  1169. }
  1170. struct cmSourceFilePathCompare
  1171. {
  1172. bool operator()(cmSourceFile* l, cmSourceFile* r)
  1173. {
  1174. return l->ResolveFullPath() < r->ResolveFullPath();
  1175. }
  1176. };
  1177. struct cmCompareTargets
  1178. {
  1179. bool operator()(cmXCodeObject* l, cmXCodeObject* r) const
  1180. {
  1181. std::string const& a = l->GetTarget()->GetName();
  1182. std::string const& b = r->GetTarget()->GetName();
  1183. if (a == "ALL_BUILD") {
  1184. return true;
  1185. }
  1186. if (b == "ALL_BUILD") {
  1187. return false;
  1188. }
  1189. return a < b;
  1190. }
  1191. };
  1192. bool cmGlobalXCodeGenerator::CreateXCodeTargets(
  1193. cmLocalGenerator* gen, std::vector<cmXCodeObject*>& targets)
  1194. {
  1195. this->SetCurrentLocalGenerator(gen);
  1196. std::vector<cmGeneratorTarget*> gts =
  1197. this->GetLocalGeneratorTargetsInOrder(gen);
  1198. for (auto gtgt : gts) {
  1199. if (!this->CreateXCodeTarget(gtgt, targets)) {
  1200. return false;
  1201. }
  1202. }
  1203. std::sort(targets.begin(), targets.end(), cmCompareTargets());
  1204. return true;
  1205. }
  1206. bool cmGlobalXCodeGenerator::CreateXCodeTarget(
  1207. cmGeneratorTarget* gtgt, std::vector<cmXCodeObject*>& targets)
  1208. {
  1209. std::string targetName = gtgt->GetName();
  1210. // make sure ALL_BUILD, INSTALL, etc are only done once
  1211. if (this->SpecialTargetEmitted(targetName)) {
  1212. return true;
  1213. }
  1214. if (!gtgt->IsInBuildSystem()) {
  1215. return true;
  1216. }
  1217. auto& gtgt_visited = this->CommandsVisited[gtgt];
  1218. auto& deps = this->GetTargetDirectDepends(gtgt);
  1219. for (auto& d : deps) {
  1220. // Take the union of visited source files of custom commands so far.
  1221. // ComputeTargetOrder ensures our dependencies already visited their
  1222. // custom commands and updated CommandsVisited.
  1223. auto& dep_visited = this->CommandsVisited[d];
  1224. gtgt_visited.insert(dep_visited.begin(), dep_visited.end());
  1225. }
  1226. if (gtgt->GetType() == cmStateEnums::UTILITY ||
  1227. gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY ||
  1228. gtgt->GetType() == cmStateEnums::GLOBAL_TARGET) {
  1229. cmXCodeObject* t = this->CreateUtilityTarget(gtgt);
  1230. if (!t) {
  1231. return false;
  1232. }
  1233. targets.push_back(t);
  1234. return true;
  1235. }
  1236. // organize the sources
  1237. std::vector<cmSourceFile*> commonSourceFiles;
  1238. if (!gtgt->GetConfigCommonSourceFiles(commonSourceFiles)) {
  1239. return false;
  1240. }
  1241. // Add CMakeLists.txt file for user convenience.
  1242. this->AddXCodeProjBuildRule(gtgt, commonSourceFiles);
  1243. // Add the Info.plist we are about to generate for an App Bundle.
  1244. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  1245. std::string plist = this->ComputeInfoPListLocation(gtgt);
  1246. cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
  1247. plist, true, cmSourceFileLocationKind::Known);
  1248. commonSourceFiles.push_back(sf);
  1249. }
  1250. std::sort(commonSourceFiles.begin(), commonSourceFiles.end(),
  1251. cmSourceFilePathCompare());
  1252. gtgt->ComputeObjectMapping();
  1253. std::vector<cmXCodeObject*> externalObjFiles;
  1254. std::vector<cmXCodeObject*> headerFiles;
  1255. std::vector<cmXCodeObject*> resourceFiles;
  1256. std::vector<cmXCodeObject*> sourceFiles;
  1257. for (auto sourceFile : commonSourceFiles) {
  1258. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1259. this->CurrentLocalGenerator, sourceFile, gtgt);
  1260. cmXCodeObject* fr = xsf->GetAttribute("fileRef");
  1261. cmXCodeObject* filetype =
  1262. fr->GetObject()->GetAttribute("explicitFileType");
  1263. if (!filetype) {
  1264. filetype = fr->GetObject()->GetAttribute("lastKnownFileType");
  1265. }
  1266. cmGeneratorTarget::SourceFileFlags tsFlags =
  1267. gtgt->GetTargetSourceFileFlags(sourceFile);
  1268. if (filetype && filetype->GetString() == "compiled.mach-o.objfile") {
  1269. if (sourceFile->GetObjectLibrary().empty()) {
  1270. externalObjFiles.push_back(xsf);
  1271. }
  1272. } else if (this->IsHeaderFile(sourceFile) ||
  1273. (tsFlags.Type ==
  1274. cmGeneratorTarget::SourceFileTypePrivateHeader) ||
  1275. (tsFlags.Type ==
  1276. cmGeneratorTarget::SourceFileTypePublicHeader)) {
  1277. headerFiles.push_back(xsf);
  1278. } else if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeResource) {
  1279. resourceFiles.push_back(xsf);
  1280. } else if (!sourceFile->GetPropertyAsBool("HEADER_FILE_ONLY") &&
  1281. !gtgt->IsSourceFilePartOfUnityBatch(
  1282. sourceFile->ResolveFullPath())) {
  1283. // Include this file in the build if it has a known language
  1284. // and has not been listed as an ignored extension for this
  1285. // generator.
  1286. if (!this->CurrentLocalGenerator->GetSourceFileLanguage(*sourceFile)
  1287. .empty() &&
  1288. !this->IgnoreFile(sourceFile->GetExtension().c_str())) {
  1289. sourceFiles.push_back(xsf);
  1290. }
  1291. }
  1292. }
  1293. // some build phases only apply to bundles and/or frameworks
  1294. bool isFrameworkTarget = gtgt->IsFrameworkOnApple();
  1295. bool isBundleTarget = gtgt->GetPropertyAsBool("MACOSX_BUNDLE");
  1296. bool isCFBundleTarget = gtgt->IsCFBundleOnApple();
  1297. cmXCodeObject* buildFiles = nullptr;
  1298. // create source build phase
  1299. cmXCodeObject* sourceBuildPhase = nullptr;
  1300. if (!sourceFiles.empty()) {
  1301. sourceBuildPhase = this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  1302. sourceBuildPhase->SetComment("Sources");
  1303. sourceBuildPhase->AddAttribute("buildActionMask",
  1304. this->CreateString("2147483647"));
  1305. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1306. for (auto& sourceFile : sourceFiles) {
  1307. buildFiles->AddObject(sourceFile);
  1308. }
  1309. sourceBuildPhase->AddAttribute("files", buildFiles);
  1310. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1311. this->CreateString("0"));
  1312. }
  1313. // create header build phase - only for framework targets
  1314. cmXCodeObject* headerBuildPhase = nullptr;
  1315. if (!headerFiles.empty() && isFrameworkTarget) {
  1316. headerBuildPhase = this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  1317. headerBuildPhase->SetComment("Headers");
  1318. headerBuildPhase->AddAttribute("buildActionMask",
  1319. this->CreateString("2147483647"));
  1320. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1321. for (auto& headerFile : headerFiles) {
  1322. buildFiles->AddObject(headerFile);
  1323. }
  1324. headerBuildPhase->AddAttribute("files", buildFiles);
  1325. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1326. this->CreateString("0"));
  1327. }
  1328. // create resource build phase - only for framework or bundle targets
  1329. cmXCodeObject* resourceBuildPhase = nullptr;
  1330. if (!resourceFiles.empty() &&
  1331. (isFrameworkTarget || isBundleTarget || isCFBundleTarget)) {
  1332. resourceBuildPhase =
  1333. this->CreateObject(cmXCodeObject::PBXResourcesBuildPhase);
  1334. resourceBuildPhase->SetComment("Resources");
  1335. resourceBuildPhase->AddAttribute("buildActionMask",
  1336. this->CreateString("2147483647"));
  1337. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1338. for (auto& resourceFile : resourceFiles) {
  1339. buildFiles->AddObject(resourceFile);
  1340. }
  1341. resourceBuildPhase->AddAttribute("files", buildFiles);
  1342. resourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1343. this->CreateString("0"));
  1344. }
  1345. // create vector of "non-resource content file" build phases - only for
  1346. // framework or bundle targets
  1347. std::vector<cmXCodeObject*> contentBuildPhases;
  1348. if (isFrameworkTarget || isBundleTarget || isCFBundleTarget) {
  1349. using mapOfVectorOfSourceFiles =
  1350. std::map<std::string, std::vector<cmSourceFile*>>;
  1351. mapOfVectorOfSourceFiles bundleFiles;
  1352. for (auto sourceFile : commonSourceFiles) {
  1353. cmGeneratorTarget::SourceFileFlags tsFlags =
  1354. gtgt->GetTargetSourceFileFlags(sourceFile);
  1355. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeMacContent) {
  1356. bundleFiles[tsFlags.MacFolder].push_back(sourceFile);
  1357. }
  1358. }
  1359. for (auto const& keySources : bundleFiles) {
  1360. cmXCodeObject* copyFilesBuildPhase =
  1361. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  1362. copyFilesBuildPhase->SetComment("Copy files");
  1363. copyFilesBuildPhase->AddAttribute("buildActionMask",
  1364. this->CreateString("2147483647"));
  1365. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  1366. this->CreateString("6"));
  1367. std::ostringstream ostr;
  1368. if (gtgt->IsFrameworkOnApple()) {
  1369. // dstPath in frameworks is relative to Versions/<version>
  1370. ostr << keySources.first;
  1371. } else if (keySources.first != "MacOS") {
  1372. if (gtgt->Target->GetMakefile()->PlatformIsAppleEmbedded()) {
  1373. ostr << keySources.first;
  1374. } else {
  1375. // dstPath in bundles is relative to Contents/MacOS
  1376. ostr << "../" << keySources.first;
  1377. }
  1378. }
  1379. copyFilesBuildPhase->AddAttribute("dstPath",
  1380. this->CreateString(ostr.str()));
  1381. copyFilesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1382. this->CreateString("0"));
  1383. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1384. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  1385. for (auto sourceFile : keySources.second) {
  1386. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1387. this->CurrentLocalGenerator, sourceFile, gtgt);
  1388. buildFiles->AddObject(xsf);
  1389. }
  1390. contentBuildPhases.push_back(copyFilesBuildPhase);
  1391. }
  1392. }
  1393. // create vector of "resource content file" build phases - only for
  1394. // framework or bundle targets
  1395. if (isFrameworkTarget || isBundleTarget || isCFBundleTarget) {
  1396. using mapOfVectorOfSourceFiles =
  1397. std::map<std::string, std::vector<cmSourceFile*>>;
  1398. mapOfVectorOfSourceFiles bundleFiles;
  1399. for (auto sourceFile : commonSourceFiles) {
  1400. cmGeneratorTarget::SourceFileFlags tsFlags =
  1401. gtgt->GetTargetSourceFileFlags(sourceFile);
  1402. if (tsFlags.Type == cmGeneratorTarget::SourceFileTypeDeepResource) {
  1403. bundleFiles[tsFlags.MacFolder].push_back(sourceFile);
  1404. }
  1405. }
  1406. for (auto const& keySources : bundleFiles) {
  1407. cmXCodeObject* copyFilesBuildPhase =
  1408. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  1409. copyFilesBuildPhase->SetComment("Copy files");
  1410. copyFilesBuildPhase->AddAttribute("buildActionMask",
  1411. this->CreateString("2147483647"));
  1412. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  1413. this->CreateString("7"));
  1414. copyFilesBuildPhase->AddAttribute("dstPath",
  1415. this->CreateString(keySources.first));
  1416. copyFilesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1417. this->CreateString("0"));
  1418. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1419. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  1420. for (auto sourceFile : keySources.second) {
  1421. cmXCodeObject* xsf = this->CreateXCodeSourceFile(
  1422. this->CurrentLocalGenerator, sourceFile, gtgt);
  1423. buildFiles->AddObject(xsf);
  1424. }
  1425. contentBuildPhases.push_back(copyFilesBuildPhase);
  1426. }
  1427. }
  1428. // Always create Link Binary With Libraries build phase
  1429. cmXCodeObject* frameworkBuildPhase = nullptr;
  1430. frameworkBuildPhase =
  1431. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  1432. frameworkBuildPhase->SetComment("Frameworks");
  1433. frameworkBuildPhase->AddAttribute("buildActionMask",
  1434. this->CreateString("2147483647"));
  1435. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1436. frameworkBuildPhase->AddAttribute("files", buildFiles);
  1437. // Add all collected .o files to this build phase
  1438. for (auto& externalObjFile : externalObjFiles) {
  1439. buildFiles->AddObject(externalObjFile);
  1440. }
  1441. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1442. this->CreateString("0"));
  1443. // create list of build phases and create the Xcode target
  1444. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1445. this->CreateCustomCommands(buildPhases, sourceBuildPhase, headerBuildPhase,
  1446. resourceBuildPhase, contentBuildPhases,
  1447. frameworkBuildPhase, gtgt);
  1448. targets.push_back(this->CreateXCodeTarget(gtgt, buildPhases));
  1449. return true;
  1450. }
  1451. void cmGlobalXCodeGenerator::ForceLinkerLanguages()
  1452. {
  1453. for (const auto& localGenerator : this->LocalGenerators) {
  1454. // All targets depend on the build-system check target.
  1455. for (const auto& tgt : localGenerator->GetGeneratorTargets()) {
  1456. // This makes sure all targets link using the proper language.
  1457. this->ForceLinkerLanguage(tgt.get());
  1458. }
  1459. }
  1460. }
  1461. void cmGlobalXCodeGenerator::ForceLinkerLanguage(cmGeneratorTarget* gtgt)
  1462. {
  1463. // This matters only for targets that link.
  1464. if (gtgt->GetType() != cmStateEnums::EXECUTABLE &&
  1465. gtgt->GetType() != cmStateEnums::SHARED_LIBRARY &&
  1466. gtgt->GetType() != cmStateEnums::MODULE_LIBRARY) {
  1467. return;
  1468. }
  1469. std::string llang = gtgt->GetLinkerLanguage("NOCONFIG");
  1470. if (llang.empty()) {
  1471. return;
  1472. }
  1473. // If the language is compiled as a source trust Xcode to link with it.
  1474. for (auto const& Language :
  1475. gtgt->GetLinkImplementation("NOCONFIG")->Languages) {
  1476. if (Language == llang) {
  1477. return;
  1478. }
  1479. }
  1480. // Add an empty source file to the target that compiles with the
  1481. // linker language. This should convince Xcode to choose the proper
  1482. // language.
  1483. cmMakefile* mf = gtgt->Target->GetMakefile();
  1484. std::string fname = cmStrCat(
  1485. gtgt->GetLocalGenerator()->GetCurrentBinaryDirectory(), "/CMakeFiles/",
  1486. gtgt->GetName(), "-CMakeForceLinker.", cmSystemTools::LowerCase(llang));
  1487. {
  1488. cmGeneratedFileStream fout(fname);
  1489. fout << "\n";
  1490. }
  1491. if (cmSourceFile* sf = mf->GetOrCreateSource(fname)) {
  1492. sf->SetProperty("LANGUAGE", llang.c_str());
  1493. gtgt->AddSource(fname);
  1494. }
  1495. }
  1496. bool cmGlobalXCodeGenerator::IsHeaderFile(cmSourceFile* sf)
  1497. {
  1498. return cm::contains(this->CMakeInstance->GetHeaderExtensions(),
  1499. sf->GetExtension());
  1500. }
  1501. cmXCodeObject* cmGlobalXCodeGenerator::CreateLegacyRunScriptBuildPhase(
  1502. const char* name, const char* name2, cmGeneratorTarget* target,
  1503. const std::vector<cmCustomCommand>& commands)
  1504. {
  1505. if (commands.empty() && strcmp(name, "CMake ReRun") != 0) {
  1506. return nullptr;
  1507. }
  1508. cmXCodeObject* buildPhase =
  1509. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1510. buildPhase->AddAttribute("buildActionMask",
  1511. this->CreateString("2147483647"));
  1512. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1513. buildPhase->AddAttribute("files", buildFiles);
  1514. buildPhase->AddAttribute("name", this->CreateString(name));
  1515. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1516. this->CreateString("0"));
  1517. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1518. this->AddCommandsToBuildPhase(buildPhase, target, commands, name2);
  1519. return buildPhase;
  1520. }
  1521. void cmGlobalXCodeGenerator::CreateCustomCommands(
  1522. cmXCodeObject* buildPhases, cmXCodeObject* sourceBuildPhase,
  1523. cmXCodeObject* headerBuildPhase, cmXCodeObject* resourceBuildPhase,
  1524. std::vector<cmXCodeObject*> const& contentBuildPhases,
  1525. cmXCodeObject* frameworkBuildPhase, cmGeneratorTarget* gtgt)
  1526. {
  1527. std::vector<cmCustomCommand> const& prebuild = gtgt->GetPreBuildCommands();
  1528. std::vector<cmCustomCommand> const& prelink = gtgt->GetPreLinkCommands();
  1529. std::vector<cmCustomCommand> postbuild = gtgt->GetPostBuildCommands();
  1530. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY &&
  1531. !gtgt->IsFrameworkOnApple()) {
  1532. std::string str_file = cmStrCat("$<TARGET_FILE:", gtgt->GetName(), '>');
  1533. std::string str_so_file =
  1534. cmStrCat("$<TARGET_SONAME_FILE:", gtgt->GetName(), '>');
  1535. std::string str_link_file =
  1536. cmStrCat("$<TARGET_LINKER_FILE:", gtgt->GetName(), '>');
  1537. bool stdPipesUTF8 = true;
  1538. cmCustomCommandLines cmd = cmMakeSingleCommandLine(
  1539. { cmSystemTools::GetCMakeCommand(), "-E", "cmake_symlink_library",
  1540. str_file, str_so_file, str_link_file });
  1541. cmCustomCommand command(
  1542. std::vector<std::string>(), std::vector<std::string>(),
  1543. std::vector<std::string>(), cmd, this->CurrentMakefile->GetBacktrace(),
  1544. "Creating symlinks", "", stdPipesUTF8);
  1545. postbuild.push_back(std::move(command));
  1546. }
  1547. cmXCodeObject* legacyCustomCommandsBuildPhase = nullptr;
  1548. cmXCodeObject* preBuildPhase = nullptr;
  1549. cmXCodeObject* preLinkPhase = nullptr;
  1550. cmXCodeObject* postBuildPhase = nullptr;
  1551. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  1552. // create prebuild phase
  1553. preBuildPhase =
  1554. this->CreateRunScriptBuildPhase("CMake PreBuild Rules", prebuild);
  1555. // create prelink phase
  1556. preLinkPhase =
  1557. this->CreateRunScriptBuildPhase("CMake PreLink Rules", prelink);
  1558. // create postbuild phase
  1559. postBuildPhase =
  1560. this->CreateRunScriptBuildPhase("CMake PostBuild Rules", postbuild);
  1561. } else {
  1562. std::vector<cmSourceFile*> classes;
  1563. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  1564. return;
  1565. }
  1566. // add all the sources
  1567. std::vector<cmCustomCommand> commands;
  1568. auto& visited = this->CommandsVisited[gtgt];
  1569. for (auto sourceFile : classes) {
  1570. if (sourceFile->GetCustomCommand() &&
  1571. visited.insert(sourceFile).second) {
  1572. commands.push_back(*sourceFile->GetCustomCommand());
  1573. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  1574. this->CustomCommandRoots[sourceFile].insert(gtgt);
  1575. }
  1576. }
  1577. }
  1578. // create custom commands phase
  1579. legacyCustomCommandsBuildPhase = this->CreateLegacyRunScriptBuildPhase(
  1580. "CMake Rules", "cmakeRulesBuildPhase", gtgt, commands);
  1581. // create prebuild phase
  1582. preBuildPhase = this->CreateLegacyRunScriptBuildPhase(
  1583. "CMake PreBuild Rules", "preBuildCommands", gtgt, prebuild);
  1584. // create prelink phase
  1585. preLinkPhase = this->CreateLegacyRunScriptBuildPhase(
  1586. "CMake PreLink Rules", "preLinkCommands", gtgt, prelink);
  1587. // create postbuild phase
  1588. postBuildPhase = this->CreateLegacyRunScriptBuildPhase(
  1589. "CMake PostBuild Rules", "postBuildPhase", gtgt, postbuild);
  1590. }
  1591. // The order here is the order they will be built in.
  1592. // The order "headers, resources, sources" mimics a native project generated
  1593. // from an xcode template...
  1594. //
  1595. if (preBuildPhase) {
  1596. buildPhases->AddObject(preBuildPhase);
  1597. }
  1598. if (legacyCustomCommandsBuildPhase) {
  1599. buildPhases->AddObject(legacyCustomCommandsBuildPhase);
  1600. }
  1601. if (this->XcodeBuildSystem >= BuildSystem::Twelve) {
  1602. this->CreateRunScriptBuildPhases(buildPhases, gtgt);
  1603. }
  1604. if (headerBuildPhase) {
  1605. buildPhases->AddObject(headerBuildPhase);
  1606. }
  1607. if (resourceBuildPhase) {
  1608. buildPhases->AddObject(resourceBuildPhase);
  1609. }
  1610. for (auto obj : contentBuildPhases) {
  1611. buildPhases->AddObject(obj);
  1612. }
  1613. if (sourceBuildPhase) {
  1614. buildPhases->AddObject(sourceBuildPhase);
  1615. }
  1616. if (preLinkPhase) {
  1617. buildPhases->AddObject(preLinkPhase);
  1618. }
  1619. if (frameworkBuildPhase) {
  1620. buildPhases->AddObject(frameworkBuildPhase);
  1621. }
  1622. if (postBuildPhase) {
  1623. buildPhases->AddObject(postBuildPhase);
  1624. }
  1625. }
  1626. void cmGlobalXCodeGenerator::CreateRunScriptBuildPhases(
  1627. cmXCodeObject* buildPhases, cmGeneratorTarget const* gt)
  1628. {
  1629. std::vector<cmSourceFile*> sources;
  1630. if (!gt->GetConfigCommonSourceFiles(sources)) {
  1631. return;
  1632. }
  1633. auto& visited = this->CommandsVisited[gt];
  1634. for (auto sf : sources) {
  1635. this->CreateRunScriptBuildPhases(buildPhases, sf, gt, visited);
  1636. }
  1637. }
  1638. void cmGlobalXCodeGenerator::CreateRunScriptBuildPhases(
  1639. cmXCodeObject* buildPhases, cmSourceFile const* sf,
  1640. cmGeneratorTarget const* gt, std::set<cmSourceFile const*>& visited)
  1641. {
  1642. cmCustomCommand const* cc = sf->GetCustomCommand();
  1643. if (cc && visited.insert(sf).second) {
  1644. this->CustomCommandRoots[sf].insert(gt);
  1645. if (std::vector<cmSourceFile*> const* depends = gt->GetSourceDepends(sf)) {
  1646. for (cmSourceFile const* di : *depends) {
  1647. this->CreateRunScriptBuildPhases(buildPhases, di, gt, visited);
  1648. }
  1649. }
  1650. cmXCodeObject* buildPhase = this->CreateRunScriptBuildPhase(sf, gt, *cc);
  1651. buildPhases->AddObject(buildPhase);
  1652. }
  1653. }
  1654. cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase(
  1655. cmSourceFile const* sf, cmGeneratorTarget const* gt,
  1656. cmCustomCommand const& cc)
  1657. {
  1658. std::set<std::string> allConfigInputs;
  1659. std::set<std::string> allConfigOutputs;
  1660. std::string shellScript = "set -e\n";
  1661. for (std::string const& configName : this->CurrentConfigurationTypes) {
  1662. cmCustomCommandGenerator ccg(cc, configName, this->CurrentLocalGenerator);
  1663. std::vector<std::string> realDepends;
  1664. realDepends.reserve(ccg.GetDepends().size());
  1665. for (auto const& d : ccg.GetDepends()) {
  1666. std::string dep;
  1667. if (this->CurrentLocalGenerator->GetRealDependency(d, configName, dep)) {
  1668. realDepends.emplace_back(std::move(dep));
  1669. }
  1670. }
  1671. allConfigInputs.insert(realDepends.begin(), realDepends.end());
  1672. allConfigOutputs.insert(ccg.GetByproducts().begin(),
  1673. ccg.GetByproducts().end());
  1674. allConfigOutputs.insert(ccg.GetOutputs().begin(), ccg.GetOutputs().end());
  1675. shellScript =
  1676. cmStrCat(shellScript, R"(if test "$CONFIGURATION" = ")", configName,
  1677. "\"; then :\n", this->ConstructScript(ccg), "fi\n");
  1678. }
  1679. cmXCodeObject* buildPhase =
  1680. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1681. buildPhase->AddAttribute("buildActionMask",
  1682. this->CreateString("2147483647"));
  1683. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1684. buildPhase->AddAttribute("files", buildFiles);
  1685. {
  1686. std::string name;
  1687. if (!allConfigOutputs.empty()) {
  1688. name = cmStrCat("Generate ",
  1689. this->RelativeToBinary(*allConfigOutputs.begin()));
  1690. } else {
  1691. name = sf->GetLocation().GetName();
  1692. }
  1693. buildPhase->AddAttribute("name", this->CreateString(name));
  1694. }
  1695. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1696. this->CreateString("0"));
  1697. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1698. buildPhase->AddAttribute("shellScript", this->CreateString(shellScript));
  1699. buildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1700. bool symbolic = false;
  1701. {
  1702. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1703. for (std::string const& i : allConfigInputs) {
  1704. inputPaths->AddUniqueObject(this->CreateString(i));
  1705. if (!symbolic) {
  1706. if (cmSourceFile* isf =
  1707. gt->GetLocalGenerator()->GetMakefile()->GetSource(
  1708. i, cmSourceFileLocationKind::Known)) {
  1709. symbolic = isf->GetPropertyAsBool("SYMBOLIC");
  1710. }
  1711. }
  1712. }
  1713. buildPhase->AddAttribute("inputPaths", inputPaths);
  1714. }
  1715. {
  1716. cmXCodeObject* outputPaths =
  1717. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1718. for (std::string const& o : allConfigOutputs) {
  1719. outputPaths->AddUniqueObject(this->CreateString(o));
  1720. if (!symbolic) {
  1721. if (cmSourceFile* osf =
  1722. gt->GetLocalGenerator()->GetMakefile()->GetSource(
  1723. o, cmSourceFileLocationKind::Known)) {
  1724. symbolic = osf->GetPropertyAsBool("SYMBOLIC");
  1725. }
  1726. }
  1727. }
  1728. buildPhase->AddAttribute("outputPaths", outputPaths);
  1729. }
  1730. if (symbolic) {
  1731. buildPhase->AddAttribute("alwaysOutOfDate", this->CreateString("1"));
  1732. }
  1733. return buildPhase;
  1734. }
  1735. cmXCodeObject* cmGlobalXCodeGenerator::CreateRunScriptBuildPhase(
  1736. std::string const& name, std::vector<cmCustomCommand> const& commands)
  1737. {
  1738. if (commands.empty()) {
  1739. return nullptr;
  1740. }
  1741. std::set<std::string> allConfigOutputs;
  1742. std::string shellScript = "set -e\n";
  1743. for (std::string const& configName : this->CurrentConfigurationTypes) {
  1744. shellScript = cmStrCat(shellScript, R"(if test "$CONFIGURATION" = ")",
  1745. configName, "\"; then :\n");
  1746. for (cmCustomCommand const& cc : commands) {
  1747. cmCustomCommandGenerator ccg(cc, configName,
  1748. this->CurrentLocalGenerator);
  1749. shellScript = cmStrCat(shellScript, this->ConstructScript(ccg));
  1750. allConfigOutputs.insert(ccg.GetByproducts().begin(),
  1751. ccg.GetByproducts().end());
  1752. }
  1753. shellScript = cmStrCat(shellScript, "fi\n");
  1754. }
  1755. cmXCodeObject* buildPhase =
  1756. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1757. buildPhase->AddAttribute("buildActionMask",
  1758. this->CreateString("2147483647"));
  1759. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1760. buildPhase->AddAttribute("files", buildFiles);
  1761. buildPhase->AddAttribute("name", this->CreateString(name));
  1762. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1763. this->CreateString("0"));
  1764. buildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  1765. buildPhase->AddAttribute("shellScript", this->CreateString(shellScript));
  1766. buildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1767. {
  1768. cmXCodeObject* outputPaths =
  1769. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1770. for (std::string const& o : allConfigOutputs) {
  1771. outputPaths->AddUniqueObject(this->CreateString(o));
  1772. }
  1773. buildPhase->AddAttribute("outputPaths", outputPaths);
  1774. }
  1775. buildPhase->AddAttribute("alwaysOutOfDate", this->CreateString("1"));
  1776. return buildPhase;
  1777. }
  1778. namespace {
  1779. void ReplaceScriptVars(std::string& cmd)
  1780. {
  1781. cmSystemTools::ReplaceString(cmd, "$(CONFIGURATION)", "$CONFIGURATION");
  1782. cmSystemTools::ReplaceString(cmd, "$(EFFECTIVE_PLATFORM_NAME)",
  1783. "$EFFECTIVE_PLATFORM_NAME");
  1784. }
  1785. }
  1786. std::string cmGlobalXCodeGenerator::ConstructScript(
  1787. cmCustomCommandGenerator const& ccg)
  1788. {
  1789. std::string script;
  1790. cmLocalGenerator* lg = this->CurrentLocalGenerator;
  1791. std::string wd = ccg.GetWorkingDirectory();
  1792. if (wd.empty()) {
  1793. wd = lg->GetCurrentBinaryDirectory();
  1794. }
  1795. wd = lg->ConvertToOutputFormat(wd, cmOutputConverter::SHELL);
  1796. ReplaceScriptVars(wd);
  1797. script = cmStrCat(script, " cd ", wd, "\n");
  1798. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  1799. std::string cmd = ccg.GetCommand(c);
  1800. if (cmd.empty()) {
  1801. continue;
  1802. }
  1803. cmSystemTools::ReplaceString(cmd, "/./", "/");
  1804. cmd = lg->ConvertToOutputFormat(cmd, cmOutputConverter::SHELL);
  1805. ccg.AppendArguments(c, cmd);
  1806. ReplaceScriptVars(cmd);
  1807. script = cmStrCat(script, " ", cmd, '\n');
  1808. }
  1809. return script;
  1810. }
  1811. // This function removes each occurrence of the flag and returns the last one
  1812. // (i.e., the dominant flag in GCC)
  1813. std::string cmGlobalXCodeGenerator::ExtractFlag(const char* flag,
  1814. std::string& flags)
  1815. {
  1816. std::string retFlag;
  1817. std::string::size_type lastOccurancePos = flags.rfind(flag);
  1818. bool saved = false;
  1819. while (lastOccurancePos != std::string::npos) {
  1820. // increment pos, we use lastOccurancePos to reduce search space on next
  1821. // inc
  1822. std::string::size_type pos = lastOccurancePos;
  1823. if (pos == 0 || flags[pos - 1] == ' ') {
  1824. while (pos < flags.size() && flags[pos] != ' ') {
  1825. if (!saved) {
  1826. retFlag += flags[pos];
  1827. }
  1828. flags[pos] = ' ';
  1829. pos++;
  1830. }
  1831. saved = true;
  1832. }
  1833. // decrement lastOccurancePos while making sure we don't loop around
  1834. // and become a very large positive number since size_type is unsigned
  1835. lastOccurancePos = lastOccurancePos == 0 ? 0 : lastOccurancePos - 1;
  1836. lastOccurancePos = flags.rfind(flag, lastOccurancePos);
  1837. }
  1838. return retFlag;
  1839. }
  1840. // This function removes each matching occurrence of the expression and
  1841. // returns the last one (i.e., the dominant flag in GCC)
  1842. std::string cmGlobalXCodeGenerator::ExtractFlagRegex(const char* exp,
  1843. int matchIndex,
  1844. std::string& flags)
  1845. {
  1846. std::string retFlag;
  1847. cmsys::RegularExpression regex(exp);
  1848. assert(regex.is_valid());
  1849. if (!regex.is_valid()) {
  1850. return retFlag;
  1851. }
  1852. std::string::size_type offset = 0;
  1853. while (regex.find(&flags[offset])) {
  1854. const std::string::size_type startPos = offset + regex.start(matchIndex);
  1855. const std::string::size_type endPos = offset + regex.end(matchIndex);
  1856. const std::string::size_type size = endPos - startPos;
  1857. offset = startPos + 1;
  1858. retFlag.assign(flags, startPos, size);
  1859. flags.replace(startPos, size, size, ' ');
  1860. }
  1861. return retFlag;
  1862. }
  1863. //----------------------------------------------------------------------------
  1864. // This function strips off Xcode attributes that do not target the current
  1865. // configuration
  1866. void cmGlobalXCodeGenerator::FilterConfigurationAttribute(
  1867. std::string const& configName, std::string& attribute)
  1868. {
  1869. // Handle [variant=<config>] condition explicitly here.
  1870. std::string::size_type beginVariant = attribute.find("[variant=");
  1871. if (beginVariant == std::string::npos) {
  1872. // There is no variant in this attribute.
  1873. return;
  1874. }
  1875. std::string::size_type endVariant = attribute.find(']', beginVariant + 9);
  1876. if (endVariant == std::string::npos) {
  1877. // There is no terminating bracket.
  1878. return;
  1879. }
  1880. // Compare the variant to the configuration.
  1881. std::string variant =
  1882. attribute.substr(beginVariant + 9, endVariant - beginVariant - 9);
  1883. if (variant == configName) {
  1884. // The variant matches the configuration so use this
  1885. // attribute but drop the [variant=<config>] condition.
  1886. attribute.erase(beginVariant, endVariant - beginVariant + 1);
  1887. } else {
  1888. // The variant does not match the configuration so
  1889. // do not use this attribute.
  1890. attribute.clear();
  1891. }
  1892. }
  1893. void cmGlobalXCodeGenerator::AddCommandsToBuildPhase(
  1894. cmXCodeObject* buildphase, cmGeneratorTarget* target,
  1895. std::vector<cmCustomCommand> const& commands, const char* name)
  1896. {
  1897. std::string dir = cmStrCat(
  1898. this->CurrentLocalGenerator->GetCurrentBinaryDirectory(), "/CMakeScripts");
  1899. cmSystemTools::MakeDirectory(dir);
  1900. std::string makefile =
  1901. cmStrCat(dir, '/', target->GetName(), '_', name, ".make");
  1902. for (const auto& currentConfig : this->CurrentConfigurationTypes) {
  1903. this->CreateCustomRulesMakefile(makefile.c_str(), target, commands,
  1904. currentConfig);
  1905. }
  1906. std::string cdir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  1907. cdir = this->ConvertToRelativeForMake(cdir);
  1908. std::string makecmd =
  1909. cmStrCat("make -C ", cdir, " -f ",
  1910. this->ConvertToRelativeForMake((makefile + "$CONFIGURATION")),
  1911. " OBJDIR=$(basename \"$OBJECT_FILE_DIR_normal\") all");
  1912. buildphase->AddAttribute("shellScript", this->CreateString(makecmd));
  1913. buildphase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  1914. }
  1915. void cmGlobalXCodeGenerator::CreateCustomRulesMakefile(
  1916. const char* makefileBasename, cmGeneratorTarget* target,
  1917. std::vector<cmCustomCommand> const& commands, const std::string& configName)
  1918. {
  1919. std::string makefileName = cmStrCat(makefileBasename, configName);
  1920. cmGeneratedFileStream makefileStream(makefileName);
  1921. if (!makefileStream) {
  1922. return;
  1923. }
  1924. makefileStream.SetCopyIfDifferent(true);
  1925. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  1926. makefileStream << "# Custom rules for " << target->GetName() << "\n";
  1927. // disable the implicit rules
  1928. makefileStream << ".SUFFIXES: "
  1929. << "\n";
  1930. // have all depend on all outputs
  1931. makefileStream << "all: ";
  1932. std::map<const cmCustomCommand*, std::string> tname;
  1933. int count = 0;
  1934. for (auto const& command : commands) {
  1935. cmCustomCommandGenerator ccg(command, configName,
  1936. this->CurrentLocalGenerator);
  1937. if (ccg.GetNumberOfCommands() > 0) {
  1938. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1939. if (!outputs.empty()) {
  1940. for (auto const& output : outputs) {
  1941. makefileStream << "\\\n\t" << this->ConvertToRelativeForMake(output);
  1942. }
  1943. } else {
  1944. std::ostringstream str;
  1945. str << "_buildpart_" << count++;
  1946. tname[&ccg.GetCC()] = target->GetName() + str.str();
  1947. makefileStream << "\\\n\t" << tname[&ccg.GetCC()];
  1948. }
  1949. }
  1950. }
  1951. makefileStream << "\n\n";
  1952. for (auto const& command : commands) {
  1953. cmCustomCommandGenerator ccg(command, configName,
  1954. this->CurrentLocalGenerator);
  1955. std::vector<std::string> realDepends;
  1956. realDepends.reserve(ccg.GetDepends().size());
  1957. for (auto const& d : ccg.GetDepends()) {
  1958. std::string dep;
  1959. if (this->CurrentLocalGenerator->GetRealDependency(d, configName, dep)) {
  1960. realDepends.emplace_back(std::move(dep));
  1961. }
  1962. }
  1963. if (ccg.GetNumberOfCommands() > 0) {
  1964. makefileStream << "\n";
  1965. const std::vector<std::string>& outputs = ccg.GetOutputs();
  1966. if (!outputs.empty()) {
  1967. // There is at least one output, start the rule for it
  1968. const char* sep = "";
  1969. for (auto const& output : outputs) {
  1970. makefileStream << sep << this->ConvertToRelativeForMake(output);
  1971. sep = " ";
  1972. }
  1973. makefileStream << ": ";
  1974. } else {
  1975. // There are no outputs. Use the generated force rule name.
  1976. makefileStream << tname[&ccg.GetCC()] << ": ";
  1977. }
  1978. for (auto const& dep : realDepends) {
  1979. makefileStream << "\\\n" << this->ConvertToRelativeForMake(dep);
  1980. }
  1981. makefileStream << "\n";
  1982. if (const char* comment = ccg.GetComment()) {
  1983. std::string echo_cmd =
  1984. cmStrCat("echo ",
  1985. (this->CurrentLocalGenerator->EscapeForShell(
  1986. comment, ccg.GetCC().GetEscapeAllowMakeVars())));
  1987. makefileStream << "\t" << echo_cmd << "\n";
  1988. }
  1989. // Add each command line to the set of commands.
  1990. for (unsigned int c = 0; c < ccg.GetNumberOfCommands(); ++c) {
  1991. // Build the command line in a single string.
  1992. std::string cmd2 = ccg.GetCommand(c);
  1993. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  1994. cmd2 = this->ConvertToRelativeForMake(cmd2);
  1995. std::string cmd;
  1996. std::string wd = ccg.GetWorkingDirectory();
  1997. if (!wd.empty()) {
  1998. cmd += "cd ";
  1999. cmd += this->ConvertToRelativeForMake(wd);
  2000. cmd += " && ";
  2001. }
  2002. cmd += cmd2;
  2003. ccg.AppendArguments(c, cmd);
  2004. makefileStream << "\t" << cmd << "\n";
  2005. }
  2006. // Symbolic inputs are not expected to exist, so add dummy rules.
  2007. for (auto const& dep : realDepends) {
  2008. if (cmSourceFile* dsf =
  2009. target->GetLocalGenerator()->GetMakefile()->GetSource(
  2010. dep, cmSourceFileLocationKind::Known)) {
  2011. if (dsf->GetPropertyAsBool("SYMBOLIC")) {
  2012. makefileStream << this->ConvertToRelativeForMake(dep) << ":\n";
  2013. }
  2014. }
  2015. }
  2016. }
  2017. }
  2018. }
  2019. void cmGlobalXCodeGenerator::AddPositionIndependentLinkAttribute(
  2020. cmGeneratorTarget* target, cmXCodeObject* buildSettings,
  2021. const std::string& configName)
  2022. {
  2023. // For now, only EXECUTABLE is concerned
  2024. if (target->GetType() != cmStateEnums::EXECUTABLE) {
  2025. return;
  2026. }
  2027. const char* PICValue = target->GetLinkPIEProperty(configName);
  2028. if (PICValue == nullptr) {
  2029. // POSITION_INDEPENDENT_CODE is not set
  2030. return;
  2031. }
  2032. buildSettings->AddAttribute(
  2033. "LD_NO_PIE", this->CreateString(cmIsOn(PICValue) ? "NO" : "YES"));
  2034. }
  2035. void cmGlobalXCodeGenerator::CreateBuildSettings(cmGeneratorTarget* gtgt,
  2036. cmXCodeObject* buildSettings,
  2037. const std::string& configName)
  2038. {
  2039. if (!gtgt->IsInBuildSystem()) {
  2040. return;
  2041. }
  2042. std::string defFlags;
  2043. bool shared = ((gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) ||
  2044. (gtgt->GetType() == cmStateEnums::MODULE_LIBRARY));
  2045. bool binary = ((gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) ||
  2046. (gtgt->GetType() == cmStateEnums::STATIC_LIBRARY) ||
  2047. (gtgt->GetType() == cmStateEnums::EXECUTABLE) || shared);
  2048. // Compute the compilation flags for each language.
  2049. std::set<std::string> languages;
  2050. gtgt->GetLanguages(languages, configName);
  2051. std::map<std::string, std::string> cflags;
  2052. for (auto const& lang : languages) {
  2053. std::string& flags = cflags[lang];
  2054. // Add language-specific flags.
  2055. this->CurrentLocalGenerator->AddLanguageFlags(flags, gtgt, lang,
  2056. configName);
  2057. if (gtgt->IsIPOEnabled(lang, configName)) {
  2058. this->CurrentLocalGenerator->AppendFeatureOptions(flags, lang, "IPO");
  2059. }
  2060. // Add shared-library flags if needed.
  2061. this->CurrentLocalGenerator->AddCMP0018Flags(flags, gtgt, lang,
  2062. configName);
  2063. this->CurrentLocalGenerator->AddVisibilityPresetFlags(flags, gtgt, lang);
  2064. this->CurrentLocalGenerator->AddCompileOptions(flags, gtgt, lang,
  2065. configName);
  2066. }
  2067. std::string llang = gtgt->GetLinkerLanguage(configName);
  2068. if (binary && llang.empty()) {
  2069. cmSystemTools::Error(
  2070. "CMake can not determine linker language for target: " +
  2071. gtgt->GetName());
  2072. return;
  2073. }
  2074. std::string const& langForPreprocessor = llang;
  2075. if (gtgt->IsIPOEnabled(llang, configName)) {
  2076. const char* ltoValue =
  2077. this->CurrentMakefile->IsOn("_CMAKE_LTO_THIN") ? "YES_THIN" : "YES";
  2078. buildSettings->AddAttribute("LLVM_LTO", this->CreateString(ltoValue));
  2079. }
  2080. // Handle PIE linker configuration
  2081. this->AddPositionIndependentLinkAttribute(gtgt, buildSettings, configName);
  2082. // Add define flags
  2083. this->CurrentLocalGenerator->AppendFlags(
  2084. defFlags, this->CurrentMakefile->GetDefineFlags());
  2085. // Add preprocessor definitions for this target and configuration.
  2086. BuildObjectListOrString ppDefs(this, true);
  2087. this->AppendDefines(
  2088. ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)\"");
  2089. if (const std::string* exportMacro = gtgt->GetExportMacro()) {
  2090. // Add the export symbol definition for shared library objects.
  2091. this->AppendDefines(ppDefs, exportMacro->c_str());
  2092. }
  2093. std::vector<std::string> targetDefines;
  2094. if (!langForPreprocessor.empty()) {
  2095. gtgt->GetCompileDefinitions(targetDefines, configName,
  2096. langForPreprocessor);
  2097. }
  2098. this->AppendDefines(ppDefs, targetDefines);
  2099. buildSettings->AddAttribute("GCC_PREPROCESSOR_DEFINITIONS",
  2100. ppDefs.CreateList());
  2101. std::string extraLinkOptionsVar;
  2102. std::string extraLinkOptions;
  2103. if (gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  2104. extraLinkOptionsVar = "CMAKE_EXE_LINKER_FLAGS";
  2105. } else if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2106. extraLinkOptionsVar = "CMAKE_SHARED_LINKER_FLAGS";
  2107. } else if (gtgt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2108. extraLinkOptionsVar = "CMAKE_MODULE_LINKER_FLAGS";
  2109. }
  2110. if (!extraLinkOptionsVar.empty()) {
  2111. this->CurrentLocalGenerator->AddConfigVariableFlags(
  2112. extraLinkOptions, extraLinkOptionsVar, configName);
  2113. }
  2114. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2115. gtgt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  2116. this->CurrentLocalGenerator->GetStaticLibraryFlags(
  2117. extraLinkOptions, configName, llang, gtgt);
  2118. } else {
  2119. cmProp targetLinkFlags = gtgt->GetProperty("LINK_FLAGS");
  2120. if (targetLinkFlags) {
  2121. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions,
  2122. *targetLinkFlags);
  2123. }
  2124. if (!configName.empty()) {
  2125. std::string linkFlagsVar =
  2126. cmStrCat("LINK_FLAGS_", cmSystemTools::UpperCase(configName));
  2127. if (cmProp linkFlags = gtgt->GetProperty(linkFlagsVar)) {
  2128. this->CurrentLocalGenerator->AppendFlags(extraLinkOptions, *linkFlags);
  2129. }
  2130. }
  2131. std::vector<std::string> opts;
  2132. gtgt->GetLinkOptions(opts, configName, llang);
  2133. // LINK_OPTIONS are escaped.
  2134. this->CurrentLocalGenerator->AppendCompileOptions(extraLinkOptions, opts);
  2135. }
  2136. // Set target-specific architectures.
  2137. std::vector<std::string> archs;
  2138. gtgt->GetAppleArchs(configName, archs);
  2139. if (!archs.empty()) {
  2140. // Enable ARCHS attribute.
  2141. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("NO"));
  2142. // Store ARCHS value.
  2143. if (archs.size() == 1) {
  2144. buildSettings->AddAttribute("ARCHS", this->CreateString(archs[0]));
  2145. } else {
  2146. cmXCodeObject* archObjects =
  2147. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2148. for (auto& arch : archs) {
  2149. archObjects->AddObject(this->CreateString(arch));
  2150. }
  2151. buildSettings->AddAttribute("ARCHS", archObjects);
  2152. }
  2153. }
  2154. // Get the product name components.
  2155. std::string pnprefix;
  2156. std::string pnbase;
  2157. std::string pnsuffix;
  2158. gtgt->GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  2159. cmProp version = gtgt->GetProperty("VERSION");
  2160. cmProp soversion = gtgt->GetProperty("SOVERSION");
  2161. if (!gtgt->HasSOName(configName) || gtgt->IsFrameworkOnApple()) {
  2162. version = nullptr;
  2163. soversion = nullptr;
  2164. }
  2165. if (version && !soversion) {
  2166. soversion = version;
  2167. }
  2168. if (!version && soversion) {
  2169. version = soversion;
  2170. }
  2171. std::string realName = pnbase;
  2172. std::string soName = pnbase;
  2173. if (version && soversion) {
  2174. realName += ".";
  2175. realName += *version;
  2176. soName += ".";
  2177. soName += *soversion;
  2178. }
  2179. // Set attributes to specify the proper name for the target.
  2180. std::string pndir = this->CurrentLocalGenerator->GetCurrentBinaryDirectory();
  2181. if (gtgt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2182. gtgt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2183. gtgt->GetType() == cmStateEnums::MODULE_LIBRARY ||
  2184. gtgt->GetType() == cmStateEnums::EXECUTABLE) {
  2185. if (!gtgt->UsesDefaultOutputDir(configName,
  2186. cmStateEnums::RuntimeBinaryArtifact)) {
  2187. std::string pncdir = gtgt->GetDirectory(configName);
  2188. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  2189. this->CreateString(pncdir));
  2190. }
  2191. if (gtgt->IsFrameworkOnApple() || gtgt->IsCFBundleOnApple()) {
  2192. pnprefix = "";
  2193. }
  2194. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  2195. this->CreateString(pnprefix));
  2196. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  2197. this->CreateString(pnsuffix));
  2198. } else if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2199. pnprefix = "lib";
  2200. pnbase = gtgt->GetName();
  2201. pnsuffix = ".a";
  2202. std::string pncdir = this->GetObjectsDirectory(
  2203. this->CurrentProject, configName, gtgt, OBJECT_LIBRARY_ARTIFACT_DIR);
  2204. buildSettings->AddAttribute("CONFIGURATION_BUILD_DIR",
  2205. this->CreateString(pncdir));
  2206. }
  2207. // Store the product name for all target types.
  2208. buildSettings->AddAttribute("PRODUCT_NAME", this->CreateString(realName));
  2209. buildSettings->AddAttribute("SYMROOT", this->CreateString(pndir));
  2210. // Handle settings for each target type.
  2211. switch (gtgt->GetType()) {
  2212. case cmStateEnums::STATIC_LIBRARY:
  2213. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  2214. std::string fw_version = gtgt->GetFrameworkVersion();
  2215. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  2216. this->CreateString(fw_version));
  2217. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2218. if (ext) {
  2219. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2220. this->CreateString(*ext));
  2221. }
  2222. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2223. // Xcode will create the final version of Info.plist at build time,
  2224. // so let it replace the framework name. This avoids creating
  2225. // a per-configuration Info.plist file.
  2226. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  2227. gtgt, "$(EXECUTABLE_NAME)", plist);
  2228. buildSettings->AddAttribute("INFOPLIST_FILE",
  2229. this->CreateString(plist));
  2230. buildSettings->AddAttribute("MACH_O_TYPE",
  2231. this->CreateString("staticlib"));
  2232. } else {
  2233. buildSettings->AddAttribute("LIBRARY_STYLE",
  2234. this->CreateString("STATIC"));
  2235. }
  2236. break;
  2237. case cmStateEnums::OBJECT_LIBRARY: {
  2238. buildSettings->AddAttribute("LIBRARY_STYLE",
  2239. this->CreateString("STATIC"));
  2240. break;
  2241. }
  2242. case cmStateEnums::MODULE_LIBRARY: {
  2243. buildSettings->AddAttribute("LIBRARY_STYLE",
  2244. this->CreateString("BUNDLE"));
  2245. if (gtgt->IsCFBundleOnApple()) {
  2246. // It turns out that a BUNDLE is basically the same
  2247. // in many ways as an application bundle, as far as
  2248. // link flags go
  2249. std::string createFlags = this->LookupFlags(
  2250. "CMAKE_SHARED_MODULE_CREATE_", llang, "_FLAGS", "-bundle");
  2251. if (!createFlags.empty()) {
  2252. extraLinkOptions += " ";
  2253. extraLinkOptions += createFlags;
  2254. }
  2255. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2256. if (ext) {
  2257. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2258. this->CreateString(*ext));
  2259. }
  2260. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2261. // Xcode will create the final version of Info.plist at build time,
  2262. // so let it replace the cfbundle name. This avoids creating
  2263. // a per-configuration Info.plist file. The cfbundle plist
  2264. // is very similar to the application bundle plist
  2265. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  2266. gtgt, "$(EXECUTABLE_NAME)", plist);
  2267. buildSettings->AddAttribute("INFOPLIST_FILE",
  2268. this->CreateString(plist));
  2269. } else {
  2270. buildSettings->AddAttribute("MACH_O_TYPE",
  2271. this->CreateString("mh_bundle"));
  2272. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  2273. this->CreateString("NO"));
  2274. // Add the flags to create an executable.
  2275. std::string createFlags =
  2276. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  2277. if (!createFlags.empty()) {
  2278. extraLinkOptions += " ";
  2279. extraLinkOptions += createFlags;
  2280. }
  2281. }
  2282. break;
  2283. }
  2284. case cmStateEnums::SHARED_LIBRARY: {
  2285. if (gtgt->GetPropertyAsBool("FRAMEWORK")) {
  2286. std::string fw_version = gtgt->GetFrameworkVersion();
  2287. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  2288. this->CreateString(fw_version));
  2289. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2290. if (ext) {
  2291. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2292. this->CreateString(*ext));
  2293. }
  2294. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2295. // Xcode will create the final version of Info.plist at build time,
  2296. // so let it replace the framework name. This avoids creating
  2297. // a per-configuration Info.plist file.
  2298. this->CurrentLocalGenerator->GenerateFrameworkInfoPList(
  2299. gtgt, "$(EXECUTABLE_NAME)", plist);
  2300. buildSettings->AddAttribute("INFOPLIST_FILE",
  2301. this->CreateString(plist));
  2302. } else {
  2303. // Add the flags to create a shared library.
  2304. std::string createFlags = this->LookupFlags(
  2305. "CMAKE_SHARED_LIBRARY_CREATE_", llang, "_FLAGS", "-dynamiclib");
  2306. if (!createFlags.empty()) {
  2307. extraLinkOptions += " ";
  2308. extraLinkOptions += createFlags;
  2309. }
  2310. }
  2311. buildSettings->AddAttribute("LIBRARY_STYLE",
  2312. this->CreateString("DYNAMIC"));
  2313. break;
  2314. }
  2315. case cmStateEnums::EXECUTABLE: {
  2316. // Add the flags to create an executable.
  2317. std::string createFlags =
  2318. this->LookupFlags("CMAKE_", llang, "_LINK_FLAGS", "");
  2319. if (!createFlags.empty()) {
  2320. extraLinkOptions += " ";
  2321. extraLinkOptions += createFlags;
  2322. }
  2323. // Handle bundles and normal executables separately.
  2324. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  2325. cmProp ext = gtgt->GetProperty("BUNDLE_EXTENSION");
  2326. if (ext) {
  2327. buildSettings->AddAttribute("WRAPPER_EXTENSION",
  2328. this->CreateString(*ext));
  2329. }
  2330. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2331. // Xcode will create the final version of Info.plist at build time,
  2332. // so let it replace the executable name. This avoids creating
  2333. // a per-configuration Info.plist file.
  2334. this->CurrentLocalGenerator->GenerateAppleInfoPList(
  2335. gtgt, "$(EXECUTABLE_NAME)", plist);
  2336. buildSettings->AddAttribute("INFOPLIST_FILE",
  2337. this->CreateString(plist));
  2338. }
  2339. } break;
  2340. default:
  2341. break;
  2342. }
  2343. BuildObjectListOrString dirs(this, true);
  2344. BuildObjectListOrString fdirs(this, true);
  2345. BuildObjectListOrString sysdirs(this, true);
  2346. BuildObjectListOrString sysfdirs(this, true);
  2347. const bool emitSystemIncludes = this->XcodeVersion >= 83;
  2348. std::vector<std::string> includes;
  2349. if (!langForPreprocessor.empty()) {
  2350. this->CurrentLocalGenerator->GetIncludeDirectories(
  2351. includes, gtgt, langForPreprocessor, configName);
  2352. }
  2353. std::set<std::string> emitted;
  2354. emitted.insert("/System/Library/Frameworks");
  2355. for (auto& include : includes) {
  2356. if (this->NameResolvesToFramework(include)) {
  2357. std::string frameworkDir = cmStrCat(include, "/../");
  2358. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  2359. if (emitted.insert(frameworkDir).second) {
  2360. std::string incpath = this->XCodeEscapePath(frameworkDir);
  2361. if (emitSystemIncludes &&
  2362. gtgt->IsSystemIncludeDirectory(frameworkDir, configName,
  2363. langForPreprocessor)) {
  2364. sysfdirs.Add(incpath);
  2365. } else {
  2366. fdirs.Add(incpath);
  2367. }
  2368. }
  2369. } else {
  2370. std::string incpath = this->XCodeEscapePath(include);
  2371. if (emitSystemIncludes &&
  2372. gtgt->IsSystemIncludeDirectory(include, configName,
  2373. langForPreprocessor)) {
  2374. sysdirs.Add(incpath);
  2375. } else {
  2376. dirs.Add(incpath);
  2377. }
  2378. }
  2379. }
  2380. // Add framework search paths needed for linking.
  2381. if (cmComputeLinkInformation* cli = gtgt->GetLinkInformation(configName)) {
  2382. for (auto const& fwDir : cli->GetFrameworkPaths()) {
  2383. if (emitted.insert(fwDir).second) {
  2384. std::string incpath = this->XCodeEscapePath(fwDir);
  2385. if (emitSystemIncludes &&
  2386. gtgt->IsSystemIncludeDirectory(fwDir, configName,
  2387. langForPreprocessor)) {
  2388. sysfdirs.Add(incpath);
  2389. } else {
  2390. fdirs.Add(incpath);
  2391. }
  2392. }
  2393. }
  2394. }
  2395. if (!fdirs.IsEmpty()) {
  2396. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS", fdirs.CreateList());
  2397. }
  2398. if (!dirs.IsEmpty()) {
  2399. buildSettings->AddAttribute("HEADER_SEARCH_PATHS", dirs.CreateList());
  2400. }
  2401. if (!sysfdirs.IsEmpty()) {
  2402. buildSettings->AddAttribute("SYSTEM_FRAMEWORK_SEARCH_PATHS",
  2403. sysfdirs.CreateList());
  2404. }
  2405. if (!sysdirs.IsEmpty()) {
  2406. buildSettings->AddAttribute("SYSTEM_HEADER_SEARCH_PATHS",
  2407. sysdirs.CreateList());
  2408. }
  2409. if (this->XcodeVersion >= 60 && !emitSystemIncludes) {
  2410. // Add those per-language flags in addition to HEADER_SEARCH_PATHS to gain
  2411. // system include directory awareness. We need to also keep on setting
  2412. // HEADER_SEARCH_PATHS to work around a missing compile options flag for
  2413. // GNU assembly files (#16449)
  2414. for (auto const& language : languages) {
  2415. std::string includeFlags = this->CurrentLocalGenerator->GetIncludeFlags(
  2416. includes, gtgt, language, true, false, configName);
  2417. if (!includeFlags.empty()) {
  2418. cflags[language] += " " + includeFlags;
  2419. }
  2420. }
  2421. }
  2422. bool same_gflags = true;
  2423. std::map<std::string, std::string> gflags;
  2424. std::string const* last_gflag = nullptr;
  2425. std::string optLevel = "0";
  2426. // Minimal map of flags to build settings.
  2427. for (auto const& language : languages) {
  2428. std::string& flags = cflags[language];
  2429. std::string& gflag = gflags[language];
  2430. std::string oflag =
  2431. this->ExtractFlagRegex("(^| )(-Ofast|-Os|-O[0-9]*)( |$)", 2, flags);
  2432. if (oflag.size() == 2) {
  2433. optLevel = "1";
  2434. } else if (oflag.size() > 2) {
  2435. optLevel = oflag.substr(2);
  2436. }
  2437. gflag = this->ExtractFlag("-g", flags);
  2438. // put back gdwarf-2 if used since there is no way
  2439. // to represent it in the gui, but we still want debug yes
  2440. if (gflag == "-gdwarf-2") {
  2441. flags += " ";
  2442. flags += gflag;
  2443. }
  2444. if (last_gflag && *last_gflag != gflag) {
  2445. same_gflags = false;
  2446. }
  2447. last_gflag = &gflag;
  2448. }
  2449. const char* debugStr = "YES";
  2450. if (!same_gflags) {
  2451. // We can't set the Xcode flag differently depending on the language,
  2452. // so put them back in this case.
  2453. for (auto const& language : languages) {
  2454. cflags[language] += " ";
  2455. cflags[language] += gflags[language];
  2456. }
  2457. debugStr = "NO";
  2458. } else if (last_gflag && (last_gflag->empty() || *last_gflag == "-g0")) {
  2459. debugStr = "NO";
  2460. }
  2461. // extract C++ stdlib
  2462. for (auto const& language : languages) {
  2463. if (language != "CXX" && language != "OBJCXX") {
  2464. continue;
  2465. }
  2466. std::string& flags = cflags[language];
  2467. auto stdlib =
  2468. this->ExtractFlagRegex("(^| )(-stdlib=[^ ]+)( |$)", 2, flags);
  2469. if (stdlib.size() > 8) {
  2470. const auto cxxLibrary = stdlib.substr(8);
  2471. if (language == "CXX" ||
  2472. !buildSettings->GetAttribute("CLANG_CXX_LIBRARY")) {
  2473. buildSettings->AddAttribute("CLANG_CXX_LIBRARY",
  2474. this->CreateString(cxxLibrary));
  2475. }
  2476. }
  2477. }
  2478. buildSettings->AddAttribute("COMBINE_HIDPI_IMAGES",
  2479. this->CreateString("YES"));
  2480. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  2481. this->CreateString(debugStr));
  2482. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  2483. this->CreateString(optLevel));
  2484. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  2485. this->CreateString("NO"));
  2486. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  2487. this->CreateString("NO"));
  2488. for (auto const& language : languages) {
  2489. std::string flags = cflags[language] + " " + defFlags;
  2490. if (language == "CXX" || language == "OBJCXX") {
  2491. if (language == "CXX" ||
  2492. !buildSettings->GetAttribute("OTHER_CPLUSPLUSFLAGS")) {
  2493. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  2494. this->CreateString(flags));
  2495. }
  2496. } else if (language == "Fortran") {
  2497. buildSettings->AddAttribute("IFORT_OTHER_FLAGS",
  2498. this->CreateString(flags));
  2499. } else if (language == "C" || language == "OBJC") {
  2500. if (language == "C" || !buildSettings->GetAttribute("OTHER_CFLAGS")) {
  2501. buildSettings->AddAttribute("OTHER_CFLAGS", this->CreateString(flags));
  2502. }
  2503. } else if (language == "Swift") {
  2504. buildSettings->AddAttribute("OTHER_SWIFT_FLAGS",
  2505. this->CreateString(flags));
  2506. }
  2507. }
  2508. // Add Fortran source format attribute if property is set.
  2509. const char* format = nullptr;
  2510. std::string const& tgtfmt = gtgt->GetSafeProperty("Fortran_FORMAT");
  2511. switch (cmOutputConverter::GetFortranFormat(tgtfmt)) {
  2512. case cmOutputConverter::FortranFormatFixed:
  2513. format = "fixed";
  2514. break;
  2515. case cmOutputConverter::FortranFormatFree:
  2516. format = "free";
  2517. break;
  2518. default:
  2519. break;
  2520. }
  2521. if (format) {
  2522. buildSettings->AddAttribute("IFORT_LANG_SRCFMT",
  2523. this->CreateString(format));
  2524. }
  2525. // Create the INSTALL_PATH attribute.
  2526. std::string install_name_dir;
  2527. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2528. // Get the install_name directory for the build tree.
  2529. install_name_dir = gtgt->GetInstallNameDirForBuildTree(configName);
  2530. // Xcode doesn't create the correct install_name in some cases.
  2531. // That is, if the INSTALL_PATH is empty, or if we have versioning
  2532. // of dylib libraries, we want to specify the install_name.
  2533. // This is done by adding a link flag to create an install_name
  2534. // with just the library soname.
  2535. std::string install_name;
  2536. if (!install_name_dir.empty()) {
  2537. // Convert to a path for the native build tool.
  2538. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  2539. install_name += install_name_dir;
  2540. install_name += "/";
  2541. }
  2542. install_name += gtgt->GetSOName(configName);
  2543. if ((realName != soName) || install_name_dir.empty()) {
  2544. install_name_dir = "";
  2545. extraLinkOptions += " -install_name ";
  2546. extraLinkOptions += XCodeEscapePath(install_name);
  2547. }
  2548. }
  2549. buildSettings->AddAttribute("INSTALL_PATH",
  2550. this->CreateString(install_name_dir));
  2551. // Create the LD_RUNPATH_SEARCH_PATHS
  2552. cmComputeLinkInformation* pcli = gtgt->GetLinkInformation(configName);
  2553. if (pcli) {
  2554. std::string search_paths;
  2555. std::vector<std::string> runtimeDirs;
  2556. pcli->GetRPath(runtimeDirs, false);
  2557. // runpath dirs needs to be unique to prevent corruption
  2558. std::set<std::string> unique_dirs;
  2559. for (auto runpath : runtimeDirs) {
  2560. runpath = this->ExpandCFGIntDir(runpath, configName);
  2561. if (unique_dirs.find(runpath) == unique_dirs.end()) {
  2562. unique_dirs.insert(runpath);
  2563. if (!search_paths.empty()) {
  2564. search_paths += " ";
  2565. }
  2566. search_paths += this->XCodeEscapePath(runpath);
  2567. }
  2568. }
  2569. if (!search_paths.empty()) {
  2570. buildSettings->AddAttribute("LD_RUNPATH_SEARCH_PATHS",
  2571. this->CreateString(search_paths));
  2572. }
  2573. }
  2574. buildSettings->AddAttribute(this->GetTargetLinkFlagsVar(gtgt),
  2575. this->CreateString(extraLinkOptions));
  2576. buildSettings->AddAttribute("OTHER_REZFLAGS", this->CreateString(""));
  2577. buildSettings->AddAttribute("SECTORDER_FLAGS", this->CreateString(""));
  2578. buildSettings->AddAttribute("USE_HEADERMAP", this->CreateString("NO"));
  2579. cmXCodeObject* group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2580. group->AddObject(this->CreateString("$(inherited)"));
  2581. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  2582. // Runtime version information.
  2583. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  2584. int major;
  2585. int minor;
  2586. int patch;
  2587. // MACHO_CURRENT_VERSION or VERSION -> current_version
  2588. gtgt->GetTargetVersionFallback("MACHO_CURRENT_VERSION", "VERSION", major,
  2589. minor, patch);
  2590. std::ostringstream v;
  2591. // Xcode always wants at least 1.0.0 or nothing
  2592. if (!(major == 0 && minor == 0 && patch == 0)) {
  2593. v << major << "." << minor << "." << patch;
  2594. }
  2595. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  2596. this->CreateString(v.str()));
  2597. // MACHO_COMPATIBILITY_VERSION or SOVERSION -> compatibility_version
  2598. gtgt->GetTargetVersionFallback("MACHO_COMPATIBILITY_VERSION", "SOVERSION",
  2599. major, minor, patch);
  2600. std::ostringstream vso;
  2601. // Xcode always wants at least 1.0.0 or nothing
  2602. if (!(major == 0 && minor == 0 && patch == 0)) {
  2603. vso << major << "." << minor << "." << patch;
  2604. }
  2605. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  2606. this->CreateString(vso.str()));
  2607. }
  2608. // Precompile Headers
  2609. std::string pchHeader = gtgt->GetPchHeader(configName, llang);
  2610. if (!pchHeader.empty()) {
  2611. buildSettings->AddAttribute("GCC_PREFIX_HEADER",
  2612. this->CreateString(pchHeader));
  2613. buildSettings->AddAttribute("GCC_PRECOMPILE_PREFIX_HEADER",
  2614. this->CreateString("YES"));
  2615. }
  2616. // put this last so it can override existing settings
  2617. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  2618. {
  2619. for (auto const& prop : gtgt->GetPropertyKeys()) {
  2620. if (cmHasLiteralPrefix(prop, "XCODE_ATTRIBUTE_")) {
  2621. std::string attribute = prop.substr(16);
  2622. this->FilterConfigurationAttribute(configName, attribute);
  2623. if (!attribute.empty()) {
  2624. std::string const& pr = gtgt->GetSafeProperty(prop);
  2625. std::string processed = cmGeneratorExpression::Evaluate(
  2626. pr, this->CurrentLocalGenerator, configName);
  2627. buildSettings->AddAttribute(attribute,
  2628. this->CreateString(processed));
  2629. }
  2630. }
  2631. }
  2632. }
  2633. }
  2634. cmXCodeObject* cmGlobalXCodeGenerator::CreateUtilityTarget(
  2635. cmGeneratorTarget* gtgt)
  2636. {
  2637. cmXCodeObject* shellBuildPhase =
  2638. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  2639. shellBuildPhase->AddAttribute("buildActionMask",
  2640. this->CreateString("2147483647"));
  2641. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2642. shellBuildPhase->AddAttribute("files", buildFiles);
  2643. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2644. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  2645. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2646. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  2647. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  2648. this->CreateString("0"));
  2649. shellBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  2650. shellBuildPhase->AddAttribute(
  2651. "shellScript", this->CreateString("# shell script goes here\nexit 0"));
  2652. shellBuildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  2653. cmXCodeObject* target =
  2654. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  2655. target->SetComment(gtgt->GetName());
  2656. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2657. std::vector<cmXCodeObject*> emptyContentVector;
  2658. this->CreateCustomCommands(buildPhases, nullptr, nullptr, nullptr,
  2659. emptyContentVector, nullptr, gtgt);
  2660. target->AddAttribute("buildPhases", buildPhases);
  2661. this->AddConfigurations(target, gtgt);
  2662. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2663. target->AddAttribute("dependencies", dependencies);
  2664. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2665. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2666. target->SetTarget(gtgt);
  2667. this->XCodeObjectMap[gtgt] = target;
  2668. // Add source files without build rules for editing convenience.
  2669. if (gtgt->GetType() != cmStateEnums::GLOBAL_TARGET &&
  2670. gtgt->GetName() != CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  2671. std::vector<cmSourceFile*> sources;
  2672. if (!gtgt->GetConfigCommonSourceFiles(sources)) {
  2673. return nullptr;
  2674. }
  2675. // Add CMakeLists.txt file for user convenience.
  2676. this->AddXCodeProjBuildRule(gtgt, sources);
  2677. for (auto sourceFile : sources) {
  2678. if (!sourceFile->GetIsGenerated()) {
  2679. this->CreateXCodeFileReference(sourceFile, gtgt);
  2680. }
  2681. }
  2682. }
  2683. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2684. return target;
  2685. }
  2686. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  2687. cmGeneratorTarget* gtgt)
  2688. {
  2689. std::vector<std::string> const configVector = cmExpandedList(
  2690. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES"));
  2691. cmXCodeObject* configlist =
  2692. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2693. cmXCodeObject* buildConfigurations =
  2694. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2695. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2696. std::string comment = cmStrCat("Build configuration list for ",
  2697. cmXCodeObject::PBXTypeNames[target->GetIsA()],
  2698. " \"", gtgt->GetName(), '"');
  2699. configlist->SetComment(comment);
  2700. target->AddAttribute("buildConfigurationList",
  2701. this->CreateObjectReference(configlist));
  2702. for (auto const& i : configVector) {
  2703. cmXCodeObject* config =
  2704. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2705. buildConfigurations->AddObject(config);
  2706. cmXCodeObject* buildSettings =
  2707. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2708. this->CreateBuildSettings(gtgt, buildSettings, i);
  2709. config->AddAttribute("name", this->CreateString(i));
  2710. config->SetComment(i);
  2711. config->AddAttribute("buildSettings", buildSettings);
  2712. }
  2713. if (!configVector.empty()) {
  2714. configlist->AddAttribute("defaultConfigurationName",
  2715. this->CreateString(configVector[0]));
  2716. configlist->AddAttribute("defaultConfigurationIsVisible",
  2717. this->CreateString("0"));
  2718. return configVector[0];
  2719. }
  2720. return "";
  2721. }
  2722. const char* cmGlobalXCodeGenerator::GetTargetLinkFlagsVar(
  2723. cmGeneratorTarget const* target) const
  2724. {
  2725. if (this->XcodeVersion >= 60 &&
  2726. (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2727. target->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
  2728. return "OTHER_LIBTOOLFLAGS";
  2729. }
  2730. return "OTHER_LDFLAGS";
  2731. }
  2732. const char* cmGlobalXCodeGenerator::GetTargetFileType(
  2733. cmGeneratorTarget* target)
  2734. {
  2735. if (cmProp e = target->GetProperty("XCODE_EXPLICIT_FILE_TYPE")) {
  2736. return e->c_str();
  2737. }
  2738. switch (target->GetType()) {
  2739. case cmStateEnums::OBJECT_LIBRARY:
  2740. return "archive.ar";
  2741. case cmStateEnums::STATIC_LIBRARY:
  2742. return (target->GetPropertyAsBool("FRAMEWORK") ? "wrapper.framework"
  2743. : "archive.ar");
  2744. case cmStateEnums::MODULE_LIBRARY:
  2745. if (target->IsXCTestOnApple()) {
  2746. return "wrapper.cfbundle";
  2747. }
  2748. if (target->IsCFBundleOnApple()) {
  2749. return "wrapper.plug-in";
  2750. }
  2751. return "compiled.mach-o.executable";
  2752. case cmStateEnums::SHARED_LIBRARY:
  2753. return (target->GetPropertyAsBool("FRAMEWORK")
  2754. ? "wrapper.framework"
  2755. : "compiled.mach-o.dylib");
  2756. case cmStateEnums::EXECUTABLE:
  2757. return "compiled.mach-o.executable";
  2758. default:
  2759. break;
  2760. }
  2761. return nullptr;
  2762. }
  2763. const char* cmGlobalXCodeGenerator::GetTargetProductType(
  2764. cmGeneratorTarget* target)
  2765. {
  2766. if (cmProp e = target->GetProperty("XCODE_PRODUCT_TYPE")) {
  2767. return e->c_str();
  2768. }
  2769. switch (target->GetType()) {
  2770. case cmStateEnums::OBJECT_LIBRARY:
  2771. return "com.apple.product-type.library.static";
  2772. case cmStateEnums::STATIC_LIBRARY:
  2773. return (target->GetPropertyAsBool("FRAMEWORK")
  2774. ? "com.apple.product-type.framework"
  2775. : "com.apple.product-type.library.static");
  2776. case cmStateEnums::MODULE_LIBRARY:
  2777. if (target->IsXCTestOnApple()) {
  2778. return "com.apple.product-type.bundle.unit-test";
  2779. } else if (target->IsCFBundleOnApple()) {
  2780. return "com.apple.product-type.bundle";
  2781. } else {
  2782. return "com.apple.product-type.tool";
  2783. }
  2784. case cmStateEnums::SHARED_LIBRARY:
  2785. return (target->GetPropertyAsBool("FRAMEWORK")
  2786. ? "com.apple.product-type.framework"
  2787. : "com.apple.product-type.library.dynamic");
  2788. case cmStateEnums::EXECUTABLE:
  2789. return (target->GetPropertyAsBool("MACOSX_BUNDLE")
  2790. ? "com.apple.product-type.application"
  2791. : "com.apple.product-type.tool");
  2792. default:
  2793. break;
  2794. }
  2795. return nullptr;
  2796. }
  2797. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeTarget(
  2798. cmGeneratorTarget* gtgt, cmXCodeObject* buildPhases)
  2799. {
  2800. if (!gtgt->IsInBuildSystem()) {
  2801. return nullptr;
  2802. }
  2803. cmXCodeObject* target = this->CreateObject(cmXCodeObject::PBXNativeTarget);
  2804. target->AddAttribute("buildPhases", buildPhases);
  2805. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2806. target->AddAttribute("buildRules", buildRules);
  2807. std::string defConfig;
  2808. defConfig = this->AddConfigurations(target, gtgt);
  2809. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2810. target->AddAttribute("dependencies", dependencies);
  2811. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2812. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2813. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  2814. if (const char* fileType = this->GetTargetFileType(gtgt)) {
  2815. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  2816. }
  2817. std::string fullName;
  2818. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2819. fullName = cmStrCat("lib", gtgt->GetName(), ".a");
  2820. } else {
  2821. fullName = gtgt->GetFullName(defConfig);
  2822. }
  2823. fileRef->AddAttribute("path", this->CreateString(fullName));
  2824. fileRef->AddAttribute("sourceTree",
  2825. this->CreateString("BUILT_PRODUCTS_DIR"));
  2826. fileRef->SetComment(gtgt->GetName());
  2827. target->AddAttribute("productReference",
  2828. this->CreateObjectReference(fileRef));
  2829. if (const char* productType = this->GetTargetProductType(gtgt)) {
  2830. target->AddAttribute("productType", this->CreateString(productType));
  2831. }
  2832. target->SetTarget(gtgt);
  2833. this->XCodeObjectMap[gtgt] = target;
  2834. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2835. return target;
  2836. }
  2837. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(
  2838. cmGeneratorTarget const* t)
  2839. {
  2840. if (!t) {
  2841. return nullptr;
  2842. }
  2843. auto const i = this->XCodeObjectMap.find(t);
  2844. if (i == this->XCodeObjectMap.end()) {
  2845. return nullptr;
  2846. }
  2847. return i->second;
  2848. }
  2849. std::string cmGlobalXCodeGenerator::GetObjectId(cmXCodeObject::PBXType ptype,
  2850. cm::string_view key)
  2851. {
  2852. std::string objectId;
  2853. if (!key.empty()) {
  2854. cmCryptoHash hash(cmCryptoHash::AlgoSHA256);
  2855. hash.Initialize();
  2856. hash.Append(&ptype, sizeof(ptype));
  2857. hash.Append(key);
  2858. objectId = cmSystemTools::UpperCase(hash.FinalizeHex().substr(0, 24));
  2859. } else {
  2860. char cUuid[40] = { 0 };
  2861. CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
  2862. CFStringRef s = CFUUIDCreateString(kCFAllocatorDefault, uuid);
  2863. CFStringGetCString(s, cUuid, sizeof(cUuid), kCFStringEncodingUTF8);
  2864. objectId = cUuid;
  2865. CFRelease(s);
  2866. CFRelease(uuid);
  2867. cmSystemTools::ReplaceString(objectId, "-", "");
  2868. if (objectId.size() > 24) {
  2869. objectId = objectId.substr(0, 24);
  2870. }
  2871. }
  2872. return objectId;
  2873. }
  2874. std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name,
  2875. const std::string& id)
  2876. {
  2877. std::string guidStoreName = cmStrCat(name, "_GUID_CMAKE");
  2878. cmProp storedGUID = this->CMakeInstance->GetCacheDefinition(guidStoreName);
  2879. if (storedGUID) {
  2880. return *storedGUID;
  2881. }
  2882. this->CMakeInstance->AddCacheEntry(guidStoreName, id.c_str(),
  2883. "Stored Xcode object GUID",
  2884. cmStateEnums::INTERNAL);
  2885. return id;
  2886. }
  2887. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2888. cmXCodeObject* dependTarget)
  2889. {
  2890. // This is called once for every edge in the target dependency graph.
  2891. cmXCodeObject* container =
  2892. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2893. container->SetComment("PBXContainerItemProxy");
  2894. container->AddAttribute("containerPortal",
  2895. this->CreateObjectReference(this->RootObject));
  2896. container->AddAttribute("proxyType", this->CreateString("1"));
  2897. container->AddAttribute("remoteGlobalIDString",
  2898. this->CreateObjectReference(dependTarget));
  2899. container->AddAttribute(
  2900. "remoteInfo", this->CreateString(dependTarget->GetTarget()->GetName()));
  2901. cmXCodeObject* targetdep =
  2902. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2903. targetdep->SetComment("PBXTargetDependency");
  2904. targetdep->AddAttribute("target", this->CreateObjectReference(dependTarget));
  2905. targetdep->AddAttribute("targetProxy",
  2906. this->CreateObjectReference(container));
  2907. cmXCodeObject* depends = target->GetAttribute("dependencies");
  2908. if (!depends) {
  2909. cmSystemTools::Error(
  2910. "target does not have dependencies attribute error..");
  2911. } else {
  2912. depends->AddUniqueObject(targetdep);
  2913. }
  2914. }
  2915. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2916. const char* attribute,
  2917. cmXCodeObject* value)
  2918. {
  2919. if (settings) {
  2920. cmXCodeObject* attr = settings->GetAttribute(attribute);
  2921. if (!attr) {
  2922. settings->AddAttribute(attribute, value);
  2923. } else {
  2924. if (value->GetType() != cmXCodeObject::OBJECT_LIST &&
  2925. value->GetType() != cmXCodeObject::STRING) {
  2926. cmSystemTools::Error("Unsupported value type for appending: " +
  2927. std::string(attribute));
  2928. return;
  2929. }
  2930. if (attr->GetType() == cmXCodeObject::OBJECT_LIST) {
  2931. if (value->GetType() == cmXCodeObject::OBJECT_LIST) {
  2932. for (auto* obj : value->GetObjectList()) {
  2933. attr->AddObject(obj);
  2934. }
  2935. } else {
  2936. attr->AddObject(value);
  2937. }
  2938. } else if (attr->GetType() == cmXCodeObject::STRING) {
  2939. if (value->GetType() == cmXCodeObject::OBJECT_LIST) {
  2940. // Add old value as a list item to new object list
  2941. // and replace the attribute with the new list
  2942. value->PrependObject(attr);
  2943. settings->AddAttribute(attribute, value);
  2944. } else {
  2945. std::string newValue =
  2946. cmStrCat(attr->GetString(), ' ', value->GetString());
  2947. attr->SetString(newValue);
  2948. }
  2949. } else {
  2950. cmSystemTools::Error("Unsupported attribute type for appending: " +
  2951. std::string(attribute));
  2952. }
  2953. }
  2954. }
  2955. }
  2956. void cmGlobalXCodeGenerator::AppendBuildSettingAttribute(
  2957. cmXCodeObject* target, const char* attribute, cmXCodeObject* value,
  2958. const std::string& configName)
  2959. {
  2960. // There are multiple configurations. Add the setting to the
  2961. // buildSettings of the configuration name given.
  2962. cmXCodeObject* configurationList =
  2963. target->GetAttribute("buildConfigurationList")->GetObject();
  2964. cmXCodeObject* buildConfigs =
  2965. configurationList->GetAttribute("buildConfigurations");
  2966. for (auto obj : buildConfigs->GetObjectList()) {
  2967. if (configName.empty() ||
  2968. obj->GetAttribute("name")->GetString() == configName) {
  2969. cmXCodeObject* settings = obj->GetAttribute("buildSettings");
  2970. this->AppendOrAddBuildSetting(settings, attribute, value);
  2971. }
  2972. }
  2973. }
  2974. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  2975. {
  2976. cmGeneratorTarget* gt = target->GetTarget();
  2977. if (!gt) {
  2978. cmSystemTools::Error("Error no target on xobject\n");
  2979. return;
  2980. }
  2981. if (!gt->IsInBuildSystem()) {
  2982. return;
  2983. }
  2984. // Add dependencies on other CMake targets.
  2985. for (const auto& dep : this->GetTargetDirectDepends(gt)) {
  2986. if (cmXCodeObject* dptarget = this->FindXCodeTarget(dep)) {
  2987. this->AddDependTarget(target, dptarget);
  2988. }
  2989. }
  2990. // Separate libraries into ones that can be linked using "Link Binary With
  2991. // Libraries" build phase and the ones that can't. Only targets that build
  2992. // Apple bundles (.app, .framework, .bundle), executables and dylibs can use
  2993. // this feature and only targets that represent actual libraries (object,
  2994. // static, dynamic or bundle, excluding executables) will be used. These are
  2995. // limitations imposed by CMake use-cases - otherwise a lot of things break.
  2996. // The rest will be linked using linker flags (OTHER_LDFLAGS setting in Xcode
  2997. // project).
  2998. std::map<std::string, std::vector<cmComputeLinkInformation::Item const*>>
  2999. configItemMap;
  3000. auto addToLinkerArguments =
  3001. [&configItemMap](const std::string& configName,
  3002. cmComputeLinkInformation::Item const* libItemPtr) {
  3003. auto& linkVector = configItemMap[configName];
  3004. if (std::find_if(linkVector.begin(), linkVector.end(),
  3005. [libItemPtr](cmComputeLinkInformation::Item const* p) {
  3006. return p == libItemPtr;
  3007. }) == linkVector.end()) {
  3008. linkVector.push_back(libItemPtr);
  3009. }
  3010. };
  3011. std::vector<cmComputeLinkInformation::Item const*> linkPhaseTargetVector;
  3012. std::map<std::string, std::vector<std::string>> targetConfigMap;
  3013. using ConfigItemPair =
  3014. std::pair<std::string, cmComputeLinkInformation::Item const*>;
  3015. std::map<std::string, std::vector<ConfigItemPair>> targetItemMap;
  3016. std::map<std::string, std::vector<std::string>> targetProductNameMap;
  3017. bool useLinkPhase = false;
  3018. bool forceLinkPhase = false;
  3019. cmProp prop =
  3020. target->GetTarget()->GetProperty("XCODE_LINK_BUILD_PHASE_MODE");
  3021. if (prop) {
  3022. if (*prop == "BUILT_ONLY") {
  3023. useLinkPhase = true;
  3024. } else if (*prop == "KNOWN_LOCATION") {
  3025. useLinkPhase = true;
  3026. forceLinkPhase = true;
  3027. } else if (*prop != "NONE") {
  3028. cmSystemTools::Error("Invalid value for XCODE_LINK_BUILD_PHASE_MODE: " +
  3029. *prop);
  3030. return;
  3031. }
  3032. }
  3033. for (auto const& configName : this->CurrentConfigurationTypes) {
  3034. cmComputeLinkInformation* cli = gt->GetLinkInformation(configName);
  3035. if (!cli) {
  3036. continue;
  3037. }
  3038. for (auto const& libItem : cli->GetItems()) {
  3039. // We want to put only static libraries, dynamic libraries, frameworks
  3040. // and bundles that are built from targets that are not imported in "Link
  3041. // Binary With Libraries" build phase. Except if the target property
  3042. // XCODE_LINK_BUILD_PHASE_MODE is KNOWN_LOCATION then all imported and
  3043. // non-target libraries will be added as well.
  3044. if (useLinkPhase &&
  3045. (gt->GetType() == cmStateEnums::EXECUTABLE ||
  3046. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3047. gt->GetType() == cmStateEnums::MODULE_LIBRARY) &&
  3048. ((libItem.Target &&
  3049. (!libItem.Target->IsImported() || forceLinkPhase) &&
  3050. (libItem.Target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3051. libItem.Target->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3052. libItem.Target->GetType() == cmStateEnums::MODULE_LIBRARY ||
  3053. libItem.Target->GetType() == cmStateEnums::UNKNOWN_LIBRARY)) ||
  3054. (!libItem.Target && libItem.IsPath && forceLinkPhase))) {
  3055. std::string libName;
  3056. bool canUseLinkPhase = true;
  3057. if (libItem.Target) {
  3058. if (libItem.Target->GetType() == cmStateEnums::UNKNOWN_LIBRARY) {
  3059. canUseLinkPhase = canUseLinkPhase && forceLinkPhase;
  3060. } else {
  3061. // If a library target uses custom build output directory Xcode
  3062. // won't pick it up so we have to resort back to linker flags, but
  3063. // that's OK as long as the custom output dir is absolute path.
  3064. for (auto const& libConfigName : this->CurrentConfigurationTypes) {
  3065. canUseLinkPhase = canUseLinkPhase &&
  3066. libItem.Target->UsesDefaultOutputDir(
  3067. libConfigName, cmStateEnums::RuntimeBinaryArtifact);
  3068. }
  3069. }
  3070. libName = libItem.Target->GetName();
  3071. } else {
  3072. libName = cmSystemTools::GetFilenameName(libItem.Value.Value);
  3073. // We don't want all the possible files here, just standard libraries
  3074. const auto libExt = cmSystemTools::GetFilenameExtension(libName);
  3075. if (!IsLinkPhaseLibraryExtension(libExt)) {
  3076. canUseLinkPhase = false;
  3077. }
  3078. }
  3079. if (canUseLinkPhase) {
  3080. // Add unique configuration name to target-config map for later
  3081. // checks
  3082. auto& configVector = targetConfigMap[libName];
  3083. if (std::find(configVector.begin(), configVector.end(),
  3084. configName) == configVector.end()) {
  3085. configVector.push_back(configName);
  3086. }
  3087. // Add a pair of config and item to target-item map
  3088. auto& itemVector = targetItemMap[libName];
  3089. itemVector.emplace_back(ConfigItemPair(configName, &libItem));
  3090. // Add product file-name to a lib-product map
  3091. auto productName =
  3092. cmSystemTools::GetFilenameName(libItem.Value.Value);
  3093. auto& productVector = targetProductNameMap[libName];
  3094. if (std::find(productVector.begin(), productVector.end(),
  3095. productName) == productVector.end()) {
  3096. productVector.push_back(productName);
  3097. }
  3098. continue;
  3099. }
  3100. }
  3101. // Add this library item to a regular linker flag list
  3102. addToLinkerArguments(configName, &libItem);
  3103. }
  3104. }
  3105. // Go through target library map and separate libraries that are linked
  3106. // in all configurations and produce only single product, from the rest.
  3107. // Only these will be linked through "Link Binary With Libraries" build
  3108. // phase.
  3109. for (auto const& targetLibConfigs : targetConfigMap) {
  3110. // Add this library to "Link Binary With Libraries" build phase if it's
  3111. // linked in all configurations and it has only one product name
  3112. auto& itemVector = targetItemMap[targetLibConfigs.first];
  3113. auto& productVector = targetProductNameMap[targetLibConfigs.first];
  3114. if (targetLibConfigs.second == this->CurrentConfigurationTypes &&
  3115. productVector.size() == 1) {
  3116. // Add this library to "Link Binary With Libraries" list
  3117. linkPhaseTargetVector.push_back(itemVector[0].second);
  3118. } else {
  3119. for (auto const& libItem : targetItemMap[targetLibConfigs.first]) {
  3120. // Add this library item to a regular linker flag list
  3121. addToLinkerArguments(libItem.first, libItem.second);
  3122. }
  3123. }
  3124. }
  3125. // Add libraries to "Link Binary With Libraries" build phase and collect
  3126. // their search paths. Xcode does not support per-configuration linking
  3127. // in this build phase so we don't have to do this for each configuration
  3128. // separately.
  3129. std::vector<std::string> linkSearchPaths;
  3130. std::vector<std::string> frameworkSearchPaths;
  3131. for (auto const& libItem : linkPhaseTargetVector) {
  3132. // Add target output directory as a library search path
  3133. std::string linkDir;
  3134. if (libItem->Target) {
  3135. linkDir = libItem->Target->GetLocationForBuild();
  3136. } else {
  3137. linkDir = libItem->Value.Value;
  3138. }
  3139. linkDir = GetLibraryOrFrameworkPath(linkDir);
  3140. bool isFramework = cmSystemTools::IsPathToFramework(linkDir);
  3141. linkDir = cmSystemTools::GetParentDirectory(linkDir);
  3142. if (isFramework) {
  3143. if (std::find(frameworkSearchPaths.begin(), frameworkSearchPaths.end(),
  3144. linkDir) == frameworkSearchPaths.end()) {
  3145. frameworkSearchPaths.push_back(linkDir);
  3146. }
  3147. } else {
  3148. if (std::find(linkSearchPaths.begin(), linkSearchPaths.end(), linkDir) ==
  3149. linkSearchPaths.end()) {
  3150. linkSearchPaths.push_back(linkDir);
  3151. }
  3152. }
  3153. // Add target dependency
  3154. if (libItem->Target && !libItem->Target->IsImported()) {
  3155. for (auto const& configName : this->CurrentConfigurationTypes) {
  3156. target->AddDependTarget(configName, libItem->Target->GetName());
  3157. }
  3158. }
  3159. // Get the library target
  3160. auto* libTarget = FindXCodeTarget(libItem->Target);
  3161. cmXCodeObject* buildFile;
  3162. if (!libTarget) {
  3163. if (libItem->IsPath) {
  3164. // Get or create a direct file ref in the root project
  3165. auto cleanPath = libItem->Value.Value;
  3166. if (cmSystemTools::FileIsFullPath(cleanPath)) {
  3167. // Some arguments are reported as paths, but they are actually not,
  3168. // so we can't collapse them, and neither can we collapse relative
  3169. // paths
  3170. cleanPath = cmSystemTools::CollapseFullPath(cleanPath);
  3171. }
  3172. auto it = this->ExternalLibRefs.find(cleanPath);
  3173. if (it == this->ExternalLibRefs.end()) {
  3174. buildFile = CreateXCodeBuildFileFromPath(cleanPath, gt, "", nullptr);
  3175. if (!buildFile) {
  3176. // Add this library item back to a regular linker flag list
  3177. for (const auto& conf : configItemMap) {
  3178. addToLinkerArguments(conf.first, libItem);
  3179. }
  3180. continue;
  3181. }
  3182. this->ExternalLibRefs.emplace(cleanPath, buildFile);
  3183. } else {
  3184. buildFile = it->second;
  3185. }
  3186. } else {
  3187. // Add this library item back to a regular linker flag list
  3188. for (const auto& conf : configItemMap) {
  3189. addToLinkerArguments(conf.first, libItem);
  3190. }
  3191. continue;
  3192. }
  3193. } else {
  3194. // Add the target output file as a build reference for other targets
  3195. // to link against
  3196. auto* fileRefObject = libTarget->GetAttribute("productReference");
  3197. if (!fileRefObject) {
  3198. // Add this library item back to a regular linker flag list
  3199. for (const auto& conf : configItemMap) {
  3200. addToLinkerArguments(conf.first, libItem);
  3201. }
  3202. continue;
  3203. }
  3204. auto it = FileRefToBuildFileMap.find(fileRefObject);
  3205. if (it == FileRefToBuildFileMap.end()) {
  3206. buildFile = this->CreateObject(cmXCodeObject::PBXBuildFile);
  3207. buildFile->AddAttribute("fileRef", fileRefObject);
  3208. FileRefToBuildFileMap[fileRefObject] = buildFile;
  3209. } else {
  3210. buildFile = it->second;
  3211. }
  3212. }
  3213. // Add this reference to current target
  3214. auto* buildPhases = target->GetAttribute("buildPhases");
  3215. if (!buildPhases) {
  3216. cmSystemTools::Error("Missing buildPhase of target");
  3217. continue;
  3218. }
  3219. auto* frameworkBuildPhase =
  3220. buildPhases->GetObject(cmXCodeObject::PBXFrameworksBuildPhase);
  3221. if (!frameworkBuildPhase) {
  3222. cmSystemTools::Error("Missing PBXFrameworksBuildPhase of buildPhase");
  3223. continue;
  3224. }
  3225. auto* buildFiles = frameworkBuildPhase->GetAttribute("files");
  3226. if (!buildFiles) {
  3227. cmSystemTools::Error("Missing files of PBXFrameworksBuildPhase");
  3228. continue;
  3229. }
  3230. if (buildFile && !buildFiles->HasObject(buildFile)) {
  3231. buildFiles->AddObject(buildFile);
  3232. }
  3233. }
  3234. // Loop over configuration types and set per-configuration info.
  3235. for (auto const& configName : this->CurrentConfigurationTypes) {
  3236. {
  3237. // Add object library contents as link flags.
  3238. BuildObjectListOrString libSearchPaths(this, true);
  3239. std::vector<cmSourceFile const*> objs;
  3240. gt->GetExternalObjects(objs, configName);
  3241. for (auto sourceFile : objs) {
  3242. if (sourceFile->GetObjectLibrary().empty()) {
  3243. continue;
  3244. }
  3245. libSearchPaths.Add(this->XCodeEscapePath(sourceFile->GetFullPath()));
  3246. }
  3247. this->AppendBuildSettingAttribute(
  3248. target, this->GetTargetLinkFlagsVar(gt), libSearchPaths.CreateList(),
  3249. configName);
  3250. }
  3251. // Skip link information for object libraries.
  3252. if (gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3253. gt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  3254. continue;
  3255. }
  3256. // Compute the link library and directory information.
  3257. cmComputeLinkInformation* cli = gt->GetLinkInformation(configName);
  3258. if (!cli) {
  3259. continue;
  3260. }
  3261. // Add dependencies directly on library files.
  3262. for (auto const& libDep : cli->GetDepends()) {
  3263. target->AddDependLibrary(configName, libDep);
  3264. }
  3265. // add the library search paths
  3266. {
  3267. BuildObjectListOrString libSearchPaths(this, true);
  3268. std::string linkDirs;
  3269. for (auto const& libDir : cli->GetDirectories()) {
  3270. if (!libDir.empty() && libDir != "/usr/lib") {
  3271. libSearchPaths.Add(this->XCodeEscapePath(
  3272. libDir + "/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"));
  3273. libSearchPaths.Add(this->XCodeEscapePath(libDir));
  3274. }
  3275. }
  3276. // Add previously collected paths where to look for libraries
  3277. // that were added to "Link Binary With Libraries"
  3278. for (auto& libDir : linkSearchPaths) {
  3279. libSearchPaths.Add(this->XCodeEscapePath(libDir));
  3280. }
  3281. // Add paths defined in project-wide build settings
  3282. libSearchPaths.Add("$(inherited)");
  3283. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  3284. libSearchPaths.CreateList(),
  3285. configName);
  3286. }
  3287. // add framework search paths
  3288. {
  3289. BuildObjectListOrString fwSearchPaths(this, true);
  3290. // Add previously collected paths where to look for frameworks
  3291. // that were added to "Link Binary With Libraries"
  3292. for (auto& fwDir : frameworkSearchPaths) {
  3293. fwSearchPaths.Add(this->XCodeEscapePath(fwDir));
  3294. }
  3295. // Add paths defined in project-wide build settings
  3296. fwSearchPaths.Add("$(inherited)");
  3297. this->AppendBuildSettingAttribute(target, "FRAMEWORK_SEARCH_PATHS",
  3298. fwSearchPaths.CreateList(),
  3299. configName);
  3300. }
  3301. // now add the left-over link libraries
  3302. {
  3303. BuildObjectListOrString libPaths(this, true);
  3304. for (auto const& libItem : configItemMap[configName]) {
  3305. auto const& libName = *libItem;
  3306. if (libName.IsPath) {
  3307. auto cleanPath = libName.Value.Value;
  3308. if (cmSystemTools::FileIsFullPath(cleanPath)) {
  3309. cleanPath = cmSystemTools::CollapseFullPath(cleanPath);
  3310. }
  3311. const auto libPath = GetLibraryOrFrameworkPath(cleanPath);
  3312. if (cmSystemTools::StringEndsWith(libPath.c_str(), ".framework")) {
  3313. const auto fwName =
  3314. cmSystemTools::GetFilenameWithoutExtension(libPath);
  3315. const auto fwDir = cmSystemTools::GetParentDirectory(libPath);
  3316. libPaths.Add("-F " + this->XCodeEscapePath(fwDir));
  3317. libPaths.Add("-framework " + fwName);
  3318. } else {
  3319. libPaths.Add(this->XCodeEscapePath(cleanPath));
  3320. }
  3321. if ((!libName.Target || libName.Target->IsImported()) &&
  3322. IsLinkPhaseLibraryExtension(libPath)) {
  3323. // Create file reference for embedding
  3324. auto it = this->ExternalLibRefs.find(cleanPath);
  3325. if (it == this->ExternalLibRefs.end()) {
  3326. auto* buildFile =
  3327. this->CreateXCodeBuildFileFromPath(cleanPath, gt, "", nullptr);
  3328. if (buildFile) {
  3329. this->ExternalLibRefs.emplace(cleanPath, buildFile);
  3330. }
  3331. }
  3332. }
  3333. } else if (!libName.Target ||
  3334. libName.Target->GetType() !=
  3335. cmStateEnums::INTERFACE_LIBRARY) {
  3336. libPaths.Add(libName.Value.Value);
  3337. }
  3338. if (libName.Target && !libName.Target->IsImported()) {
  3339. target->AddDependTarget(configName, libName.Target->GetName());
  3340. }
  3341. }
  3342. this->AppendBuildSettingAttribute(target,
  3343. this->GetTargetLinkFlagsVar(gt),
  3344. libPaths.CreateList(), configName);
  3345. }
  3346. }
  3347. }
  3348. bool cmGlobalXCodeGenerator::CreateGroups(
  3349. std::vector<cmLocalGenerator*>& generators)
  3350. {
  3351. for (auto& generator : generators) {
  3352. cmMakefile* mf = generator->GetMakefile();
  3353. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  3354. for (const auto& gtgt : generator->GetGeneratorTargets()) {
  3355. // Same skipping logic here as in CreateXCodeTargets so that we do not
  3356. // end up with (empty anyhow) ZERO_CHECK, install, or test source
  3357. // groups:
  3358. //
  3359. if (!gtgt->IsInBuildSystem() ||
  3360. gtgt->GetType() == cmStateEnums::GLOBAL_TARGET ||
  3361. gtgt->GetName() == CMAKE_CHECK_BUILD_SYSTEM_TARGET) {
  3362. continue;
  3363. }
  3364. auto addSourceToGroup = [this, mf, &gtgt,
  3365. &sourceGroups](std::string const& source) {
  3366. cmSourceGroup* sourceGroup = mf->FindSourceGroup(source, sourceGroups);
  3367. cmXCodeObject* pbxgroup =
  3368. this->CreateOrGetPBXGroup(gtgt.get(), sourceGroup);
  3369. std::string key = GetGroupMapKeyFromPath(gtgt.get(), source);
  3370. this->GroupMap[key] = pbxgroup;
  3371. };
  3372. // Put cmSourceFile instances in proper groups:
  3373. for (auto const& si : gtgt->GetAllConfigSources()) {
  3374. cmSourceFile const* sf = si.Source;
  3375. if (!sf->GetObjectLibrary().empty()) {
  3376. // Object library files go on the link line instead.
  3377. continue;
  3378. }
  3379. addSourceToGroup(sf->GetFullPath());
  3380. }
  3381. // Add CMakeLists.txt file for user convenience.
  3382. {
  3383. std::string listfile =
  3384. cmStrCat(gtgt->GetLocalGenerator()->GetCurrentSourceDirectory(),
  3385. "/CMakeLists.txt");
  3386. cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
  3387. listfile, false, cmSourceFileLocationKind::Known);
  3388. addSourceToGroup(sf->ResolveFullPath());
  3389. }
  3390. // Add the Info.plist we are about to generate for an App Bundle.
  3391. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  3392. std::string plist = this->ComputeInfoPListLocation(gtgt.get());
  3393. cmSourceFile* sf = gtgt->Makefile->GetOrCreateSource(
  3394. plist, true, cmSourceFileLocationKind::Known);
  3395. addSourceToGroup(sf->ResolveFullPath());
  3396. }
  3397. }
  3398. }
  3399. return true;
  3400. }
  3401. cmXCodeObject* cmGlobalXCodeGenerator::CreatePBXGroup(cmXCodeObject* parent,
  3402. const std::string& name)
  3403. {
  3404. cmXCodeObject* parentChildren = nullptr;
  3405. if (parent) {
  3406. parentChildren = parent->GetAttribute("children");
  3407. }
  3408. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  3409. cmXCodeObject* groupChildren =
  3410. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3411. group->AddAttribute("name", this->CreateString(name));
  3412. group->AddAttribute("children", groupChildren);
  3413. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  3414. if (parentChildren) {
  3415. parentChildren->AddObject(group);
  3416. }
  3417. return group;
  3418. }
  3419. cmXCodeObject* cmGlobalXCodeGenerator::CreateOrGetPBXGroup(
  3420. cmGeneratorTarget* gtgt, cmSourceGroup* sg)
  3421. {
  3422. std::string s;
  3423. std::string target;
  3424. const std::string targetFolder = gtgt->GetEffectiveFolderName();
  3425. if (!targetFolder.empty()) {
  3426. target = cmStrCat(targetFolder, '/');
  3427. }
  3428. target += gtgt->GetName();
  3429. s = cmStrCat(target, '/', sg->GetFullName());
  3430. auto it = this->GroupNameMap.find(s);
  3431. if (it != this->GroupNameMap.end()) {
  3432. return it->second;
  3433. }
  3434. it = this->TargetGroup.find(target);
  3435. cmXCodeObject* tgroup = nullptr;
  3436. if (it != this->TargetGroup.end()) {
  3437. tgroup = it->second;
  3438. } else {
  3439. std::vector<std::string> tgt_folders = cmTokenize(target, "/");
  3440. std::string curr_tgt_folder;
  3441. for (std::vector<std::string>::size_type i = 0; i < tgt_folders.size();
  3442. i++) {
  3443. if (i != 0) {
  3444. curr_tgt_folder += "/";
  3445. }
  3446. curr_tgt_folder += tgt_folders[i];
  3447. it = this->TargetGroup.find(curr_tgt_folder);
  3448. if (it != this->TargetGroup.end()) {
  3449. tgroup = it->second;
  3450. continue;
  3451. }
  3452. tgroup = this->CreatePBXGroup(tgroup, tgt_folders[i]);
  3453. this->TargetGroup[curr_tgt_folder] = tgroup;
  3454. if (i == 0) {
  3455. this->MainGroupChildren->AddObject(tgroup);
  3456. }
  3457. }
  3458. }
  3459. this->TargetGroup[target] = tgroup;
  3460. // If it's the default source group (empty name) then put the source file
  3461. // directly in the tgroup...
  3462. //
  3463. if (sg->GetFullName().empty()) {
  3464. this->GroupNameMap[s] = tgroup;
  3465. return tgroup;
  3466. }
  3467. // It's a recursive folder structure, let's find the real parent group
  3468. if (sg->GetFullName() != sg->GetName()) {
  3469. std::string curr_folder = cmStrCat(target, '/');
  3470. for (auto const& folder : cmTokenize(sg->GetFullName(), "\\")) {
  3471. curr_folder += folder;
  3472. auto const i_folder = this->GroupNameMap.find(curr_folder);
  3473. // Create new folder
  3474. if (i_folder == this->GroupNameMap.end()) {
  3475. cmXCodeObject* group = this->CreatePBXGroup(tgroup, folder);
  3476. this->GroupNameMap[curr_folder] = group;
  3477. tgroup = group;
  3478. } else {
  3479. tgroup = i_folder->second;
  3480. }
  3481. curr_folder += "\\";
  3482. }
  3483. return tgroup;
  3484. }
  3485. cmXCodeObject* group = this->CreatePBXGroup(tgroup, sg->GetName());
  3486. this->GroupNameMap[s] = group;
  3487. return group;
  3488. }
  3489. bool cmGlobalXCodeGenerator::CreateXCodeObjects(
  3490. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  3491. {
  3492. this->ClearXCodeObjects();
  3493. this->RootObject = nullptr;
  3494. this->MainGroupChildren = nullptr;
  3495. this->FrameworkGroup = nullptr;
  3496. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3497. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  3498. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3499. for (const std::string& CurrentConfigurationType :
  3500. this->CurrentConfigurationTypes) {
  3501. cmXCodeObject* buildStyle =
  3502. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  3503. const std::string& name = CurrentConfigurationType;
  3504. buildStyle->AddAttribute("name", this->CreateString(name));
  3505. buildStyle->SetComment(name);
  3506. cmXCodeObject* sgroup = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3507. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  3508. buildStyle->AddAttribute("buildSettings", sgroup);
  3509. listObjs->AddObject(buildStyle);
  3510. }
  3511. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  3512. this->MainGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3513. mainGroup->AddAttribute("children", this->MainGroupChildren);
  3514. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  3515. // now create the cmake groups
  3516. if (!this->CreateGroups(generators)) {
  3517. return false;
  3518. }
  3519. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  3520. productGroup->AddAttribute("name", this->CreateString("Products"));
  3521. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  3522. cmXCodeObject* productGroupChildren =
  3523. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3524. productGroup->AddAttribute("children", productGroupChildren);
  3525. this->MainGroupChildren->AddObject(productGroup);
  3526. this->FrameworkGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  3527. this->FrameworkGroup->AddAttribute("name", this->CreateString("Frameworks"));
  3528. this->FrameworkGroup->AddAttribute("sourceTree",
  3529. this->CreateString("<group>"));
  3530. cmXCodeObject* frameworkGroupChildren =
  3531. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3532. this->FrameworkGroup->AddAttribute("children", frameworkGroupChildren);
  3533. this->MainGroupChildren->AddObject(this->FrameworkGroup);
  3534. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  3535. this->RootObject->SetComment("Project object");
  3536. std::string project_id = cmStrCat("PROJECT_", root->GetProjectName());
  3537. this->RootObject->SetId(
  3538. this->GetOrCreateId(project_id, this->RootObject->GetId()));
  3539. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3540. this->RootObject->AddAttribute("mainGroup",
  3541. this->CreateObjectReference(mainGroup));
  3542. this->RootObject->AddAttribute("buildSettings", group);
  3543. this->RootObject->AddAttribute("buildStyles", listObjs);
  3544. this->RootObject->AddAttribute("hasScannedForEncodings",
  3545. this->CreateString("0"));
  3546. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3547. group->AddAttribute("BuildIndependentTargetsInParallel",
  3548. this->CreateString("YES"));
  3549. std::ostringstream v;
  3550. v << std::setfill('0') << std::setw(4) << XcodeVersion * 10;
  3551. group->AddAttribute("LastUpgradeCheck", this->CreateString(v.str()));
  3552. this->RootObject->AddAttribute("attributes", group);
  3553. this->RootObject->AddAttribute("compatibilityVersion",
  3554. this->CreateString("Xcode 3.2"));
  3555. // Point Xcode at the top of the source tree.
  3556. {
  3557. std::string pdir =
  3558. this->RelativeToBinary(root->GetCurrentSourceDirectory());
  3559. this->RootObject->AddAttribute("projectDirPath", this->CreateString(pdir));
  3560. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  3561. }
  3562. cmXCodeObject* configlist =
  3563. this->CreateObject(cmXCodeObject::XCConfigurationList);
  3564. cmXCodeObject* buildConfigurations =
  3565. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3566. using Configs = std::vector<std::pair<std::string, cmXCodeObject*>>;
  3567. Configs configs;
  3568. std::string defaultConfigName;
  3569. for (const auto& name : this->CurrentConfigurationTypes) {
  3570. if (defaultConfigName.empty()) {
  3571. defaultConfigName = name;
  3572. }
  3573. cmXCodeObject* config =
  3574. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  3575. config->AddAttribute("name", this->CreateString(name));
  3576. configs.push_back(std::make_pair(name, config));
  3577. }
  3578. if (defaultConfigName.empty()) {
  3579. defaultConfigName = "Debug";
  3580. }
  3581. for (auto& config : configs) {
  3582. buildConfigurations->AddObject(config.second);
  3583. }
  3584. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  3585. std::string comment = cmStrCat("Build configuration list for PBXProject \"",
  3586. this->CurrentProject, '"');
  3587. configlist->SetComment(comment);
  3588. configlist->AddAttribute("defaultConfigurationIsVisible",
  3589. this->CreateString("0"));
  3590. configlist->AddAttribute("defaultConfigurationName",
  3591. this->CreateString(defaultConfigName));
  3592. cmXCodeObject* buildSettings =
  3593. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  3594. cmProp sysroot = this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  3595. cmProp deploymentTarget =
  3596. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  3597. if (sysroot) {
  3598. buildSettings->AddAttribute("SDKROOT", this->CreateString(*sysroot));
  3599. }
  3600. // recompute this as it may have been changed since enable language
  3601. this->ComputeArchitectures(this->CurrentMakefile);
  3602. std::string const archs = cmJoin(this->Architectures, " ");
  3603. if (archs.empty()) {
  3604. // Tell Xcode to use NATIVE_ARCH instead of ARCHS.
  3605. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("YES"));
  3606. // When targeting macOS, use only the host architecture.
  3607. if (this->SystemName == "Darwin"_s &&
  3608. (!cmNonempty(sysroot) ||
  3609. cmSystemTools::LowerCase(*sysroot).find("macos") !=
  3610. std::string::npos)) {
  3611. buildSettings->AddAttribute("ARCHS",
  3612. this->CreateString("$(NATIVE_ARCH_ACTUAL)"));
  3613. }
  3614. } else {
  3615. // Tell Xcode to use ARCHS (ONLY_ACTIVE_ARCH defaults to NO).
  3616. buildSettings->AddAttribute("ARCHS", this->CreateString(archs));
  3617. }
  3618. if (cmNonempty(deploymentTarget)) {
  3619. buildSettings->AddAttribute(GetDeploymentPlatform(root->GetMakefile()),
  3620. this->CreateString(*deploymentTarget));
  3621. }
  3622. if (!this->GeneratorToolset.empty()) {
  3623. buildSettings->AddAttribute("GCC_VERSION",
  3624. this->CreateString(this->GeneratorToolset));
  3625. }
  3626. if (this->GetLanguageEnabled("Swift")) {
  3627. std::string swiftVersion;
  3628. if (cmProp vers = this->CurrentMakefile->GetDefinition(
  3629. "CMAKE_Swift_LANGUAGE_VERSION")) {
  3630. swiftVersion = *vers;
  3631. } else if (this->XcodeVersion >= 102) {
  3632. swiftVersion = "4.0";
  3633. } else if (this->XcodeVersion >= 83) {
  3634. swiftVersion = "3.0";
  3635. } else {
  3636. swiftVersion = "2.3";
  3637. }
  3638. buildSettings->AddAttribute("SWIFT_VERSION",
  3639. this->CreateString(swiftVersion));
  3640. }
  3641. std::string symroot = cmStrCat(root->GetCurrentBinaryDirectory(), "/build");
  3642. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot));
  3643. // Inside a try_compile project, do not require signing on any platform.
  3644. if (this->CMakeInstance->GetIsInTryCompile()) {
  3645. buildSettings->AddAttribute("CODE_SIGNING_ALLOWED",
  3646. this->CreateString("NO"));
  3647. }
  3648. for (auto& config : configs) {
  3649. cmXCodeObject* buildSettingsForCfg = this->CreateFlatClone(buildSettings);
  3650. // Put this last so it can override existing settings
  3651. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  3652. for (const auto& var : this->CurrentMakefile->GetDefinitions()) {
  3653. if (cmHasLiteralPrefix(var, "CMAKE_XCODE_ATTRIBUTE_")) {
  3654. std::string attribute = var.substr(22);
  3655. this->FilterConfigurationAttribute(config.first, attribute);
  3656. if (!attribute.empty()) {
  3657. std::string processed = cmGeneratorExpression::Evaluate(
  3658. this->CurrentMakefile->GetSafeDefinition(var),
  3659. this->CurrentLocalGenerator, config.first);
  3660. buildSettingsForCfg->AddAttribute(attribute,
  3661. this->CreateString(processed));
  3662. }
  3663. }
  3664. }
  3665. // store per-config buildSettings into configuration object
  3666. config.second->AddAttribute("buildSettings", buildSettingsForCfg);
  3667. }
  3668. this->RootObject->AddAttribute("buildConfigurationList",
  3669. this->CreateObjectReference(configlist));
  3670. std::vector<cmXCodeObject*> targets;
  3671. for (auto& generator : generators) {
  3672. if (!this->CreateXCodeTargets(generator, targets)) {
  3673. return false;
  3674. }
  3675. for (auto const& ccRoot : this->CustomCommandRoots) {
  3676. if (ccRoot.second.size() > 1) {
  3677. std::string e = "The custom command ";
  3678. std::vector<std::string> const& outputs =
  3679. ccRoot.first->GetCustomCommand()->GetOutputs();
  3680. if (!outputs.empty()) {
  3681. e = cmStrCat(e, "generating\n ", outputs[0]);
  3682. } else {
  3683. e = cmStrCat(e, "driven by\n ", ccRoot.first->GetFullPath());
  3684. }
  3685. e = cmStrCat(e, "\nis attached to multiple targets:");
  3686. for (cmGeneratorTarget const* gt : ccRoot.second) {
  3687. e = cmStrCat(e, "\n ", gt->GetName());
  3688. }
  3689. e = cmStrCat(
  3690. e,
  3691. "\nbut none of these is a common dependency of the other(s). "
  3692. "This is not allowed by the Xcode \"new build system\".");
  3693. generator->IssueMessage(MessageType::FATAL_ERROR, e);
  3694. return false;
  3695. }
  3696. }
  3697. this->CustomCommandRoots.clear();
  3698. }
  3699. // loop over all targets and add link and depend info
  3700. for (auto t : targets) {
  3701. this->AddDependAndLinkInformation(t);
  3702. }
  3703. if (this->XcodeBuildSystem == BuildSystem::One) {
  3704. this->CreateXCodeDependHackMakefile(targets);
  3705. }
  3706. // now add all targets to the root object
  3707. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  3708. for (auto t : targets) {
  3709. allTargets->AddObject(t);
  3710. cmXCodeObject* productRef = t->GetAttribute("productReference");
  3711. if (productRef) {
  3712. productGroupChildren->AddObject(productRef->GetObject());
  3713. }
  3714. }
  3715. this->RootObject->AddAttribute("targets", allTargets);
  3716. return true;
  3717. }
  3718. std::string cmGlobalXCodeGenerator::GetObjectsDirectory(
  3719. const std::string& projName, const std::string& configName,
  3720. const cmGeneratorTarget* t, const std::string& variant) const
  3721. {
  3722. std::string dir = cmStrCat(
  3723. t->GetLocalGenerator()->GetCurrentBinaryDirectory(), '/', projName,
  3724. ".build/", configName, '/', t->GetName(), ".build/", variant);
  3725. return dir;
  3726. }
  3727. void cmGlobalXCodeGenerator::ComputeArchitectures(cmMakefile* mf)
  3728. {
  3729. this->Architectures.clear();
  3730. cmProp sysroot = mf->GetDefinition("CMAKE_OSX_SYSROOT");
  3731. if (sysroot) {
  3732. mf->GetDefExpandList("CMAKE_OSX_ARCHITECTURES", this->Architectures);
  3733. }
  3734. if (this->Architectures.empty()) {
  3735. mf->GetDefExpandList("_CMAKE_APPLE_ARCHS_DEFAULT", this->Architectures);
  3736. }
  3737. if (this->Architectures.empty()) {
  3738. // With no ARCHS we use ONLY_ACTIVE_ARCH and possibly a
  3739. // platform-specific default ARCHS placeholder value.
  3740. // Look up the arch that Xcode chooses in this case.
  3741. if (cmProp arch = mf->GetDefinition("CMAKE_XCODE_ARCHS")) {
  3742. this->ObjectDirArchDefault = *arch;
  3743. // We expect only one arch but choose the first just in case.
  3744. std::string::size_type pos = this->ObjectDirArchDefault.find(';');
  3745. if (pos != std::string::npos) {
  3746. this->ObjectDirArchDefault = this->ObjectDirArchDefault.substr(0, pos);
  3747. }
  3748. }
  3749. }
  3750. this->ComputeObjectDirArch(mf);
  3751. }
  3752. void cmGlobalXCodeGenerator::ComputeObjectDirArch(cmMakefile* mf)
  3753. {
  3754. if (this->Architectures.size() > 1 || this->UseEffectivePlatformName(mf)) {
  3755. this->ObjectDirArch = "$(CURRENT_ARCH)";
  3756. } else if (!this->Architectures.empty()) {
  3757. this->ObjectDirArch = this->Architectures[0];
  3758. } else {
  3759. this->ObjectDirArch = this->ObjectDirArchDefault;
  3760. }
  3761. }
  3762. void cmGlobalXCodeGenerator::CreateXCodeDependHackMakefile(
  3763. std::vector<cmXCodeObject*>& targets)
  3764. {
  3765. cmGeneratedFileStream makefileStream(this->CurrentXCodeHackMakefile);
  3766. if (!makefileStream) {
  3767. cmSystemTools::Error("Could not create " + this->CurrentXCodeHackMakefile);
  3768. return;
  3769. }
  3770. makefileStream.SetCopyIfDifferent(true);
  3771. // one more pass for external depend information not handled
  3772. // correctly by xcode
  3773. /* clang-format off */
  3774. makefileStream << "# DO NOT EDIT\n";
  3775. makefileStream << "# This makefile makes sure all linkable targets are\n";
  3776. makefileStream << "# up-to-date with anything they link to\n"
  3777. "default:\n"
  3778. "\techo \"Do not invoke directly\"\n"
  3779. "\n";
  3780. /* clang-format on */
  3781. std::set<std::string> dummyRules;
  3782. // Write rules to help Xcode relink things at the right time.
  3783. /* clang-format off */
  3784. makefileStream <<
  3785. "# Rules to remove targets that are older than anything to which they\n"
  3786. "# link. This forces Xcode to relink the targets from scratch. It\n"
  3787. "# does not seem to check these dependencies itself.\n";
  3788. /* clang-format on */
  3789. for (const auto& configName : this->CurrentConfigurationTypes) {
  3790. for (auto target : targets) {
  3791. cmGeneratorTarget* gt = target->GetTarget();
  3792. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  3793. gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  3794. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3795. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3796. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3797. // Declare an entry point for the target post-build phase.
  3798. makefileStream << this->PostBuildMakeTarget(gt->GetName(), configName)
  3799. << ":\n";
  3800. }
  3801. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  3802. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  3803. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  3804. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  3805. std::string tfull = gt->GetFullPath(configName);
  3806. std::string trel = this->ConvertToRelativeForMake(tfull);
  3807. // Add this target to the post-build phases of its dependencies.
  3808. auto const y = target->GetDependTargets().find(configName);
  3809. if (y != target->GetDependTargets().end()) {
  3810. for (auto const& deptgt : y->second) {
  3811. makefileStream << this->PostBuildMakeTarget(deptgt, configName)
  3812. << ": " << trel << "\n";
  3813. }
  3814. }
  3815. std::vector<cmGeneratorTarget*> objlibs;
  3816. gt->GetObjectLibrariesCMP0026(objlibs);
  3817. for (auto objLib : objlibs) {
  3818. makefileStream << this->PostBuildMakeTarget(objLib->GetName(),
  3819. configName)
  3820. << ": " << trel << "\n";
  3821. }
  3822. // Create a rule for this target.
  3823. makefileStream << trel << ":";
  3824. // List dependencies if any exist.
  3825. auto const x = target->GetDependLibraries().find(configName);
  3826. if (x != target->GetDependLibraries().end()) {
  3827. for (auto const& deplib : x->second) {
  3828. std::string file = this->ConvertToRelativeForMake(deplib);
  3829. makefileStream << "\\\n\t" << file;
  3830. dummyRules.insert(file);
  3831. }
  3832. }
  3833. for (auto objLib : objlibs) {
  3834. const std::string objLibName = objLib->GetName();
  3835. std::string d = cmStrCat(
  3836. this->GetObjectsDirectory(this->CurrentProject, configName, objLib,
  3837. OBJECT_LIBRARY_ARTIFACT_DIR),
  3838. "lib", objLibName, ".a");
  3839. std::string dependency = this->ConvertToRelativeForMake(d);
  3840. makefileStream << "\\\n\t" << dependency;
  3841. dummyRules.insert(dependency);
  3842. }
  3843. // Write the action to remove the target if it is out of date.
  3844. makefileStream << "\n";
  3845. makefileStream << "\t/bin/rm -f "
  3846. << this->ConvertToRelativeForMake(tfull) << "\n";
  3847. // if building for more than one architecture
  3848. // then remove those executables as well
  3849. if (this->Architectures.size() > 1) {
  3850. std::string universal = this->GetObjectsDirectory(
  3851. this->CurrentProject, configName, gt, "$(OBJDIR)/");
  3852. for (const auto& architecture : this->Architectures) {
  3853. std::string universalFile = cmStrCat(universal, architecture, '/',
  3854. gt->GetFullName(configName));
  3855. makefileStream << "\t/bin/rm -f "
  3856. << this->ConvertToRelativeForMake(universalFile)
  3857. << "\n";
  3858. }
  3859. }
  3860. makefileStream << "\n\n";
  3861. }
  3862. }
  3863. }
  3864. makefileStream << "\n\n"
  3865. << "# For each target create a dummy rule"
  3866. << "so the target does not have to exist\n";
  3867. for (auto const& dummyRule : dummyRules) {
  3868. makefileStream << dummyRule << ":\n";
  3869. }
  3870. }
  3871. void cmGlobalXCodeGenerator::OutputXCodeProject(
  3872. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  3873. {
  3874. if (generators.empty()) {
  3875. return;
  3876. }
  3877. if (!this->CreateXCodeObjects(root, generators)) {
  3878. return;
  3879. }
  3880. std::string xcodeDir = cmStrCat(root->GetCurrentBinaryDirectory(), '/',
  3881. root->GetProjectName(), ".xcodeproj");
  3882. cmSystemTools::MakeDirectory(xcodeDir);
  3883. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  3884. cmGeneratedFileStream fout(xcodeProjFile);
  3885. fout.SetCopyIfDifferent(true);
  3886. if (!fout) {
  3887. return;
  3888. }
  3889. this->WriteXCodePBXProj(fout, root, generators);
  3890. bool hasGeneratedSchemes = this->OutputXCodeSharedSchemes(xcodeDir, root);
  3891. this->OutputXCodeWorkspaceSettings(xcodeDir, hasGeneratedSchemes);
  3892. this->ClearXCodeObjects();
  3893. // Since this call may have created new cache entries, save the cache:
  3894. //
  3895. root->GetMakefile()->GetCMakeInstance()->SaveCache(
  3896. root->GetBinaryDirectory());
  3897. }
  3898. bool cmGlobalXCodeGenerator::OutputXCodeSharedSchemes(
  3899. const std::string& xcProjDir, cmLocalGenerator* root)
  3900. {
  3901. // collect all tests for the targets
  3902. std::map<std::string, cmXCodeScheme::TestObjects> testables;
  3903. for (const auto& obj : this->XCodeObjects) {
  3904. if (obj->GetType() != cmXCodeObject::OBJECT ||
  3905. obj->GetIsA() != cmXCodeObject::PBXNativeTarget) {
  3906. continue;
  3907. }
  3908. if (!obj->GetTarget()->IsXCTestOnApple()) {
  3909. continue;
  3910. }
  3911. cmProp testee = obj->GetTarget()->GetProperty("XCTEST_TESTEE");
  3912. if (!testee) {
  3913. continue;
  3914. }
  3915. testables[*testee].push_back(obj.get());
  3916. }
  3917. // generate scheme
  3918. bool ret = false;
  3919. // Since the lowest available Xcode version for testing was 6.4,
  3920. // I'm setting this as a limit then
  3921. if (this->XcodeVersion >= 64) {
  3922. for (const auto& obj : this->XCodeObjects) {
  3923. if (obj->GetType() == cmXCodeObject::OBJECT &&
  3924. (obj->GetIsA() == cmXCodeObject::PBXNativeTarget ||
  3925. obj->GetIsA() == cmXCodeObject::PBXAggregateTarget) &&
  3926. (root->GetMakefile()->GetCMakeInstance()->GetIsInTryCompile() ||
  3927. obj->GetTarget()->GetPropertyAsBool("XCODE_GENERATE_SCHEME"))) {
  3928. const std::string& targetName = obj->GetTarget()->GetName();
  3929. cmXCodeScheme schm(root, obj.get(), testables[targetName],
  3930. this->CurrentConfigurationTypes,
  3931. this->XcodeVersion);
  3932. schm.WriteXCodeSharedScheme(xcProjDir,
  3933. this->RelativeToSource(xcProjDir));
  3934. ret = true;
  3935. }
  3936. }
  3937. }
  3938. return ret;
  3939. }
  3940. void cmGlobalXCodeGenerator::OutputXCodeWorkspaceSettings(
  3941. const std::string& xcProjDir, bool hasGeneratedSchemes)
  3942. {
  3943. std::string xcodeSharedDataDir =
  3944. cmStrCat(xcProjDir, "/project.xcworkspace/xcshareddata");
  3945. cmSystemTools::MakeDirectory(xcodeSharedDataDir);
  3946. std::string workspaceSettingsFile =
  3947. cmStrCat(xcodeSharedDataDir, "/WorkspaceSettings.xcsettings");
  3948. cmGeneratedFileStream fout(workspaceSettingsFile);
  3949. fout.SetCopyIfDifferent(true);
  3950. if (!fout) {
  3951. return;
  3952. }
  3953. cmXMLWriter xout(fout);
  3954. xout.StartDocument();
  3955. xout.Doctype("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\""
  3956. "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
  3957. xout.StartElement("plist");
  3958. xout.Attribute("version", "1.0");
  3959. xout.StartElement("dict");
  3960. if (this->XcodeVersion >= 100) {
  3961. xout.Element("key", "BuildSystemType");
  3962. switch (this->XcodeBuildSystem) {
  3963. case BuildSystem::One:
  3964. xout.Element("string", "Original");
  3965. xout.Element("key", "DisableBuildSystemDeprecationWarning");
  3966. xout.Element("true");
  3967. break;
  3968. case BuildSystem::Twelve:
  3969. xout.Element("string", "Latest");
  3970. break;
  3971. }
  3972. }
  3973. if (hasGeneratedSchemes) {
  3974. xout.Element("key",
  3975. "IDEWorkspaceSharedSettings_AutocreateContextsIfNeeded");
  3976. xout.Element("false");
  3977. }
  3978. xout.EndElement(); // dict
  3979. xout.EndElement(); // plist
  3980. xout.EndDocument();
  3981. }
  3982. void cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  3983. cmLocalGenerator*,
  3984. std::vector<cmLocalGenerator*>&)
  3985. {
  3986. SortXCodeObjects();
  3987. fout << "// !$*UTF8*$!\n";
  3988. fout << "{\n";
  3989. cmXCodeObject::Indent(1, fout);
  3990. fout << "archiveVersion = 1;\n";
  3991. cmXCodeObject::Indent(1, fout);
  3992. fout << "classes = {\n";
  3993. cmXCodeObject::Indent(1, fout);
  3994. fout << "};\n";
  3995. cmXCodeObject::Indent(1, fout);
  3996. fout << "objectVersion = 46;\n";
  3997. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  3998. cmXCodeObject::Indent(1, fout);
  3999. fout << "rootObject = " << this->RootObject->GetId()
  4000. << " /* Project object */;\n";
  4001. fout << "}\n";
  4002. }
  4003. const char* cmGlobalXCodeGenerator::GetCMakeCFGIntDir() const
  4004. {
  4005. return "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  4006. }
  4007. std::string cmGlobalXCodeGenerator::ExpandCFGIntDir(
  4008. const std::string& str, const std::string& config) const
  4009. {
  4010. std::string replace1 = "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  4011. std::string replace2 = "$(CONFIGURATION)";
  4012. std::string tmp = str;
  4013. for (std::string::size_type i = tmp.find(replace1); i != std::string::npos;
  4014. i = tmp.find(replace1, i)) {
  4015. tmp.replace(i, replace1.size(), config);
  4016. i += config.size();
  4017. }
  4018. for (std::string::size_type i = tmp.find(replace2); i != std::string::npos;
  4019. i = tmp.find(replace2, i)) {
  4020. tmp.replace(i, replace2.size(), config);
  4021. i += config.size();
  4022. }
  4023. return tmp;
  4024. }
  4025. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  4026. {
  4027. entry.Name = cmGlobalXCodeGenerator::GetActualName();
  4028. entry.Brief = "Generate Xcode project files.";
  4029. }
  4030. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(
  4031. std::string const& p)
  4032. {
  4033. return cmSystemTools::ConvertToOutputPath(p);
  4034. }
  4035. std::string cmGlobalXCodeGenerator::RelativeToSource(const std::string& p)
  4036. {
  4037. // We force conversion because Xcode breakpoints do not work unless
  4038. // they are in a file named relative to the source tree.
  4039. return cmSystemTools::ForceToRelativePath(
  4040. cmSystemTools::JoinPath(this->ProjectSourceDirectoryComponents), p);
  4041. }
  4042. std::string cmGlobalXCodeGenerator::RelativeToBinary(const std::string& p)
  4043. {
  4044. return this->CurrentLocalGenerator->MaybeConvertToRelativePath(
  4045. cmSystemTools::JoinPath(this->ProjectOutputDirectoryComponents), p);
  4046. }
  4047. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const std::string& p)
  4048. {
  4049. if (p.find_first_of(" []") != std::string::npos) {
  4050. std::string t = cmStrCat('"', p, '"');
  4051. return t;
  4052. }
  4053. return p;
  4054. }
  4055. void cmGlobalXCodeGenerator::AppendDirectoryForConfig(
  4056. const std::string& prefix, const std::string& config,
  4057. const std::string& suffix, std::string& dir)
  4058. {
  4059. if (!config.empty()) {
  4060. dir += prefix;
  4061. dir += config;
  4062. dir += suffix;
  4063. }
  4064. }
  4065. std::string cmGlobalXCodeGenerator::LookupFlags(
  4066. const std::string& varNamePrefix, const std::string& varNameLang,
  4067. const std::string& varNameSuffix, const std::string& default_flags)
  4068. {
  4069. if (!varNameLang.empty()) {
  4070. std::string varName = cmStrCat(varNamePrefix, varNameLang, varNameSuffix);
  4071. if (cmProp varValue = this->CurrentMakefile->GetDefinition(varName)) {
  4072. if (!varValue->empty()) {
  4073. return *varValue;
  4074. }
  4075. }
  4076. }
  4077. return default_flags;
  4078. }
  4079. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  4080. const char* defines_list,
  4081. bool dflag)
  4082. {
  4083. // Skip this if there are no definitions.
  4084. if (!defines_list) {
  4085. return;
  4086. }
  4087. // Expand the list of definitions.
  4088. std::vector<std::string> defines = cmExpandedList(defines_list);
  4089. // Store the definitions in the string.
  4090. this->AppendDefines(defs, defines, dflag);
  4091. }
  4092. void cmGlobalXCodeGenerator::AppendDefines(
  4093. BuildObjectListOrString& defs, std::vector<std::string> const& defines,
  4094. bool dflag)
  4095. {
  4096. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  4097. std::string def;
  4098. for (auto const& define : defines) {
  4099. // Start with -D if requested.
  4100. def = cmStrCat(dflag ? "-D" : "", define);
  4101. // Append the flag with needed escapes.
  4102. std::string tmp;
  4103. this->AppendFlag(tmp, def);
  4104. defs.Add(tmp);
  4105. }
  4106. }
  4107. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  4108. std::string const& flag) const
  4109. {
  4110. // Short-circuit for an empty flag.
  4111. if (flag.empty()) {
  4112. return;
  4113. }
  4114. // Separate from previous flags.
  4115. if (!flags.empty()) {
  4116. flags += " ";
  4117. }
  4118. // Check if the flag needs quoting.
  4119. bool quoteFlag =
  4120. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != std::string::npos;
  4121. // We escape a flag as follows:
  4122. // - Place each flag in single quotes ''
  4123. // - Escape a single quote as \'
  4124. // - Escape a backslash as \\ since it itself is an escape
  4125. // Note that in the code below we need one more level of escapes for
  4126. // C string syntax in this source file.
  4127. //
  4128. // The final level of escaping is done when the string is stored
  4129. // into the project file by cmXCodeObject::PrintString.
  4130. if (quoteFlag) {
  4131. // Open single quote.
  4132. flags += "'";
  4133. }
  4134. // Flag value with escaped quotes and backslashes.
  4135. for (auto c : flag) {
  4136. if (c == '\'') {
  4137. flags += "'\\''";
  4138. } else if (c == '\\') {
  4139. flags += "\\\\";
  4140. } else {
  4141. flags += c;
  4142. }
  4143. }
  4144. if (quoteFlag) {
  4145. // Close single quote.
  4146. flags += "'";
  4147. }
  4148. }
  4149. std::string cmGlobalXCodeGenerator::ComputeInfoPListLocation(
  4150. cmGeneratorTarget* target)
  4151. {
  4152. std::string plist =
  4153. cmStrCat(target->GetLocalGenerator()->GetCurrentBinaryDirectory(),
  4154. "/CMakeFiles/", target->GetName(), ".dir/Info.plist");
  4155. return plist;
  4156. }
  4157. // Return true if the generated build tree may contain multiple builds.
  4158. // i.e. "Can I build Debug and Release in the same tree?"
  4159. bool cmGlobalXCodeGenerator::IsMultiConfig() const
  4160. {
  4161. // Newer Xcode versions are multi config:
  4162. return true;
  4163. }
  4164. bool cmGlobalXCodeGenerator::HasKnownObjectFileLocation(
  4165. std::string* reason) const
  4166. {
  4167. if (this->ObjectDirArch.find('$') != std::string::npos) {
  4168. if (reason != nullptr) {
  4169. *reason = " under Xcode with multiple architectures";
  4170. }
  4171. return false;
  4172. }
  4173. return true;
  4174. }
  4175. bool cmGlobalXCodeGenerator::UseEffectivePlatformName(cmMakefile* mf) const
  4176. {
  4177. cmProp epnValue = this->GetCMakeInstance()->GetState()->GetGlobalProperty(
  4178. "XCODE_EMIT_EFFECTIVE_PLATFORM_NAME");
  4179. if (!epnValue) {
  4180. return mf->PlatformIsAppleEmbedded();
  4181. }
  4182. return cmIsOn(*epnValue);
  4183. }
  4184. bool cmGlobalXCodeGenerator::ShouldStripResourcePath(cmMakefile*) const
  4185. {
  4186. // Xcode determines Resource location itself
  4187. return true;
  4188. }
  4189. void cmGlobalXCodeGenerator::ComputeTargetObjectDirectory(
  4190. cmGeneratorTarget* gt) const
  4191. {
  4192. std::string configName = this->GetCMakeCFGIntDir();
  4193. std::string dir =
  4194. cmStrCat(this->GetObjectsDirectory("$(PROJECT_NAME)", configName, gt,
  4195. "$(OBJECT_FILE_DIR_normal:base)/"),
  4196. this->ObjectDirArch, '/');
  4197. gt->ObjectDirectory = dir;
  4198. }
  4199. std::string cmGlobalXCodeGenerator::GetDeploymentPlatform(const cmMakefile* mf)
  4200. {
  4201. switch (mf->GetAppleSDKType()) {
  4202. case cmMakefile::AppleSDK::AppleTVOS:
  4203. case cmMakefile::AppleSDK::AppleTVSimulator:
  4204. return "TVOS_DEPLOYMENT_TARGET";
  4205. case cmMakefile::AppleSDK::IPhoneOS:
  4206. case cmMakefile::AppleSDK::IPhoneSimulator:
  4207. return "IPHONEOS_DEPLOYMENT_TARGET";
  4208. case cmMakefile::AppleSDK::WatchOS:
  4209. case cmMakefile::AppleSDK::WatchSimulator:
  4210. return "WATCHOS_DEPLOYMENT_TARGET";
  4211. case cmMakefile::AppleSDK::MacOS:
  4212. default:
  4213. return "MACOSX_DEPLOYMENT_TARGET";
  4214. }
  4215. }