cmGlobalXCodeGenerator.cxx 134 KB

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