cmGlobalXCodeGenerator.cxx 111 KB

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