cmGlobalXCodeGenerator.cxx 129 KB

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