cmGlobalXCodeGenerator.cxx 129 KB

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