cmGlobalXCodeGenerator.cxx 104 KB

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