cmGlobalXCodeGenerator.cxx 129 KB

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