cmGlobalXCodeGenerator.cxx 133 KB

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