cmGlobalXCodeGenerator.cxx 133 KB

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