cmGlobalXCodeGenerator.cxx 118 KB

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