cmGlobalXCodeGenerator.cxx 100 KB

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