cmGlobalXCodeGenerator.cxx 128 KB

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