cmGlobalXCodeGenerator.cxx 129 KB

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