cmGlobalXCodeGenerator.cxx 126 KB

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