cmGlobalXCodeGenerator.cxx 134 KB

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