cmGlobalXCodeGenerator.cxx 134 KB

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