cmGlobalXCodeGenerator.cxx 123 KB

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