cmGlobalXCodeGenerator.cxx 119 KB

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