cmGlobalXCodeGenerator.cxx 102 KB

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