cmGlobalXCodeGenerator.cxx 88 KB

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