cmGlobalXCodeGenerator.cxx 129 KB

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