cmGlobalXCodeGenerator.cxx 129 KB

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