cmGlobalXCodeGenerator.cxx 126 KB

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