cmGlobalXCodeGenerator.cxx 130 KB

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