cmGlobalXCodeGenerator.cxx 100 KB

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