cmGlobalXCodeGenerator.cxx 133 KB

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