cmGlobalXCodeGenerator.cxx 102 KB

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