cmGlobalXCodeGenerator.cxx 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103
  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(this);
  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. std::string& fileType,
  1287. std::string& productType,
  1288. std::string& productName,
  1289. const char* configName)
  1290. {
  1291. std::string flags;
  1292. std::string defFlags;
  1293. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  1294. (target.GetType() == cmTarget::MODULE_LIBRARY));
  1295. const char* lang = target.GetLinkerLanguage(this);
  1296. std::string cflags;
  1297. if(lang)
  1298. {
  1299. // for c++ projects get the c flags as well
  1300. if(strcmp(lang, "CXX") == 0)
  1301. {
  1302. this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
  1303. this->CurrentLocalGenerator->AddSharedFlags(cflags, lang, shared);
  1304. }
  1305. // Add language-specific flags.
  1306. this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
  1307. // Add shared-library flags if needed.
  1308. this->CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
  1309. }
  1310. // Add define flags
  1311. this->CurrentLocalGenerator->
  1312. AppendFlags(defFlags,
  1313. this->CurrentMakefile->GetDefineFlags());
  1314. // Add preprocessor definitions for this target and configuration.
  1315. BuildObjectListOrString ppDefs(this, this->XcodeVersion >= 30);
  1316. if(this->XcodeVersion > 15)
  1317. {
  1318. this->AppendDefines(ppDefs, "CMAKE_INTDIR=\"$(CONFIGURATION)\"");
  1319. }
  1320. if(const char* exportMacro = target.GetExportMacro())
  1321. {
  1322. // Add the export symbol definition for shared library objects.
  1323. this->AppendDefines(ppDefs, exportMacro);
  1324. }
  1325. this->AppendDefines
  1326. (ppDefs, this->CurrentMakefile->GetProperty("COMPILE_DEFINITIONS"));
  1327. this->AppendDefines(ppDefs, target.GetProperty("COMPILE_DEFINITIONS"));
  1328. if(configName)
  1329. {
  1330. std::string defVarName = "COMPILE_DEFINITIONS_";
  1331. defVarName += cmSystemTools::UpperCase(configName);
  1332. this->AppendDefines
  1333. (ppDefs, this->CurrentMakefile->GetProperty(defVarName.c_str()));
  1334. this->AppendDefines(ppDefs, target.GetProperty(defVarName.c_str()));
  1335. }
  1336. buildSettings->AddAttribute
  1337. ("GCC_PREPROCESSOR_DEFINITIONS", ppDefs.CreateList());
  1338. std::string extraLinkOptions;
  1339. if(target.GetType() == cmTarget::EXECUTABLE)
  1340. {
  1341. extraLinkOptions =
  1342. this->CurrentMakefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1343. }
  1344. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1345. {
  1346. extraLinkOptions = this->CurrentMakefile->
  1347. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1348. }
  1349. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1350. {
  1351. extraLinkOptions = this->CurrentMakefile->
  1352. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1353. }
  1354. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1355. if(targetLinkFlags)
  1356. {
  1357. extraLinkOptions += " ";
  1358. extraLinkOptions += targetLinkFlags;
  1359. }
  1360. // The product name is the full name of the target for this configuration.
  1361. productName = target.GetFullName(configName);
  1362. // Get the product name components.
  1363. std::string pnprefix;
  1364. std::string pnbase;
  1365. std::string pnsuffix;
  1366. target.GetFullNameComponents(pnprefix, pnbase, pnsuffix, configName);
  1367. // Store the product name for all target types.
  1368. buildSettings->AddAttribute("PRODUCT_NAME",
  1369. this->CreateString(pnbase.c_str()));
  1370. // Set attributes to specify the proper name for the target.
  1371. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1372. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1373. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1374. target.GetType() == cmTarget::EXECUTABLE)
  1375. {
  1376. std::string pndir = target.GetDirectory();
  1377. buildSettings->AddAttribute("SYMROOT",
  1378. this->CreateString(pndir.c_str()));
  1379. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1380. this->CreateString(pnprefix.c_str()));
  1381. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1382. this->CreateString(pnsuffix.c_str()));
  1383. }
  1384. // Handle settings for each target type.
  1385. switch(target.GetType())
  1386. {
  1387. case cmTarget::STATIC_LIBRARY:
  1388. {
  1389. fileType = "archive.ar";
  1390. productType = "com.apple.product-type.library.static";
  1391. buildSettings->AddAttribute("LIBRARY_STYLE",
  1392. this->CreateString("STATIC"));
  1393. break;
  1394. }
  1395. case cmTarget::MODULE_LIBRARY:
  1396. {
  1397. buildSettings->AddAttribute("LIBRARY_STYLE",
  1398. this->CreateString("BUNDLE"));
  1399. if(this->XcodeVersion >= 22)
  1400. {
  1401. fileType = "compiled.mach-o.executable";
  1402. productType = "com.apple.product-type.tool";
  1403. buildSettings->AddAttribute("MACH_O_TYPE",
  1404. this->CreateString("mh_bundle"));
  1405. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1406. this->CreateString("NO"));
  1407. // Add the flags to create an executable.
  1408. std::string createFlags =
  1409. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1410. if(!createFlags.empty())
  1411. {
  1412. extraLinkOptions += " ";
  1413. extraLinkOptions += createFlags;
  1414. }
  1415. }
  1416. else
  1417. {
  1418. fileType = "compiled.mach-o.dylib";
  1419. productType = "com.apple.product-type.library.dynamic";
  1420. // Add the flags to create a module.
  1421. std::string createFlags =
  1422. this->LookupFlags("CMAKE_SHARED_MODULE_CREATE_", lang, "_FLAGS",
  1423. "-bundle");
  1424. if(!createFlags.empty())
  1425. {
  1426. extraLinkOptions += " ";
  1427. extraLinkOptions += createFlags;
  1428. }
  1429. }
  1430. break;
  1431. }
  1432. case cmTarget::SHARED_LIBRARY:
  1433. {
  1434. if(target.GetPropertyAsBool("FRAMEWORK"))
  1435. {
  1436. fileType = "wrapper.framework";
  1437. productType = "com.apple.product-type.framework";
  1438. std::string version = target.GetFrameworkVersion();
  1439. buildSettings->AddAttribute("FRAMEWORK_VERSION",
  1440. this->CreateString(version.c_str()));
  1441. std::string plist = this->ComputeInfoPListLocation(target);
  1442. // Xcode will create the final version of Info.plist at build time,
  1443. // so let it replace the framework name. This avoids creating
  1444. // a per-configuration Info.plist file.
  1445. this->CurrentLocalGenerator
  1446. ->GenerateFrameworkInfoPList(&target, "$(EXECUTABLE_NAME)",
  1447. plist.c_str());
  1448. std::string path =
  1449. this->ConvertToRelativeForXCode(plist.c_str());
  1450. buildSettings->AddAttribute("INFOPLIST_FILE",
  1451. this->CreateString(path.c_str()));
  1452. }
  1453. else
  1454. {
  1455. fileType = "compiled.mach-o.dylib";
  1456. productType = "com.apple.product-type.library.dynamic";
  1457. // Add the flags to create a shared library.
  1458. std::string createFlags =
  1459. this->LookupFlags("CMAKE_SHARED_LIBRARY_CREATE_", lang, "_FLAGS",
  1460. "-dynamiclib");
  1461. if(!createFlags.empty())
  1462. {
  1463. extraLinkOptions += " ";
  1464. extraLinkOptions += createFlags;
  1465. }
  1466. }
  1467. buildSettings->AddAttribute("LIBRARY_STYLE",
  1468. this->CreateString("DYNAMIC"));
  1469. break;
  1470. }
  1471. case cmTarget::EXECUTABLE:
  1472. {
  1473. fileType = "compiled.mach-o.executable";
  1474. // Add the flags to create an executable.
  1475. std::string createFlags =
  1476. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1477. if(!createFlags.empty())
  1478. {
  1479. extraLinkOptions += " ";
  1480. extraLinkOptions += createFlags;
  1481. }
  1482. // Handle bundles and normal executables separately.
  1483. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1484. {
  1485. productType = "com.apple.product-type.application";
  1486. std::string plist = this->ComputeInfoPListLocation(target);
  1487. // Xcode will create the final version of Info.plist at build time,
  1488. // so let it replace the executable name. This avoids creating
  1489. // a per-configuration Info.plist file.
  1490. this->CurrentLocalGenerator
  1491. ->GenerateAppleInfoPList(&target, "$(EXECUTABLE_NAME)",
  1492. plist.c_str());
  1493. std::string path =
  1494. this->ConvertToRelativeForXCode(plist.c_str());
  1495. buildSettings->AddAttribute("INFOPLIST_FILE",
  1496. this->CreateString(path.c_str()));
  1497. }
  1498. else
  1499. {
  1500. productType = "com.apple.product-type.tool";
  1501. }
  1502. }
  1503. break;
  1504. default:
  1505. break;
  1506. }
  1507. if(this->XcodeVersion >= 22)
  1508. {
  1509. buildSettings->AddAttribute("PREBINDING",
  1510. this->CreateString("NO"));
  1511. }
  1512. BuildObjectListOrString dirs(this, this->XcodeVersion >= 30);
  1513. BuildObjectListOrString fdirs(this, this->XcodeVersion >= 30);
  1514. std::vector<std::string> includes;
  1515. this->CurrentLocalGenerator->GetIncludeDirectories(includes);
  1516. std::set<cmStdString> emitted;
  1517. emitted.insert("/System/Library/Frameworks");
  1518. for(std::vector<std::string>::iterator i = includes.begin();
  1519. i != includes.end(); ++i)
  1520. {
  1521. if(this->NameResolvesToFramework(i->c_str()))
  1522. {
  1523. std::string frameworkDir = *i;
  1524. frameworkDir += "/../";
  1525. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1526. if(emitted.insert(frameworkDir).second)
  1527. {
  1528. fdirs.Add(this->XCodeEscapePath(frameworkDir.c_str()).c_str());
  1529. }
  1530. }
  1531. else
  1532. {
  1533. std::string incpath =
  1534. this->XCodeEscapePath(i->c_str());
  1535. dirs.Add(incpath.c_str());
  1536. }
  1537. }
  1538. std::vector<std::string>& frameworks = target.GetFrameworks();
  1539. if(frameworks.size())
  1540. {
  1541. for(std::vector<std::string>::iterator fmIt = frameworks.begin();
  1542. fmIt != frameworks.end(); ++fmIt)
  1543. {
  1544. if(emitted.insert(*fmIt).second)
  1545. {
  1546. fdirs.Add(this->XCodeEscapePath(fmIt->c_str()).c_str());
  1547. }
  1548. }
  1549. }
  1550. if(!fdirs.IsEmpty())
  1551. {
  1552. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS",
  1553. fdirs.CreateList());
  1554. }
  1555. if(!dirs.IsEmpty())
  1556. {
  1557. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  1558. dirs.CreateList());
  1559. }
  1560. std::string oflagc = this->ExtractFlag("-O", cflags);
  1561. char optLevel[2];
  1562. optLevel[0] = '0';
  1563. optLevel[1] = 0;
  1564. if(oflagc.size() == 3)
  1565. {
  1566. optLevel[0] = oflagc[2];
  1567. }
  1568. if(oflagc.size() == 2)
  1569. {
  1570. optLevel[0] = '1';
  1571. }
  1572. std::string oflag = this->ExtractFlag("-O", flags);
  1573. if(oflag.size() == 3)
  1574. {
  1575. optLevel[0] = oflag[2];
  1576. }
  1577. if(oflag.size() == 2)
  1578. {
  1579. optLevel[0] = '1';
  1580. }
  1581. std::string gflagc = this->ExtractFlag("-g", cflags);
  1582. // put back gdwarf-2 if used since there is no way
  1583. // to represent it in the gui, but we still want debug yes
  1584. if(gflagc == "-gdwarf-2")
  1585. {
  1586. cflags += " ";
  1587. cflags += gflagc;
  1588. }
  1589. std::string gflag = this->ExtractFlag("-g", flags);
  1590. if(gflag == "-gdwarf-2")
  1591. {
  1592. flags += " ";
  1593. flags += gflag;
  1594. }
  1595. const char* debugStr = "YES";
  1596. if(gflagc.size() ==0 && gflag.size() == 0)
  1597. {
  1598. debugStr = "NO";
  1599. }
  1600. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1601. this->CreateString(debugStr));
  1602. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1603. this->CreateString(optLevel));
  1604. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1605. this->CreateString("NO"));
  1606. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1607. this->CreateString("NO"));
  1608. if(lang && strcmp(lang, "CXX") == 0)
  1609. {
  1610. flags += " ";
  1611. flags += defFlags;
  1612. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1613. this->CreateString(flags.c_str()));
  1614. cflags += " ";
  1615. cflags += defFlags;
  1616. buildSettings->AddAttribute("OTHER_CFLAGS",
  1617. this->CreateString(cflags.c_str()));
  1618. }
  1619. else
  1620. {
  1621. flags += " ";
  1622. flags += defFlags;
  1623. buildSettings->AddAttribute("OTHER_CFLAGS",
  1624. this->CreateString(flags.c_str()));
  1625. }
  1626. // Create the INSTALL_PATH attribute.
  1627. std::string install_name_dir;
  1628. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1629. {
  1630. // Get the install_name directory for the build tree.
  1631. install_name_dir = target.GetInstallNameDirForBuildTree(configName, true);
  1632. if(install_name_dir.empty())
  1633. {
  1634. // Xcode will not pass the -install_name option at all if INSTALL_PATH
  1635. // is not given or is empty. We must explicitly put the flag in the
  1636. // link flags to create an install_name with just the library soname.
  1637. extraLinkOptions += " -install_name ";
  1638. extraLinkOptions += productName;
  1639. }
  1640. else
  1641. {
  1642. // Convert to a path for the native build tool.
  1643. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1644. // do not escape spaces on this since it is only a single path
  1645. }
  1646. }
  1647. buildSettings->AddAttribute("INSTALL_PATH",
  1648. this->CreateString(install_name_dir.c_str()));
  1649. buildSettings->AddAttribute("OTHER_LDFLAGS",
  1650. this->CreateString(extraLinkOptions.c_str()));
  1651. buildSettings->AddAttribute("OTHER_REZFLAGS",
  1652. this->CreateString(""));
  1653. buildSettings->AddAttribute("SECTORDER_FLAGS",
  1654. this->CreateString(""));
  1655. buildSettings->AddAttribute("USE_HEADERMAP",
  1656. this->CreateString("NO"));
  1657. if (this->XcodeVersion >= 30)
  1658. {
  1659. cmXCodeObject *group = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1660. group->AddObject(this->CreateString("-Wmost"));
  1661. group->AddObject(this->CreateString("-Wno-four-char-constants"));
  1662. group->AddObject(this->CreateString("-Wno-unknown-pragmas"));
  1663. buildSettings->AddAttribute("WARNING_CFLAGS", group);
  1664. }
  1665. else
  1666. {
  1667. buildSettings->AddAttribute("WARNING_CFLAGS",
  1668. this->CreateString(
  1669. "-Wmost -Wno-four-char-constants"
  1670. " -Wno-unknown-pragmas"));
  1671. }
  1672. // Runtime version information.
  1673. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1674. {
  1675. int major;
  1676. int minor;
  1677. int patch;
  1678. // VERSION -> current_version
  1679. target.GetTargetVersion(false, major, minor, patch);
  1680. if(major == 0 && minor == 0 && patch == 0)
  1681. {
  1682. // Xcode always wants at least 1.0.0
  1683. major = 1;
  1684. }
  1685. cmOStringStream v;
  1686. v << major << "." << minor << "." << patch;
  1687. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1688. this->CreateString(v.str().c_str()));
  1689. // SOVERSION -> compatibility_version
  1690. target.GetTargetVersion(true, major, minor, patch);
  1691. if(major == 0 && minor == 0 && patch == 0)
  1692. {
  1693. // Xcode always wants at least 1.0.0
  1694. major = 1;
  1695. }
  1696. cmOStringStream vso;
  1697. vso << major << "." << minor << "." << patch;
  1698. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1699. this->CreateString(vso.str().c_str()));
  1700. }
  1701. // put this last so it can override existing settings
  1702. // Convert "XCODE_ATTRIBUTE_*" properties directly.
  1703. {
  1704. cmPropertyMap const& props = target.GetProperties();
  1705. for(cmPropertyMap::const_iterator i = props.begin();
  1706. i != props.end(); ++i)
  1707. {
  1708. if(i->first.find("XCODE_ATTRIBUTE_") == 0)
  1709. {
  1710. buildSettings->AddAttribute(i->first.substr(16).c_str(),
  1711. this->CreateString(i->second.GetValue()));
  1712. }
  1713. }
  1714. }
  1715. }
  1716. //----------------------------------------------------------------------------
  1717. cmXCodeObject*
  1718. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  1719. {
  1720. cmXCodeObject* shellBuildPhase =
  1721. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1722. shellBuildPhase->AddAttribute("buildActionMask",
  1723. this->CreateString("2147483647"));
  1724. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1725. shellBuildPhase->AddAttribute("files", buildFiles);
  1726. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1727. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1728. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1729. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1730. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1731. this->CreateString("0"));
  1732. shellBuildPhase->AddAttribute("shellPath",
  1733. this->CreateString("/bin/sh"));
  1734. shellBuildPhase->AddAttribute("shellScript",
  1735. this->CreateString(
  1736. "# shell script goes here\nexit 0"));
  1737. cmXCodeObject* target =
  1738. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1739. target->SetComment(cmtarget.GetName());
  1740. cmXCodeObject* buildPhases =
  1741. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1742. std::vector<cmXCodeObject*> emptyContentVector;
  1743. this->CreateCustomCommands(buildPhases, 0, 0, 0, emptyContentVector, 0,
  1744. cmtarget);
  1745. target->AddAttribute("buildPhases", buildPhases);
  1746. cmXCodeObject* buildSettings =
  1747. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1748. std::string fileTypeString;
  1749. std::string productTypeString;
  1750. std::string productName;
  1751. const char* globalConfig = 0;
  1752. if(this->XcodeVersion > 20)
  1753. {
  1754. this->AddConfigurations(target, cmtarget);
  1755. }
  1756. else
  1757. {
  1758. globalConfig = this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1759. }
  1760. this->CreateBuildSettings(cmtarget,
  1761. buildSettings, fileTypeString,
  1762. productTypeString, productName, globalConfig);
  1763. target->AddAttribute("buildSettings", buildSettings);
  1764. cmXCodeObject* dependencies =
  1765. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1766. target->AddAttribute("dependencies", dependencies);
  1767. target->AddAttribute("name", this->CreateString(productName.c_str()));
  1768. target->AddAttribute("productName",this->CreateString(productName.c_str()));
  1769. target->SetTarget(&cmtarget);
  1770. // Add source files without build rules for editing convenience.
  1771. if(cmtarget.GetType() == cmTarget::UTILITY)
  1772. {
  1773. std::vector<cmSourceFile*> const& sources = cmtarget.GetSourceFiles();
  1774. for(std::vector<cmSourceFile*>::const_iterator i = sources.begin();
  1775. i != sources.end(); ++i)
  1776. {
  1777. if(!(*i)->GetPropertyAsBool("GENERATED"))
  1778. {
  1779. this->CreateXCodeFileReference(*i, cmtarget);
  1780. }
  1781. }
  1782. }
  1783. return target;
  1784. }
  1785. //----------------------------------------------------------------------------
  1786. void cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  1787. cmTarget& cmtarget)
  1788. {
  1789. std::string configTypes =
  1790. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  1791. std::vector<std::string> configVectorIn;
  1792. std::vector<std::string> configVector;
  1793. configVectorIn.push_back(configTypes);
  1794. cmSystemTools::ExpandList(configVectorIn, configVector);
  1795. cmXCodeObject* configlist =
  1796. this->CreateObject(cmXCodeObject::XCConfigurationList);
  1797. cmXCodeObject* buildConfigurations =
  1798. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1799. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  1800. std::string comment = "Build configuration list for ";
  1801. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  1802. comment += " \"";
  1803. comment += cmtarget.GetName();
  1804. comment += "\"";
  1805. configlist->SetComment(comment.c_str());
  1806. target->AddAttribute("buildConfigurationList",
  1807. this->CreateObjectReference(configlist));
  1808. for(unsigned int i = 0; i < configVector.size(); ++i)
  1809. {
  1810. cmXCodeObject* config =
  1811. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  1812. buildConfigurations->AddObject(config);
  1813. cmXCodeObject* buildSettings =
  1814. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1815. std::string fileTypeString;
  1816. std::string productTypeString;
  1817. std::string productName;
  1818. this->CreateBuildSettings(cmtarget,
  1819. buildSettings, fileTypeString,
  1820. productTypeString, productName,
  1821. configVector[i].c_str());
  1822. config->AddAttribute("name", this->CreateString(configVector[i].c_str()));
  1823. config->SetComment(configVector[i].c_str());
  1824. config->AddAttribute("buildSettings", buildSettings);
  1825. }
  1826. if(configVector.size())
  1827. {
  1828. configlist->AddAttribute("defaultConfigurationName",
  1829. this->CreateString(configVector[0].c_str()));
  1830. configlist->AddAttribute("defaultConfigurationIsVisible",
  1831. this->CreateString("0"));
  1832. }
  1833. }
  1834. //----------------------------------------------------------------------------
  1835. cmXCodeObject*
  1836. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  1837. cmXCodeObject* buildPhases)
  1838. {
  1839. cmXCodeObject* target =
  1840. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  1841. target->AddAttribute("buildPhases", buildPhases);
  1842. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1843. target->AddAttribute("buildRules", buildRules);
  1844. cmXCodeObject* buildSettings =
  1845. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1846. std::string fileTypeString;
  1847. std::string productTypeString;
  1848. std::string productName;
  1849. const char* globalConfig = 0;
  1850. if(this->XcodeVersion > 20)
  1851. {
  1852. this->AddConfigurations(target, cmtarget);
  1853. }
  1854. else
  1855. {
  1856. globalConfig = this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1857. }
  1858. this->CreateBuildSettings(cmtarget,
  1859. buildSettings, fileTypeString,
  1860. productTypeString, productName, globalConfig);
  1861. target->AddAttribute("buildSettings", buildSettings);
  1862. cmXCodeObject* dependencies =
  1863. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1864. target->AddAttribute("dependencies", dependencies);
  1865. target->AddAttribute("name", this->CreateString(productName.c_str()));
  1866. target->AddAttribute("productName",this->CreateString(productName.c_str()));
  1867. cmXCodeObject* fileRef =
  1868. this->CreateObject(cmXCodeObject::PBXFileReference);
  1869. fileRef->AddAttribute("explicitFileType",
  1870. this->CreateString(fileTypeString.c_str()));
  1871. fileRef->AddAttribute("path", this->CreateString(productName.c_str()));
  1872. fileRef->AddAttribute("refType", this->CreateString("0"));
  1873. fileRef->AddAttribute("sourceTree",
  1874. this->CreateString("BUILT_PRODUCTS_DIR"));
  1875. fileRef->SetComment(cmtarget.GetName());
  1876. target->AddAttribute("productReference",
  1877. this->CreateObjectReference(fileRef));
  1878. target->AddAttribute("productType",
  1879. this->CreateString(productTypeString.c_str()));
  1880. target->SetTarget(&cmtarget);
  1881. return target;
  1882. }
  1883. //----------------------------------------------------------------------------
  1884. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  1885. {
  1886. if(!t)
  1887. {
  1888. return 0;
  1889. }
  1890. for(std::vector<cmXCodeObject*>::iterator i = this->XCodeObjects.begin();
  1891. i != this->XCodeObjects.end(); ++i)
  1892. {
  1893. cmXCodeObject* o = *i;
  1894. if(o->GetTarget() == t)
  1895. {
  1896. return o;
  1897. }
  1898. }
  1899. return 0;
  1900. }
  1901. //----------------------------------------------------------------------------
  1902. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  1903. cmXCodeObject* dependTarget)
  1904. {
  1905. // make sure a target does not depend on itself
  1906. if(target == dependTarget)
  1907. {
  1908. return;
  1909. }
  1910. // now avoid circular references if dependTarget already
  1911. // depends on target then skip it. Circular references crashes
  1912. // xcode
  1913. cmXCodeObject* dependTargetDepends =
  1914. dependTarget->GetObject("dependencies");
  1915. if(dependTargetDepends)
  1916. {
  1917. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  1918. {
  1919. return;
  1920. }
  1921. }
  1922. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  1923. if(!targetdep)
  1924. {
  1925. cmXCodeObject* container =
  1926. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  1927. container->SetComment("PBXContainerItemProxy");
  1928. container->AddAttribute("containerPortal",
  1929. this->CreateObjectReference(this->RootObject));
  1930. container->AddAttribute("proxyType", this->CreateString("1"));
  1931. container->AddAttribute("remoteGlobalIDString",
  1932. this->CreateObjectReference(dependTarget));
  1933. container->AddAttribute("remoteInfo",
  1934. this->CreateString(
  1935. dependTarget->GetTarget()->GetName()));
  1936. targetdep =
  1937. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  1938. targetdep->SetComment("PBXTargetDependency");
  1939. targetdep->AddAttribute("target",
  1940. this->CreateObjectReference(dependTarget));
  1941. targetdep->AddAttribute("targetProxy",
  1942. this->CreateObjectReference(container));
  1943. dependTarget->SetPBXTargetDependency(targetdep);
  1944. }
  1945. cmXCodeObject* depends = target->GetObject("dependencies");
  1946. if(!depends)
  1947. {
  1948. cmSystemTools::
  1949. Error("target does not have dependencies attribute error..");
  1950. }
  1951. else
  1952. {
  1953. depends->AddUniqueObject(targetdep);
  1954. }
  1955. }
  1956. //----------------------------------------------------------------------------
  1957. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  1958. const char* attribute,
  1959. const char* value)
  1960. {
  1961. if(settings)
  1962. {
  1963. cmXCodeObject* attr = settings->GetObject(attribute);
  1964. if(!attr)
  1965. {
  1966. settings->AddAttribute(attribute, this->CreateString(value));
  1967. }
  1968. else
  1969. {
  1970. std::string oldValue = attr->GetString();
  1971. oldValue += " ";
  1972. oldValue += value;
  1973. attr->SetString(oldValue.c_str());
  1974. }
  1975. }
  1976. }
  1977. //----------------------------------------------------------------------------
  1978. void cmGlobalXCodeGenerator
  1979. ::AppendBuildSettingAttribute(cmXCodeObject* target,
  1980. const char* attribute,
  1981. const char* value,
  1982. const char* configName)
  1983. {
  1984. if(this->XcodeVersion < 21)
  1985. {
  1986. // There is only one configuration. Add the setting to the buildSettings
  1987. // of the target.
  1988. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  1989. attribute, value);
  1990. }
  1991. else
  1992. {
  1993. // There are multiple configurations. Add the setting to the
  1994. // buildSettings of the configuration name given.
  1995. cmXCodeObject* configurationList =
  1996. target->GetObject("buildConfigurationList")->GetObject();
  1997. cmXCodeObject* buildConfigs =
  1998. configurationList->GetObject("buildConfigurations");
  1999. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  2000. // each configuration and the target itself has a buildSettings in it
  2001. //list.push_back(target);
  2002. for(std::vector<cmXCodeObject*>::iterator i = list.begin();
  2003. i != list.end(); ++i)
  2004. {
  2005. if(configName)
  2006. {
  2007. if(strcmp((*i)->GetObject("name")->GetString(), configName) == 0)
  2008. {
  2009. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2010. this->AppendOrAddBuildSetting(settings, attribute, value);
  2011. }
  2012. }
  2013. else
  2014. {
  2015. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  2016. this->AppendOrAddBuildSetting(settings, attribute, value);
  2017. }
  2018. }
  2019. }
  2020. }
  2021. //----------------------------------------------------------------------------
  2022. void cmGlobalXCodeGenerator
  2023. ::AddDependAndLinkInformation(cmXCodeObject* target)
  2024. {
  2025. cmTarget* cmtarget = target->GetTarget();
  2026. if(!cmtarget)
  2027. {
  2028. cmSystemTools::Error("Error no target on xobject\n");
  2029. return;
  2030. }
  2031. // Add dependencies on other CMake targets.
  2032. TargetDependSet const& deps = this->GetTargetDirectDepends(*cmtarget);
  2033. for(TargetDependSet::const_iterator i = deps.begin(); i != deps.end(); ++i)
  2034. {
  2035. if(cmXCodeObject* dptarget = this->FindXCodeTarget(*i))
  2036. {
  2037. this->AddDependTarget(target, dptarget);
  2038. }
  2039. }
  2040. // Skip link information for static libraries.
  2041. if(cmtarget->GetType() == cmTarget::STATIC_LIBRARY)
  2042. {
  2043. return;
  2044. }
  2045. // Loop over configuration types and set per-configuration info.
  2046. for(std::vector<std::string>::iterator i =
  2047. this->CurrentConfigurationTypes.begin();
  2048. i != this->CurrentConfigurationTypes.end(); ++i)
  2049. {
  2050. // Get the current configuration name.
  2051. const char* configName = i->c_str();
  2052. if(!*configName)
  2053. {
  2054. configName = 0;
  2055. }
  2056. // Compute the link library and directory information.
  2057. cmComputeLinkInformation* pcli = cmtarget->GetLinkInformation(configName);
  2058. if(!pcli)
  2059. {
  2060. continue;
  2061. }
  2062. cmComputeLinkInformation& cli = *pcli;
  2063. // Add dependencies directly on library files.
  2064. {
  2065. std::vector<std::string> const& libDeps = cli.GetDepends();
  2066. for(std::vector<std::string>::const_iterator j = libDeps.begin();
  2067. j != libDeps.end(); ++j)
  2068. {
  2069. target->AddDependLibrary(configName, j->c_str());
  2070. }
  2071. }
  2072. // add the library search paths
  2073. {
  2074. std::vector<std::string> const& libDirs = cli.GetDirectories();
  2075. std::string linkDirs;
  2076. for(std::vector<std::string>::const_iterator libDir = libDirs.begin();
  2077. libDir != libDirs.end(); ++libDir)
  2078. {
  2079. if(libDir->size() && *libDir != "/usr/lib")
  2080. {
  2081. if(this->XcodeVersion > 15)
  2082. {
  2083. // now add the same one but append $(CONFIGURATION) to it:
  2084. linkDirs += " ";
  2085. linkDirs += this->XCodeEscapePath(
  2086. (*libDir + "/$(CONFIGURATION)").c_str());
  2087. }
  2088. linkDirs += " ";
  2089. linkDirs += this->XCodeEscapePath(libDir->c_str());
  2090. }
  2091. }
  2092. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  2093. linkDirs.c_str(), configName);
  2094. }
  2095. // add the framework search paths
  2096. {
  2097. const char* sep = "";
  2098. std::string fdirs;
  2099. std::vector<std::string> const& fwDirs = cli.GetFrameworkPaths();
  2100. for(std::vector<std::string>::const_iterator fdi = fwDirs.begin();
  2101. fdi != fwDirs.end(); ++fdi)
  2102. {
  2103. fdirs += sep;
  2104. sep = " ";
  2105. fdirs += this->XCodeEscapePath(fdi->c_str());
  2106. }
  2107. if(!fdirs.empty())
  2108. {
  2109. this->AppendBuildSettingAttribute(target, "FRAMEWORK_SEARCH_PATHS",
  2110. fdirs.c_str(), configName);
  2111. }
  2112. }
  2113. // now add the link libraries
  2114. {
  2115. std::string linkLibs;
  2116. const char* sep = "";
  2117. typedef cmComputeLinkInformation::ItemVector ItemVector;
  2118. ItemVector const& libNames = cli.GetItems();
  2119. for(ItemVector::const_iterator li = libNames.begin();
  2120. li != libNames.end(); ++li)
  2121. {
  2122. linkLibs += sep;
  2123. sep = " ";
  2124. if(li->IsPath)
  2125. {
  2126. linkLibs += this->XCodeEscapePath(li->Value.c_str());
  2127. }
  2128. else
  2129. {
  2130. linkLibs += li->Value;
  2131. }
  2132. }
  2133. this->AppendBuildSettingAttribute(target, "OTHER_LDFLAGS",
  2134. linkLibs.c_str(), configName);
  2135. }
  2136. }
  2137. }
  2138. //----------------------------------------------------------------------------
  2139. void cmGlobalXCodeGenerator::CreateGroups(cmLocalGenerator* root,
  2140. std::vector<cmLocalGenerator*>&
  2141. generators)
  2142. {
  2143. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2144. i != generators.end(); ++i)
  2145. {
  2146. if(this->IsExcluded(root, *i))
  2147. {
  2148. continue;
  2149. }
  2150. cmMakefile* mf = (*i)->GetMakefile();
  2151. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2152. cmTargets &tgts = mf->GetTargets();
  2153. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  2154. {
  2155. cmTarget& cmtarget = l->second;
  2156. // Same skipping logic here as in CreateXCodeTargets so that we do not
  2157. // end up with (empty anyhow) ALL_BUILD and XCODE_DEPEND_HELPER source
  2158. // groups:
  2159. //
  2160. if(cmtarget.GetType() == cmTarget::GLOBAL_TARGET)
  2161. {
  2162. continue;
  2163. }
  2164. // add the soon to be generated Info.plist file as a source for a
  2165. // MACOSX_BUNDLE file
  2166. if(cmtarget.GetPropertyAsBool("MACOSX_BUNDLE"))
  2167. {
  2168. std::string plist = this->ComputeInfoPListLocation(cmtarget);
  2169. cmSourceFile* sf = mf->GetOrCreateSource(plist.c_str(), true);
  2170. cmtarget.AddSourceFile(sf);
  2171. }
  2172. std::vector<cmSourceFile*> classes = cmtarget.GetSourceFiles();
  2173. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  2174. s != classes.end(); s++)
  2175. {
  2176. cmSourceFile* sf = *s;
  2177. // Add the file to the list of sources.
  2178. std::string const& source = sf->GetFullPath();
  2179. cmSourceGroup& sourceGroup =
  2180. mf->FindSourceGroup(source.c_str(), sourceGroups);
  2181. cmXCodeObject* pbxgroup =
  2182. this->CreateOrGetPBXGroup(cmtarget, &sourceGroup);
  2183. cmStdString key = GetGroupMapKey(cmtarget, sf);
  2184. this->GroupMap[key] = pbxgroup;
  2185. }
  2186. }
  2187. }
  2188. }
  2189. //----------------------------------------------------------------------------
  2190. cmXCodeObject* cmGlobalXCodeGenerator
  2191. ::CreateOrGetPBXGroup(cmTarget& cmtarget, cmSourceGroup* sg)
  2192. {
  2193. cmStdString s = cmtarget.GetName();
  2194. s += "/";
  2195. s += sg->GetName();
  2196. std::map<cmStdString, cmXCodeObject* >::iterator i =
  2197. this->GroupNameMap.find(s);
  2198. if(i != this->GroupNameMap.end())
  2199. {
  2200. return i->second;
  2201. }
  2202. i = this->TargetGroup.find(cmtarget.GetName());
  2203. cmXCodeObject* tgroup = 0;
  2204. if(i != this->TargetGroup.end())
  2205. {
  2206. tgroup = i->second;
  2207. }
  2208. else
  2209. {
  2210. tgroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2211. this->TargetGroup[cmtarget.GetName()] = tgroup;
  2212. cmXCodeObject* tgroupChildren =
  2213. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2214. tgroup->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  2215. tgroup->AddAttribute("children", tgroupChildren);
  2216. if(this->XcodeVersion == 15)
  2217. {
  2218. tgroup->AddAttribute("refType", this->CreateString("4"));
  2219. }
  2220. tgroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2221. this->SourcesGroupChildren->AddObject(tgroup);
  2222. }
  2223. // If it's the default source group (empty name) then put the source file
  2224. // directly in the tgroup...
  2225. //
  2226. if (cmStdString(sg->GetName()) == "")
  2227. {
  2228. this->GroupNameMap[s] = tgroup;
  2229. return tgroup;
  2230. }
  2231. cmXCodeObject* tgroupChildren = tgroup->GetObject("children");
  2232. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  2233. cmXCodeObject* groupChildren =
  2234. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2235. group->AddAttribute("name", this->CreateString(sg->GetName()));
  2236. group->AddAttribute("children", groupChildren);
  2237. if(this->XcodeVersion == 15)
  2238. {
  2239. group->AddAttribute("refType", this->CreateString("4"));
  2240. }
  2241. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  2242. tgroupChildren->AddObject(group);
  2243. this->GroupNameMap[s] = group;
  2244. return group;
  2245. }
  2246. //----------------------------------------------------------------------------
  2247. void cmGlobalXCodeGenerator
  2248. ::CreateXCodeObjects(cmLocalGenerator* root,
  2249. std::vector<cmLocalGenerator*>&
  2250. generators)
  2251. {
  2252. this->ClearXCodeObjects();
  2253. this->RootObject = 0;
  2254. this->SourcesGroupChildren = 0;
  2255. this->ResourcesGroupChildren = 0;
  2256. this->MainGroupChildren = 0;
  2257. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2258. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2259. cmXCodeObject* developBuildStyle =
  2260. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2261. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2262. if(this->XcodeVersion == 15)
  2263. {
  2264. developBuildStyle->AddAttribute("name",
  2265. this->CreateString("Development"));
  2266. developBuildStyle->AddAttribute("buildSettings", group);
  2267. listObjs->AddObject(developBuildStyle);
  2268. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2269. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  2270. cmXCodeObject* deployBuildStyle =
  2271. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2272. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  2273. deployBuildStyle->AddAttribute("buildSettings", group);
  2274. listObjs->AddObject(deployBuildStyle);
  2275. }
  2276. else
  2277. {
  2278. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2279. {
  2280. cmXCodeObject* buildStyle =
  2281. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  2282. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2283. buildStyle->AddAttribute("name", this->CreateString(name));
  2284. buildStyle->SetComment(name);
  2285. cmXCodeObject* sgroup =
  2286. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2287. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  2288. buildStyle->AddAttribute("buildSettings", sgroup);
  2289. listObjs->AddObject(buildStyle);
  2290. }
  2291. }
  2292. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2293. this->MainGroupChildren =
  2294. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2295. mainGroup->AddAttribute("children", this->MainGroupChildren);
  2296. if(this->XcodeVersion == 15)
  2297. {
  2298. mainGroup->AddAttribute("refType", this->CreateString("4"));
  2299. }
  2300. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2301. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2302. this->SourcesGroupChildren =
  2303. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2304. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  2305. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  2306. if(this->XcodeVersion == 15)
  2307. {
  2308. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2309. }
  2310. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2311. this->MainGroupChildren->AddObject(sourcesGroup);
  2312. cmXCodeObject* resourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2313. this->ResourcesGroupChildren =
  2314. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2315. resourcesGroup->AddAttribute("name", this->CreateString("Resources"));
  2316. resourcesGroup->AddAttribute("children", this->ResourcesGroupChildren);
  2317. if(this->XcodeVersion == 15)
  2318. {
  2319. resourcesGroup->AddAttribute("refType", this->CreateString("4"));
  2320. }
  2321. resourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2322. this->MainGroupChildren->AddObject(resourcesGroup);
  2323. // now create the cmake groups
  2324. this->CreateGroups(root, generators);
  2325. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  2326. productGroup->AddAttribute("name", this->CreateString("Products"));
  2327. if(this->XcodeVersion == 15)
  2328. {
  2329. productGroup->AddAttribute("refType", this->CreateString("4"));
  2330. }
  2331. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  2332. cmXCodeObject* productGroupChildren =
  2333. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2334. productGroup->AddAttribute("children", productGroupChildren);
  2335. this->MainGroupChildren->AddObject(productGroup);
  2336. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  2337. this->RootObject->SetComment("Project object");
  2338. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2339. this->RootObject->AddAttribute("mainGroup",
  2340. this->CreateObjectReference(mainGroup));
  2341. this->RootObject->AddAttribute("buildSettings", group);
  2342. this->RootObject->AddAttribute("buildStyles", listObjs);
  2343. this->RootObject->AddAttribute("hasScannedForEncodings",
  2344. this->CreateString("0"));
  2345. if (this->XcodeVersion >= 30)
  2346. {
  2347. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2348. group->AddAttribute("BuildIndependentTargetsInParallel",
  2349. this->CreateString("YES"));
  2350. this->RootObject->AddAttribute("attributes", group);
  2351. if (this->XcodeVersion >= 31)
  2352. this->RootObject->AddAttribute("compatibilityVersion",
  2353. this->CreateString("Xcode 3.1"));
  2354. else
  2355. this->RootObject->AddAttribute("compatibilityVersion",
  2356. this->CreateString("Xcode 3.0"));
  2357. this->RootObject->AddAttribute("projectDirPath", this->CreateString(""));
  2358. }
  2359. // Point Xcode at the top of the source tree.
  2360. {
  2361. std::string proot = root->GetMakefile()->GetCurrentDirectory();
  2362. proot = this->ConvertToRelativeForXCode(proot.c_str());
  2363. this->RootObject->AddAttribute("projectRoot",
  2364. this->CreateString(proot.c_str()));
  2365. }
  2366. cmXCodeObject* configlist =
  2367. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2368. cmXCodeObject* buildConfigurations =
  2369. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2370. std::vector<cmXCodeObject*> configs;
  2371. if(this->XcodeVersion == 15)
  2372. {
  2373. cmXCodeObject* configDebug =
  2374. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2375. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2376. configs.push_back(configDebug);
  2377. cmXCodeObject* configRelease =
  2378. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2379. configRelease->AddAttribute("name", this->CreateString("Release"));
  2380. configs.push_back(configRelease);
  2381. }
  2382. else
  2383. {
  2384. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2385. {
  2386. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2387. cmXCodeObject* config =
  2388. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2389. config->AddAttribute("name", this->CreateString(name));
  2390. configs.push_back(config);
  2391. }
  2392. }
  2393. for(std::vector<cmXCodeObject*>::iterator c = configs.begin();
  2394. c != configs.end(); ++c)
  2395. {
  2396. buildConfigurations->AddObject(*c);
  2397. }
  2398. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2399. std::string comment = "Build configuration list for PBXProject ";
  2400. comment += " \"";
  2401. comment += this->CurrentProject;
  2402. comment += "\"";
  2403. configlist->SetComment(comment.c_str());
  2404. configlist->AddAttribute("defaultConfigurationIsVisible",
  2405. this->CreateString("0"));
  2406. configlist->AddAttribute("defaultConfigurationName",
  2407. this->CreateString("Debug"));
  2408. cmXCodeObject* buildSettings =
  2409. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2410. const char* osxArch =
  2411. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2412. const char* sysroot =
  2413. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2414. const char* sysrootDefault =
  2415. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT_DEFAULT");
  2416. const char* deploymentTarget =
  2417. this->CurrentMakefile->GetDefinition("CMAKE_OSX_DEPLOYMENT_TARGET");
  2418. if(osxArch && sysroot)
  2419. {
  2420. bool flagsUsed = false;
  2421. // recompute this as it may have been changed since enable language
  2422. this->Architectures.clear();
  2423. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2424. this->Architectures);
  2425. bool addArchFlag = true;
  2426. if(this->Architectures.size() == 1)
  2427. {
  2428. const char* archOrig =
  2429. this->
  2430. CurrentMakefile->GetSafeDefinition("CMAKE_OSX_ARCHITECTURES_DEFAULT");
  2431. if(this->Architectures[0] == archOrig)
  2432. {
  2433. addArchFlag = false;
  2434. }
  2435. }
  2436. if(addArchFlag)
  2437. {
  2438. flagsUsed = true;
  2439. buildSettings->AddAttribute("SDKROOT",
  2440. this->CreateString(sysroot));
  2441. std::string archString;
  2442. for( std::vector<std::string>::iterator i =
  2443. this->Architectures.begin();
  2444. i != this->Architectures.end(); ++i)
  2445. {
  2446. archString += *i;
  2447. archString += " ";
  2448. }
  2449. buildSettings->AddAttribute("ARCHS",
  2450. this->CreateString(archString.c_str()));
  2451. }
  2452. if(!flagsUsed && sysrootDefault &&
  2453. strcmp(sysroot, sysrootDefault) != 0)
  2454. {
  2455. buildSettings->AddAttribute("SDKROOT",
  2456. this->CreateString(sysroot));
  2457. }
  2458. }
  2459. if(deploymentTarget && *deploymentTarget)
  2460. {
  2461. buildSettings->AddAttribute("MACOSX_DEPLOYMENT_TARGET",
  2462. this->CreateString(deploymentTarget));
  2463. }
  2464. for( std::vector<cmXCodeObject*>::iterator i = configs.begin();
  2465. i != configs.end(); ++i)
  2466. {
  2467. (*i)->AddAttribute("buildSettings", buildSettings);
  2468. }
  2469. this->RootObject->AddAttribute("buildConfigurationList",
  2470. this->CreateObjectReference(configlist));
  2471. std::vector<cmXCodeObject*> targets;
  2472. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2473. i != generators.end(); ++i)
  2474. {
  2475. if(!this->IsExcluded(root, *i))
  2476. {
  2477. this->CreateXCodeTargets(*i, targets);
  2478. }
  2479. }
  2480. // loop over all targets and add link and depend info
  2481. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2482. i != targets.end(); ++i)
  2483. {
  2484. cmXCodeObject* t = *i;
  2485. this->AddDependAndLinkInformation(t);
  2486. }
  2487. // now create xcode depend hack makefile
  2488. this->CreateXCodeDependHackTarget(targets);
  2489. // now add all targets to the root object
  2490. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2491. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2492. i != targets.end(); ++i)
  2493. {
  2494. cmXCodeObject* t = *i;
  2495. allTargets->AddObject(t);
  2496. cmXCodeObject* productRef = t->GetObject("productReference");
  2497. if(productRef)
  2498. {
  2499. productGroupChildren->AddObject(productRef->GetObject());
  2500. }
  2501. }
  2502. this->RootObject->AddAttribute("targets", allTargets);
  2503. }
  2504. //----------------------------------------------------------------------------
  2505. void
  2506. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2507. std::vector<cmXCodeObject*>& targets)
  2508. {
  2509. cmGeneratedFileStream
  2510. makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2511. if(!makefileStream)
  2512. {
  2513. cmSystemTools::Error("Could not create",
  2514. this->CurrentXCodeHackMakefile.c_str());
  2515. return;
  2516. }
  2517. makefileStream.SetCopyIfDifferent(true);
  2518. // one more pass for external depend information not handled
  2519. // correctly by xcode
  2520. makefileStream << "# DO NOT EDIT\n";
  2521. makefileStream << "# This makefile makes sure all linkable targets are\n";
  2522. makefileStream << "# up-to-date with anything they link to, avoiding a "
  2523. "bug in XCode 1.5\n";
  2524. for(std::vector<std::string>::const_iterator
  2525. ct = this->CurrentConfigurationTypes.begin();
  2526. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2527. {
  2528. if(this->XcodeVersion < 21 || ct->empty())
  2529. {
  2530. makefileStream << "all: ";
  2531. }
  2532. else
  2533. {
  2534. makefileStream << "all." << *ct << ": ";
  2535. }
  2536. const char* configName = 0;
  2537. if(!ct->empty())
  2538. {
  2539. configName = ct->c_str();
  2540. }
  2541. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2542. i != targets.end(); ++i)
  2543. {
  2544. cmXCodeObject* target = *i;
  2545. cmTarget* t =target->GetTarget();
  2546. if(t->GetType() == cmTarget::EXECUTABLE ||
  2547. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2548. t->GetType() == cmTarget::MODULE_LIBRARY)
  2549. {
  2550. std::string tfull = t->GetFullPath(configName);
  2551. makefileStream << "\\\n\t" <<
  2552. this->ConvertToRelativeForMake(tfull.c_str());
  2553. }
  2554. }
  2555. makefileStream << "\n\n";
  2556. }
  2557. makefileStream
  2558. << "# For each target create a dummy rule "
  2559. "so the target does not have to exist\n";
  2560. std::set<cmStdString> emitted;
  2561. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2562. i != targets.end(); ++i)
  2563. {
  2564. cmXCodeObject* target = *i;
  2565. std::map<cmStdString, cmXCodeObject::StringVec> const& deplibs =
  2566. target->GetDependLibraries();
  2567. for(std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator ci
  2568. = deplibs.begin(); ci != deplibs.end(); ++ci)
  2569. {
  2570. for(cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2571. d != ci->second.end(); ++d)
  2572. {
  2573. if(emitted.insert(*d).second)
  2574. {
  2575. makefileStream <<
  2576. this->ConvertToRelativeForMake(d->c_str()) << ":\n";
  2577. }
  2578. }
  2579. }
  2580. }
  2581. makefileStream << "\n\n";
  2582. // Write rules to help Xcode relink things at the right time.
  2583. makefileStream <<
  2584. "# Rules to remove targets that are older than anything to which they\n"
  2585. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2586. "# does not seem to check these dependencies itself.\n";
  2587. for(std::vector<std::string>::const_iterator
  2588. ct = this->CurrentConfigurationTypes.begin();
  2589. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2590. {
  2591. const char* configName = 0;
  2592. if(!ct->empty())
  2593. {
  2594. configName = ct->c_str();
  2595. }
  2596. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2597. i != targets.end(); ++i)
  2598. {
  2599. cmXCodeObject* target = *i;
  2600. cmTarget* t =target->GetTarget();
  2601. if(t->GetType() == cmTarget::EXECUTABLE ||
  2602. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2603. t->GetType() == cmTarget::MODULE_LIBRARY)
  2604. {
  2605. // Create a rule for this target.
  2606. std::string tfull = t->GetFullPath(configName);
  2607. makefileStream << this->ConvertToRelativeForMake(tfull.c_str())
  2608. << ":";
  2609. // List dependencies if any exist.
  2610. std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator
  2611. x = target->GetDependLibraries().find(*ct);
  2612. if(x != target->GetDependLibraries().end())
  2613. {
  2614. std::vector<cmStdString> const& deplibs = x->second;
  2615. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  2616. d != deplibs.end(); ++d)
  2617. {
  2618. makefileStream << "\\\n\t" <<
  2619. this->ConvertToRelativeForMake(d->c_str());
  2620. }
  2621. }
  2622. // Write the action to remove the target if it is out of date.
  2623. makefileStream << "\n";
  2624. makefileStream << "\t/bin/rm -f "
  2625. << this->ConvertToRelativeForMake(tfull.c_str())
  2626. << "\n";
  2627. // if building for more than one architecture
  2628. // then remove those exectuables as well
  2629. if(this->Architectures.size() > 1)
  2630. {
  2631. std::string universal = t->GetDirectory();
  2632. universal += "/";
  2633. universal += this->CurrentProject;
  2634. universal += ".build/";
  2635. universal += configName;
  2636. universal += "/";
  2637. universal += t->GetFullName(configName);
  2638. universal += ".build/Objects-normal/";
  2639. for( std::vector<std::string>::iterator arch =
  2640. this->Architectures.begin();
  2641. arch != this->Architectures.end(); ++arch)
  2642. {
  2643. std::string universalFile = universal;
  2644. universalFile += *arch;
  2645. universalFile += "/";
  2646. universalFile += t->GetFullName(configName);
  2647. makefileStream << "\t/bin/rm -f "
  2648. <<
  2649. this->ConvertToRelativeForMake(universalFile.c_str())
  2650. << "\n";
  2651. }
  2652. }
  2653. makefileStream << "\n\n";
  2654. }
  2655. }
  2656. }
  2657. }
  2658. //----------------------------------------------------------------------------
  2659. void
  2660. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  2661. std::vector<cmLocalGenerator*>&
  2662. generators)
  2663. {
  2664. if(generators.size() == 0)
  2665. {
  2666. return;
  2667. }
  2668. // Skip local generators that are excluded from this project.
  2669. for(std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2670. g != generators.end(); ++g)
  2671. {
  2672. if(this->IsExcluded(root, *g))
  2673. {
  2674. continue;
  2675. }
  2676. }
  2677. this->CreateXCodeObjects(root,
  2678. generators);
  2679. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  2680. xcodeDir += "/";
  2681. xcodeDir += root->GetMakefile()->GetProjectName();
  2682. xcodeDir += ".xcode";
  2683. if(this->XcodeVersion > 20)
  2684. {
  2685. xcodeDir += "proj";
  2686. }
  2687. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2688. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2689. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2690. fout.SetCopyIfDifferent(true);
  2691. if(!fout)
  2692. {
  2693. return;
  2694. }
  2695. this->WriteXCodePBXProj(fout, root, generators);
  2696. this->ClearXCodeObjects();
  2697. }
  2698. //----------------------------------------------------------------------------
  2699. void
  2700. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  2701. cmLocalGenerator* ,
  2702. std::vector<cmLocalGenerator*>& )
  2703. {
  2704. fout << "// !$*UTF8*$!\n";
  2705. fout << "{\n";
  2706. cmXCodeObject::Indent(1, fout);
  2707. fout << "archiveVersion = 1;\n";
  2708. cmXCodeObject::Indent(1, fout);
  2709. fout << "classes = {\n";
  2710. cmXCodeObject::Indent(1, fout);
  2711. fout << "};\n";
  2712. cmXCodeObject::Indent(1, fout);
  2713. fout << "objectVersion = 39;\n";
  2714. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  2715. cmXCodeObject::Indent(1, fout);
  2716. fout << "rootObject = " << this->RootObject->GetId() << ";\n";
  2717. fout << "}\n";
  2718. }
  2719. //----------------------------------------------------------------------------
  2720. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  2721. const
  2722. {
  2723. entry.Name = this->GetName();
  2724. entry.Brief = "Generate XCode project files.";
  2725. entry.Full = "";
  2726. }
  2727. //----------------------------------------------------------------------------
  2728. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  2729. {
  2730. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2731. {
  2732. return cmSystemTools::ConvertToOutputPath(p);
  2733. }
  2734. else
  2735. {
  2736. std::string ret =
  2737. this->CurrentLocalGenerator->
  2738. ConvertToRelativePath(this->CurrentOutputDirectoryComponents, p);
  2739. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2740. }
  2741. }
  2742. //----------------------------------------------------------------------------
  2743. std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
  2744. {
  2745. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2746. {
  2747. return cmSystemTools::ConvertToOutputPath(p);
  2748. }
  2749. else
  2750. {
  2751. std::string ret =
  2752. this->CurrentLocalGenerator->
  2753. ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2754. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2755. }
  2756. }
  2757. //----------------------------------------------------------------------------
  2758. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
  2759. {
  2760. std::string ret = p;
  2761. if(ret.find(' ') != ret.npos)
  2762. {
  2763. std::string t = ret;
  2764. ret = "\"";
  2765. ret += t;
  2766. ret += "\"";
  2767. }
  2768. return ret;
  2769. }
  2770. //----------------------------------------------------------------------------
  2771. void cmGlobalXCodeGenerator::
  2772. GetTargetObjectFileDirectories(cmTarget* target,
  2773. std::vector<std::string>&
  2774. dirs)
  2775. {
  2776. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  2777. dir += "/";
  2778. dir += this->CurrentMakefile->GetProjectName();
  2779. dir += ".build/";
  2780. dir += this->GetCMakeCFGInitDirectory();
  2781. dir += "/";
  2782. if(target->GetType() != cmTarget::EXECUTABLE)
  2783. {
  2784. dir += "lib";
  2785. }
  2786. dir += target->GetName();
  2787. if(target->GetType() == cmTarget::STATIC_LIBRARY)
  2788. {
  2789. dir += ".a";
  2790. }
  2791. if(target->GetType() == cmTarget::SHARED_LIBRARY)
  2792. {
  2793. dir += ".dylib";
  2794. }
  2795. if(target->GetType() == cmTarget::MODULE_LIBRARY)
  2796. {
  2797. dir += ".so";
  2798. }
  2799. dir += ".build/Objects-normal/";
  2800. std::string dirsave = dir;
  2801. if(this->Architectures.size())
  2802. {
  2803. for(std::vector<std::string>::iterator i = this->Architectures.begin();
  2804. i != this->Architectures.end(); ++i)
  2805. {
  2806. dir += *i;
  2807. dirs.push_back(dir);
  2808. dir = dirsave;
  2809. }
  2810. }
  2811. else
  2812. {
  2813. dirs.push_back(dir);
  2814. }
  2815. }
  2816. //----------------------------------------------------------------------------
  2817. void
  2818. cmGlobalXCodeGenerator
  2819. ::AppendDirectoryForConfig(const char* prefix,
  2820. const char* config,
  2821. const char* suffix,
  2822. std::string& dir)
  2823. {
  2824. if(this->XcodeVersion > 20)
  2825. {
  2826. if(config)
  2827. {
  2828. dir += prefix;
  2829. dir += config;
  2830. dir += suffix;
  2831. }
  2832. }
  2833. }
  2834. //----------------------------------------------------------------------------
  2835. std::string cmGlobalXCodeGenerator::LookupFlags(const char* varNamePrefix,
  2836. const char* varNameLang,
  2837. const char* varNameSuffix,
  2838. const char* default_flags)
  2839. {
  2840. if(varNameLang)
  2841. {
  2842. std::string varName = varNamePrefix;
  2843. varName += varNameLang;
  2844. varName += varNameSuffix;
  2845. if(const char* varValue =
  2846. this->CurrentMakefile->GetDefinition(varName.c_str()))
  2847. {
  2848. if(*varValue)
  2849. {
  2850. return varValue;
  2851. }
  2852. }
  2853. }
  2854. return default_flags;
  2855. }
  2856. //----------------------------------------------------------------------------
  2857. void cmGlobalXCodeGenerator::AppendDefines(BuildObjectListOrString& defs,
  2858. const char* defines_list,
  2859. bool dflag)
  2860. {
  2861. // Skip this if there are no definitions.
  2862. if(!defines_list)
  2863. {
  2864. return;
  2865. }
  2866. // Expand the list of definitions.
  2867. std::vector<std::string> defines;
  2868. cmSystemTools::ExpandListArgument(defines_list, defines);
  2869. // GCC_PREPROCESSOR_DEFINITIONS is a space-separated list of definitions.
  2870. // We escape everything as follows:
  2871. // - Place each definition in single quotes ''
  2872. // - Escape a single quote as \\'
  2873. // - Escape a backslash as \\\\ since it itself is an escape
  2874. // Note that in the code below we need one more level of escapes for
  2875. // C string syntax in this source file.
  2876. for(std::vector<std::string>::const_iterator di = defines.begin();
  2877. di != defines.end(); ++di)
  2878. {
  2879. std::string def;
  2880. // Open single quote.
  2881. def += "'";
  2882. // Add -D flag if requested.
  2883. if(dflag)
  2884. {
  2885. def += "-D";
  2886. }
  2887. // Escaped definition string.
  2888. for(const char* c = di->c_str(); *c; ++c)
  2889. {
  2890. if(*c == '\'')
  2891. {
  2892. def += "\\\\'";
  2893. }
  2894. else if(*c == '\\')
  2895. {
  2896. def += "\\\\\\\\";
  2897. }
  2898. else
  2899. {
  2900. def += *c;
  2901. }
  2902. }
  2903. // Close single quote.
  2904. def += "'";
  2905. defs.Add(def.c_str());
  2906. }
  2907. }
  2908. //----------------------------------------------------------------------------
  2909. std::string
  2910. cmGlobalXCodeGenerator::ComputeInfoPListLocation(cmTarget& target)
  2911. {
  2912. std::string plist = target.GetMakefile()->GetCurrentOutputDirectory();
  2913. plist += cmake::GetCMakeFilesDirectory();
  2914. plist += "/";
  2915. plist += target.GetName();
  2916. plist += ".dir/Info.plist";
  2917. return plist;
  2918. }