cmGlobalXCodeGenerator.cxx 90 KB

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