cmGlobalXCodeGenerator.cxx 88 KB

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