cmGlobalXCodeGenerator.cxx 129 KB

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