cmGlobalXCodeGenerator.cxx 129 KB

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