cmGlobalXCodeGenerator.cxx 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544
  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::vector<std::string> includes;
  1759. this->CurrentLocalGenerator->GetIncludeDirectories(includes, gtgt, "C",
  1760. configName);
  1761. std::set<std::string> emitted;
  1762. emitted.insert("/System/Library/Frameworks");
  1763. for (std::vector<std::string>::iterator i = includes.begin();
  1764. i != includes.end(); ++i) {
  1765. if (this->NameResolvesToFramework(*i)) {
  1766. std::string frameworkDir = *i;
  1767. frameworkDir += "/../";
  1768. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir);
  1769. if (emitted.insert(frameworkDir).second) {
  1770. fdirs.Add(this->XCodeEscapePath(frameworkDir));
  1771. }
  1772. } else {
  1773. std::string incpath = this->XCodeEscapePath(*i);
  1774. dirs.Add(incpath);
  1775. }
  1776. }
  1777. // Add framework search paths needed for linking.
  1778. if (cmComputeLinkInformation* cli = gtgt->GetLinkInformation(configName)) {
  1779. std::vector<std::string> const& fwDirs = cli->GetFrameworkPaths();
  1780. for (std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  1781. fdi != fwDirs.end(); ++fdi) {
  1782. if (emitted.insert(*fdi).second) {
  1783. fdirs.Add(this->XCodeEscapePath(*fdi));
  1784. }
  1785. }
  1786. }
  1787. if (!fdirs.IsEmpty()) {
  1788. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS", fdirs.CreateList());
  1789. }
  1790. if (!dirs.IsEmpty()) {
  1791. buildSettings->AddAttribute("HEADER_SEARCH_PATHS", dirs.CreateList());
  1792. }
  1793. bool same_gflags = true;
  1794. std::map<std::string, std::string> gflags;
  1795. std::string const* last_gflag = 0;
  1796. std::string optLevel = "0";
  1797. // Minimal map of flags to build settings.
  1798. for (std::set<std::string>::iterator li = languages.begin();
  1799. li != languages.end(); ++li) {
  1800. std::string& flags = cflags[*li];
  1801. std::string& gflag = gflags[*li];
  1802. std::string oflag =
  1803. this->ExtractFlagRegex("(^| )(-Ofast|-Os|-O[0-9]*)( |$)", 2, flags);
  1804. if (oflag.size() == 2) {
  1805. optLevel = "1";
  1806. } else if (oflag.size() > 2) {
  1807. optLevel = oflag.substr(2);
  1808. }
  1809. gflag = this->ExtractFlag("-g", flags);
  1810. // put back gdwarf-2 if used since there is no way
  1811. // to represent it in the gui, but we still want debug yes
  1812. if (gflag == "-gdwarf-2") {
  1813. flags += " ";
  1814. flags += gflag;
  1815. }
  1816. if (last_gflag && *last_gflag != gflag) {
  1817. same_gflags = false;
  1818. }
  1819. last_gflag = &gflag;
  1820. }
  1821. const char* debugStr = "YES";
  1822. if (!same_gflags) {
  1823. // We can't set the Xcode flag differently depending on the language,
  1824. // so put them back in this case.
  1825. for (std::set<std::string>::iterator li = languages.begin();
  1826. li != languages.end(); ++li) {
  1827. cflags[*li] += " ";
  1828. cflags[*li] += gflags[*li];
  1829. }
  1830. debugStr = "NO";
  1831. } else if (last_gflag && (last_gflag->empty() || *last_gflag == "-g0")) {
  1832. debugStr = "NO";
  1833. }
  1834. buildSettings->AddAttribute("COMBINE_HIDPI_IMAGES",
  1835. this->CreateString("YES"));
  1836. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1837. this->CreateString(debugStr));
  1838. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1839. this->CreateString(optLevel));
  1840. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1841. this->CreateString("NO"));
  1842. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1843. this->CreateString("NO"));
  1844. for (std::set<std::string>::iterator li = languages.begin();
  1845. li != languages.end(); ++li) {
  1846. std::string flags = cflags[*li] + " " + defFlags;
  1847. if (*li == "CXX") {
  1848. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1849. this->CreateString(flags));
  1850. } else if (*li == "Fortran") {
  1851. buildSettings->AddAttribute("IFORT_OTHER_FLAGS",
  1852. this->CreateString(flags));
  1853. } else if (*li == "C") {
  1854. buildSettings->AddAttribute("OTHER_CFLAGS", this->CreateString(flags));
  1855. } else if (*li == "Swift") {
  1856. buildSettings->AddAttribute("OTHER_SWIFT_FLAGS",
  1857. this->CreateString(flags));
  1858. }
  1859. }
  1860. // Add Fortran source format attribute if property is set.
  1861. const char* format = 0;
  1862. const char* tgtfmt = gtgt->GetProperty("Fortran_FORMAT");
  1863. switch (cmOutputConverter::GetFortranFormat(tgtfmt)) {
  1864. case cmOutputConverter::FortranFormatFixed:
  1865. format = "fixed";
  1866. break;
  1867. case cmOutputConverter::FortranFormatFree:
  1868. format = "free";
  1869. break;
  1870. default:
  1871. break;
  1872. }
  1873. if (format) {
  1874. buildSettings->AddAttribute("IFORT_LANG_SRCFMT",
  1875. this->CreateString(format));
  1876. }
  1877. // Create the INSTALL_PATH attribute.
  1878. std::string install_name_dir;
  1879. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1880. // Get the install_name directory for the build tree.
  1881. install_name_dir = gtgt->GetInstallNameDirForBuildTree(configName);
  1882. // Xcode doesn't create the correct install_name in some cases.
  1883. // That is, if the INSTALL_PATH is empty, or if we have versioning
  1884. // of dylib libraries, we want to specify the install_name.
  1885. // This is done by adding a link flag to create an install_name
  1886. // with just the library soname.
  1887. std::string install_name;
  1888. if (!install_name_dir.empty()) {
  1889. // Convert to a path for the native build tool.
  1890. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1891. install_name += install_name_dir;
  1892. install_name += "/";
  1893. }
  1894. install_name += gtgt->GetSOName(configName);
  1895. if ((realName != soName) || install_name_dir.empty()) {
  1896. install_name_dir = "";
  1897. extraLinkOptions += " -install_name ";
  1898. extraLinkOptions += XCodeEscapePath(install_name);
  1899. }
  1900. }
  1901. buildSettings->AddAttribute("INSTALL_PATH",
  1902. this->CreateString(install_name_dir));
  1903. // Create the LD_RUNPATH_SEARCH_PATHS
  1904. cmComputeLinkInformation* pcli = gtgt->GetLinkInformation(configName);
  1905. if (pcli) {
  1906. std::string search_paths;
  1907. std::vector<std::string> runtimeDirs;
  1908. pcli->GetRPath(runtimeDirs, false);
  1909. // runpath dirs needs to be unique to prevent corruption
  1910. std::set<std::string> unique_dirs;
  1911. for (std::vector<std::string>::const_iterator i = runtimeDirs.begin();
  1912. i != runtimeDirs.end(); ++i) {
  1913. std::string runpath = *i;
  1914. runpath = this->ExpandCFGIntDir(runpath, configName);
  1915. if (unique_dirs.find(runpath) == unique_dirs.end()) {
  1916. unique_dirs.insert(runpath);
  1917. if (!search_paths.empty()) {
  1918. search_paths += " ";
  1919. }
  1920. search_paths += this->XCodeEscapePath(runpath);
  1921. }
  1922. }
  1923. if (!search_paths.empty()) {
  1924. buildSettings->AddAttribute("LD_RUNPATH_SEARCH_PATHS",
  1925. this->CreateString(search_paths));
  1926. }
  1927. }
  1928. buildSettings->AddAttribute(this->GetTargetLinkFlagsVar(gtgt),
  1929. this->CreateString(extraLinkOptions));
  1930. buildSettings->AddAttribute("OTHER_REZFLAGS", this->CreateString(""));
  1931. buildSettings->AddAttribute("SECTORDER_FLAGS", this->CreateString(""));
  1932. buildSettings->AddAttribute("USE_HEADERMAP", this->CreateString("NO"));
  1933. if (this->XcodeVersion >= 30) {
  1934. cmXCodeObject* group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1935. group->AddObject(this->CreateString("-Wmost"));
  1936. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  1937. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  1938. group->AddObject(this->CreateString("$(inherited)"));
  1939. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  1940. } else {
  1941. buildSettings->AddAttribute(
  1942. "WARNING_CFLAGS", this->CreateString("-Wmost -Wno-four-char-constants"
  1943. " -Wno-unknown-pragmas"));
  1944. }
  1945. // Runtime version information.
  1946. if (gtgt->GetType() == cmStateEnums::SHARED_LIBRARY) {
  1947. int major;
  1948. int minor;
  1949. int patch;
  1950. // VERSION -> current_version
  1951. gtgt->GetTargetVersion(false, major, minor, patch);
  1952. std::ostringstream v;
  1953. // Xcode always wants at least 1.0.0 or nothing
  1954. if (!(major == 0 && minor == 0 && patch == 0)) {
  1955. v << major << "." << minor << "." << patch;
  1956. }
  1957. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1958. this->CreateString(v.str()));
  1959. // SOVERSION -> compatibility_version
  1960. gtgt->GetTargetVersion(true, major, minor, patch);
  1961. std::ostringstream vso;
  1962. // Xcode always wants at least 1.0.0 or nothing
  1963. if (!(major == 0 && minor == 0 && patch == 0)) {
  1964. vso << major << "." << minor << "." << patch;
  1965. }
  1966. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1967. this->CreateString(vso.str()));
  1968. }
  1969. // put this last so it can override existing settings
  1970. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  1971. {
  1972. std::vector<std::string> const& props = gtgt->GetPropertyKeys();
  1973. for (std::vector<std::string>::const_iterator i = props.begin();
  1974. i != props.end(); ++i) {
  1975. if (i->find("XCODE_ATTRIBUTE_") == 0) {
  1976. std::string attribute = i->substr(16);
  1977. this->FilterConfigurationAttribute(configName, attribute);
  1978. if (!attribute.empty()) {
  1979. cmGeneratorExpression ge;
  1980. std::string processed =
  1981. ge.Parse(gtgt->GetProperty(*i))
  1982. ->Evaluate(this->CurrentLocalGenerator, configName);
  1983. buildSettings->AddAttribute(attribute,
  1984. this->CreateString(processed));
  1985. }
  1986. }
  1987. }
  1988. }
  1989. }
  1990. cmXCodeObject* cmGlobalXCodeGenerator::CreateUtilityTarget(
  1991. cmGeneratorTarget* gtgt)
  1992. {
  1993. cmXCodeObject* shellBuildPhase =
  1994. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1995. shellBuildPhase->AddAttribute("buildActionMask",
  1996. this->CreateString("2147483647"));
  1997. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1998. shellBuildPhase->AddAttribute("files", buildFiles);
  1999. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2000. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  2001. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2002. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  2003. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  2004. this->CreateString("0"));
  2005. shellBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  2006. shellBuildPhase->AddAttribute(
  2007. "shellScript", this->CreateString("# shell script goes here\nexit 0"));
  2008. shellBuildPhase->AddAttribute("showEnvVarsInLog", this->CreateString("0"));
  2009. cmXCodeObject* target =
  2010. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  2011. target->SetComment(gtgt->GetName());
  2012. cmXCodeObject* buildPhases = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2013. std::vector<cmXCodeObject*> emptyContentVector;
  2014. this->CreateCustomCommands(buildPhases, 0, 0, 0, emptyContentVector, 0,
  2015. gtgt);
  2016. target->AddAttribute("buildPhases", buildPhases);
  2017. if (this->XcodeVersion > 20) {
  2018. this->AddConfigurations(target, gtgt);
  2019. } else {
  2020. std::string theConfig =
  2021. this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2022. cmXCodeObject* buildSettings =
  2023. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2024. this->CreateBuildSettings(gtgt, buildSettings, theConfig);
  2025. target->AddAttribute("buildSettings", buildSettings);
  2026. }
  2027. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2028. target->AddAttribute("dependencies", dependencies);
  2029. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2030. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2031. target->SetTarget(gtgt);
  2032. this->XCodeObjectMap[gtgt] = target;
  2033. // Add source files without build rules for editing convenience.
  2034. if (gtgt->GetType() == cmStateEnums::UTILITY) {
  2035. std::vector<cmSourceFile*> sources;
  2036. if (!gtgt->GetConfigCommonSourceFiles(sources)) {
  2037. return 0;
  2038. }
  2039. for (std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  2040. i != sources.end(); ++i) {
  2041. if (!(*i)->GetPropertyAsBool("GENERATED")) {
  2042. this->CreateXCodeFileReference(*i, gtgt);
  2043. }
  2044. }
  2045. }
  2046. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2047. return target;
  2048. }
  2049. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  2050. cmGeneratorTarget* gtgt)
  2051. {
  2052. std::string configTypes =
  2053. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  2054. std::vector<std::string> configVectorIn;
  2055. std::vector<std::string> configVector;
  2056. configVectorIn.push_back(configTypes);
  2057. cmSystemTools::ExpandList(configVectorIn, configVector);
  2058. cmXCodeObject* configlist =
  2059. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2060. cmXCodeObject* buildConfigurations =
  2061. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2062. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2063. std::string comment = "Build configuration list for ";
  2064. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  2065. comment += " \"";
  2066. comment += gtgt->GetName();
  2067. comment += "\"";
  2068. configlist->SetComment(comment);
  2069. target->AddAttribute("buildConfigurationList",
  2070. this->CreateObjectReference(configlist));
  2071. for (unsigned int i = 0; i < configVector.size(); ++i) {
  2072. cmXCodeObject* config =
  2073. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2074. buildConfigurations->AddObject(config);
  2075. cmXCodeObject* buildSettings =
  2076. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2077. this->CreateBuildSettings(gtgt, buildSettings, configVector[i]);
  2078. config->AddAttribute("name", this->CreateString(configVector[i]));
  2079. config->SetComment(configVector[i]);
  2080. config->AddAttribute("buildSettings", buildSettings);
  2081. }
  2082. if (!configVector.empty()) {
  2083. configlist->AddAttribute("defaultConfigurationName",
  2084. this->CreateString(configVector[0]));
  2085. configlist->AddAttribute("defaultConfigurationIsVisible",
  2086. this->CreateString("0"));
  2087. return configVector[0];
  2088. }
  2089. return "";
  2090. }
  2091. const char* cmGlobalXCodeGenerator::GetTargetLinkFlagsVar(
  2092. cmGeneratorTarget const* target) const
  2093. {
  2094. if (this->XcodeVersion >= 60 &&
  2095. (target->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2096. target->GetType() == cmStateEnums::OBJECT_LIBRARY)) {
  2097. return "OTHER_LIBTOOLFLAGS";
  2098. } else {
  2099. return "OTHER_LDFLAGS";
  2100. }
  2101. }
  2102. const char* cmGlobalXCodeGenerator::GetTargetFileType(
  2103. cmGeneratorTarget* target)
  2104. {
  2105. switch (target->GetType()) {
  2106. case cmStateEnums::OBJECT_LIBRARY:
  2107. case cmStateEnums::STATIC_LIBRARY:
  2108. return "archive.ar";
  2109. case cmStateEnums::MODULE_LIBRARY:
  2110. if (target->IsXCTestOnApple())
  2111. return "wrapper.cfbundle";
  2112. else if (target->IsCFBundleOnApple())
  2113. return "wrapper.plug-in";
  2114. else
  2115. return ((this->XcodeVersion >= 22) ? "compiled.mach-o.executable"
  2116. : "compiled.mach-o.dylib");
  2117. case cmStateEnums::SHARED_LIBRARY:
  2118. return (target->GetPropertyAsBool("FRAMEWORK")
  2119. ? "wrapper.framework"
  2120. : "compiled.mach-o.dylib");
  2121. case cmStateEnums::EXECUTABLE:
  2122. return "compiled.mach-o.executable";
  2123. default:
  2124. break;
  2125. }
  2126. return 0;
  2127. }
  2128. const char* cmGlobalXCodeGenerator::GetTargetProductType(
  2129. cmGeneratorTarget* target)
  2130. {
  2131. if (const char* e = target->GetProperty("XCODE_PRODUCT_TYPE")) {
  2132. return e;
  2133. }
  2134. switch (target->GetType()) {
  2135. case cmStateEnums::OBJECT_LIBRARY:
  2136. case cmStateEnums::STATIC_LIBRARY:
  2137. return "com.apple.product-type.library.static";
  2138. case cmStateEnums::MODULE_LIBRARY:
  2139. if (target->IsXCTestOnApple())
  2140. return "com.apple.product-type.bundle.unit-test";
  2141. else if (target->IsCFBundleOnApple())
  2142. return "com.apple.product-type.bundle";
  2143. else
  2144. return ((this->XcodeVersion >= 22)
  2145. ? "com.apple.product-type.tool"
  2146. : "com.apple.product-type.library.dynamic");
  2147. case cmStateEnums::SHARED_LIBRARY:
  2148. return (target->GetPropertyAsBool("FRAMEWORK")
  2149. ? "com.apple.product-type.framework"
  2150. : "com.apple.product-type.library.dynamic");
  2151. case cmStateEnums::EXECUTABLE:
  2152. return (target->GetPropertyAsBool("MACOSX_BUNDLE")
  2153. ? "com.apple.product-type.application"
  2154. : "com.apple.product-type.tool");
  2155. default:
  2156. break;
  2157. }
  2158. return 0;
  2159. }
  2160. cmXCodeObject* cmGlobalXCodeGenerator::CreateXCodeTarget(
  2161. cmGeneratorTarget* gtgt, cmXCodeObject* buildPhases)
  2162. {
  2163. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2164. return 0;
  2165. }
  2166. cmXCodeObject* target = this->CreateObject(cmXCodeObject::PBXNativeTarget);
  2167. target->AddAttribute("buildPhases", buildPhases);
  2168. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2169. target->AddAttribute("buildRules", buildRules);
  2170. std::string defConfig;
  2171. if (this->XcodeVersion > 20) {
  2172. defConfig = this->AddConfigurations(target, gtgt);
  2173. } else {
  2174. cmXCodeObject* buildSettings =
  2175. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2176. defConfig = this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  2177. this->CreateBuildSettings(gtgt, buildSettings, defConfig);
  2178. target->AddAttribute("buildSettings", buildSettings);
  2179. }
  2180. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2181. target->AddAttribute("dependencies", dependencies);
  2182. target->AddAttribute("name", this->CreateString(gtgt->GetName()));
  2183. target->AddAttribute("productName", this->CreateString(gtgt->GetName()));
  2184. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  2185. if (const char* fileType = this->GetTargetFileType(gtgt)) {
  2186. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  2187. }
  2188. std::string fullName;
  2189. if (gtgt->GetType() == cmStateEnums::OBJECT_LIBRARY) {
  2190. fullName = "lib";
  2191. fullName += gtgt->GetName();
  2192. fullName += ".a";
  2193. } else {
  2194. fullName = gtgt->GetFullName(defConfig);
  2195. }
  2196. fileRef->AddAttribute("path", this->CreateString(fullName));
  2197. if (this->XcodeVersion == 15) {
  2198. fileRef->AddAttribute("refType", this->CreateString("0"));
  2199. }
  2200. fileRef->AddAttribute("sourceTree",
  2201. this->CreateString("BUILT_PRODUCTS_DIR"));
  2202. fileRef->SetComment(gtgt->GetName());
  2203. target->AddAttribute("productReference",
  2204. this->CreateObjectReference(fileRef));
  2205. if (const char* productType = this->GetTargetProductType(gtgt)) {
  2206. target->AddAttribute("productType", this->CreateString(productType));
  2207. }
  2208. target->SetTarget(gtgt);
  2209. this->XCodeObjectMap[gtgt] = target;
  2210. target->SetId(this->GetOrCreateId(gtgt->GetName(), target->GetId()));
  2211. return target;
  2212. }
  2213. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(
  2214. cmGeneratorTarget const* t)
  2215. {
  2216. if (!t) {
  2217. return 0;
  2218. }
  2219. std::map<cmGeneratorTarget const*, cmXCodeObject*>::const_iterator const i =
  2220. this->XCodeObjectMap.find(t);
  2221. if (i == this->XCodeObjectMap.end()) {
  2222. return 0;
  2223. }
  2224. return i->second;
  2225. }
  2226. std::string cmGlobalXCodeGenerator::GetOrCreateId(const std::string& name,
  2227. const std::string& id)
  2228. {
  2229. std::string guidStoreName = name;
  2230. guidStoreName += "_GUID_CMAKE";
  2231. const char* storedGUID =
  2232. this->CMakeInstance->GetCacheDefinition(guidStoreName);
  2233. if (storedGUID) {
  2234. return storedGUID;
  2235. }
  2236. this->CMakeInstance->AddCacheEntry(guidStoreName, id.c_str(),
  2237. "Stored Xcode object GUID",
  2238. cmStateEnums::INTERNAL);
  2239. return id;
  2240. }
  2241. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  2242. cmXCodeObject* dependTarget)
  2243. {
  2244. // This is called once for every edge in the target dependency graph.
  2245. cmXCodeObject* container =
  2246. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  2247. container->SetComment("PBXContainerItemProxy");
  2248. container->AddAttribute("containerPortal",
  2249. this->CreateObjectReference(this->RootObject));
  2250. container->AddAttribute("proxyType", this->CreateString("1"));
  2251. container->AddAttribute("remoteGlobalIDString",
  2252. this->CreateObjectReference(dependTarget));
  2253. container->AddAttribute(
  2254. "remoteInfo", this->CreateString(dependTarget->GetTarget()->GetName()));
  2255. cmXCodeObject* targetdep =
  2256. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2257. targetdep->SetComment("PBXTargetDependency");
  2258. targetdep->AddAttribute("target", this->CreateObjectReference(dependTarget));
  2259. targetdep->AddAttribute("targetProxy",
  2260. this->CreateObjectReference(container));
  2261. cmXCodeObject* depends = target->GetObject("dependencies");
  2262. if (!depends) {
  2263. cmSystemTools::Error(
  2264. "target does not have dependencies attribute error..");
  2265. } else {
  2266. depends->AddUniqueObject(targetdep);
  2267. }
  2268. }
  2269. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2270. const char* attribute,
  2271. const char* value)
  2272. {
  2273. if (settings) {
  2274. cmXCodeObject* attr = settings->GetObject(attribute);
  2275. if (!attr) {
  2276. settings->AddAttribute(attribute, this->CreateString(value));
  2277. } else {
  2278. std::string oldValue = attr->GetString();
  2279. oldValue += " ";
  2280. oldValue += value;
  2281. attr->SetString(oldValue);
  2282. }
  2283. }
  2284. }
  2285. void cmGlobalXCodeGenerator::AppendBuildSettingAttribute(
  2286. cmXCodeObject* target, const char* attribute, const char* value,
  2287. const std::string& configName)
  2288. {
  2289. if (this->XcodeVersion < 21) {
  2290. // There is only one configuration. Add the setting to the buildSettings
  2291. // of the target.
  2292. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  2293. attribute, value);
  2294. } else {
  2295. // There are multiple configurations. Add the setting to the
  2296. // buildSettings of the configuration name given.
  2297. cmXCodeObject* configurationList =
  2298. target->GetObject("buildConfigurationList")->GetObject();
  2299. cmXCodeObject* buildConfigs =
  2300. configurationList->GetObject("buildConfigurations");
  2301. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  2302. // each configuration and the target itself has a buildSettings in it
  2303. // list.push_back(target);
  2304. for (std::vector<cmXCodeObject*>::iterator i = list.begin();
  2305. i != list.end(); ++i) {
  2306. if (!configName.empty()) {
  2307. if ((*i)->GetObject("name")->GetString() == configName) {
  2308. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2309. this->AppendOrAddBuildSetting(settings, attribute, value);
  2310. }
  2311. } else {
  2312. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2313. this->AppendOrAddBuildSetting(settings, attribute, value);
  2314. }
  2315. }
  2316. }
  2317. }
  2318. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  2319. {
  2320. cmGeneratorTarget* gt = target->GetTarget();
  2321. if (!gt) {
  2322. cmSystemTools::Error("Error no target on xobject\n");
  2323. return;
  2324. }
  2325. if (gt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2326. return;
  2327. }
  2328. // Add dependencies on other CMake targets.
  2329. TargetDependSet const& deps = this->GetTargetDirectDepends(gt);
  2330. for (TargetDependSet::const_iterator i = deps.begin(); i != deps.end();
  2331. ++i) {
  2332. if (cmXCodeObject* dptarget = this->FindXCodeTarget(*i)) {
  2333. this->AddDependTarget(target, dptarget);
  2334. }
  2335. }
  2336. // Loop over configuration types and set per-configuration info.
  2337. for (std::vector<std::string>::iterator i =
  2338. this->CurrentConfigurationTypes.begin();
  2339. i != this->CurrentConfigurationTypes.end(); ++i) {
  2340. // Get the current configuration name.
  2341. std::string configName = *i;
  2342. if (this->XcodeVersion >= 50) {
  2343. // Add object library contents as link flags.
  2344. std::string linkObjs;
  2345. const char* sep = "";
  2346. std::vector<std::string> objs;
  2347. gt->UseObjectLibraries(objs, "");
  2348. for (std::vector<std::string>::const_iterator oi = objs.begin();
  2349. oi != objs.end(); ++oi) {
  2350. linkObjs += sep;
  2351. sep = " ";
  2352. linkObjs += this->XCodeEscapePath(*oi);
  2353. }
  2354. this->AppendBuildSettingAttribute(
  2355. target, this->GetTargetLinkFlagsVar(gt), linkObjs.c_str(), configName);
  2356. }
  2357. // Skip link information for object libraries.
  2358. if (gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2359. gt->GetType() == cmStateEnums::STATIC_LIBRARY) {
  2360. continue;
  2361. }
  2362. // Compute the link library and directory information.
  2363. cmComputeLinkInformation* pcli = gt->GetLinkInformation(configName);
  2364. if (!pcli) {
  2365. continue;
  2366. }
  2367. cmComputeLinkInformation& cli = *pcli;
  2368. // Add dependencies directly on library files.
  2369. {
  2370. std::vector<std::string> const& libDeps = cli.GetDepends();
  2371. for (std::vector<std::string>::const_iterator j = libDeps.begin();
  2372. j != libDeps.end(); ++j) {
  2373. target->AddDependLibrary(configName, *j);
  2374. }
  2375. }
  2376. // add the library search paths
  2377. {
  2378. std::vector<std::string> const& libDirs = cli.GetDirectories();
  2379. std::string linkDirs;
  2380. for (std::vector<std::string>::const_iterator libDir = libDirs.begin();
  2381. libDir != libDirs.end(); ++libDir) {
  2382. if (libDir->size() && *libDir != "/usr/lib") {
  2383. if (this->XcodeVersion > 15) {
  2384. // Now add the same one but append
  2385. // $(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME) to it:
  2386. linkDirs += " ";
  2387. linkDirs += this->XCodeEscapePath(
  2388. *libDir + "/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)");
  2389. }
  2390. linkDirs += " ";
  2391. linkDirs += this->XCodeEscapePath(*libDir);
  2392. }
  2393. }
  2394. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2395. linkDirs.c_str(), configName);
  2396. }
  2397. // now add the link libraries
  2398. {
  2399. std::string linkLibs;
  2400. const char* sep = "";
  2401. typedef cmComputeLinkInformation::ItemVector ItemVector;
  2402. ItemVector const& libNames = cli.GetItems();
  2403. for (ItemVector::const_iterator li = libNames.begin();
  2404. li != libNames.end(); ++li) {
  2405. linkLibs += sep;
  2406. sep = " ";
  2407. if (li->IsPath) {
  2408. linkLibs += this->XCodeEscapePath(li->Value);
  2409. } else if (!li->Target ||
  2410. li->Target->GetType() != cmStateEnums::INTERFACE_LIBRARY) {
  2411. linkLibs += li->Value;
  2412. }
  2413. if (li->Target && !li->Target->IsImported()) {
  2414. target->AddDependTarget(configName, li->Target->GetName());
  2415. }
  2416. }
  2417. this->AppendBuildSettingAttribute(
  2418. target, this->GetTargetLinkFlagsVar(gt), linkLibs.c_str(), configName);
  2419. }
  2420. }
  2421. }
  2422. bool cmGlobalXCodeGenerator::CreateGroups(
  2423. std::vector<cmLocalGenerator*>& generators)
  2424. {
  2425. for (std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2426. i != generators.end(); ++i) {
  2427. cmMakefile* mf = (*i)->GetMakefile();
  2428. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2429. std::vector<cmGeneratorTarget*> tgts = (*i)->GetGeneratorTargets();
  2430. for (std::vector<cmGeneratorTarget*>::iterator l = tgts.begin();
  2431. l != tgts.end(); l++) {
  2432. cmGeneratorTarget* gtgt = *l;
  2433. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2434. // end up with (empty anyhow) ALL_BUILD and XCODE_DEPEND_HELPER source
  2435. // groups:
  2436. //
  2437. if (gtgt->GetType() == cmStateEnums::GLOBAL_TARGET) {
  2438. continue;
  2439. }
  2440. if (gtgt->GetType() == cmStateEnums::INTERFACE_LIBRARY) {
  2441. continue;
  2442. }
  2443. // add the soon to be generated Info.plist file as a source for a
  2444. // MACOSX_BUNDLE file
  2445. if (gtgt->GetPropertyAsBool("MACOSX_BUNDLE")) {
  2446. std::string plist = this->ComputeInfoPListLocation(gtgt);
  2447. mf->GetOrCreateSource(plist, true);
  2448. gtgt->AddSource(plist);
  2449. }
  2450. std::vector<cmSourceFile*> classes;
  2451. if (!gtgt->GetConfigCommonSourceFiles(classes)) {
  2452. return false;
  2453. }
  2454. // Put cmSourceFile instances in proper groups:
  2455. for (std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  2456. s != classes.end(); s++) {
  2457. cmSourceFile* sf = *s;
  2458. // Add the file to the list of sources.
  2459. std::string const& source = sf->GetFullPath();
  2460. cmSourceGroup* sourceGroup =
  2461. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2462. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2463. std::string key = GetGroupMapKey(gtgt, sf);
  2464. this->GroupMap[key] = pbxgroup;
  2465. }
  2466. // Put OBJECT_LIBRARY objects in proper groups:
  2467. std::vector<std::string> objs;
  2468. gtgt->UseObjectLibraries(objs, "");
  2469. for (std::vector<std::string>::const_iterator oi = objs.begin();
  2470. oi != objs.end(); ++oi) {
  2471. std::string const& source = *oi;
  2472. cmSourceGroup* sourceGroup =
  2473. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2474. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(gtgt, sourceGroup);
  2475. std::string key = GetGroupMapKeyFromPath(gtgt, source);
  2476. this->GroupMap[key] = pbxgroup;
  2477. }
  2478. }
  2479. }
  2480. return true;
  2481. }
  2482. cmXCodeObject* cmGlobalXCodeGenerator::CreatePBXGroup(cmXCodeObject* parent,
  2483. std::string name)
  2484. {
  2485. cmXCodeObject* parentChildren = NULL;
  2486. if (parent)
  2487. parentChildren = parent->GetObject("children");
  2488. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2489. cmXCodeObject* groupChildren =
  2490. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2491. group->AddAttribute("name", this->CreateString(name));
  2492. group->AddAttribute("children", groupChildren);
  2493. if (this->XcodeVersion == 15) {
  2494. group->AddAttribute("refType", this->CreateString("4"));
  2495. }
  2496. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2497. if (parentChildren)
  2498. parentChildren->AddObject(group);
  2499. return group;
  2500. }
  2501. cmXCodeObject* cmGlobalXCodeGenerator::CreateOrGetPBXGroup(
  2502. cmGeneratorTarget* gtgt, cmSourceGroup* sg)
  2503. {
  2504. std::string s;
  2505. std::string target;
  2506. const std::string targetFolder = gtgt->GetEffectiveFolderName();
  2507. if (!targetFolder.empty()) {
  2508. target = targetFolder;
  2509. target += "/";
  2510. }
  2511. target += gtgt->GetName();
  2512. s = target + "/";
  2513. s += sg->GetFullName();
  2514. std::map<std::string, cmXCodeObject*>::iterator it =
  2515. this->GroupNameMap.find(s);
  2516. if (it != this->GroupNameMap.end()) {
  2517. return it->second;
  2518. }
  2519. it = this->TargetGroup.find(target);
  2520. cmXCodeObject* tgroup = 0;
  2521. if (it != this->TargetGroup.end()) {
  2522. tgroup = it->second;
  2523. } else {
  2524. std::vector<std::string> tgt_folders =
  2525. cmSystemTools::tokenize(target, "/");
  2526. std::string curr_tgt_folder;
  2527. for (std::vector<std::string>::size_type i = 0; i < tgt_folders.size();
  2528. i++) {
  2529. if (i != 0) {
  2530. curr_tgt_folder += "/";
  2531. }
  2532. curr_tgt_folder += tgt_folders[i];
  2533. it = this->TargetGroup.find(curr_tgt_folder);
  2534. if (it != this->TargetGroup.end()) {
  2535. tgroup = it->second;
  2536. continue;
  2537. }
  2538. tgroup = this->CreatePBXGroup(tgroup, tgt_folders[i]);
  2539. this->TargetGroup[curr_tgt_folder] = tgroup;
  2540. if (i == 0) {
  2541. this->SourcesGroupChildren->AddObject(tgroup);
  2542. }
  2543. }
  2544. }
  2545. this->TargetGroup[target] = tgroup;
  2546. // If it's the default source group (empty name) then put the source file
  2547. // directly in the tgroup...
  2548. //
  2549. if (std::string(sg->GetFullName()) == "") {
  2550. this->GroupNameMap[s] = tgroup;
  2551. return tgroup;
  2552. }
  2553. // It's a recursive folder structure, let's find the real parent group
  2554. if (std::string(sg->GetFullName()) != std::string(sg->GetName())) {
  2555. std::vector<std::string> folders =
  2556. cmSystemTools::tokenize(sg->GetFullName(), "\\");
  2557. std::string curr_folder = target;
  2558. curr_folder += "/";
  2559. for (std::vector<std::string>::size_type i = 0; i < folders.size(); i++) {
  2560. curr_folder += folders[i];
  2561. std::map<std::string, cmXCodeObject*>::iterator i_folder =
  2562. this->GroupNameMap.find(curr_folder);
  2563. // Create new folder
  2564. if (i_folder == this->GroupNameMap.end()) {
  2565. cmXCodeObject* group = this->CreatePBXGroup(tgroup, folders[i]);
  2566. this->GroupNameMap[curr_folder] = group;
  2567. tgroup = group;
  2568. } else {
  2569. tgroup = i_folder->second;
  2570. }
  2571. curr_folder = curr_folder + "\\";
  2572. }
  2573. return tgroup;
  2574. }
  2575. cmXCodeObject* group = this->CreatePBXGroup(tgroup, sg->GetName());
  2576. this->GroupNameMap[s] = group;
  2577. return group;
  2578. }
  2579. bool cmGlobalXCodeGenerator::CreateXCodeObjects(
  2580. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2581. {
  2582. this->ClearXCodeObjects();
  2583. this->RootObject = 0;
  2584. this->SourcesGroupChildren = 0;
  2585. this->ResourcesGroupChildren = 0;
  2586. this->MainGroupChildren = 0;
  2587. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2588. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2589. cmXCodeObject* developBuildStyle =
  2590. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2591. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2592. if (this->XcodeVersion == 15) {
  2593. developBuildStyle->AddAttribute("name", this->CreateString("Development"));
  2594. developBuildStyle->AddAttribute("buildSettings", group);
  2595. listObjs->AddObject(developBuildStyle);
  2596. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2597. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  2598. cmXCodeObject* deployBuildStyle =
  2599. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2600. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  2601. deployBuildStyle->AddAttribute("buildSettings", group);
  2602. listObjs->AddObject(deployBuildStyle);
  2603. } else {
  2604. for (unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i) {
  2605. cmXCodeObject* buildStyle =
  2606. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2607. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2608. buildStyle->AddAttribute("name", this->CreateString(name));
  2609. buildStyle->SetComment(name);
  2610. cmXCodeObject* sgroup =
  2611. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2612. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2613. buildStyle->AddAttribute("buildSettings", sgroup);
  2614. listObjs->AddObject(buildStyle);
  2615. }
  2616. }
  2617. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2618. this->MainGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2619. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2620. if (this->XcodeVersion == 15) {
  2621. mainGroup->AddAttribute("refType", this->CreateString("4"));
  2622. }
  2623. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2624. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2625. this->SourcesGroupChildren = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2626. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  2627. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  2628. if (this->XcodeVersion == 15) {
  2629. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2630. }
  2631. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2632. this->MainGroupChildren->AddObject(sourcesGroup);
  2633. cmXCodeObject* resourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2634. this->ResourcesGroupChildren =
  2635. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2636. resourcesGroup->AddAttribute("name", this->CreateString("Resources"));
  2637. resourcesGroup->AddAttribute("children", this->ResourcesGroupChildren);
  2638. if (this->XcodeVersion == 15) {
  2639. resourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2640. }
  2641. resourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2642. this->MainGroupChildren->AddObject(resourcesGroup);
  2643. // now create the cmake groups
  2644. if (!this->CreateGroups(generators)) {
  2645. return false;
  2646. }
  2647. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2648. productGroup->AddAttribute("name", this->CreateString("Products"));
  2649. if (this->XcodeVersion == 15) {
  2650. productGroup->AddAttribute("refType", this->CreateString("4"));
  2651. }
  2652. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2653. cmXCodeObject* productGroupChildren =
  2654. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2655. productGroup->AddAttribute("children", productGroupChildren);
  2656. this->MainGroupChildren->AddObject(productGroup);
  2657. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2658. this->RootObject->SetComment("Project object");
  2659. std::string project_id = "PROJECT_";
  2660. project_id += root->GetProjectName();
  2661. this->RootObject->SetId(
  2662. this->GetOrCreateId(project_id.c_str(), this->RootObject->GetId()));
  2663. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2664. this->RootObject->AddAttribute("mainGroup",
  2665. this->CreateObjectReference(mainGroup));
  2666. this->RootObject->AddAttribute("buildSettings", group);
  2667. this->RootObject->AddAttribute("buildStyles", listObjs);
  2668. this->RootObject->AddAttribute("hasScannedForEncodings",
  2669. this->CreateString("0"));
  2670. if (this->XcodeVersion >= 30) {
  2671. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2672. group->AddAttribute("BuildIndependentTargetsInParallel",
  2673. this->CreateString("YES"));
  2674. std::ostringstream v;
  2675. v << std::setfill('0') << std::setw(4) << XcodeVersion * 10;
  2676. group->AddAttribute("LastUpgradeCheck", this->CreateString(v.str()));
  2677. this->RootObject->AddAttribute("attributes", group);
  2678. if (this->XcodeVersion >= 32)
  2679. this->RootObject->AddAttribute("compatibilityVersion",
  2680. this->CreateString("Xcode 3.2"));
  2681. else if (this->XcodeVersion >= 31)
  2682. this->RootObject->AddAttribute("compatibilityVersion",
  2683. this->CreateString("Xcode 3.1"));
  2684. else
  2685. this->RootObject->AddAttribute("compatibilityVersion",
  2686. this->CreateString("Xcode 3.0"));
  2687. }
  2688. // Point Xcode at the top of the source tree.
  2689. {
  2690. std::string pdir =
  2691. this->RelativeToBinary(root->GetCurrentSourceDirectory());
  2692. this->RootObject->AddAttribute("projectDirPath", this->CreateString(pdir));
  2693. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  2694. }
  2695. cmXCodeObject* configlist =
  2696. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2697. cmXCodeObject* buildConfigurations =
  2698. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2699. typedef std::vector<std::pair<std::string, cmXCodeObject*> > Configs;
  2700. Configs configs;
  2701. const char* defaultConfigName = "Debug";
  2702. if (this->XcodeVersion == 15) {
  2703. cmXCodeObject* configDebug =
  2704. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2705. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2706. configs.push_back(std::make_pair("Debug", configDebug));
  2707. cmXCodeObject* configRelease =
  2708. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2709. configRelease->AddAttribute("name", this->CreateString("Release"));
  2710. configs.push_back(std::make_pair("Release", configRelease));
  2711. } else {
  2712. for (unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i) {
  2713. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2714. if (0 == i) {
  2715. defaultConfigName = name;
  2716. }
  2717. cmXCodeObject* config =
  2718. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2719. config->AddAttribute("name", this->CreateString(name));
  2720. configs.push_back(std::make_pair(name, config));
  2721. }
  2722. }
  2723. for (Configs::iterator c = configs.begin(); c != configs.end(); ++c) {
  2724. buildConfigurations->AddObject(c->second);
  2725. }
  2726. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2727. std::string comment = "Build configuration list for PBXProject";
  2728. comment += " \"";
  2729. comment += this->CurrentProject;
  2730. comment += "\"";
  2731. configlist->SetComment(comment);
  2732. configlist->AddAttribute("defaultConfigurationIsVisible",
  2733. this->CreateString("0"));
  2734. configlist->AddAttribute("defaultConfigurationName",
  2735. this->CreateString(defaultConfigName));
  2736. cmXCodeObject* buildSettings =
  2737. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2738. const char* osxArch =
  2739. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2740. const char* sysroot =
  2741. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2742. const char* deploymentTarget =
  2743. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2744. std::string archs;
  2745. if (sysroot) {
  2746. if (osxArch) {
  2747. // recompute this as it may have been changed since enable language
  2748. this->Architectures.clear();
  2749. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2750. this->Architectures);
  2751. archs = cmJoin(this->Architectures, " ");
  2752. }
  2753. buildSettings->AddAttribute("SDKROOT", this->CreateString(sysroot));
  2754. }
  2755. if (archs.empty()) {
  2756. // Tell Xcode to use NATIVE_ARCH instead of ARCHS.
  2757. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH", this->CreateString("YES"));
  2758. } else {
  2759. // Tell Xcode to use ARCHS (ONLY_ACTIVE_ARCH defaults to NO).
  2760. buildSettings->AddAttribute("ARCHS", this->CreateString(archs));
  2761. }
  2762. if (deploymentTarget && *deploymentTarget) {
  2763. buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
  2764. this->CreateString(deploymentTarget));
  2765. }
  2766. if (!this->GeneratorToolset.empty()) {
  2767. buildSettings->AddAttribute("GCC_VERSION",
  2768. this->CreateString(this->GeneratorToolset));
  2769. }
  2770. if (this->GetLanguageEnabled("Swift")) {
  2771. std::string swiftVersion = "2.3";
  2772. if (const char* vers = this->CurrentMakefile->GetDefinition(
  2773. "CMAKE_Swift_LANGUAGE_VERSION")) {
  2774. swiftVersion = vers;
  2775. }
  2776. buildSettings->AddAttribute("SWIFT_VERSION",
  2777. this->CreateString(swiftVersion));
  2778. }
  2779. std::string symroot = root->GetCurrentBinaryDirectory();
  2780. symroot += "/build";
  2781. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot));
  2782. for (Configs::iterator i = configs.begin(); i != configs.end(); ++i) {
  2783. cmXCodeObject* buildSettingsForCfg = this->CreateFlatClone(buildSettings);
  2784. // Put this last so it can override existing settings
  2785. // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly.
  2786. std::vector<std::string> vars = this->CurrentMakefile->GetDefinitions();
  2787. for (std::vector<std::string>::const_iterator d = vars.begin();
  2788. d != vars.end(); ++d) {
  2789. if (d->find("CMAKE_XCODE_ATTRIBUTE_") == 0) {
  2790. std::string attribute = d->substr(22);
  2791. this->FilterConfigurationAttribute(i->first, attribute);
  2792. if (!attribute.empty()) {
  2793. cmGeneratorExpression ge;
  2794. std::string processed =
  2795. ge.Parse(this->CurrentMakefile->GetDefinition(*d))
  2796. ->Evaluate(this->CurrentLocalGenerator, i->first);
  2797. buildSettingsForCfg->AddAttribute(attribute,
  2798. this->CreateString(processed));
  2799. }
  2800. }
  2801. }
  2802. // store per-config buildSettings into configuration object
  2803. i->second->AddAttribute("buildSettings", buildSettingsForCfg);
  2804. }
  2805. this->RootObject->AddAttribute("buildConfigurationList",
  2806. this->CreateObjectReference(configlist));
  2807. std::vector<cmXCodeObject*> targets;
  2808. for (std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2809. i != generators.end(); ++i) {
  2810. if (!this->CreateXCodeTargets(*i, targets)) {
  2811. return false;
  2812. }
  2813. }
  2814. // loop over all targets and add link and depend info
  2815. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2816. i != targets.end(); ++i) {
  2817. cmXCodeObject* t = *i;
  2818. this->AddDependAndLinkInformation(t);
  2819. }
  2820. if (this->XcodeVersion < 50) {
  2821. // now create xcode depend hack makefile
  2822. this->CreateXCodeDependHackTarget(targets);
  2823. }
  2824. // now add all targets to the root object
  2825. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2826. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2827. i != targets.end(); ++i) {
  2828. cmXCodeObject* t = *i;
  2829. allTargets->AddObject(t);
  2830. cmXCodeObject* productRef = t->GetObject("productReference");
  2831. if (productRef) {
  2832. productGroupChildren->AddObject(productRef->GetObject());
  2833. }
  2834. }
  2835. this->RootObject->AddAttribute("targets", allTargets);
  2836. return true;
  2837. }
  2838. std::string cmGlobalXCodeGenerator::GetObjectsNormalDirectory(
  2839. const std::string& projName, const std::string& configName,
  2840. const cmGeneratorTarget* t) const
  2841. {
  2842. std::string dir = t->GetLocalGenerator()->GetCurrentBinaryDirectory();
  2843. dir += "/";
  2844. dir += projName;
  2845. dir += ".build/";
  2846. dir += configName;
  2847. dir += "/";
  2848. dir += t->GetName();
  2849. dir += ".build/Objects-normal/";
  2850. return dir;
  2851. }
  2852. void cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2853. std::vector<cmXCodeObject*>& targets)
  2854. {
  2855. cmGeneratedFileStream makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2856. if (!makefileStream) {
  2857. cmSystemTools::Error("Could not create",
  2858. this->CurrentXCodeHackMakefile.c_str());
  2859. return;
  2860. }
  2861. makefileStream.SetCopyIfDifferent(true);
  2862. // one more pass for external depend information not handled
  2863. // correctly by xcode
  2864. /* clang-format off */
  2865. makefileStream << "# DO NOT EDIT\n";
  2866. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2867. makefileStream << "# up-to-date with anything they link to\n"
  2868. "default:\n"
  2869. "\techo \"Do not invoke directly\"\n"
  2870. "\n";
  2871. makefileStream
  2872. << "# For each target create a dummy rule "
  2873. "so the target does not have to exist\n";
  2874. /* clang-format on */
  2875. std::set<std::string> emitted;
  2876. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2877. i != targets.end(); ++i) {
  2878. cmXCodeObject* target = *i;
  2879. std::map<std::string, cmXCodeObject::StringVec> const& deplibs =
  2880. target->GetDependLibraries();
  2881. for (std::map<std::string, cmXCodeObject::StringVec>::const_iterator ci =
  2882. deplibs.begin();
  2883. ci != deplibs.end(); ++ci) {
  2884. for (cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2885. d != ci->second.end(); ++d) {
  2886. if (emitted.insert(*d).second) {
  2887. makefileStream << this->ConvertToRelativeForMake(d->c_str())
  2888. << ":\n";
  2889. }
  2890. }
  2891. }
  2892. }
  2893. makefileStream << "\n\n";
  2894. // Write rules to help Xcode relink things at the right time.
  2895. /* clang-format off */
  2896. makefileStream <<
  2897. "# Rules to remove targets that are older than anything to which they\n"
  2898. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2899. "# does not seem to check these dependencies itself.\n";
  2900. /* clang-format on */
  2901. for (std::vector<std::string>::const_iterator ct =
  2902. this->CurrentConfigurationTypes.begin();
  2903. ct != this->CurrentConfigurationTypes.end(); ++ct) {
  2904. std::string configName = *ct;
  2905. for (std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2906. i != targets.end(); ++i) {
  2907. cmXCodeObject* target = *i;
  2908. cmGeneratorTarget* gt = target->GetTarget();
  2909. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  2910. // Nope - no post-build for OBJECT_LIRBRARY
  2911. // gt->GetType() == cmStateEnums::OBJECT_LIBRARY ||
  2912. gt->GetType() == cmStateEnums::STATIC_LIBRARY ||
  2913. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2914. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2915. // Declare an entry point for the target post-build phase.
  2916. makefileStream << this->PostBuildMakeTarget(gt->GetName(), *ct)
  2917. << ":\n";
  2918. }
  2919. if (gt->GetType() == cmStateEnums::EXECUTABLE ||
  2920. gt->GetType() == cmStateEnums::SHARED_LIBRARY ||
  2921. gt->GetType() == cmStateEnums::MODULE_LIBRARY) {
  2922. std::string tfull = gt->GetFullPath(configName);
  2923. std::string trel = this->ConvertToRelativeForMake(tfull.c_str());
  2924. // Add this target to the post-build phases of its dependencies.
  2925. std::map<std::string, cmXCodeObject::StringVec>::const_iterator y =
  2926. target->GetDependTargets().find(*ct);
  2927. if (y != target->GetDependTargets().end()) {
  2928. std::vector<std::string> const& deptgts = y->second;
  2929. for (std::vector<std::string>::const_iterator d = deptgts.begin();
  2930. d != deptgts.end(); ++d) {
  2931. makefileStream << this->PostBuildMakeTarget(*d, *ct) << ": "
  2932. << trel << "\n";
  2933. }
  2934. }
  2935. // Create a rule for this target.
  2936. makefileStream << trel << ":";
  2937. // List dependencies if any exist.
  2938. std::map<std::string, cmXCodeObject::StringVec>::const_iterator x =
  2939. target->GetDependLibraries().find(*ct);
  2940. if (x != target->GetDependLibraries().end()) {
  2941. std::vector<std::string> const& deplibs = x->second;
  2942. for (std::vector<std::string>::const_iterator d = deplibs.begin();
  2943. d != deplibs.end(); ++d) {
  2944. makefileStream << "\\\n\t"
  2945. << this->ConvertToRelativeForMake(d->c_str());
  2946. }
  2947. }
  2948. // Write the action to remove the target if it is out of date.
  2949. makefileStream << "\n";
  2950. makefileStream << "\t/bin/rm -f "
  2951. << this->ConvertToRelativeForMake(tfull.c_str())
  2952. << "\n";
  2953. // if building for more than one architecture
  2954. // then remove those exectuables as well
  2955. if (this->Architectures.size() > 1) {
  2956. std::string universal = this->GetObjectsNormalDirectory(
  2957. this->CurrentProject, configName, gt);
  2958. for (std::vector<std::string>::iterator arch =
  2959. this->Architectures.begin();
  2960. arch != this->Architectures.end(); ++arch) {
  2961. std::string universalFile = universal;
  2962. universalFile += *arch;
  2963. universalFile += "/";
  2964. universalFile += gt->GetFullName(configName);
  2965. makefileStream << "\t/bin/rm -f "
  2966. << this->ConvertToRelativeForMake(
  2967. universalFile.c_str())
  2968. << "\n";
  2969. }
  2970. }
  2971. makefileStream << "\n\n";
  2972. }
  2973. }
  2974. }
  2975. }
  2976. void cmGlobalXCodeGenerator::OutputXCodeProject(
  2977. cmLocalGenerator* root, std::vector<cmLocalGenerator*>& generators)
  2978. {
  2979. if (generators.size() == 0) {
  2980. return;
  2981. }
  2982. // Skip local generators that are excluded from this project.
  2983. for (std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2984. g != generators.end(); ++g) {
  2985. if (this->IsExcluded(root, *g)) {
  2986. continue;
  2987. }
  2988. }
  2989. if (!this->CreateXCodeObjects(root, generators)) {
  2990. return;
  2991. }
  2992. std::string xcodeDir = root->GetCurrentBinaryDirectory();
  2993. xcodeDir += "/";
  2994. xcodeDir += root->GetProjectName();
  2995. xcodeDir += ".xcode";
  2996. if (this->XcodeVersion > 20) {
  2997. xcodeDir += "proj";
  2998. }
  2999. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  3000. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  3001. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  3002. fout.SetCopyIfDifferent(true);
  3003. if (!fout) {
  3004. return;
  3005. }
  3006. this->WriteXCodePBXProj(fout, root, generators);
  3007. this->ClearXCodeObjects();
  3008. // Since this call may have created new cache entries, save the cache:
  3009. //
  3010. root->GetMakefile()->GetCMakeInstance()->SaveCache(
  3011. root->GetBinaryDirectory());
  3012. }
  3013. void cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  3014. cmLocalGenerator*,
  3015. std::vector<cmLocalGenerator*>&)
  3016. {
  3017. SortXCodeObjects();
  3018. fout << "// !$*UTF8*$!\n";
  3019. fout << "{\n";
  3020. cmXCodeObject::Indent(1, fout);
  3021. fout << "archiveVersion = 1;\n";
  3022. cmXCodeObject::Indent(1, fout);
  3023. fout << "classes = {\n";
  3024. cmXCodeObject::Indent(1, fout);
  3025. fout << "};\n";
  3026. cmXCodeObject::Indent(1, fout);
  3027. if (this->XcodeVersion >= 21) {
  3028. if (this->XcodeVersion >= 32)
  3029. fout << "objectVersion = 46;\n";
  3030. else if (this->XcodeVersion >= 31)
  3031. fout << "objectVersion = 45;\n";
  3032. else if (this->XcodeVersion >= 30)
  3033. fout << "objectVersion = 44;\n";
  3034. else
  3035. fout << "objectVersion = 42;\n";
  3036. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  3037. } else {
  3038. fout << "objectVersion = 39;\n";
  3039. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  3040. }
  3041. cmXCodeObject::Indent(1, fout);
  3042. fout << "rootObject = " << this->RootObject->GetId()
  3043. << " /* Project object */;\n";
  3044. fout << "}\n";
  3045. }
  3046. const char* cmGlobalXCodeGenerator::GetCMakeCFGIntDir() const
  3047. {
  3048. return this->XcodeVersion >= 21
  3049. ? "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)"
  3050. : ".";
  3051. }
  3052. std::string cmGlobalXCodeGenerator::ExpandCFGIntDir(
  3053. const std::string& str, const std::string& config) const
  3054. {
  3055. std::string replace1 = "$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)";
  3056. std::string replace2 = "$(CONFIGURATION)";
  3057. std::string tmp = str;
  3058. for (std::string::size_type i = tmp.find(replace1); i != std::string::npos;
  3059. i = tmp.find(replace1, i)) {
  3060. tmp.replace(i, replace1.size(), config);
  3061. i += config.size();
  3062. }
  3063. for (std::string::size_type i = tmp.find(replace2); i != std::string::npos;
  3064. i = tmp.find(replace2, i)) {
  3065. tmp.replace(i, replace2.size(), config);
  3066. i += config.size();
  3067. }
  3068. return tmp;
  3069. }
  3070. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  3071. {
  3072. entry.Name = cmGlobalXCodeGenerator::GetActualName();
  3073. entry.Brief = "Generate Xcode project files.";
  3074. }
  3075. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  3076. {
  3077. return cmSystemTools::ConvertToOutputPath(p);
  3078. }
  3079. std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
  3080. {
  3081. // We force conversion because Xcode breakpoints do not work unless
  3082. // they are in a file named relative to the source tree.
  3083. return cmOutputConverter::ForceToRelativePath(
  3084. cmSystemTools::JoinPath(this->ProjectSourceDirectoryComponents), p);
  3085. }
  3086. std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
  3087. {
  3088. return this->CurrentLocalGenerator->ConvertToRelativePath(
  3089. cmSystemTools::JoinPath(this->ProjectOutputDirectoryComponents), p);
  3090. }
  3091. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const std::string& p)
  3092. {
  3093. if (p.find(' ') != p.npos) {
  3094. std::string t = "\"";
  3095. t += p;
  3096. t += "\"";
  3097. return t;
  3098. }
  3099. return p;
  3100. }
  3101. void cmGlobalXCodeGenerator::AppendDirectoryForConfig(
  3102. const std::string& prefix, const std::string& config,
  3103. const std::string& suffix, std::string& dir)
  3104. {
  3105. if (this->XcodeVersion > 20) {
  3106. if (!config.empty()) {
  3107. dir += prefix;
  3108. dir += config;
  3109. dir += suffix;
  3110. }
  3111. }
  3112. }
  3113. std::string cmGlobalXCodeGenerator::LookupFlags(
  3114. const std::string& varNamePrefix, const std::string& varNameLang,
  3115. const std::string& varNameSuffix, const std::string& default_flags)
  3116. {
  3117. if (!varNameLang.empty()) {
  3118. std::string varName = varNamePrefix;
  3119. varName += varNameLang;
  3120. varName += varNameSuffix;
  3121. if (const char* varValue = this->CurrentMakefile->GetDefinition(varName)) {
  3122. if (*varValue) {
  3123. return varValue;
  3124. }
  3125. }
  3126. }
  3127. return default_flags;
  3128. }
  3129. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  3130. const char* defines_list,
  3131. bool dflag)
  3132. {
  3133. // Skip this if there are no definitions.
  3134. if (!defines_list) {
  3135. return;
  3136. }
  3137. // Expand the list of definitions.
  3138. std::vector<std::string> defines;
  3139. cmSystemTools::ExpandListArgument(defines_list, defines);
  3140. // Store the definitions in the string.
  3141. this->AppendDefines(defs, defines, dflag);
  3142. }
  3143. void cmGlobalXCodeGenerator::AppendDefines(
  3144. BuildObjectListOrString& defs, std::vector<std::string> const& defines,
  3145. bool dflag)
  3146. {
  3147. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  3148. std::string def;
  3149. for (std::vector<std::string>::const_iterator di = defines.begin();
  3150. di != defines.end(); ++di) {
  3151. // Start with -D if requested.
  3152. def = dflag ? "-D" : "";
  3153. def += *di;
  3154. // Append the flag with needed escapes.
  3155. std::string tmp;
  3156. this->AppendFlag(tmp, def);
  3157. defs.Add(tmp);
  3158. }
  3159. }
  3160. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  3161. std::string const& flag)
  3162. {
  3163. // Short-circuit for an empty flag.
  3164. if (flag.empty()) {
  3165. return;
  3166. }
  3167. // Separate from previous flags.
  3168. if (!flags.empty()) {
  3169. flags += " ";
  3170. }
  3171. // Check if the flag needs quoting.
  3172. bool quoteFlag =
  3173. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != flag.npos;
  3174. // We escape a flag as follows:
  3175. // - Place each flag in single quotes ''
  3176. // - Escape a single quote as \'
  3177. // - Escape a backslash as \\ since it itself is an escape
  3178. // Note that in the code below we need one more level of escapes for
  3179. // C string syntax in this source file.
  3180. //
  3181. // The final level of escaping is done when the string is stored
  3182. // into the project file by cmXCodeObject::PrintString.
  3183. if (quoteFlag) {
  3184. // Open single quote.
  3185. flags += "'";
  3186. }
  3187. // Flag value with escaped quotes and backslashes.
  3188. for (const char* c = flag.c_str(); *c; ++c) {
  3189. if (*c == '\'') {
  3190. if (this->XcodeVersion >= 40) {
  3191. flags += "'\\''";
  3192. } else {
  3193. flags += "\\'";
  3194. }
  3195. } else if (*c == '\\') {
  3196. flags += "\\\\";
  3197. } else {
  3198. flags += *c;
  3199. }
  3200. }
  3201. if (quoteFlag) {
  3202. // Close single quote.
  3203. flags += "'";
  3204. }
  3205. }
  3206. std::string cmGlobalXCodeGenerator::ComputeInfoPListLocation(
  3207. cmGeneratorTarget* target)
  3208. {
  3209. std::string plist = target->GetLocalGenerator()->GetCurrentBinaryDirectory();
  3210. plist += cmake::GetCMakeFilesDirectory();
  3211. plist += "/";
  3212. plist += target->GetName();
  3213. plist += ".dir/Info.plist";
  3214. return plist;
  3215. }
  3216. // Return true if the generated build tree may contain multiple builds.
  3217. // i.e. "Can I build Debug and Release in the same tree?"
  3218. bool cmGlobalXCodeGenerator::IsMultiConfig() const
  3219. {
  3220. // Old Xcode 1.5 is single config:
  3221. if (this->XcodeVersion == 15) {
  3222. return false;
  3223. }
  3224. // Newer Xcode versions are multi config:
  3225. return true;
  3226. }
  3227. void cmGlobalXCodeGenerator::ComputeTargetObjectDirectory(
  3228. cmGeneratorTarget* gt) const
  3229. {
  3230. std::string configName = this->GetCMakeCFGIntDir();
  3231. std::string dir =
  3232. this->GetObjectsNormalDirectory("$(PROJECT_NAME)", configName, gt);
  3233. if (this->XcodeVersion >= 21) {
  3234. dir += "$(CURRENT_ARCH)/";
  3235. } else {
  3236. #ifdef __ppc__
  3237. dir += "ppc/";
  3238. #endif
  3239. #ifdef __i386
  3240. dir += "i386/";
  3241. #endif
  3242. }
  3243. gt->ObjectDirectory = dir;
  3244. }