cmGlobalXCodeGenerator.cxx 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247
  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")
  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(ext == "png" || ext == "gif" || ext == "jpg")
  608. {
  609. sourcecode = "image";
  610. }
  611. else if(ext == "txt")
  612. {
  613. sourcecode += ".text";
  614. }
  615. //else
  616. // {
  617. // // Already specialized above or we leave sourcecode == "sourcecode"
  618. // // which is probably the most correct choice. Extensionless headers,
  619. // // for example... Or file types unknown to Xcode that do not map to a
  620. // // valid lastKnownFileType value.
  621. // }
  622. fileRef->AddAttribute("lastKnownFileType",
  623. this->CreateString(sourcecode.c_str()));
  624. // Store the file path relative to the top of the source tree.
  625. std::string path = this->RelativeToSource(sf->GetFullPath().c_str());
  626. std::string name = cmSystemTools::GetFilenameName(path.c_str());
  627. const char* sourceTree = (cmSystemTools::FileIsFullPath(path.c_str())?
  628. "<absolute>" : "SOURCE_ROOT");
  629. fileRef->AddAttribute("name", this->CreateString(name.c_str()));
  630. fileRef->AddAttribute("path", this->CreateString(path.c_str()));
  631. fileRef->AddAttribute("sourceTree", this->CreateString(sourceTree));
  632. if(this->XcodeVersion == 15)
  633. {
  634. fileRef->AddAttribute("refType", this->CreateString("4"));
  635. }
  636. return fileRef;
  637. }
  638. //----------------------------------------------------------------------------
  639. bool cmGlobalXCodeGenerator::SpecialTargetEmitted(std::string const& tname)
  640. {
  641. if(tname == "ALL_BUILD" || tname == "XCODE_DEPEND_HELPER" ||
  642. tname == "install" || tname == "package" || tname == "RUN_TESTS" )
  643. {
  644. if(this->TargetDoneSet.find(tname) != this->TargetDoneSet.end())
  645. {
  646. return true;
  647. }
  648. this->TargetDoneSet.insert(tname);
  649. return false;
  650. }
  651. return false;
  652. }
  653. //----------------------------------------------------------------------------
  654. void cmGlobalXCodeGenerator::SetCurrentLocalGenerator(cmLocalGenerator* gen)
  655. {
  656. this->CurrentLocalGenerator = gen;
  657. this->CurrentMakefile = gen->GetMakefile();
  658. std::string outdir =
  659. cmSystemTools::CollapseFullPath(this->CurrentMakefile->
  660. GetCurrentOutputDirectory());
  661. cmSystemTools::SplitPath(outdir.c_str(),
  662. this->CurrentOutputDirectoryComponents);
  663. // Select the current set of configuration types.
  664. this->CurrentConfigurationTypes.clear();
  665. if(this->XcodeVersion > 20)
  666. {
  667. if(const char* types =
  668. this->CurrentMakefile->GetDefinition("CMAKE_CONFIGURATION_TYPES"))
  669. {
  670. cmSystemTools::ExpandListArgument(types,
  671. this->CurrentConfigurationTypes);
  672. }
  673. }
  674. if(this->CurrentConfigurationTypes.empty())
  675. {
  676. if(const char* buildType =
  677. this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE"))
  678. {
  679. this->CurrentConfigurationTypes.push_back(buildType);
  680. }
  681. else
  682. {
  683. this->CurrentConfigurationTypes.push_back("");
  684. }
  685. }
  686. }
  687. //----------------------------------------------------------------------------
  688. void
  689. cmGlobalXCodeGenerator::CreateXCodeTargets(cmLocalGenerator* gen,
  690. std::vector<cmXCodeObject*>&
  691. targets)
  692. {
  693. this->SetCurrentLocalGenerator(gen);
  694. cmTargets &tgts = this->CurrentMakefile->GetTargets();
  695. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  696. {
  697. cmTarget& cmtarget = l->second;
  698. // make sure ALL_BUILD, INSTALL, etc are only done once
  699. if(this->SpecialTargetEmitted(l->first.c_str()))
  700. {
  701. continue;
  702. }
  703. if(cmtarget.GetType() == cmTarget::UTILITY ||
  704. cmtarget.GetType() == cmTarget::GLOBAL_TARGET)
  705. {
  706. targets.push_back(this->CreateUtilityTarget(cmtarget));
  707. continue;
  708. }
  709. // organize the sources
  710. std::vector<cmSourceFile*> const &classes = cmtarget.GetSourceFiles();
  711. std::vector<cmXCodeObject*> externalObjFiles;
  712. std::vector<cmXCodeObject*> headerFiles;
  713. std::vector<cmXCodeObject*> resourceFiles;
  714. std::vector<cmXCodeObject*> sourceFiles;
  715. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  716. i != classes.end(); ++i)
  717. {
  718. cmXCodeObject* xsf =
  719. this->CreateXCodeSourceFile(this->CurrentLocalGenerator,
  720. *i, cmtarget);
  721. cmXCodeObject* fr = xsf->GetObject("fileRef");
  722. cmXCodeObject* filetype =
  723. fr->GetObject()->GetObject("lastKnownFileType");
  724. cmTarget::SourceFileFlags tsFlags =
  725. cmtarget.GetTargetSourceFileFlags(*i);
  726. if(strcmp(filetype->GetString(), "compiled.mach-o.objfile") == 0)
  727. {
  728. externalObjFiles.push_back(xsf);
  729. }
  730. else if(this->IsHeaderFile(*i) ||
  731. (tsFlags.Type == cmTarget::SourceFileTypePrivateHeader) ||
  732. (tsFlags.Type == cmTarget::SourceFileTypePublicHeader))
  733. {
  734. headerFiles.push_back(xsf);
  735. }
  736. else if(tsFlags.Type == cmTarget::SourceFileTypeResource)
  737. {
  738. resourceFiles.push_back(xsf);
  739. }
  740. else if(!(*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  741. {
  742. // Include this file in the build if it has a known language
  743. // and has not been listed as an ignored extension for this
  744. // generator.
  745. if(this->CurrentLocalGenerator->GetSourceFileLanguage(**i) &&
  746. !this->IgnoreFile((*i)->GetExtension().c_str()))
  747. {
  748. sourceFiles.push_back(xsf);
  749. }
  750. }
  751. }
  752. // some build phases only apply to bundles and/or frameworks
  753. bool isFrameworkTarget = cmtarget.IsFrameworkOnApple();
  754. bool isBundleTarget = cmtarget.GetPropertyAsBool("MACOSX_BUNDLE");
  755. cmXCodeObject* buildFiles = 0;
  756. // create source build phase
  757. cmXCodeObject* sourceBuildPhase = 0;
  758. if (!sourceFiles.empty())
  759. {
  760. sourceBuildPhase =
  761. this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  762. sourceBuildPhase->SetComment("Sources");
  763. sourceBuildPhase->AddAttribute("buildActionMask",
  764. this->CreateString("2147483647"));
  765. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  766. for(std::vector<cmXCodeObject*>::iterator i = sourceFiles.begin();
  767. i != sourceFiles.end(); ++i)
  768. {
  769. buildFiles->AddObject(*i);
  770. }
  771. sourceBuildPhase->AddAttribute("files", buildFiles);
  772. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  773. this->CreateString("0"));
  774. }
  775. // create header build phase - only for framework targets
  776. cmXCodeObject* headerBuildPhase = 0;
  777. if (!headerFiles.empty() && isFrameworkTarget)
  778. {
  779. headerBuildPhase =
  780. this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  781. headerBuildPhase->SetComment("Headers");
  782. headerBuildPhase->AddAttribute("buildActionMask",
  783. this->CreateString("2147483647"));
  784. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  785. for(std::vector<cmXCodeObject*>::iterator i = headerFiles.begin();
  786. i != headerFiles.end(); ++i)
  787. {
  788. buildFiles->AddObject(*i);
  789. }
  790. headerBuildPhase->AddAttribute("files", buildFiles);
  791. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  792. this->CreateString("0"));
  793. }
  794. // create resource build phase - only for framework or bundle targets
  795. cmXCodeObject* resourceBuildPhase = 0;
  796. if (!resourceFiles.empty() && (isFrameworkTarget || isBundleTarget))
  797. {
  798. resourceBuildPhase =
  799. this->CreateObject(cmXCodeObject::PBXResourcesBuildPhase);
  800. resourceBuildPhase->SetComment("Resources");
  801. resourceBuildPhase->AddAttribute("buildActionMask",
  802. this->CreateString("2147483647"));
  803. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  804. for(std::vector<cmXCodeObject*>::iterator i = resourceFiles.begin();
  805. i != resourceFiles.end(); ++i)
  806. {
  807. buildFiles->AddObject(*i);
  808. }
  809. resourceBuildPhase->AddAttribute("files", buildFiles);
  810. resourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  811. this->CreateString("0"));
  812. }
  813. // create vector of "non-resource content file" build phases - only for
  814. // framework or bundle targets
  815. std::vector<cmXCodeObject*> contentBuildPhases;
  816. if (isFrameworkTarget || isBundleTarget)
  817. {
  818. typedef std::map<cmStdString, std::vector<cmSourceFile*> >
  819. mapOfVectorOfSourceFiles;
  820. mapOfVectorOfSourceFiles bundleFiles;
  821. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  822. i != classes.end(); ++i)
  823. {
  824. cmTarget::SourceFileFlags tsFlags =
  825. cmtarget.GetTargetSourceFileFlags(*i);
  826. if(tsFlags.Type == cmTarget::SourceFileTypeMacContent)
  827. {
  828. bundleFiles[tsFlags.MacFolder].push_back(*i);
  829. }
  830. }
  831. mapOfVectorOfSourceFiles::iterator mit;
  832. for ( mit = bundleFiles.begin(); mit != bundleFiles.end(); ++ mit )
  833. {
  834. cmXCodeObject* copyFilesBuildPhase =
  835. this->CreateObject(cmXCodeObject::PBXCopyFilesBuildPhase);
  836. copyFilesBuildPhase->SetComment("Copy files");
  837. copyFilesBuildPhase->AddAttribute("buildActionMask",
  838. this->CreateString("2147483647"));
  839. copyFilesBuildPhase->AddAttribute("dstSubfolderSpec",
  840. this->CreateString("6"));
  841. cmOStringStream ostr;
  842. if (cmtarget.IsFrameworkOnApple())
  843. {
  844. // dstPath in frameworks is relative to Versions/<version>
  845. ostr << mit->first;
  846. }
  847. else if ( mit->first != "MacOS" )
  848. {
  849. // dstPath in bundles is relative to Contents/MacOS
  850. ostr << "../" << mit->first.c_str();
  851. }
  852. copyFilesBuildPhase->AddAttribute("dstPath",
  853. this->CreateString(ostr.str().c_str()));
  854. copyFilesBuildPhase->AddAttribute(
  855. "runOnlyForDeploymentPostprocessing", this->CreateString("0"));
  856. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  857. copyFilesBuildPhase->AddAttribute("files", buildFiles);
  858. std::vector<cmSourceFile*>::iterator sfIt;
  859. for ( sfIt = mit->second.begin(); sfIt != mit->second.end(); ++ sfIt )
  860. {
  861. cmXCodeObject* xsf =
  862. this->CreateXCodeSourceFile(this->CurrentLocalGenerator,
  863. *sfIt, cmtarget);
  864. buildFiles->AddObject(xsf);
  865. }
  866. contentBuildPhases.push_back(copyFilesBuildPhase);
  867. }
  868. }
  869. // create framework build phase
  870. cmXCodeObject* frameworkBuildPhase = 0;
  871. if (!externalObjFiles.empty())
  872. {
  873. frameworkBuildPhase =
  874. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  875. frameworkBuildPhase->SetComment("Frameworks");
  876. frameworkBuildPhase->AddAttribute("buildActionMask",
  877. this->CreateString("2147483647"));
  878. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  879. frameworkBuildPhase->AddAttribute("files", buildFiles);
  880. for(std::vector<cmXCodeObject*>::iterator i = externalObjFiles.begin();
  881. i != externalObjFiles.end(); ++i)
  882. {
  883. buildFiles->AddObject(*i);
  884. }
  885. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  886. this->CreateString("0"));
  887. }
  888. // create list of build phases and create the XCode target
  889. cmXCodeObject* buildPhases =
  890. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  891. this->CreateCustomCommands(buildPhases, sourceBuildPhase,
  892. headerBuildPhase, resourceBuildPhase,
  893. contentBuildPhases,
  894. frameworkBuildPhase, cmtarget);
  895. targets.push_back(this->CreateXCodeTarget(cmtarget, buildPhases));
  896. }
  897. }
  898. //----------------------------------------------------------------------------
  899. void cmGlobalXCodeGenerator::ForceLinkerLanguages()
  900. {
  901. // This makes sure all targets link using the proper language.
  902. for(std::map<cmStdString, cmTarget*>::const_iterator
  903. ti = this->TotalTargets.begin(); ti != this->TotalTargets.end(); ++ti)
  904. {
  905. this->ForceLinkerLanguage(*ti->second);
  906. }
  907. }
  908. //----------------------------------------------------------------------------
  909. void cmGlobalXCodeGenerator::ForceLinkerLanguage(cmTarget& cmtarget)
  910. {
  911. // This matters only for targets that link.
  912. if(cmtarget.GetType() != cmTarget::EXECUTABLE &&
  913. cmtarget.GetType() != cmTarget::SHARED_LIBRARY &&
  914. cmtarget.GetType() != cmTarget::MODULE_LIBRARY)
  915. {
  916. return;
  917. }
  918. const char* llang = cmtarget.GetLinkerLanguage("NOCONFIG");
  919. if(!llang) { return; }
  920. // If the language is compiled as a source trust Xcode to link with it.
  921. cmTarget::LinkImplementation const* impl =
  922. cmtarget.GetLinkImplementation("NOCONFIG");
  923. for(std::vector<std::string>::const_iterator li = impl->Languages.begin();
  924. li != impl->Languages.end(); ++li)
  925. {
  926. if(*li == llang) { return; }
  927. }
  928. // Add an empty source file to the target that compiles with the
  929. // linker language. This should convince Xcode to choose the proper
  930. // language.
  931. cmMakefile* mf = cmtarget.GetMakefile();
  932. std::string fname = mf->GetCurrentOutputDirectory();
  933. fname += cmake::GetCMakeFilesDirectory();
  934. fname += "/";
  935. fname += cmtarget.GetName();
  936. fname += "-CMakeForceLinker";
  937. fname += ".";
  938. fname += cmSystemTools::LowerCase(llang);
  939. {
  940. cmGeneratedFileStream fout(fname.c_str());
  941. fout << "\n";
  942. }
  943. if(cmSourceFile* sf = mf->GetOrCreateSource(fname.c_str()))
  944. {
  945. sf->SetProperty("LANGUAGE", llang);
  946. cmtarget.AddSourceFile(sf);
  947. }
  948. }
  949. //----------------------------------------------------------------------------
  950. bool cmGlobalXCodeGenerator::IsHeaderFile(cmSourceFile* sf)
  951. {
  952. const std::vector<std::string>& hdrExts =
  953. this->CurrentMakefile->GetHeaderExtensions();
  954. return (std::find(hdrExts.begin(), hdrExts.end(), sf->GetExtension()) !=
  955. hdrExts.end());
  956. }
  957. //----------------------------------------------------------------------------
  958. cmXCodeObject*
  959. cmGlobalXCodeGenerator::CreateBuildPhase(const char* name,
  960. const char* name2,
  961. cmTarget& cmtarget,
  962. const std::vector<cmCustomCommand>&
  963. commands)
  964. {
  965. if(commands.size() == 0 && strcmp(name, "CMake ReRun") != 0)
  966. {
  967. return 0;
  968. }
  969. cmXCodeObject* buildPhase =
  970. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  971. buildPhase->AddAttribute("buildActionMask",
  972. this->CreateString("2147483647"));
  973. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  974. buildPhase->AddAttribute("files", buildFiles);
  975. buildPhase->AddAttribute("name",
  976. this->CreateString(name));
  977. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  978. this->CreateString("0"));
  979. buildPhase->AddAttribute("shellPath",
  980. this->CreateString("/bin/sh"));
  981. this->AddCommandsToBuildPhase(buildPhase, cmtarget, commands,
  982. name2);
  983. return buildPhase;
  984. }
  985. //----------------------------------------------------------------------------
  986. void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
  987. cmXCodeObject*
  988. sourceBuildPhase,
  989. cmXCodeObject*
  990. headerBuildPhase,
  991. cmXCodeObject*
  992. resourceBuildPhase,
  993. std::vector<cmXCodeObject*>
  994. contentBuildPhases,
  995. cmXCodeObject*
  996. frameworkBuildPhase,
  997. cmTarget& cmtarget)
  998. {
  999. std::vector<cmCustomCommand> const & prebuild
  1000. = cmtarget.GetPreBuildCommands();
  1001. std::vector<cmCustomCommand> const & prelink
  1002. = cmtarget.GetPreLinkCommands();
  1003. std::vector<cmCustomCommand> const & postbuild
  1004. = cmtarget.GetPostBuildCommands();
  1005. std::vector<cmSourceFile*>const &classes = cmtarget.GetSourceFiles();
  1006. // add all the sources
  1007. std::vector<cmCustomCommand> commands;
  1008. for(std::vector<cmSourceFile*>::const_iterator i = classes.begin();
  1009. i != classes.end(); ++i)
  1010. {
  1011. if((*i)->GetCustomCommand())
  1012. {
  1013. commands.push_back(*(*i)->GetCustomCommand());
  1014. }
  1015. }
  1016. std::vector<cmCustomCommand> reruncom;
  1017. cmXCodeObject* cmakeReRunPhase =
  1018. this->CreateBuildPhase("CMake ReRun", "cmakeReRunPhase",
  1019. cmtarget, reruncom);
  1020. buildPhases->AddObject(cmakeReRunPhase);
  1021. // create prebuild phase
  1022. cmXCodeObject* cmakeRulesBuildPhase =
  1023. this->CreateBuildPhase("CMake Rules",
  1024. "cmakeRulesBuildPhase",
  1025. cmtarget, commands);
  1026. // create prebuild phase
  1027. cmXCodeObject* preBuildPhase =
  1028. this->CreateBuildPhase("CMake PreBuild Rules", "preBuildCommands",
  1029. cmtarget, prebuild);
  1030. // create prelink phase
  1031. cmXCodeObject* preLinkPhase =
  1032. this->CreateBuildPhase("CMake PreLink Rules", "preLinkCommands",
  1033. cmtarget, prelink);
  1034. // create postbuild phase
  1035. cmXCodeObject* postBuildPhase =
  1036. this->CreateBuildPhase("CMake PostBuild Rules", "postBuildPhase",
  1037. cmtarget, postbuild);
  1038. // The order here is the order they will be built in.
  1039. // The order "headers, resources, sources" mimics a native project generated
  1040. // from an xcode template...
  1041. //
  1042. if(preBuildPhase)
  1043. {
  1044. buildPhases->AddObject(preBuildPhase);
  1045. }
  1046. if(cmakeRulesBuildPhase)
  1047. {
  1048. buildPhases->AddObject(cmakeRulesBuildPhase);
  1049. }
  1050. if(headerBuildPhase)
  1051. {
  1052. buildPhases->AddObject(headerBuildPhase);
  1053. }
  1054. if(resourceBuildPhase)
  1055. {
  1056. buildPhases->AddObject(resourceBuildPhase);
  1057. }
  1058. std::vector<cmXCodeObject*>::iterator cit;
  1059. for (cit = contentBuildPhases.begin(); cit != contentBuildPhases.end();
  1060. ++cit)
  1061. {
  1062. buildPhases->AddObject(*cit);
  1063. }
  1064. if(sourceBuildPhase)
  1065. {
  1066. buildPhases->AddObject(sourceBuildPhase);
  1067. }
  1068. if(preLinkPhase)
  1069. {
  1070. buildPhases->AddObject(preLinkPhase);
  1071. }
  1072. if(frameworkBuildPhase)
  1073. {
  1074. buildPhases->AddObject(frameworkBuildPhase);
  1075. }
  1076. if(postBuildPhase)
  1077. {
  1078. buildPhases->AddObject(postBuildPhase);
  1079. }
  1080. }
  1081. //----------------------------------------------------------------------------
  1082. std::string cmGlobalXCodeGenerator::ExtractFlag(const char* flag,
  1083. std::string& flags)
  1084. {
  1085. std::string retFlag;
  1086. std::string::size_type pos = flags.find(flag);
  1087. if(pos != flags.npos && (pos ==0 || flags[pos-1]==' '))
  1088. {
  1089. while(pos < flags.size() && flags[pos] != ' ')
  1090. {
  1091. retFlag += flags[pos];
  1092. flags[pos] = ' ';
  1093. pos++;
  1094. }
  1095. }
  1096. return retFlag;
  1097. }
  1098. //----------------------------------------------------------------------------
  1099. void
  1100. cmGlobalXCodeGenerator::AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  1101. cmTarget& target,
  1102. std::vector<cmCustomCommand>
  1103. const & commands,
  1104. const char* name)
  1105. {
  1106. if(strcmp(name, "cmakeReRunPhase") == 0)
  1107. {
  1108. std::string cdir = this->CurrentMakefile->GetHomeOutputDirectory();
  1109. cdir = this->ConvertToRelativeForMake(cdir.c_str());
  1110. std::string makecmd = "make -C ";
  1111. makecmd += cdir;
  1112. makecmd += " -f ";
  1113. makecmd +=
  1114. this->ConvertToRelativeForMake(this->CurrentReRunCMakeMakefile.c_str());
  1115. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  1116. buildphase->AddAttribute("shellScript",
  1117. this->CreateString(makecmd.c_str()));
  1118. return;
  1119. }
  1120. // collect multiple outputs of custom commands into a set
  1121. // which will be used for every configuration
  1122. std::map<cmStdString, cmStdString> multipleOutputPairs;
  1123. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1124. i != commands.end(); ++i)
  1125. {
  1126. cmCustomCommand const& cc = *i;
  1127. if(!cc.GetCommandLines().empty())
  1128. {
  1129. const std::vector<std::string>& outputs = cc.GetOutputs();
  1130. if(!outputs.empty())
  1131. {
  1132. // If there are more than one outputs treat the
  1133. // first as the primary output and make the rest depend on it.
  1134. std::vector<std::string>::const_iterator o = outputs.begin();
  1135. std::string primaryOutput = this->ConvertToRelativeForMake(o->c_str());
  1136. for(++o; o != outputs.end(); ++o)
  1137. {
  1138. std::string currentOutput=this->ConvertToRelativeForMake(o->c_str());
  1139. multipleOutputPairs[currentOutput] = primaryOutput;
  1140. }
  1141. }
  1142. }
  1143. }
  1144. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1145. dir += "/CMakeScripts";
  1146. cmSystemTools::MakeDirectory(dir.c_str());
  1147. std::string makefile = dir;
  1148. makefile += "/";
  1149. makefile += target.GetName();
  1150. makefile += "_";
  1151. makefile += name;
  1152. makefile += ".make";
  1153. for (std::vector<std::string>::const_iterator currentConfig=
  1154. this->CurrentConfigurationTypes.begin();
  1155. currentConfig!=this->CurrentConfigurationTypes.end();
  1156. currentConfig++ )
  1157. {
  1158. this->CreateCustomRulesMakefile(makefile.c_str(),
  1159. target,
  1160. commands,
  1161. currentConfig->c_str(),
  1162. multipleOutputPairs);
  1163. }
  1164. std::string cdir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1165. cdir = this->ConvertToRelativeForXCode(cdir.c_str());
  1166. std::string makecmd = "make -C ";
  1167. makecmd += cdir;
  1168. makecmd += " -f ";
  1169. makecmd += this->ConvertToRelativeForMake(
  1170. (makefile+"$CONFIGURATION").c_str());
  1171. if(!multipleOutputPairs.empty())
  1172. {
  1173. makecmd += " cmake_check_multiple_outputs";
  1174. }
  1175. makecmd += " all";
  1176. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  1177. buildphase->AddAttribute("shellScript",
  1178. this->CreateString(makecmd.c_str()));
  1179. }
  1180. //----------------------------------------------------------------------------
  1181. void cmGlobalXCodeGenerator
  1182. ::CreateCustomRulesMakefile(const char* makefileBasename,
  1183. cmTarget& target,
  1184. std::vector<cmCustomCommand>
  1185. const & commands,
  1186. const char* configName,
  1187. const std::map<cmStdString,
  1188. cmStdString>& multipleOutputPairs
  1189. )
  1190. {
  1191. std::string makefileName=makefileBasename;
  1192. makefileName+=configName;
  1193. cmGeneratedFileStream makefileStream(makefileName.c_str());
  1194. if(!makefileStream)
  1195. {
  1196. return;
  1197. }
  1198. makefileStream.SetCopyIfDifferent(true);
  1199. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  1200. makefileStream << "# Custom rules for " << target.GetName() << "\n";
  1201. // have all depend on all outputs
  1202. makefileStream << "all: ";
  1203. std::map<const cmCustomCommand*, cmStdString> tname;
  1204. int count = 0;
  1205. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1206. i != commands.end(); ++i)
  1207. {
  1208. cmCustomCommand const& cc = *i;
  1209. if(!cc.GetCommandLines().empty())
  1210. {
  1211. const std::vector<std::string>& outputs = cc.GetOutputs();
  1212. if(!outputs.empty())
  1213. {
  1214. for(std::vector<std::string>::const_iterator o = outputs.begin();
  1215. o != outputs.end(); ++o)
  1216. {
  1217. makefileStream
  1218. << "\\\n\t" << this->ConvertToRelativeForMake(o->c_str());
  1219. }
  1220. }
  1221. else
  1222. {
  1223. cmOStringStream str;
  1224. str << "_buildpart_" << count++ ;
  1225. tname[&cc] = std::string(target.GetName()) + str.str();
  1226. makefileStream << "\\\n\t" << tname[&cc];
  1227. }
  1228. }
  1229. }
  1230. makefileStream << "\n\n";
  1231. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  1232. i != commands.end(); ++i)
  1233. {
  1234. cmCustomCommand const& cc = *i;
  1235. if(!cc.GetCommandLines().empty())
  1236. {
  1237. bool escapeOldStyle = cc.GetEscapeOldStyle();
  1238. bool escapeAllowMakeVars = cc.GetEscapeAllowMakeVars();
  1239. makefileStream << "\n";
  1240. const std::vector<std::string>& outputs = cc.GetOutputs();
  1241. if(!outputs.empty())
  1242. {
  1243. // There is at least one output, start the rule for it
  1244. std::string primary_output =
  1245. this->ConvertToRelativeForMake(outputs.begin()->c_str());
  1246. makefileStream << primary_output << ": ";
  1247. }
  1248. else
  1249. {
  1250. // There are no outputs. Use the generated force rule name.
  1251. makefileStream << tname[&cc] << ": ";
  1252. }
  1253. for(std::vector<std::string>::const_iterator d =
  1254. cc.GetDepends().begin();
  1255. d != cc.GetDepends().end(); ++d)
  1256. {
  1257. std::string dep =
  1258. this->CurrentLocalGenerator->GetRealDependency(d->c_str(),
  1259. configName);
  1260. makefileStream << "\\\n" << this
  1261. ->ConvertToRelativeForMake(dep.c_str());
  1262. }
  1263. makefileStream << "\n";
  1264. if(const char* comment = cc.GetComment())
  1265. {
  1266. std::string echo_cmd = "echo ";
  1267. echo_cmd += (this->CurrentLocalGenerator->
  1268. EscapeForShell(comment, escapeAllowMakeVars));
  1269. makefileStream << "\t" << echo_cmd.c_str() << "\n";
  1270. }
  1271. // Add each command line to the set of commands.
  1272. for(cmCustomCommandLines::const_iterator cl =
  1273. cc.GetCommandLines().begin();
  1274. cl != cc.GetCommandLines().end(); ++cl)
  1275. {
  1276. // Build the command line in a single string.
  1277. const cmCustomCommandLine& commandLine = *cl;
  1278. std::string cmd2 = this->CurrentLocalGenerator
  1279. ->GetRealLocation(commandLine[0].c_str(), configName);
  1280. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  1281. cmd2 = this->ConvertToRelativeForMake(cmd2.c_str());
  1282. std::string cmd;
  1283. if(cc.GetWorkingDirectory())
  1284. {
  1285. cmd += "cd ";
  1286. cmd += this->ConvertToRelativeForMake(cc.GetWorkingDirectory());
  1287. cmd += " && ";
  1288. }
  1289. cmd += cmd2;
  1290. for(unsigned int j=1; j < commandLine.size(); ++j)
  1291. {
  1292. cmd += " ";
  1293. if(escapeOldStyle)
  1294. {
  1295. cmd += (this->CurrentLocalGenerator
  1296. ->EscapeForShellOldStyle(commandLine[j].c_str()));
  1297. }
  1298. else
  1299. {
  1300. cmd += (this->CurrentLocalGenerator->
  1301. EscapeForShell(commandLine[j].c_str(),
  1302. escapeAllowMakeVars));
  1303. }
  1304. }
  1305. makefileStream << "\t" << cmd.c_str() << "\n";
  1306. }
  1307. }
  1308. }
  1309. // Add rules to deal with multiple outputs of custom commands.
  1310. if(!multipleOutputPairs.empty())
  1311. {
  1312. makefileStream <<
  1313. "\n# Dependencies of multiple outputs to their primary outputs \n";
  1314. for(std::map<cmStdString, cmStdString>::const_iterator o =
  1315. multipleOutputPairs.begin(); o != multipleOutputPairs.end(); ++o)
  1316. {
  1317. makefileStream << o->first << ": " << o->second << "\n";
  1318. }
  1319. makefileStream <<
  1320. "\n"
  1321. "cmake_check_multiple_outputs:\n";
  1322. for(std::map<cmStdString, cmStdString>::const_iterator o =
  1323. multipleOutputPairs.begin(); o != multipleOutputPairs.end(); ++o)
  1324. {
  1325. makefileStream << "\t@if [ ! -f "
  1326. << o->first << " ]; then rm -f "
  1327. << o->second << "; fi\n";
  1328. }
  1329. }
  1330. }
  1331. //----------------------------------------------------------------------------
  1332. void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
  1333. cmXCodeObject* buildSettings,
  1334. const char* configName)
  1335. {
  1336. std::string flags;
  1337. std::string defFlags;
  1338. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  1339. (target.GetType() == cmTarget::MODULE_LIBRARY));
  1340. const char* lang = target.GetLinkerLanguage(configName);
  1341. std::string cflags;
  1342. if(lang)
  1343. {
  1344. // for c++ projects get the c flags as well
  1345. if(strcmp(lang, "CXX") == 0)
  1346. {
  1347. this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
  1348. this->CurrentLocalGenerator->AddSharedFlags(cflags, lang, shared);
  1349. }
  1350. // Add language-specific flags.
  1351. this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
  1352. // Add shared-library flags if needed.
  1353. this->CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
  1354. }
  1355. // Add define flags
  1356. this->CurrentLocalGenerator->
  1357. AppendFlags(defFlags,
  1358. this->CurrentMakefile->GetDefineFlags());
  1359. // Add preprocessor definitions for this target and configuration.
  1360. BuildObjectListOrString ppDefs(this, this->XcodeVersion >= 30);
  1361. if(this->XcodeVersion > 15)
  1362. {
  1363. this->AppendDefines(ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)\"");
  1364. }
  1365. if(const char* exportMacro = target.GetExportMacro())
  1366. {
  1367. // Add the export symbol definition for shared library objects.
  1368. this->AppendDefines(ppDefs, exportMacro);
  1369. }
  1370. this->AppendDefines
  1371. (ppDefs, this->CurrentMakefile->GetProperty("COMPILE_DEFINITIONS"));
  1372. this->AppendDefines(ppDefs, target.GetProperty("COMPILE_DEFINITIONS"));
  1373. if(configName)
  1374. {
  1375. std::string defVarName = "COMPILE_DEFINITIONS_";
  1376. defVarName += cmSystemTools::UpperCase(configName);
  1377. this->AppendDefines
  1378. (ppDefs, this->CurrentMakefile->GetProperty(defVarName.c_str()));
  1379. this->AppendDefines(ppDefs, target.GetProperty(defVarName.c_str()));
  1380. }
  1381. buildSettings->AddAttribute
  1382. ("GCC_PREPROCESSOR_DEFINITIONS", ppDefs.CreateList());
  1383. std::string extraLinkOptions;
  1384. if(target.GetType() == cmTarget::EXECUTABLE)
  1385. {
  1386. extraLinkOptions =
  1387. this->CurrentMakefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1388. std::string var = "CMAKE_EXE_LINKER_FLAGS_";
  1389. var += cmSystemTools::UpperCase(configName);
  1390. std::string val =
  1391. this->CurrentMakefile->GetSafeDefinition(var.c_str());
  1392. if(val.size())
  1393. {
  1394. extraLinkOptions += " ";
  1395. extraLinkOptions += val;
  1396. }
  1397. }
  1398. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1399. {
  1400. extraLinkOptions = this->CurrentMakefile->
  1401. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1402. }
  1403. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1404. {
  1405. extraLinkOptions = this->CurrentMakefile->
  1406. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1407. }
  1408. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1409. if(targetLinkFlags)
  1410. {
  1411. extraLinkOptions += " ";
  1412. extraLinkOptions += targetLinkFlags;
  1413. }
  1414. // Get the product name components.
  1415. std::string pnprefix;
  1416. std::string pnbase;
  1417. std::string pnsuffix;
  1418. target.GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  1419. // Store the product name for all target types.
  1420. buildSettings->AddAttribute("PRODUCT_NAME",
  1421. this->CreateString(pnbase.c_str()));
  1422. // Set attributes to specify the proper name for the target.
  1423. std::string pndir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1424. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1425. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1426. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1427. target.GetType() == cmTarget::EXECUTABLE)
  1428. {
  1429. pndir = target.GetDirectory();
  1430. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1431. this->CreateString(pnprefix.c_str()));
  1432. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1433. this->CreateString(pnsuffix.c_str()));
  1434. }
  1435. buildSettings->AddAttribute("SYMROOT",
  1436. this->CreateString(pndir.c_str()));
  1437. // Handle settings for each target type.
  1438. switch(target.GetType())
  1439. {
  1440. case cmTarget::STATIC_LIBRARY:
  1441. {
  1442. buildSettings->AddAttribute("LIBRARY_STYLE",
  1443. this->CreateString("STATIC"));
  1444. break;
  1445. }
  1446. case cmTarget::MODULE_LIBRARY:
  1447. {
  1448. buildSettings->AddAttribute("LIBRARY_STYLE",
  1449. this->CreateString("BUNDLE"));
  1450. if(this->XcodeVersion >= 22)
  1451. {
  1452. buildSettings->AddAttribute("MACH_O_TYPE",
  1453. this->CreateString("mh_bundle"));
  1454. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1455. this->CreateString("NO"));
  1456. // Add the flags to create an executable.
  1457. std::string createFlags =
  1458. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1459. if(!createFlags.empty())
  1460. {
  1461. extraLinkOptions += " ";
  1462. extraLinkOptions += createFlags;
  1463. }
  1464. }
  1465. else
  1466. {
  1467. // Add the flags to create a module.
  1468. std::string createFlags =
  1469. this->LookupFlags("CMAKE_SHARED_MODULE_CREATE_", lang, "_FLAGS",
  1470. "-bundle");
  1471. if(!createFlags.empty())
  1472. {
  1473. extraLinkOptions += " ";
  1474. extraLinkOptions += createFlags;
  1475. }
  1476. }
  1477. break;
  1478. }
  1479. case cmTarget::SHARED_LIBRARY:
  1480. {
  1481. if(target.GetPropertyAsBool("FRAMEWORK"))
  1482. {
  1483. std::string version = target.GetFrameworkVersion();
  1484. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1485. this->CreateString(version.c_str()));
  1486. std::string plist = this->ComputeInfoPListLocation(target);
  1487. // Xcode will create the final version of Info.plist at build time,
  1488. // so let it replace the framework name. This avoids creating
  1489. // a per-configuration Info.plist file.
  1490. this->CurrentLocalGenerator
  1491. ->GenerateFrameworkInfoPList(&target, "$(EXECUTABLE_NAME)",
  1492. plist.c_str());
  1493. std::string path =
  1494. this->ConvertToRelativeForXCode(plist.c_str());
  1495. buildSettings->AddAttribute("INFOPLIST_FILE",
  1496. this->CreateString(path.c_str()));
  1497. }
  1498. else
  1499. {
  1500. // Add the flags to create a shared library.
  1501. std::string createFlags =
  1502. this->LookupFlags("CMAKE_SHARED_LIBRARY_CREATE_", lang, "_FLAGS",
  1503. "-dynamiclib");
  1504. if(!createFlags.empty())
  1505. {
  1506. extraLinkOptions += " ";
  1507. extraLinkOptions += createFlags;
  1508. }
  1509. }
  1510. buildSettings->AddAttribute("LIBRARY_STYLE",
  1511. this->CreateString("DYNAMIC"));
  1512. break;
  1513. }
  1514. case cmTarget::EXECUTABLE:
  1515. {
  1516. // Add the flags to create an executable.
  1517. std::string createFlags =
  1518. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1519. if(!createFlags.empty())
  1520. {
  1521. extraLinkOptions += " ";
  1522. extraLinkOptions += createFlags;
  1523. }
  1524. // Handle bundles and normal executables separately.
  1525. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1526. {
  1527. std::string plist = this->ComputeInfoPListLocation(target);
  1528. // Xcode will create the final version of Info.plist at build time,
  1529. // so let it replace the executable name. This avoids creating
  1530. // a per-configuration Info.plist file.
  1531. this->CurrentLocalGenerator
  1532. ->GenerateAppleInfoPList(&target, "$(EXECUTABLE_NAME)",
  1533. plist.c_str());
  1534. std::string path =
  1535. this->ConvertToRelativeForXCode(plist.c_str());
  1536. buildSettings->AddAttribute("INFOPLIST_FILE",
  1537. this->CreateString(path.c_str()));
  1538. }
  1539. }
  1540. break;
  1541. default:
  1542. break;
  1543. }
  1544. if(this->XcodeVersion >= 22)
  1545. {
  1546. buildSettings->AddAttribute("PREBINDING",
  1547. this->CreateString("NO"));
  1548. }
  1549. BuildObjectListOrString dirs(this, this->XcodeVersion >= 30);
  1550. BuildObjectListOrString fdirs(this, this->XcodeVersion >= 30);
  1551. std::vector<std::string> includes;
  1552. this->CurrentLocalGenerator->GetIncludeDirectories(includes);
  1553. std::set<cmStdString> emitted;
  1554. emitted.insert("/System/Library/Frameworks");
  1555. for(std::vector<std::string>::iterator i = includes.begin();
  1556. i != includes.end(); ++i)
  1557. {
  1558. if(this->NameResolvesToFramework(i->c_str()))
  1559. {
  1560. std::string frameworkDir = *i;
  1561. frameworkDir += "/../";
  1562. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1563. if(emitted.insert(frameworkDir).second)
  1564. {
  1565. fdirs.Add(this->XCodeEscapePath(frameworkDir.c_str()).c_str());
  1566. }
  1567. }
  1568. else
  1569. {
  1570. std::string incpath =
  1571. this->XCodeEscapePath(i->c_str());
  1572. dirs.Add(incpath.c_str());
  1573. }
  1574. }
  1575. std::vector<std::string>& frameworks = target.GetFrameworks();
  1576. if(frameworks.size())
  1577. {
  1578. for(std::vector<std::string>::iterator fmIt = frameworks.begin();
  1579. fmIt != frameworks.end(); ++fmIt)
  1580. {
  1581. if(emitted.insert(*fmIt).second)
  1582. {
  1583. fdirs.Add(this->XCodeEscapePath(fmIt->c_str()).c_str());
  1584. }
  1585. }
  1586. }
  1587. if(!fdirs.IsEmpty())
  1588. {
  1589. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS",
  1590. fdirs.CreateList());
  1591. }
  1592. if(!dirs.IsEmpty())
  1593. {
  1594. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  1595. dirs.CreateList());
  1596. }
  1597. std::string oflagc = this->ExtractFlag("-O", cflags);
  1598. char optLevel[2];
  1599. optLevel[0] = '0';
  1600. optLevel[1] = 0;
  1601. if(oflagc.size() == 3)
  1602. {
  1603. optLevel[0] = oflagc[2];
  1604. }
  1605. if(oflagc.size() == 2)
  1606. {
  1607. optLevel[0] = '1';
  1608. }
  1609. std::string oflag = this->ExtractFlag("-O", flags);
  1610. if(oflag.size() == 3)
  1611. {
  1612. optLevel[0] = oflag[2];
  1613. }
  1614. if(oflag.size() == 2)
  1615. {
  1616. optLevel[0] = '1';
  1617. }
  1618. std::string gflagc = this->ExtractFlag("-g", cflags);
  1619. // put back gdwarf-2 if used since there is no way
  1620. // to represent it in the gui, but we still want debug yes
  1621. if(gflagc == "-gdwarf-2")
  1622. {
  1623. cflags += " ";
  1624. cflags += gflagc;
  1625. }
  1626. std::string gflag = this->ExtractFlag("-g", flags);
  1627. if(gflag == "-gdwarf-2")
  1628. {
  1629. flags += " ";
  1630. flags += gflag;
  1631. }
  1632. const char* debugStr = "YES";
  1633. if(gflagc.size() ==0 && gflag.size() == 0)
  1634. {
  1635. debugStr = "NO";
  1636. }
  1637. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1638. this->CreateString(debugStr));
  1639. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1640. this->CreateString(optLevel));
  1641. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1642. this->CreateString("NO"));
  1643. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1644. this->CreateString("NO"));
  1645. if(lang && strcmp(lang, "CXX") == 0)
  1646. {
  1647. flags += " ";
  1648. flags += defFlags;
  1649. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1650. this->CreateString(flags.c_str()));
  1651. cflags += " ";
  1652. cflags += defFlags;
  1653. buildSettings->AddAttribute("OTHER_CFLAGS",
  1654. this->CreateString(cflags.c_str()));
  1655. }
  1656. else
  1657. {
  1658. flags += " ";
  1659. flags += defFlags;
  1660. buildSettings->AddAttribute("OTHER_CFLAGS",
  1661. this->CreateString(flags.c_str()));
  1662. }
  1663. // Create the INSTALL_PATH attribute.
  1664. std::string install_name_dir;
  1665. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1666. {
  1667. // Get the install_name directory for the build tree.
  1668. install_name_dir = target.GetInstallNameDirForBuildTree(configName, true);
  1669. if(install_name_dir.empty())
  1670. {
  1671. // Xcode will not pass the -install_name option at all if INSTALL_PATH
  1672. // is not given or is empty. We must explicitly put the flag in the
  1673. // link flags to create an install_name with just the library soname.
  1674. extraLinkOptions += " -install_name ";
  1675. extraLinkOptions += target.GetFullName(configName);
  1676. }
  1677. else
  1678. {
  1679. // Convert to a path for the native build tool.
  1680. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1681. // do not escape spaces on this since it is only a single path
  1682. }
  1683. }
  1684. buildSettings->AddAttribute("INSTALL_PATH",
  1685. this->CreateString(install_name_dir.c_str()));
  1686. buildSettings->AddAttribute("OTHER_LDFLAGS",
  1687. this->CreateString(extraLinkOptions.c_str()));
  1688. buildSettings->AddAttribute("OTHER_REZFLAGS",
  1689. this->CreateString(""));
  1690. buildSettings->AddAttribute("SECTORDER_FLAGS",
  1691. this->CreateString(""));
  1692. buildSettings->AddAttribute("USE_HEADERMAP",
  1693. this->CreateString("NO"));
  1694. if (this->XcodeVersion >= 30)
  1695. {
  1696. cmXCodeObject *group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1697. group->AddObject(this->CreateString("-Wmost"));
  1698. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  1699. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  1700. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  1701. }
  1702. else
  1703. {
  1704. buildSettings->AddAttribute("WARNING_CFLAGS",
  1705. this->CreateString(
  1706. "-Wmost -Wno-four-char-constants"
  1707. " -Wno-unknown-pragmas"));
  1708. }
  1709. // Runtime version information.
  1710. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1711. {
  1712. int major;
  1713. int minor;
  1714. int patch;
  1715. // VERSION -> current_version
  1716. target.GetTargetVersion(false, major, minor, patch);
  1717. if(major == 0 && minor == 0 && patch == 0)
  1718. {
  1719. // Xcode always wants at least 1.0.0
  1720. major = 1;
  1721. }
  1722. cmOStringStream v;
  1723. v << major << "." << minor << "." << patch;
  1724. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1725. this->CreateString(v.str().c_str()));
  1726. // SOVERSION -> compatibility_version
  1727. target.GetTargetVersion(true, major, minor, patch);
  1728. if(major == 0 && minor == 0 && patch == 0)
  1729. {
  1730. // Xcode always wants at least 1.0.0
  1731. major = 1;
  1732. }
  1733. cmOStringStream vso;
  1734. vso << major << "." << minor << "." << patch;
  1735. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1736. this->CreateString(vso.str().c_str()));
  1737. }
  1738. // put this last so it can override existing settings
  1739. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  1740. {
  1741. cmPropertyMap const& props = target.GetProperties();
  1742. for(cmPropertyMap::const_iterator i = props.begin();
  1743. i != props.end(); ++i)
  1744. {
  1745. if(i->first.find("XCODE_ATTRIBUTE_") == 0)
  1746. {
  1747. buildSettings->AddAttribute(i->first.substr(16).c_str(),
  1748. this->CreateString(i->second.GetValue()));
  1749. }
  1750. }
  1751. }
  1752. }
  1753. //----------------------------------------------------------------------------
  1754. cmXCodeObject*
  1755. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  1756. {
  1757. cmXCodeObject* shellBuildPhase =
  1758. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1759. shellBuildPhase->AddAttribute("buildActionMask",
  1760. this->CreateString("2147483647"));
  1761. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1762. shellBuildPhase->AddAttribute("files", buildFiles);
  1763. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1764. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1765. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1766. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1767. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1768. this->CreateString("0"));
  1769. shellBuildPhase->AddAttribute("shellPath",
  1770. this->CreateString("/bin/sh"));
  1771. shellBuildPhase->AddAttribute("shellScript",
  1772. this->CreateString(
  1773. "# shell script goes here\nexit 0"));
  1774. cmXCodeObject* target =
  1775. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1776. target->SetComment(cmtarget.GetName());
  1777. cmXCodeObject* buildPhases =
  1778. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1779. std::vector<cmXCodeObject*> emptyContentVector;
  1780. this->CreateCustomCommands(buildPhases, 0, 0, 0, emptyContentVector, 0,
  1781. cmtarget);
  1782. target->AddAttribute("buildPhases", buildPhases);
  1783. if(this->XcodeVersion > 20)
  1784. {
  1785. this->AddConfigurations(target, cmtarget);
  1786. }
  1787. else
  1788. {
  1789. const char* theConfig =
  1790. this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1791. cmXCodeObject* buildSettings =
  1792. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1793. this->CreateBuildSettings(cmtarget, buildSettings, theConfig);
  1794. target->AddAttribute("buildSettings", buildSettings);
  1795. }
  1796. cmXCodeObject* dependencies =
  1797. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1798. target->AddAttribute("dependencies", dependencies);
  1799. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1800. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1801. target->SetTarget(&cmtarget);
  1802. // Add source files without build rules for editing convenience.
  1803. if(cmtarget.GetType() == cmTarget::UTILITY)
  1804. {
  1805. std::vector<cmSourceFile*> const& sources = cmtarget.GetSourceFiles();
  1806. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1807. i != sources.end(); ++i)
  1808. {
  1809. if(!(*i)->GetPropertyAsBool("GENERATED"))
  1810. {
  1811. this->CreateXCodeFileReference(*i, cmtarget);
  1812. }
  1813. }
  1814. }
  1815. return target;
  1816. }
  1817. //----------------------------------------------------------------------------
  1818. std::string cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  1819. cmTarget& cmtarget)
  1820. {
  1821. std::string configTypes =
  1822. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  1823. std::vector<std::string> configVectorIn;
  1824. std::vector<std::string> configVector;
  1825. configVectorIn.push_back(configTypes);
  1826. cmSystemTools::ExpandList(configVectorIn, configVector);
  1827. cmXCodeObject* configlist =
  1828. this->CreateObject(cmXCodeObject::XCConfigurationList);
  1829. cmXCodeObject* buildConfigurations =
  1830. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1831. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  1832. std::string comment = "Build configuration list for ";
  1833. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  1834. comment += " \"";
  1835. comment += cmtarget.GetName();
  1836. comment += "\"";
  1837. configlist->SetComment(comment.c_str());
  1838. target->AddAttribute("buildConfigurationList",
  1839. this->CreateObjectReference(configlist));
  1840. for(unsigned int i = 0; i < configVector.size(); ++i)
  1841. {
  1842. cmXCodeObject* config =
  1843. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  1844. buildConfigurations->AddObject(config);
  1845. cmXCodeObject* buildSettings =
  1846. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1847. this->CreateBuildSettings(cmtarget, buildSettings,
  1848. configVector[i].c_str());
  1849. config->AddAttribute("name", this->CreateString(configVector[i].c_str()));
  1850. config->SetComment(configVector[i].c_str());
  1851. config->AddAttribute("buildSettings", buildSettings);
  1852. }
  1853. if(configVector.size())
  1854. {
  1855. configlist->AddAttribute("defaultConfigurationName",
  1856. this->CreateString(configVector[0].c_str()));
  1857. configlist->AddAttribute("defaultConfigurationIsVisible",
  1858. this->CreateString("0"));
  1859. return configVector[0];
  1860. }
  1861. return "";
  1862. }
  1863. //----------------------------------------------------------------------------
  1864. const char* cmGlobalXCodeGenerator::GetTargetFileType(cmTarget& cmtarget)
  1865. {
  1866. switch(cmtarget.GetType())
  1867. {
  1868. case cmTarget::STATIC_LIBRARY:
  1869. return "archive.ar";
  1870. case cmTarget::MODULE_LIBRARY:
  1871. return ((this->XcodeVersion >= 22)?
  1872. "compiled.mach-o.executable" : "compiled.mach-o.dylib");
  1873. case cmTarget::SHARED_LIBRARY:
  1874. return (cmtarget.GetPropertyAsBool("FRAMEWORK")?
  1875. "wrapper.framework" : "compiled.mach-o.dylib");
  1876. case cmTarget::EXECUTABLE:
  1877. return "compiled.mach-o.executable";
  1878. default: break;
  1879. }
  1880. return 0;
  1881. }
  1882. //----------------------------------------------------------------------------
  1883. const char* cmGlobalXCodeGenerator::GetTargetProductType(cmTarget& cmtarget)
  1884. {
  1885. switch(cmtarget.GetType())
  1886. {
  1887. case cmTarget::STATIC_LIBRARY:
  1888. return "com.apple.product-type.library.static";
  1889. case cmTarget::MODULE_LIBRARY:
  1890. return ((this->XcodeVersion >= 22)? "com.apple.product-type.tool" :
  1891. "com.apple.product-type.library.dynamic");
  1892. case cmTarget::SHARED_LIBRARY:
  1893. return (cmtarget.GetPropertyAsBool("FRAMEWORK")?
  1894. "com.apple.product-type.framework" :
  1895. "com.apple.product-type.library.dynamic");
  1896. case cmTarget::EXECUTABLE:
  1897. return (cmtarget.GetPropertyAsBool("MACOSX_BUNDLE")?
  1898. "com.apple.product-type.application" :
  1899. "com.apple.product-type.tool");
  1900. default: break;
  1901. }
  1902. return 0;
  1903. }
  1904. //----------------------------------------------------------------------------
  1905. cmXCodeObject*
  1906. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  1907. cmXCodeObject* buildPhases)
  1908. {
  1909. cmXCodeObject* target =
  1910. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  1911. target->AddAttribute("buildPhases", buildPhases);
  1912. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1913. target->AddAttribute("buildRules", buildRules);
  1914. std::string defConfig;
  1915. if(this->XcodeVersion > 20)
  1916. {
  1917. defConfig = this->AddConfigurations(target, cmtarget);
  1918. }
  1919. else
  1920. {
  1921. cmXCodeObject* buildSettings =
  1922. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1923. defConfig = this->CurrentMakefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  1924. this->CreateBuildSettings(cmtarget, buildSettings, defConfig.c_str());
  1925. target->AddAttribute("buildSettings", buildSettings);
  1926. }
  1927. cmXCodeObject* dependencies =
  1928. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1929. target->AddAttribute("dependencies", dependencies);
  1930. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1931. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1932. cmXCodeObject* fileRef =
  1933. this->CreateObject(cmXCodeObject::PBXFileReference);
  1934. if(const char* fileType = this->GetTargetFileType(cmtarget))
  1935. {
  1936. fileRef->AddAttribute("explicitFileType", this->CreateString(fileType));
  1937. }
  1938. std::string fullName = cmtarget.GetFullName(defConfig.c_str());
  1939. fileRef->AddAttribute("path", this->CreateString(fullName.c_str()));
  1940. fileRef->AddAttribute("refType", this->CreateString("0"));
  1941. fileRef->AddAttribute("sourceTree",
  1942. this->CreateString("BUILT_PRODUCTS_DIR"));
  1943. fileRef->SetComment(cmtarget.GetName());
  1944. target->AddAttribute("productReference",
  1945. this->CreateObjectReference(fileRef));
  1946. if(const char* productType = this->GetTargetProductType(cmtarget))
  1947. {
  1948. target->AddAttribute("productType", this->CreateString(productType));
  1949. }
  1950. target->SetTarget(&cmtarget);
  1951. return target;
  1952. }
  1953. //----------------------------------------------------------------------------
  1954. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  1955. {
  1956. if(!t)
  1957. {
  1958. return 0;
  1959. }
  1960. for(std::vector<cmXCodeObject*>::iterator i = this->XCodeObjects.begin();
  1961. i != this->XCodeObjects.end(); ++i)
  1962. {
  1963. cmXCodeObject* o = *i;
  1964. if(o->GetTarget() == t)
  1965. {
  1966. return o;
  1967. }
  1968. }
  1969. return 0;
  1970. }
  1971. //----------------------------------------------------------------------------
  1972. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  1973. cmXCodeObject* dependTarget)
  1974. {
  1975. // make sure a target does not depend on itself
  1976. if(target == dependTarget)
  1977. {
  1978. return;
  1979. }
  1980. // now avoid circular references if dependTarget already
  1981. // depends on target then skip it. Circular references crashes
  1982. // xcode
  1983. cmXCodeObject* dependTargetDepends =
  1984. dependTarget->GetObject("dependencies");
  1985. if(dependTargetDepends)
  1986. {
  1987. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  1988. {
  1989. return;
  1990. }
  1991. }
  1992. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  1993. if(!targetdep)
  1994. {
  1995. cmXCodeObject* container =
  1996. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  1997. container->SetComment("PBXContainerItemProxy");
  1998. container->AddAttribute("containerPortal",
  1999. this->CreateObjectReference(this->RootObject));
  2000. container->AddAttribute("proxyType", this->CreateString("1"));
  2001. container->AddAttribute("remoteGlobalIDString",
  2002. this->CreateObjectReference(dependTarget));
  2003. container->AddAttribute("remoteInfo",
  2004. this->CreateString(
  2005. dependTarget->GetTarget()->GetName()));
  2006. targetdep =
  2007. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  2008. targetdep->SetComment("PBXTargetDependency");
  2009. targetdep->AddAttribute("target",
  2010. this->CreateObjectReference(dependTarget));
  2011. targetdep->AddAttribute("targetProxy",
  2012. this->CreateObjectReference(container));
  2013. dependTarget->SetPBXTargetDependency(targetdep);
  2014. }
  2015. cmXCodeObject* depends = target->GetObject("dependencies");
  2016. if(!depends)
  2017. {
  2018. cmSystemTools::
  2019. Error("target does not have dependencies attribute error..");
  2020. }
  2021. else
  2022. {
  2023. depends->AddUniqueObject(targetdep);
  2024. }
  2025. }
  2026. //----------------------------------------------------------------------------
  2027. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  2028. const char* attribute,
  2029. const char* value)
  2030. {
  2031. if(settings)
  2032. {
  2033. cmXCodeObject* attr = settings->GetObject(attribute);
  2034. if(!attr)
  2035. {
  2036. settings->AddAttribute(attribute, this->CreateString(value));
  2037. }
  2038. else
  2039. {
  2040. std::string oldValue = attr->GetString();
  2041. oldValue += " ";
  2042. oldValue += value;
  2043. attr->SetString(oldValue.c_str());
  2044. }
  2045. }
  2046. }
  2047. //----------------------------------------------------------------------------
  2048. void cmGlobalXCodeGenerator
  2049. ::AppendBuildSettingAttribute(cmXCodeObject* target,
  2050. const char* attribute,
  2051. const char* value,
  2052. const char* configName)
  2053. {
  2054. if(this->XcodeVersion < 21)
  2055. {
  2056. // There is only one configuration. Add the setting to the buildSettings
  2057. // of the target.
  2058. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  2059. attribute, value);
  2060. }
  2061. else
  2062. {
  2063. // There are multiple configurations. Add the setting to the
  2064. // buildSettings of the configuration name given.
  2065. cmXCodeObject* configurationList =
  2066. target->GetObject("buildConfigurationList")->GetObject();
  2067. cmXCodeObject* buildConfigs =
  2068. configurationList->GetObject("buildConfigurations");
  2069. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  2070. // each configuration and the target itself has a buildSettings in it
  2071. //list.push_back(target);
  2072. for(std::vector<cmXCodeObject*>::iterator i = list.begin();
  2073. i != list.end(); ++i)
  2074. {
  2075. if(configName)
  2076. {
  2077. if(strcmp((*i)->GetObject("name")->GetString(), configName) == 0)
  2078. {
  2079. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2080. this->AppendOrAddBuildSetting(settings, attribute, value);
  2081. }
  2082. }
  2083. else
  2084. {
  2085. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2086. this->AppendOrAddBuildSetting(settings, attribute, value);
  2087. }
  2088. }
  2089. }
  2090. }
  2091. //----------------------------------------------------------------------------
  2092. void cmGlobalXCodeGenerator
  2093. ::AddDependAndLinkInformation(cmXCodeObject* target)
  2094. {
  2095. cmTarget* cmtarget = target->GetTarget();
  2096. if(!cmtarget)
  2097. {
  2098. cmSystemTools::Error("Error no target on xobject\n");
  2099. return;
  2100. }
  2101. // Add dependencies on other CMake targets.
  2102. TargetDependSet const& deps = this->GetTargetDirectDepends(*cmtarget);
  2103. for(TargetDependSet::const_iterator i = deps.begin(); i != deps.end(); ++i)
  2104. {
  2105. if(cmXCodeObject* dptarget = this->FindXCodeTarget(*i))
  2106. {
  2107. this->AddDependTarget(target, dptarget);
  2108. }
  2109. }
  2110. // Skip link information for static libraries.
  2111. if(cmtarget->GetType() == cmTarget::STATIC_LIBRARY)
  2112. {
  2113. return;
  2114. }
  2115. // Loop over configuration types and set per-configuration info.
  2116. for(std::vector<std::string>::iterator i =
  2117. this->CurrentConfigurationTypes.begin();
  2118. i != this->CurrentConfigurationTypes.end(); ++i)
  2119. {
  2120. // Get the current configuration name.
  2121. const char* configName = i->c_str();
  2122. if(!*configName)
  2123. {
  2124. configName = 0;
  2125. }
  2126. // Compute the link library and directory information.
  2127. cmComputeLinkInformation* pcli = cmtarget->GetLinkInformation(configName);
  2128. if(!pcli)
  2129. {
  2130. continue;
  2131. }
  2132. cmComputeLinkInformation& cli = *pcli;
  2133. // Add dependencies directly on library files.
  2134. {
  2135. std::vector<std::string> const& libDeps = cli.GetDepends();
  2136. for(std::vector<std::string>::const_iterator j = libDeps.begin();
  2137. j != libDeps.end(); ++j)
  2138. {
  2139. target->AddDependLibrary(configName, j->c_str());
  2140. }
  2141. }
  2142. // add the library search paths
  2143. {
  2144. std::vector<std::string> const& libDirs = cli.GetDirectories();
  2145. std::string linkDirs;
  2146. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  2147. libDir != libDirs.end(); ++libDir)
  2148. {
  2149. if(libDir->size() && *libDir != "/usr/lib")
  2150. {
  2151. if(this->XcodeVersion > 15)
  2152. {
  2153. // now add the same one but append $(CONFIGURATION) to it:
  2154. linkDirs += " ";
  2155. linkDirs += this->XCodeEscapePath(
  2156. (*libDir + "/$(CONFIGURATION)").c_str());
  2157. }
  2158. linkDirs += " ";
  2159. linkDirs += this->XCodeEscapePath(libDir->c_str());
  2160. }
  2161. }
  2162. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2163. linkDirs.c_str(), configName);
  2164. }
  2165. // add the framework search paths
  2166. {
  2167. const char* sep = "";
  2168. std::string fdirs;
  2169. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  2170. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  2171. fdi != fwDirs.end(); ++fdi)
  2172. {
  2173. fdirs += sep;
  2174. sep = " ";
  2175. fdirs += this->XCodeEscapePath(fdi->c_str());
  2176. }
  2177. if(!fdirs.empty())
  2178. {
  2179. this->AppendBuildSettingAttribute(target, "FRAMEWORK_SEARCH_PATHS",
  2180. fdirs.c_str(), configName);
  2181. }
  2182. }
  2183. // now add the link libraries
  2184. {
  2185. std::string linkLibs;
  2186. const char* sep = "";
  2187. typedef cmComputeLinkInformation::ItemVector ItemVector;
  2188. ItemVector const& libNames = cli.GetItems();
  2189. for(ItemVector::const_iterator li = libNames.begin();
  2190. li != libNames.end(); ++li)
  2191. {
  2192. linkLibs += sep;
  2193. sep = " ";
  2194. if(li->IsPath)
  2195. {
  2196. linkLibs += this->XCodeEscapePath(li->Value.c_str());
  2197. }
  2198. else
  2199. {
  2200. linkLibs += li->Value;
  2201. }
  2202. }
  2203. this->AppendBuildSettingAttribute(target, "OTHER_LDFLAGS",
  2204. linkLibs.c_str(), configName);
  2205. }
  2206. }
  2207. }
  2208. //----------------------------------------------------------------------------
  2209. void cmGlobalXCodeGenerator::CreateGroups(cmLocalGenerator* root,
  2210. std::vector<cmLocalGenerator*>&
  2211. generators)
  2212. {
  2213. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2214. i != generators.end(); ++i)
  2215. {
  2216. if(this->IsExcluded(root, *i))
  2217. {
  2218. continue;
  2219. }
  2220. cmMakefile* mf = (*i)->GetMakefile();
  2221. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2222. cmTargets &tgts = mf->GetTargets();
  2223. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  2224. {
  2225. cmTarget& cmtarget = l->second;
  2226. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2227. // end up with (empty anyhow) ALL_BUILD and XCODE_DEPEND_HELPER source
  2228. // groups:
  2229. //
  2230. if(cmtarget.GetType() == cmTarget::GLOBAL_TARGET)
  2231. {
  2232. continue;
  2233. }
  2234. // add the soon to be generated Info.plist file as a source for a
  2235. // MACOSX_BUNDLE file
  2236. if(cmtarget.GetPropertyAsBool("MACOSX_BUNDLE"))
  2237. {
  2238. std::string plist = this->ComputeInfoPListLocation(cmtarget);
  2239. cmSourceFile* sf = mf->GetOrCreateSource(plist.c_str(), true);
  2240. cmtarget.AddSourceFile(sf);
  2241. }
  2242. std::vector<cmSourceFile*> classes = cmtarget.GetSourceFiles();
  2243. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  2244. s != classes.end(); s++)
  2245. {
  2246. cmSourceFile* sf = *s;
  2247. // Add the file to the list of sources.
  2248. std::string const& source = sf->GetFullPath();
  2249. cmSourceGroup& sourceGroup =
  2250. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2251. cmXCodeObject* pbxgroup =
  2252. this->CreateOrGetPBXGroup(cmtarget, &sourceGroup);
  2253. cmStdString key = GetGroupMapKey(cmtarget, sf);
  2254. this->GroupMap[key] = pbxgroup;
  2255. }
  2256. }
  2257. }
  2258. }
  2259. //----------------------------------------------------------------------------
  2260. cmXCodeObject* cmGlobalXCodeGenerator
  2261. ::CreateOrGetPBXGroup(cmTarget& cmtarget, cmSourceGroup* sg)
  2262. {
  2263. cmStdString s = cmtarget.GetName();
  2264. s += "/";
  2265. s += sg->GetName();
  2266. std::map<cmStdString, cmXCodeObject* >::iterator i =
  2267. this->GroupNameMap.find(s);
  2268. if(i != this->GroupNameMap.end())
  2269. {
  2270. return i->second;
  2271. }
  2272. i = this->TargetGroup.find(cmtarget.GetName());
  2273. cmXCodeObject* tgroup = 0;
  2274. if(i != this->TargetGroup.end())
  2275. {
  2276. tgroup = i->second;
  2277. }
  2278. else
  2279. {
  2280. tgroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2281. this->TargetGroup[cmtarget.GetName()] = tgroup;
  2282. cmXCodeObject* tgroupChildren =
  2283. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2284. tgroup->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  2285. tgroup->AddAttribute("children", tgroupChildren);
  2286. if(this->XcodeVersion == 15)
  2287. {
  2288. tgroup->AddAttribute("refType", this->CreateString("4"));
  2289. }
  2290. tgroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2291. this->SourcesGroupChildren->AddObject(tgroup);
  2292. }
  2293. // If it's the default source group (empty name) then put the source file
  2294. // directly in the tgroup...
  2295. //
  2296. if (cmStdString(sg->GetName()) == "")
  2297. {
  2298. this->GroupNameMap[s] = tgroup;
  2299. return tgroup;
  2300. }
  2301. cmXCodeObject* tgroupChildren = tgroup->GetObject("children");
  2302. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2303. cmXCodeObject* groupChildren =
  2304. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2305. group->AddAttribute("name", this->CreateString(sg->GetName()));
  2306. group->AddAttribute("children", groupChildren);
  2307. if(this->XcodeVersion == 15)
  2308. {
  2309. group->AddAttribute("refType", this->CreateString("4"));
  2310. }
  2311. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2312. tgroupChildren->AddObject(group);
  2313. this->GroupNameMap[s] = group;
  2314. return group;
  2315. }
  2316. //----------------------------------------------------------------------------
  2317. void cmGlobalXCodeGenerator
  2318. ::CreateXCodeObjects(cmLocalGenerator* root,
  2319. std::vector<cmLocalGenerator*>&
  2320. generators)
  2321. {
  2322. this->ClearXCodeObjects();
  2323. this->RootObject = 0;
  2324. this->SourcesGroupChildren = 0;
  2325. this->ResourcesGroupChildren = 0;
  2326. this->MainGroupChildren = 0;
  2327. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2328. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2329. cmXCodeObject* developBuildStyle =
  2330. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2331. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2332. if(this->XcodeVersion == 15)
  2333. {
  2334. developBuildStyle->AddAttribute("name",
  2335. this->CreateString("Development"));
  2336. developBuildStyle->AddAttribute("buildSettings", group);
  2337. listObjs->AddObject(developBuildStyle);
  2338. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2339. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  2340. cmXCodeObject* deployBuildStyle =
  2341. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2342. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  2343. deployBuildStyle->AddAttribute("buildSettings", group);
  2344. listObjs->AddObject(deployBuildStyle);
  2345. }
  2346. else
  2347. {
  2348. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2349. {
  2350. cmXCodeObject* buildStyle =
  2351. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2352. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2353. buildStyle->AddAttribute("name", this->CreateString(name));
  2354. buildStyle->SetComment(name);
  2355. cmXCodeObject* sgroup =
  2356. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2357. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2358. buildStyle->AddAttribute("buildSettings", sgroup);
  2359. listObjs->AddObject(buildStyle);
  2360. }
  2361. }
  2362. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2363. this->MainGroupChildren =
  2364. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2365. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2366. if(this->XcodeVersion == 15)
  2367. {
  2368. mainGroup->AddAttribute("refType", this->CreateString("4"));
  2369. }
  2370. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2371. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2372. this->SourcesGroupChildren =
  2373. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2374. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  2375. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  2376. if(this->XcodeVersion == 15)
  2377. {
  2378. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2379. }
  2380. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2381. this->MainGroupChildren->AddObject(sourcesGroup);
  2382. cmXCodeObject* resourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2383. this->ResourcesGroupChildren =
  2384. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2385. resourcesGroup->AddAttribute("name", this->CreateString("Resources"));
  2386. resourcesGroup->AddAttribute("children", this->ResourcesGroupChildren);
  2387. if(this->XcodeVersion == 15)
  2388. {
  2389. resourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2390. }
  2391. resourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2392. this->MainGroupChildren->AddObject(resourcesGroup);
  2393. // now create the cmake groups
  2394. this->CreateGroups(root, generators);
  2395. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2396. productGroup->AddAttribute("name", this->CreateString("Products"));
  2397. if(this->XcodeVersion == 15)
  2398. {
  2399. productGroup->AddAttribute("refType", this->CreateString("4"));
  2400. }
  2401. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2402. cmXCodeObject* productGroupChildren =
  2403. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2404. productGroup->AddAttribute("children", productGroupChildren);
  2405. this->MainGroupChildren->AddObject(productGroup);
  2406. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2407. this->RootObject->SetComment("Project object");
  2408. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2409. this->RootObject->AddAttribute("mainGroup",
  2410. this->CreateObjectReference(mainGroup));
  2411. this->RootObject->AddAttribute("buildSettings", group);
  2412. this->RootObject->AddAttribute("buildStyles", listObjs);
  2413. this->RootObject->AddAttribute("hasScannedForEncodings",
  2414. this->CreateString("0"));
  2415. if (this->XcodeVersion >= 30)
  2416. {
  2417. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2418. group->AddAttribute("BuildIndependentTargetsInParallel",
  2419. this->CreateString("YES"));
  2420. this->RootObject->AddAttribute("attributes", group);
  2421. if (this->XcodeVersion >= 31)
  2422. this->RootObject->AddAttribute("compatibilityVersion",
  2423. this->CreateString("Xcode 3.1"));
  2424. else
  2425. this->RootObject->AddAttribute("compatibilityVersion",
  2426. this->CreateString("Xcode 3.0"));
  2427. }
  2428. // Point Xcode at the top of the source tree.
  2429. {
  2430. std::string pdir =
  2431. this->RelativeToBinary(root->GetMakefile()->GetCurrentDirectory());
  2432. this->RootObject->AddAttribute("projectDirPath",
  2433. this->CreateString(pdir.c_str()));
  2434. this->RootObject->AddAttribute("projectRoot", this->CreateString(""));
  2435. }
  2436. cmXCodeObject* configlist =
  2437. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2438. cmXCodeObject* buildConfigurations =
  2439. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2440. std::vector<cmXCodeObject*> configs;
  2441. if(this->XcodeVersion == 15)
  2442. {
  2443. cmXCodeObject* configDebug =
  2444. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2445. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2446. configs.push_back(configDebug);
  2447. cmXCodeObject* configRelease =
  2448. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2449. configRelease->AddAttribute("name", this->CreateString("Release"));
  2450. configs.push_back(configRelease);
  2451. }
  2452. else
  2453. {
  2454. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2455. {
  2456. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2457. cmXCodeObject* config =
  2458. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2459. config->AddAttribute("name", this->CreateString(name));
  2460. configs.push_back(config);
  2461. }
  2462. }
  2463. for(std::vector<cmXCodeObject*>::iterator c = configs.begin();
  2464. c != configs.end(); ++c)
  2465. {
  2466. buildConfigurations->AddObject(*c);
  2467. }
  2468. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2469. std::string comment = "Build configuration list for PBXProject ";
  2470. comment += " \"";
  2471. comment += this->CurrentProject;
  2472. comment += "\"";
  2473. configlist->SetComment(comment.c_str());
  2474. configlist->AddAttribute("defaultConfigurationIsVisible",
  2475. this->CreateString("0"));
  2476. configlist->AddAttribute("defaultConfigurationName",
  2477. this->CreateString("Debug"));
  2478. cmXCodeObject* buildSettings =
  2479. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2480. const char* osxArch =
  2481. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2482. if(strlen(osxArch) == 0)
  2483. {
  2484. if(this->XcodeVersion >= 32)
  2485. {
  2486. osxArch = "$(ARCHS_STANDARD_32_64_BIT)";
  2487. }
  2488. else if(this->XcodeVersion <= 25)
  2489. {
  2490. #ifdef __i386
  2491. osxArch = "i386";
  2492. #endif
  2493. #ifdef __ppc__
  2494. osxArch = "ppc";
  2495. #endif
  2496. }
  2497. else
  2498. {
  2499. osxArch = "$(ARCHS_STANDARD_32_BIT)";
  2500. }
  2501. buildSettings->AddAttribute("ONLY_ACTIVE_ARCH",
  2502. this->CreateString("YES"));
  2503. }
  2504. const char* sysroot =
  2505. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2506. const char* sysrootDefault =
  2507. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT_DEFAULT");
  2508. const char* deploymentTarget =
  2509. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2510. if(osxArch && sysroot)
  2511. {
  2512. bool flagsUsed = false;
  2513. // recompute this as it may have been changed since enable language
  2514. this->Architectures.clear();
  2515. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2516. this->Architectures);
  2517. flagsUsed = true;
  2518. buildSettings->AddAttribute("SDKROOT",
  2519. this->CreateString(sysroot));
  2520. std::string archString;
  2521. for( std::vector<std::string>::iterator i =
  2522. this->Architectures.begin();
  2523. i != this->Architectures.end(); ++i)
  2524. {
  2525. archString += *i;
  2526. archString += " ";
  2527. }
  2528. buildSettings->AddAttribute("ARCHS",
  2529. this->CreateString(archString.c_str()));
  2530. if(!flagsUsed && sysrootDefault &&
  2531. strcmp(sysroot, sysrootDefault) != 0)
  2532. {
  2533. buildSettings->AddAttribute("SDKROOT",
  2534. this->CreateString(sysroot));
  2535. }
  2536. }
  2537. if(deploymentTarget && *deploymentTarget)
  2538. {
  2539. buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
  2540. this->CreateString(deploymentTarget));
  2541. }
  2542. std::string symroot = root->GetMakefile()->GetCurrentOutputDirectory();
  2543. symroot += "/build";
  2544. buildSettings->AddAttribute("SYMROOT", this->CreateString(symroot.c_str()));
  2545. for( std::vector<cmXCodeObject*>::iterator i = configs.begin();
  2546. i != configs.end(); ++i)
  2547. {
  2548. (*i)->AddAttribute("buildSettings", buildSettings);
  2549. }
  2550. this->RootObject->AddAttribute("buildConfigurationList",
  2551. this->CreateObjectReference(configlist));
  2552. std::vector<cmXCodeObject*> targets;
  2553. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2554. i != generators.end(); ++i)
  2555. {
  2556. if(!this->IsExcluded(root, *i))
  2557. {
  2558. this->CreateXCodeTargets(*i, targets);
  2559. }
  2560. }
  2561. // loop over all targets and add link and depend info
  2562. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2563. i != targets.end(); ++i)
  2564. {
  2565. cmXCodeObject* t = *i;
  2566. this->AddDependAndLinkInformation(t);
  2567. }
  2568. // now create xcode depend hack makefile
  2569. this->CreateXCodeDependHackTarget(targets);
  2570. // now add all targets to the root object
  2571. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2572. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2573. i != targets.end(); ++i)
  2574. {
  2575. cmXCodeObject* t = *i;
  2576. allTargets->AddObject(t);
  2577. cmXCodeObject* productRef = t->GetObject("productReference");
  2578. if(productRef)
  2579. {
  2580. productGroupChildren->AddObject(productRef->GetObject());
  2581. }
  2582. }
  2583. this->RootObject->AddAttribute("targets", allTargets);
  2584. }
  2585. //----------------------------------------------------------------------------
  2586. void
  2587. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2588. std::vector<cmXCodeObject*>& targets)
  2589. {
  2590. cmGeneratedFileStream
  2591. makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2592. if(!makefileStream)
  2593. {
  2594. cmSystemTools::Error("Could not create",
  2595. this->CurrentXCodeHackMakefile.c_str());
  2596. return;
  2597. }
  2598. makefileStream.SetCopyIfDifferent(true);
  2599. // one more pass for external depend information not handled
  2600. // correctly by xcode
  2601. makefileStream << "# DO NOT EDIT\n";
  2602. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2603. makefileStream << "# up-to-date with anything they link to, avoiding a "
  2604. "bug in XCode 1.5\n";
  2605. for(std::vector<std::string>::const_iterator
  2606. ct = this->CurrentConfigurationTypes.begin();
  2607. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2608. {
  2609. if(this->XcodeVersion < 21 || ct->empty())
  2610. {
  2611. makefileStream << "all: ";
  2612. }
  2613. else
  2614. {
  2615. makefileStream << "all." << *ct << ": ";
  2616. }
  2617. const char* configName = 0;
  2618. if(!ct->empty())
  2619. {
  2620. configName = ct->c_str();
  2621. }
  2622. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2623. i != targets.end(); ++i)
  2624. {
  2625. cmXCodeObject* target = *i;
  2626. cmTarget* t =target->GetTarget();
  2627. if(t->GetType() == cmTarget::EXECUTABLE ||
  2628. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2629. t->GetType() == cmTarget::MODULE_LIBRARY)
  2630. {
  2631. std::string tfull = t->GetFullPath(configName);
  2632. makefileStream << "\\\n\t" <<
  2633. this->ConvertToRelativeForMake(tfull.c_str());
  2634. }
  2635. }
  2636. makefileStream << "\n\n";
  2637. }
  2638. makefileStream
  2639. << "# For each target create a dummy rule "
  2640. "so the target does not have to exist\n";
  2641. std::set<cmStdString> emitted;
  2642. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2643. i != targets.end(); ++i)
  2644. {
  2645. cmXCodeObject* target = *i;
  2646. std::map<cmStdString, cmXCodeObject::StringVec> const& deplibs =
  2647. target->GetDependLibraries();
  2648. for(std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator ci
  2649. = deplibs.begin(); ci != deplibs.end(); ++ci)
  2650. {
  2651. for(cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2652. d != ci->second.end(); ++d)
  2653. {
  2654. if(emitted.insert(*d).second)
  2655. {
  2656. makefileStream <<
  2657. this->ConvertToRelativeForMake(d->c_str()) << ":\n";
  2658. }
  2659. }
  2660. }
  2661. }
  2662. makefileStream << "\n\n";
  2663. // Write rules to help Xcode relink things at the right time.
  2664. makefileStream <<
  2665. "# Rules to remove targets that are older than anything to which they\n"
  2666. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2667. "# does not seem to check these dependencies itself.\n";
  2668. for(std::vector<std::string>::const_iterator
  2669. ct = this->CurrentConfigurationTypes.begin();
  2670. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2671. {
  2672. const char* configName = 0;
  2673. if(!ct->empty())
  2674. {
  2675. configName = ct->c_str();
  2676. }
  2677. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2678. i != targets.end(); ++i)
  2679. {
  2680. cmXCodeObject* target = *i;
  2681. cmTarget* t =target->GetTarget();
  2682. if(t->GetType() == cmTarget::EXECUTABLE ||
  2683. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2684. t->GetType() == cmTarget::MODULE_LIBRARY)
  2685. {
  2686. // Create a rule for this target.
  2687. std::string tfull = t->GetFullPath(configName);
  2688. makefileStream << this->ConvertToRelativeForMake(tfull.c_str())
  2689. << ":";
  2690. // List dependencies if any exist.
  2691. std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator
  2692. x = target->GetDependLibraries().find(*ct);
  2693. if(x != target->GetDependLibraries().end())
  2694. {
  2695. std::vector<cmStdString> const& deplibs = x->second;
  2696. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  2697. d != deplibs.end(); ++d)
  2698. {
  2699. makefileStream << "\\\n\t" <<
  2700. this->ConvertToRelativeForMake(d->c_str());
  2701. }
  2702. }
  2703. // Write the action to remove the target if it is out of date.
  2704. makefileStream << "\n";
  2705. makefileStream << "\t/bin/rm -f "
  2706. << this->ConvertToRelativeForMake(tfull.c_str())
  2707. << "\n";
  2708. // if building for more than one architecture
  2709. // then remove those exectuables as well
  2710. if(this->Architectures.size() > 1)
  2711. {
  2712. std::string universal = t->GetDirectory();
  2713. universal += "/";
  2714. universal += this->CurrentProject;
  2715. universal += ".build/";
  2716. universal += configName;
  2717. universal += "/";
  2718. universal += t->GetName();
  2719. universal += ".build/Objects-normal/";
  2720. for( std::vector<std::string>::iterator arch =
  2721. this->Architectures.begin();
  2722. arch != this->Architectures.end(); ++arch)
  2723. {
  2724. std::string universalFile = universal;
  2725. universalFile += *arch;
  2726. universalFile += "/";
  2727. universalFile += t->GetFullName(configName);
  2728. makefileStream << "\t/bin/rm -f "
  2729. <<
  2730. this->ConvertToRelativeForMake(universalFile.c_str())
  2731. << "\n";
  2732. }
  2733. }
  2734. makefileStream << "\n\n";
  2735. }
  2736. }
  2737. }
  2738. }
  2739. //----------------------------------------------------------------------------
  2740. void
  2741. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  2742. std::vector<cmLocalGenerator*>&
  2743. generators)
  2744. {
  2745. if(generators.size() == 0)
  2746. {
  2747. return;
  2748. }
  2749. // Skip local generators that are excluded from this project.
  2750. for(std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2751. g != generators.end(); ++g)
  2752. {
  2753. if(this->IsExcluded(root, *g))
  2754. {
  2755. continue;
  2756. }
  2757. }
  2758. this->CreateXCodeObjects(root,
  2759. generators);
  2760. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  2761. xcodeDir += "/";
  2762. xcodeDir += root->GetMakefile()->GetProjectName();
  2763. xcodeDir += ".xcode";
  2764. if(this->XcodeVersion > 20)
  2765. {
  2766. xcodeDir += "proj";
  2767. }
  2768. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2769. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2770. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2771. fout.SetCopyIfDifferent(true);
  2772. if(!fout)
  2773. {
  2774. return;
  2775. }
  2776. this->WriteXCodePBXProj(fout, root, generators);
  2777. this->ClearXCodeObjects();
  2778. }
  2779. //----------------------------------------------------------------------------
  2780. void
  2781. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  2782. cmLocalGenerator* ,
  2783. std::vector<cmLocalGenerator*>& )
  2784. {
  2785. fout << "// !$*UTF8*$!\n";
  2786. fout << "{\n";
  2787. cmXCodeObject::Indent(1, fout);
  2788. fout << "archiveVersion = 1;\n";
  2789. cmXCodeObject::Indent(1, fout);
  2790. fout << "classes = {\n";
  2791. cmXCodeObject::Indent(1, fout);
  2792. fout << "};\n";
  2793. cmXCodeObject::Indent(1, fout);
  2794. if(this->XcodeVersion >= 21)
  2795. {
  2796. if (this->XcodeVersion >= 31)
  2797. fout << "objectVersion = 45;\n";
  2798. else if (this->XcodeVersion >= 30)
  2799. fout << "objectVersion = 44;\n";
  2800. else
  2801. fout << "objectVersion = 42;\n";
  2802. cmXCode21Object::PrintList(this->XCodeObjects, fout);
  2803. }
  2804. else
  2805. {
  2806. fout << "objectVersion = 39;\n";
  2807. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  2808. }
  2809. cmXCodeObject::Indent(1, fout);
  2810. fout << "rootObject = " << this->RootObject->GetId() << ";\n";
  2811. fout << "}\n";
  2812. }
  2813. //----------------------------------------------------------------------------
  2814. const char* cmGlobalXCodeGenerator::GetCMakeCFGInitDirectory()
  2815. {
  2816. return this->XcodeVersion >= 21? "$(CONFIGURATION)" : ".";
  2817. }
  2818. //----------------------------------------------------------------------------
  2819. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  2820. const
  2821. {
  2822. entry.Name = this->GetName();
  2823. entry.Brief = "Generate XCode project files.";
  2824. entry.Full = "";
  2825. }
  2826. //----------------------------------------------------------------------------
  2827. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  2828. {
  2829. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2830. {
  2831. return cmSystemTools::ConvertToOutputPath(p);
  2832. }
  2833. else
  2834. {
  2835. std::string ret =
  2836. this->CurrentLocalGenerator->
  2837. ConvertToRelativePath(this->CurrentOutputDirectoryComponents, p);
  2838. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2839. }
  2840. }
  2841. //----------------------------------------------------------------------------
  2842. std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
  2843. {
  2844. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2845. {
  2846. return cmSystemTools::ConvertToOutputPath(p);
  2847. }
  2848. else
  2849. {
  2850. std::string ret =
  2851. this->CurrentLocalGenerator->
  2852. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2853. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2854. }
  2855. }
  2856. //----------------------------------------------------------------------------
  2857. std::string cmGlobalXCodeGenerator::RelativeToSource(const char* p)
  2858. {
  2859. // We force conversion because Xcode breakpoints do not work unless
  2860. // they are in a file named relative to the source tree.
  2861. return this->CurrentLocalGenerator->
  2862. ConvertToRelativePath(this->ProjectSourceDirectoryComponents, p, true);
  2863. }
  2864. //----------------------------------------------------------------------------
  2865. std::string cmGlobalXCodeGenerator::RelativeToBinary(const char* p)
  2866. {
  2867. return this->CurrentLocalGenerator->
  2868. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2869. }
  2870. //----------------------------------------------------------------------------
  2871. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
  2872. {
  2873. std::string ret = p;
  2874. if(ret.find(' ') != ret.npos)
  2875. {
  2876. std::string t = ret;
  2877. ret = "\"";
  2878. ret += t;
  2879. ret += "\"";
  2880. }
  2881. return ret;
  2882. }
  2883. //----------------------------------------------------------------------------
  2884. void cmGlobalXCodeGenerator::
  2885. GetTargetObjectFileDirectories(cmTarget* target,
  2886. std::vector<std::string>&
  2887. dirs)
  2888. {
  2889. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  2890. dir += "/";
  2891. dir += this->CurrentMakefile->GetProjectName();
  2892. dir += ".build/";
  2893. dir += this->GetCMakeCFGInitDirectory();
  2894. dir += "/";
  2895. dir += target->GetName();
  2896. dir += ".build/Objects-normal/";
  2897. std::string dirsave = dir;
  2898. if(this->Architectures.size())
  2899. {
  2900. for(std::vector<std::string>::iterator i = this->Architectures.begin();
  2901. i != this->Architectures.end(); ++i)
  2902. {
  2903. dir += *i;
  2904. dirs.push_back(dir);
  2905. dir = dirsave;
  2906. }
  2907. }
  2908. else
  2909. {
  2910. dirs.push_back(dir);
  2911. }
  2912. }
  2913. //----------------------------------------------------------------------------
  2914. void
  2915. cmGlobalXCodeGenerator
  2916. ::AppendDirectoryForConfig(const char* prefix,
  2917. const char* config,
  2918. const char* suffix,
  2919. std::string& dir)
  2920. {
  2921. if(this->XcodeVersion > 20)
  2922. {
  2923. if(config)
  2924. {
  2925. dir += prefix;
  2926. dir += config;
  2927. dir += suffix;
  2928. }
  2929. }
  2930. }
  2931. //----------------------------------------------------------------------------
  2932. std::string cmGlobalXCodeGenerator::LookupFlags(const char* varNamePrefix,
  2933. const char* varNameLang,
  2934. const char* varNameSuffix,
  2935. const char* default_flags)
  2936. {
  2937. if(varNameLang)
  2938. {
  2939. std::string varName = varNamePrefix;
  2940. varName += varNameLang;
  2941. varName += varNameSuffix;
  2942. if(const char* varValue =
  2943. this->CurrentMakefile->GetDefinition(varName.c_str()))
  2944. {
  2945. if(*varValue)
  2946. {
  2947. return varValue;
  2948. }
  2949. }
  2950. }
  2951. return default_flags;
  2952. }
  2953. //----------------------------------------------------------------------------
  2954. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  2955. const char* defines_list,
  2956. bool dflag)
  2957. {
  2958. // Skip this if there are no definitions.
  2959. if(!defines_list)
  2960. {
  2961. return;
  2962. }
  2963. // Expand the list of definitions.
  2964. std::vector<std::string> defines;
  2965. cmSystemTools::ExpandListArgument(defines_list, defines);
  2966. // Store the definitions in the string.
  2967. this->AppendDefines(defs, defines, dflag);
  2968. }
  2969. //----------------------------------------------------------------------------
  2970. void
  2971. cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  2972. std::vector<std::string> const& defines,
  2973. bool dflag)
  2974. {
  2975. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  2976. std::string def;
  2977. for(std::vector<std::string>::const_iterator di = defines.begin();
  2978. di != defines.end(); ++di)
  2979. {
  2980. // Start with -D if requested.
  2981. def = dflag? "-D": "";
  2982. def += *di;
  2983. // Append the flag with needed escapes.
  2984. std::string tmp;
  2985. this->AppendFlag(tmp, def);
  2986. defs.Add(tmp.c_str());
  2987. }
  2988. }
  2989. //----------------------------------------------------------------------------
  2990. void cmGlobalXCodeGenerator::AppendFlag(std::string& flags,
  2991. std::string const& flag)
  2992. {
  2993. // Short-circuit for an empty flag.
  2994. if(flag.empty())
  2995. {
  2996. return;
  2997. }
  2998. // Separate from previous flags.
  2999. if(!flags.empty())
  3000. {
  3001. flags += " ";
  3002. }
  3003. // Check if the flag needs quoting.
  3004. bool quoteFlag =
  3005. flag.find_first_of("`~!@#$%^&*()+={}[]|:;\"'<>,.? ") != flag.npos;
  3006. // We escape a flag as follows:
  3007. // - Place each flag in single quotes ''
  3008. // - Escape a single quote as \\'
  3009. // - Escape a backslash as \\\\ since it itself is an escape
  3010. // Note that in the code below we need one more level of escapes for
  3011. // C string syntax in this source file.
  3012. //
  3013. // The final level of escaping is done when the string is stored
  3014. // into the project file by cmXCodeObject::PrintString.
  3015. if(quoteFlag)
  3016. {
  3017. // Open single quote.
  3018. flags += "'";
  3019. }
  3020. // Flag value with escaped quotes and backslashes.
  3021. for(const char* c = flag.c_str(); *c; ++c)
  3022. {
  3023. if(*c == '\'')
  3024. {
  3025. flags += "\\\\'";
  3026. }
  3027. else if(*c == '\\')
  3028. {
  3029. flags += "\\\\\\\\";
  3030. }
  3031. else
  3032. {
  3033. flags += *c;
  3034. }
  3035. }
  3036. if(quoteFlag)
  3037. {
  3038. // Close single quote.
  3039. flags += "'";
  3040. }
  3041. }
  3042. //----------------------------------------------------------------------------
  3043. std::string
  3044. cmGlobalXCodeGenerator::ComputeInfoPListLocation(cmTarget& target)
  3045. {
  3046. std::string plist = target.GetMakefile()->GetCurrentOutputDirectory();
  3047. plist += cmake::GetCMakeFilesDirectory();
  3048. plist += "/";
  3049. plist += target.GetName();
  3050. plist += ".dir/Info.plist";
  3051. return plist;
  3052. }