cmGlobalXCodeGenerator.cxx 135 KB

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