cmGlobalXCodeGenerator.cxx 130 KB

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