cmGlobalXCodeGenerator.cxx 129 KB

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