cmGlobalXCodeGenerator.cxx 134 KB

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