cmGlobalXCodeGenerator.cxx 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596
  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. if(flags[pos + strlen(flag)] != ' ')
  826. {
  827. return flags;
  828. }
  829. retFlag = flag;
  830. // remove the flag
  831. flags[pos]=' ';
  832. flags[pos+1]=' ';
  833. char pos2 = flags[pos+2];
  834. // if the pos after the option
  835. if(pos2 != ' ' && pos2 != 0 )
  836. {
  837. retFlag += pos2;
  838. // remove the next part of the flag
  839. flags[pos+2] = ' ';
  840. }
  841. }
  842. return retFlag;
  843. }
  844. //----------------------------------------------------------------------------
  845. void
  846. cmGlobalXCodeGenerator::AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  847. cmTarget& target,
  848. std::vector<cmCustomCommand>
  849. const & commands,
  850. const char* name)
  851. {
  852. if(strcmp(name, "cmakeReRunPhase") == 0)
  853. {
  854. std::string cdir = this->CurrentMakefile->GetHomeOutputDirectory();
  855. cdir = this->ConvertToRelativeForMake(cdir.c_str());
  856. std::string makecmd = "make -C ";
  857. makecmd += cdir;
  858. makecmd += " -f ";
  859. makecmd +=
  860. this->ConvertToRelativeForMake(this->CurrentReRunCMakeMakefile.c_str());
  861. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  862. buildphase->AddAttribute("shellScript",
  863. this->CreateString(makecmd.c_str()));
  864. return;
  865. }
  866. std::map<cmStdString, cmStdString> multipleOutputPairs;
  867. std::string dir = this->CurrentMakefile->GetCurrentOutputDirectory();
  868. dir += "/CMakeScripts";
  869. cmSystemTools::MakeDirectory(dir.c_str());
  870. std::string makefile = dir;
  871. makefile += "/";
  872. makefile += target.GetName();
  873. makefile += "_";
  874. makefile += name;
  875. makefile += ".make";
  876. cmGeneratedFileStream makefileStream(makefile.c_str());
  877. if(!makefileStream)
  878. {
  879. return;
  880. }
  881. makefileStream.SetCopyIfDifferent(true);
  882. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  883. makefileStream << "# Custom rules for " << target.GetName() << "\n";
  884. // have all depend on all outputs
  885. makefileStream << "all: ";
  886. std::map<const cmCustomCommand*, cmStdString> tname;
  887. int count = 0;
  888. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  889. i != commands.end(); ++i)
  890. {
  891. cmCustomCommand const& cc = *i;
  892. if(!cc.GetCommandLines().empty())
  893. {
  894. const std::vector<std::string>& outputs = cc.GetOutputs();
  895. if(!outputs.empty())
  896. {
  897. for(std::vector<std::string>::const_iterator o = outputs.begin();
  898. o != outputs.end(); ++o)
  899. {
  900. makefileStream
  901. << "\\\n\t" << this->ConvertToRelativeForMake(o->c_str());
  902. }
  903. }
  904. else
  905. {
  906. cmOStringStream str;
  907. str << "_buildpart_" << count++ ;
  908. tname[&cc] = std::string(target.GetName()) + str.str();
  909. makefileStream << "\\\n\t" << tname[&cc];
  910. }
  911. }
  912. }
  913. makefileStream << "\n\n";
  914. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  915. i != commands.end(); ++i)
  916. {
  917. cmCustomCommand const& cc = *i;
  918. if(!cc.GetCommandLines().empty())
  919. {
  920. bool escapeOldStyle = cc.GetEscapeOldStyle();
  921. bool escapeAllowMakeVars = cc.GetEscapeAllowMakeVars();
  922. makefileStream << "\n#" << "Custom command rule: ";
  923. if(cc.GetComment())
  924. {
  925. makefileStream << cc.GetComment();
  926. }
  927. makefileStream << "\n";
  928. const std::vector<std::string>& outputs = cc.GetOutputs();
  929. if(!outputs.empty())
  930. {
  931. // There is at least one output. If there is more than one treat the
  932. // first as the primary output and make the rest depend on it.
  933. std::vector<std::string>::const_iterator o = outputs.begin();
  934. std::string primary_output =
  935. this->ConvertToRelativeForMake(o->c_str());
  936. for(++o; o != outputs.end(); ++o)
  937. {
  938. std::string current_output =
  939. this->ConvertToRelativeForMake(o->c_str());
  940. makefileStream << current_output << ": "
  941. << primary_output << "\n";
  942. multipleOutputPairs[current_output] = primary_output;
  943. }
  944. // Start the rule for the primary output.
  945. makefileStream << primary_output << ": ";
  946. }
  947. else
  948. {
  949. // There are no outputs. Use the generated force rule name.
  950. makefileStream << tname[&cc] << ": ";
  951. }
  952. for(std::vector<std::string>::const_iterator d =
  953. cc.GetDepends().begin();
  954. d != cc.GetDepends().end(); ++d)
  955. {
  956. if(!this->FindTarget(this->CurrentProject.c_str(),
  957. d->c_str()))
  958. {
  959. // if the depend is not a target but
  960. // is a full path then use it, if not then
  961. // just skip it
  962. if(cmSystemTools::FileIsFullPath(d->c_str()))
  963. {
  964. makefileStream << "\\\n" << this
  965. ->ConvertToRelativeForMake(d->c_str());
  966. }
  967. }
  968. else
  969. {
  970. // if the depend is a target then make
  971. // the target with the source that is a custom command
  972. // depend on the that target via a AddUtility call
  973. target.AddUtility(d->c_str());
  974. }
  975. }
  976. makefileStream << "\n";
  977. // Add each command line to the set of commands.
  978. for(cmCustomCommandLines::const_iterator cl =
  979. cc.GetCommandLines().begin();
  980. cl != cc.GetCommandLines().end(); ++cl)
  981. {
  982. // Build the command line in a single string.
  983. const cmCustomCommandLine& commandLine = *cl;
  984. std::string cmd2 = commandLine[0];
  985. cmSystemTools::ReplaceString(cmd2, "/./", "/");
  986. cmd2 = this->ConvertToRelativeForMake(cmd2.c_str());
  987. std::string cmd;
  988. if(cc.GetWorkingDirectory())
  989. {
  990. cmd += "cd ";
  991. cmd += this->ConvertToRelativeForMake(cc.GetWorkingDirectory());
  992. cmd += " && ";
  993. }
  994. cmd += cmd2;
  995. for(unsigned int j=1; j < commandLine.size(); ++j)
  996. {
  997. cmd += " ";
  998. if(escapeOldStyle)
  999. {
  1000. cmd += (this->CurrentLocalGenerator
  1001. ->EscapeForShellOldStyle(commandLine[j].c_str()));
  1002. }
  1003. else
  1004. {
  1005. cmd += (this->CurrentLocalGenerator->
  1006. EscapeForShell(commandLine[j].c_str(),
  1007. escapeAllowMakeVars));
  1008. }
  1009. }
  1010. makefileStream << "\t" << cmd.c_str() << "\n";
  1011. }
  1012. }
  1013. }
  1014. // Add a rule to deal with multiple outputs of custom commands.
  1015. if(!multipleOutputPairs.empty())
  1016. {
  1017. makefileStream <<
  1018. "\n"
  1019. "cmake_check_multiple_outputs:\n";
  1020. for(std::map<cmStdString, cmStdString>::const_iterator o =
  1021. multipleOutputPairs.begin(); o != multipleOutputPairs.end(); ++o)
  1022. {
  1023. makefileStream << "\t@if [ ! -f "
  1024. << o->first << " ]; then rm -f "
  1025. << o->second << "; fi\n";
  1026. }
  1027. }
  1028. std::string cdir = this->CurrentMakefile->GetCurrentOutputDirectory();
  1029. cdir = this->ConvertToRelativeForXCode(cdir.c_str());
  1030. std::string makecmd = "make -C ";
  1031. makecmd += cdir;
  1032. makecmd += " -f ";
  1033. makecmd += this->ConvertToRelativeForMake(makefile.c_str());
  1034. if(!multipleOutputPairs.empty())
  1035. {
  1036. makecmd += " cmake_check_multiple_outputs";
  1037. }
  1038. makecmd += " all";
  1039. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  1040. buildphase->AddAttribute("shellScript",
  1041. this->CreateString(makecmd.c_str()));
  1042. }
  1043. //----------------------------------------------------------------------------
  1044. void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
  1045. cmXCodeObject* buildSettings,
  1046. std::string& fileType,
  1047. std::string& productType,
  1048. std::string& productName,
  1049. const char* configName)
  1050. {
  1051. this->ConfigureOutputPaths();
  1052. std::string flags;
  1053. std::string defFlags;
  1054. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  1055. (target.GetType() == cmTarget::MODULE_LIBRARY));
  1056. if(shared)
  1057. {
  1058. defFlags += "-D";
  1059. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  1060. {
  1061. defFlags += custom_export_name;
  1062. }
  1063. else
  1064. {
  1065. std::string in = target.GetName();
  1066. in += "_EXPORTS";
  1067. defFlags += cmSystemTools::MakeCindentifier(in.c_str());
  1068. }
  1069. }
  1070. const char* lang = target.GetLinkerLanguage(this);
  1071. std::string cflags;
  1072. if(lang)
  1073. {
  1074. // for c++ projects get the c flags as well
  1075. if(strcmp(lang, "CXX") == 0)
  1076. {
  1077. this->CurrentLocalGenerator->AddLanguageFlags(cflags, "C", configName);
  1078. this->CurrentLocalGenerator->AddSharedFlags(cflags, lang, shared);
  1079. }
  1080. // Add language-specific flags.
  1081. this->CurrentLocalGenerator->AddLanguageFlags(flags, lang, configName);
  1082. // Add shared-library flags if needed.
  1083. this->CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
  1084. }
  1085. // Add define flags
  1086. this->CurrentLocalGenerator->
  1087. AppendFlags(defFlags,
  1088. this->CurrentMakefile->GetDefineFlags());
  1089. cmSystemTools::ReplaceString(defFlags, "\"", "\\\"");
  1090. cmSystemTools::ReplaceString(flags, "\"", "\\\"");
  1091. cmSystemTools::ReplaceString(cflags, "\"", "\\\"");
  1092. if(this->XcodeVersion > 15)
  1093. {
  1094. buildSettings->AddAttribute
  1095. ("GCC_PREPROCESSOR_DEFINITIONS",
  1096. this->CreateString("CMAKE_INTDIR=\\\\\\\"$(CONFIGURATION)\\\\\\\""));
  1097. }
  1098. std::string extraLinkOptions;
  1099. if(target.GetType() == cmTarget::EXECUTABLE)
  1100. {
  1101. extraLinkOptions =
  1102. this->CurrentMakefile->GetRequiredDefinition("CMAKE_EXE_LINKER_FLAGS");
  1103. }
  1104. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1105. {
  1106. extraLinkOptions = this->CurrentMakefile->
  1107. GetRequiredDefinition("CMAKE_SHARED_LINKER_FLAGS");
  1108. }
  1109. if(target.GetType() == cmTarget::MODULE_LIBRARY)
  1110. {
  1111. extraLinkOptions = this->CurrentMakefile->
  1112. GetRequiredDefinition("CMAKE_MODULE_LINKER_FLAGS");
  1113. }
  1114. const char* targetLinkFlags = target.GetProperty("LINK_FLAGS");
  1115. if(targetLinkFlags)
  1116. {
  1117. extraLinkOptions += " ";
  1118. extraLinkOptions += targetLinkFlags;
  1119. }
  1120. // The product name is the full name of the target for this configuration.
  1121. productName = target.GetFullName(configName);
  1122. // Get the product name components.
  1123. std::string pnprefix;
  1124. std::string pnbase;
  1125. std::string pnsuffix;
  1126. target.GetFullName(pnprefix, pnbase, pnsuffix, configName);
  1127. // Store the product name for all target types.
  1128. buildSettings->AddAttribute("PRODUCT_NAME",
  1129. this->CreateString(pnbase.c_str()));
  1130. // Set attributes to specify the proper name for the target.
  1131. if(target.GetType() == cmTarget::STATIC_LIBRARY ||
  1132. target.GetType() == cmTarget::SHARED_LIBRARY ||
  1133. target.GetType() == cmTarget::MODULE_LIBRARY ||
  1134. target.GetType() == cmTarget::EXECUTABLE)
  1135. {
  1136. std::string pndir = target.GetDirectory();
  1137. buildSettings->AddAttribute("SYMROOT",
  1138. this->CreateString(pndir.c_str()));
  1139. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  1140. this->CreateString(pnprefix.c_str()));
  1141. buildSettings->AddAttribute("EXECUTABLE_SUFFIX",
  1142. this->CreateString(pnsuffix.c_str()));
  1143. }
  1144. // Handle settings for each target type.
  1145. switch(target.GetType())
  1146. {
  1147. case cmTarget::STATIC_LIBRARY:
  1148. {
  1149. fileType = "archive.ar";
  1150. productType = "com.apple.product-type.library.static";
  1151. buildSettings->AddAttribute("LIBRARY_STYLE",
  1152. this->CreateString("STATIC"));
  1153. break;
  1154. }
  1155. case cmTarget::MODULE_LIBRARY:
  1156. {
  1157. buildSettings->AddAttribute("LIBRARY_STYLE",
  1158. this->CreateString("BUNDLE"));
  1159. if(this->XcodeVersion >= 22)
  1160. {
  1161. fileType = "compiled.mach-o.executable";
  1162. productType = "com.apple.product-type.tool";
  1163. buildSettings->AddAttribute("MACH_O_TYPE",
  1164. this->CreateString("mh_bundle"));
  1165. buildSettings->AddAttribute("GCC_DYNAMIC_NO_PIC",
  1166. this->CreateString("NO"));
  1167. // Add the flags to create an executable.
  1168. std::string createFlags =
  1169. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1170. if(!createFlags.empty())
  1171. {
  1172. extraLinkOptions += " ";
  1173. extraLinkOptions += createFlags;
  1174. }
  1175. }
  1176. else
  1177. {
  1178. fileType = "compiled.mach-o.dylib";
  1179. productType = "com.apple.product-type.library.dynamic";
  1180. // Add the flags to create a module.
  1181. std::string createFlags =
  1182. this->LookupFlags("CMAKE_SHARED_MODULE_CREATE_", lang, "_FLAGS",
  1183. "-bundle");
  1184. if(!createFlags.empty())
  1185. {
  1186. extraLinkOptions += " ";
  1187. extraLinkOptions += createFlags;
  1188. }
  1189. }
  1190. break;
  1191. }
  1192. case cmTarget::SHARED_LIBRARY:
  1193. {
  1194. fileType = "compiled.mach-o.dylib";
  1195. productType = "com.apple.product-type.library.dynamic";
  1196. buildSettings->AddAttribute("LIBRARY_STYLE",
  1197. this->CreateString("DYNAMIC"));
  1198. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  1199. this->CreateString("1"));
  1200. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  1201. this->CreateString("1"));
  1202. // Add the flags to create a shared library.
  1203. std::string createFlags =
  1204. this->LookupFlags("CMAKE_SHARED_LIBRARY_CREATE_", lang, "_FLAGS",
  1205. "-dynamiclib");
  1206. if(!createFlags.empty())
  1207. {
  1208. extraLinkOptions += " ";
  1209. extraLinkOptions += createFlags;
  1210. }
  1211. break;
  1212. }
  1213. case cmTarget::EXECUTABLE:
  1214. {
  1215. fileType = "compiled.mach-o.executable";
  1216. // Add the flags to create an executable.
  1217. std::string createFlags =
  1218. this->LookupFlags("CMAKE_", lang, "_LINK_FLAGS", "");
  1219. if(!createFlags.empty())
  1220. {
  1221. extraLinkOptions += " ";
  1222. extraLinkOptions += createFlags;
  1223. }
  1224. // Handle bundles and normal executables separately.
  1225. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  1226. {
  1227. productType = "com.apple.product-type.application";
  1228. std::string f1 =
  1229. this->CurrentMakefile->GetModulesFile("MacOSXBundleInfo.plist.in");
  1230. if ( f1.size() == 0 )
  1231. {
  1232. cmSystemTools::Error("could not find Mac OSX bundle template file.");
  1233. }
  1234. std::string f2 = this->CurrentMakefile->GetCurrentOutputDirectory();
  1235. f2 += "/Info.plist";
  1236. this->CurrentMakefile->ConfigureFile(f1.c_str(), f2.c_str(),
  1237. false, false, false);
  1238. std::string path =
  1239. this->ConvertToRelativeForXCode(f2.c_str());
  1240. buildSettings->AddAttribute("INFOPLIST_FILE",
  1241. this->CreateString(path.c_str()));
  1242. }
  1243. else
  1244. {
  1245. productType = "com.apple.product-type.tool";
  1246. }
  1247. }
  1248. break;
  1249. default:
  1250. break;
  1251. }
  1252. if(this->XcodeVersion >= 22)
  1253. {
  1254. buildSettings->AddAttribute("PREBINDING",
  1255. this->CreateString("NO"));
  1256. }
  1257. std::string dirs;
  1258. std::vector<std::string> includes;
  1259. this->CurrentLocalGenerator->GetIncludeDirectories(includes);
  1260. std::vector<std::string>::iterator i = includes.begin();
  1261. std::string fdirs;
  1262. std::set<cmStdString> emitted;
  1263. for(;i != includes.end(); ++i)
  1264. {
  1265. if(cmSystemTools::IsPathToFramework(i->c_str()))
  1266. {
  1267. std::string frameworkDir = *i;
  1268. frameworkDir += "/../";
  1269. frameworkDir = cmSystemTools::CollapseFullPath(frameworkDir.c_str());
  1270. if(emitted.insert(frameworkDir).second)
  1271. {
  1272. fdirs += this->XCodeEscapePath(frameworkDir.c_str());
  1273. fdirs += " ";
  1274. }
  1275. }
  1276. else
  1277. {
  1278. std::string incpath =
  1279. this->XCodeEscapePath(i->c_str());
  1280. dirs += incpath + " ";
  1281. }
  1282. }
  1283. std::vector<std::string>& frameworks = target.GetFrameworks();
  1284. if(frameworks.size())
  1285. {
  1286. for(std::vector<std::string>::iterator fmIt = frameworks.begin();
  1287. fmIt != frameworks.end(); ++fmIt)
  1288. {
  1289. if(emitted.insert(*fmIt).second)
  1290. {
  1291. fdirs += this->XCodeEscapePath(fmIt->c_str());
  1292. fdirs += " ";
  1293. }
  1294. }
  1295. }
  1296. if(fdirs.size())
  1297. {
  1298. buildSettings->AddAttribute("FRAMEWORK_SEARCH_PATHS",
  1299. this->CreateString(fdirs.c_str()));
  1300. }
  1301. if(dirs.size())
  1302. {
  1303. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  1304. this->CreateString(dirs.c_str()));
  1305. }
  1306. std::string oflagc = this->ExtractFlag("-O", cflags);
  1307. char optLevel[2];
  1308. optLevel[0] = '0';
  1309. optLevel[1] = 0;
  1310. if(oflagc.size() == 3)
  1311. {
  1312. optLevel[0] = oflagc[2];
  1313. }
  1314. if(oflagc.size() == 2)
  1315. {
  1316. optLevel[0] = '1';
  1317. }
  1318. std::string oflag = this->ExtractFlag("-O", flags);
  1319. if(oflag.size() == 3)
  1320. {
  1321. optLevel[0] = oflag[2];
  1322. }
  1323. if(oflag.size() == 2)
  1324. {
  1325. optLevel[0] = '1';
  1326. }
  1327. std::string gflagc = this->ExtractFlag("-g", cflags);
  1328. std::string gflag = this->ExtractFlag("-g", flags);
  1329. const char* debugStr = "YES";
  1330. if(gflagc.size() ==0 && gflag.size() == 0)
  1331. {
  1332. debugStr = "NO";
  1333. }
  1334. buildSettings->AddAttribute("GCC_GENERATE_DEBUGGING_SYMBOLS",
  1335. this->CreateString(debugStr));
  1336. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1337. this->CreateString(optLevel));
  1338. buildSettings->AddAttribute("OPTIMIZATION_CFLAGS",
  1339. this->CreateString(oflagc.c_str()));
  1340. buildSettings->AddAttribute("GCC_SYMBOLS_PRIVATE_EXTERN",
  1341. this->CreateString("NO"));
  1342. buildSettings->AddAttribute("GCC_INLINES_ARE_PRIVATE_EXTERN",
  1343. this->CreateString("NO"));
  1344. if(lang && strcmp(lang, "CXX") == 0)
  1345. {
  1346. flags += " ";
  1347. flags += defFlags;
  1348. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1349. this->CreateString(flags.c_str()));
  1350. cflags += " ";
  1351. cflags += defFlags;
  1352. buildSettings->AddAttribute("OTHER_CFLAGS",
  1353. this->CreateString(cflags.c_str()));
  1354. }
  1355. else
  1356. {
  1357. flags += " ";
  1358. flags += defFlags;
  1359. buildSettings->AddAttribute("OTHER_CFLAGS",
  1360. this->CreateString(flags.c_str()));
  1361. }
  1362. // Create the INSTALL_PATH attribute.
  1363. std::string install_name_dir;
  1364. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1365. {
  1366. // Get the install_name directory for the build tree.
  1367. install_name_dir = target.GetInstallNameDirForBuildTree(configName);
  1368. if(install_name_dir.empty())
  1369. {
  1370. // Xcode will not pass the -install_name option at all if INSTALL_PATH
  1371. // is not given or is empty. We must explicitly put the flag in the
  1372. // link flags to create an install_name with just the library soname.
  1373. extraLinkOptions += " -install_name ";
  1374. extraLinkOptions += productName;
  1375. }
  1376. else
  1377. {
  1378. // Convert to a path for the native build tool.
  1379. cmSystemTools::ConvertToUnixSlashes(install_name_dir);
  1380. // do not escape spaces on this since it is only a single path
  1381. }
  1382. }
  1383. buildSettings->AddAttribute("INSTALL_PATH",
  1384. this->CreateString(install_name_dir.c_str()));
  1385. buildSettings->AddAttribute("OTHER_LDFLAGS",
  1386. this->CreateString(extraLinkOptions.c_str()));
  1387. buildSettings->AddAttribute("OTHER_REZFLAGS",
  1388. this->CreateString(""));
  1389. buildSettings->AddAttribute("SECTORDER_FLAGS",
  1390. this->CreateString(""));
  1391. buildSettings->AddAttribute("USE_HEADERMAP",
  1392. this->CreateString("NO"));
  1393. buildSettings->AddAttribute("WARNING_CFLAGS",
  1394. this->CreateString(
  1395. "-Wmost -Wno-four-char-constants"
  1396. " -Wno-unknown-pragmas"));
  1397. }
  1398. //----------------------------------------------------------------------------
  1399. cmXCodeObject*
  1400. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  1401. {
  1402. cmXCodeObject* shellBuildPhase =
  1403. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1404. shellBuildPhase->AddAttribute("buildActionMask",
  1405. this->CreateString("2147483647"));
  1406. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1407. shellBuildPhase->AddAttribute("files", buildFiles);
  1408. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1409. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1410. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1411. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1412. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1413. this->CreateString("0"));
  1414. shellBuildPhase->AddAttribute("shellPath",
  1415. this->CreateString("/bin/sh"));
  1416. shellBuildPhase->AddAttribute("shellScript",
  1417. this->CreateString(
  1418. "# shell script goes here\nexit 0"));
  1419. cmXCodeObject* target =
  1420. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1421. target->SetComment(cmtarget.GetName());
  1422. cmXCodeObject* buildPhases =
  1423. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1424. this->CreateCustomCommands(buildPhases, 0, 0, 0, cmtarget);
  1425. target->AddAttribute("buildPhases", buildPhases);
  1426. cmXCodeObject* buildSettings =
  1427. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1428. std::string fileTypeString;
  1429. std::string productTypeString;
  1430. std::string productName;
  1431. const char* globalConfig = 0;
  1432. if(this->XcodeVersion > 20)
  1433. {
  1434. this->AddConfigurations(target, cmtarget);
  1435. }
  1436. else
  1437. {
  1438. globalConfig = this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1439. }
  1440. this->CreateBuildSettings(cmtarget,
  1441. buildSettings, fileTypeString,
  1442. productTypeString, productName, globalConfig);
  1443. target->AddAttribute("buildSettings", buildSettings);
  1444. cmXCodeObject* dependencies =
  1445. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1446. target->AddAttribute("dependencies", dependencies);
  1447. target->AddAttribute("name", this->CreateString(productName.c_str()));
  1448. target->AddAttribute("productName",this->CreateString(productName.c_str()));
  1449. target->SetTarget(&cmtarget);
  1450. return target;
  1451. }
  1452. //----------------------------------------------------------------------------
  1453. void cmGlobalXCodeGenerator::AddConfigurations(cmXCodeObject* target,
  1454. cmTarget& cmtarget)
  1455. {
  1456. std::string configTypes =
  1457. this->CurrentMakefile->GetRequiredDefinition("CMAKE_CONFIGURATION_TYPES");
  1458. std::vector<std::string> configVectorIn;
  1459. std::vector<std::string> configVector;
  1460. configVectorIn.push_back(configTypes);
  1461. cmSystemTools::ExpandList(configVectorIn, configVector);
  1462. cmXCodeObject* configlist =
  1463. this->CreateObject(cmXCodeObject::XCConfigurationList);
  1464. cmXCodeObject* buildConfigurations =
  1465. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1466. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  1467. std::string comment = "Build configuration list for ";
  1468. comment += cmXCodeObject::PBXTypeNames[target->GetIsA()];
  1469. comment += " \"";
  1470. comment += cmtarget.GetName();
  1471. comment += "\"";
  1472. configlist->SetComment(comment.c_str());
  1473. target->AddAttribute("buildConfigurationList",
  1474. this->CreateObjectReference(configlist));
  1475. for(unsigned int i = 0; i < configVector.size(); ++i)
  1476. {
  1477. cmXCodeObject* config =
  1478. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  1479. buildConfigurations->AddObject(config);
  1480. cmXCodeObject* buildSettings =
  1481. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1482. std::string fileTypeString;
  1483. std::string productTypeString;
  1484. std::string productName;
  1485. this->CreateBuildSettings(cmtarget,
  1486. buildSettings, fileTypeString,
  1487. productTypeString, productName,
  1488. configVector[i].c_str());
  1489. config->AddAttribute("name", this->CreateString(configVector[i].c_str()));
  1490. config->SetComment(configVector[i].c_str());
  1491. config->AddAttribute("buildSettings", buildSettings);
  1492. }
  1493. if(configVector.size())
  1494. {
  1495. configlist->AddAttribute("defaultConfigurationName",
  1496. this->CreateString(configVector[0].c_str()));
  1497. configlist->AddAttribute("defaultConfigurationIsVisible",
  1498. this->CreateString("0"));
  1499. }
  1500. }
  1501. //----------------------------------------------------------------------------
  1502. cmXCodeObject*
  1503. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  1504. cmXCodeObject* buildPhases)
  1505. {
  1506. cmXCodeObject* target =
  1507. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  1508. target->AddAttribute("buildPhases", buildPhases);
  1509. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1510. target->AddAttribute("buildRules", buildRules);
  1511. cmXCodeObject* buildSettings =
  1512. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1513. std::string fileTypeString;
  1514. std::string productTypeString;
  1515. std::string productName;
  1516. const char* globalConfig = 0;
  1517. if(this->XcodeVersion > 20)
  1518. {
  1519. this->AddConfigurations(target, cmtarget);
  1520. }
  1521. else
  1522. {
  1523. globalConfig = this->CurrentMakefile->GetDefinition("CMAKE_BUILD_TYPE");
  1524. }
  1525. this->CreateBuildSettings(cmtarget,
  1526. buildSettings, fileTypeString,
  1527. productTypeString, productName, globalConfig);
  1528. target->AddAttribute("buildSettings", buildSettings);
  1529. cmXCodeObject* dependencies =
  1530. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1531. target->AddAttribute("dependencies", dependencies);
  1532. target->AddAttribute("name", this->CreateString(productName.c_str()));
  1533. target->AddAttribute("productName",this->CreateString(productName.c_str()));
  1534. cmXCodeObject* fileRef =
  1535. this->CreateObject(cmXCodeObject::PBXFileReference);
  1536. fileRef->AddAttribute("explicitFileType",
  1537. this->CreateString(fileTypeString.c_str()));
  1538. fileRef->AddAttribute("path", this->CreateString(productName.c_str()));
  1539. fileRef->AddAttribute("refType", this->CreateString("0"));
  1540. fileRef->AddAttribute("sourceTree",
  1541. this->CreateString("BUILT_PRODUCTS_DIR"));
  1542. fileRef->SetComment(cmtarget.GetName());
  1543. target->AddAttribute("productReference",
  1544. this->CreateObjectReference(fileRef));
  1545. target->AddAttribute("productType",
  1546. this->CreateString(productTypeString.c_str()));
  1547. target->SetTarget(&cmtarget);
  1548. return target;
  1549. }
  1550. //----------------------------------------------------------------------------
  1551. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  1552. {
  1553. if(!t)
  1554. {
  1555. return 0;
  1556. }
  1557. for(std::vector<cmXCodeObject*>::iterator i = this->XCodeObjects.begin();
  1558. i != this->XCodeObjects.end(); ++i)
  1559. {
  1560. cmXCodeObject* o = *i;
  1561. if(o->GetTarget() == t)
  1562. {
  1563. return o;
  1564. }
  1565. }
  1566. return 0;
  1567. }
  1568. //----------------------------------------------------------------------------
  1569. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  1570. cmXCodeObject* dependTarget)
  1571. {
  1572. // make sure a target does not depend on itself
  1573. if(target == dependTarget)
  1574. {
  1575. return;
  1576. }
  1577. // now avoid circular references if dependTarget already
  1578. // depends on target then skip it. Circular references crashes
  1579. // xcode
  1580. cmXCodeObject* dependTargetDepends =
  1581. dependTarget->GetObject("dependencies");
  1582. if(dependTargetDepends)
  1583. {
  1584. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  1585. {
  1586. return;
  1587. }
  1588. }
  1589. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  1590. if(!targetdep)
  1591. {
  1592. cmXCodeObject* container =
  1593. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  1594. container->SetComment("PBXContainerItemProxy");
  1595. container->AddAttribute("containerPortal",
  1596. this->CreateObjectReference(this->RootObject));
  1597. container->AddAttribute("proxyType", this->CreateString("1"));
  1598. container->AddAttribute("remoteGlobalIDString",
  1599. this->CreateObjectReference(dependTarget));
  1600. container->AddAttribute("remoteInfo",
  1601. this->CreateString(
  1602. dependTarget->GetTarget()->GetName()));
  1603. targetdep =
  1604. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  1605. targetdep->SetComment("PBXTargetDependency");
  1606. targetdep->AddAttribute("target",
  1607. this->CreateObjectReference(dependTarget));
  1608. targetdep->AddAttribute("targetProxy",
  1609. this->CreateObjectReference(container));
  1610. dependTarget->SetPBXTargetDependency(targetdep);
  1611. }
  1612. cmXCodeObject* depends = target->GetObject("dependencies");
  1613. if(!depends)
  1614. {
  1615. cmSystemTools::
  1616. Error("target does not have dependencies attribute error..");
  1617. }
  1618. else
  1619. {
  1620. depends->AddUniqueObject(targetdep);
  1621. }
  1622. }
  1623. //----------------------------------------------------------------------------
  1624. void cmGlobalXCodeGenerator::AppendOrAddBuildSetting(cmXCodeObject* settings,
  1625. const char* attribute,
  1626. const char* value)
  1627. {
  1628. if(settings)
  1629. {
  1630. cmXCodeObject* attr = settings->GetObject(attribute);
  1631. if(!attr)
  1632. {
  1633. settings->AddAttribute(attribute, this->CreateString(value));
  1634. }
  1635. else
  1636. {
  1637. std::string oldValue = attr->GetString();
  1638. cmSystemTools::ReplaceString(oldValue, "\"", "");
  1639. oldValue += " ";
  1640. oldValue += value;
  1641. attr->SetString(oldValue.c_str());
  1642. }
  1643. }
  1644. }
  1645. //----------------------------------------------------------------------------
  1646. void cmGlobalXCodeGenerator
  1647. ::AppendBuildSettingAttribute(cmXCodeObject* target,
  1648. const char* attribute,
  1649. const char* value,
  1650. const char* configName)
  1651. {
  1652. if(this->XcodeVersion < 21)
  1653. {
  1654. // There is only one configuration. Add the setting to the buildSettings
  1655. // of the target.
  1656. this->AppendOrAddBuildSetting(target->GetObject("buildSettings"),
  1657. attribute, value);
  1658. }
  1659. else
  1660. {
  1661. // There are multiple configurations. Add the setting to the
  1662. // buildSettings of the configuration name given.
  1663. cmXCodeObject* configurationList =
  1664. target->GetObject("buildConfigurationList")->GetObject();
  1665. cmXCodeObject* buildConfigs =
  1666. configurationList->GetObject("buildConfigurations");
  1667. std::vector<cmXCodeObject*> list = buildConfigs->GetObjectList();
  1668. // each configuration and the target itself has a buildSettings in it
  1669. //list.push_back(target);
  1670. for(std::vector<cmXCodeObject*>::iterator i = list.begin();
  1671. i != list.end(); ++i)
  1672. {
  1673. if(configName)
  1674. {
  1675. if(strcmp((*i)->GetObject("name")->GetString(), configName) == 0)
  1676. {
  1677. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  1678. this->AppendOrAddBuildSetting(settings, attribute, value);
  1679. }
  1680. }
  1681. else
  1682. {
  1683. cmXCodeObject* settings = (*i)->GetObject("buildSettings");
  1684. this->AppendOrAddBuildSetting(settings, attribute, value);
  1685. }
  1686. }
  1687. }
  1688. }
  1689. //----------------------------------------------------------------------------
  1690. void cmGlobalXCodeGenerator
  1691. ::AddDependAndLinkInformation(cmXCodeObject* target)
  1692. {
  1693. cmTarget* cmtarget = target->GetTarget();
  1694. if(!cmtarget)
  1695. {
  1696. cmSystemTools::Error("Error no target on xobject\n");
  1697. return;
  1698. }
  1699. // Add dependencies on other CMake targets.
  1700. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1701. {
  1702. // Keep track of dependencies already listed.
  1703. std::set<cmStdString> emitted;
  1704. // A target should not depend on itself.
  1705. emitted.insert(cmtarget->GetName());
  1706. // Loop over all library dependencies.
  1707. const cmTarget::LinkLibraryVectorType& tlibs =
  1708. cmtarget->GetLinkLibraries();
  1709. for(cmTarget::LinkLibraryVectorType::const_iterator lib = tlibs.begin();
  1710. lib != tlibs.end(); ++lib)
  1711. {
  1712. // Don't emit the same library twice for this target.
  1713. if(emitted.insert(lib->first).second)
  1714. {
  1715. // Add this dependency.
  1716. cmTarget* t = this->FindTarget(this->CurrentProject.c_str(),
  1717. lib->first.c_str());
  1718. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1719. if(dptarget)
  1720. {
  1721. this->AddDependTarget(target, dptarget);
  1722. }
  1723. }
  1724. }
  1725. }
  1726. // write utility dependencies.
  1727. for(std::set<cmStdString>::const_iterator i
  1728. = cmtarget->GetUtilities().begin();
  1729. i != cmtarget->GetUtilities().end(); ++i)
  1730. {
  1731. cmTarget* t = this->FindTarget(this->CurrentProject.c_str(),
  1732. i->c_str());
  1733. // if the target is in this project then make target depend
  1734. // on it. It may not be in this project if this is a sub
  1735. // project from the top.
  1736. if(t)
  1737. {
  1738. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1739. if(dptarget)
  1740. {
  1741. this->AddDependTarget(target, dptarget);
  1742. }
  1743. else
  1744. {
  1745. std::string m = "Error Utility: ";
  1746. m += i->c_str();
  1747. m += "\n";
  1748. m += "cmtarget ";
  1749. if(t)
  1750. {
  1751. m += t->GetName();
  1752. }
  1753. m += "\n";
  1754. m += "Is on the target ";
  1755. m += cmtarget->GetName();
  1756. m += "\n";
  1757. m += "But it has no xcode target created yet??\n";
  1758. m += "Current project is ";
  1759. m += this->CurrentProject.c_str();
  1760. cmSystemTools::Error(m.c_str());
  1761. }
  1762. }
  1763. }
  1764. // Loop over configuration types and set per-configuration info.
  1765. for(std::vector<std::string>::iterator i =
  1766. this->CurrentConfigurationTypes.begin();
  1767. i != this->CurrentConfigurationTypes.end(); ++i)
  1768. {
  1769. // Get the current configuration name.
  1770. const char* configName = i->c_str();
  1771. if(!*configName)
  1772. {
  1773. configName = 0;
  1774. }
  1775. // Compute the link library and directory information.
  1776. std::vector<cmStdString> libNames;
  1777. std::vector<cmStdString> libDirs;
  1778. std::vector<cmStdString> fullPathLibs;
  1779. this->CurrentLocalGenerator->ComputeLinkInformation(*cmtarget, configName,
  1780. libNames, libDirs,
  1781. &fullPathLibs);
  1782. // Add dependencies directly on library files.
  1783. for(std::vector<cmStdString>::iterator j = fullPathLibs.begin();
  1784. j != fullPathLibs.end(); ++j)
  1785. {
  1786. target->AddDependLibrary(configName, j->c_str());
  1787. }
  1788. std::string linkDirs;
  1789. // add the library search paths
  1790. for(std::vector<cmStdString>::const_iterator libDir = libDirs.begin();
  1791. libDir != libDirs.end(); ++libDir)
  1792. {
  1793. if(libDir->size() && *libDir != "/usr/lib")
  1794. {
  1795. if(this->XcodeVersion > 15)
  1796. {
  1797. // now add the same one but append $(CONFIGURATION) to it:
  1798. linkDirs += " ";
  1799. linkDirs += this->XCodeEscapePath(libDir->c_str());
  1800. linkDirs += "/$(CONFIGURATION)";
  1801. }
  1802. linkDirs += " ";
  1803. linkDirs += this->XCodeEscapePath(libDir->c_str());
  1804. }
  1805. }
  1806. this->AppendBuildSettingAttribute(target, "LIBRARY_SEARCH_PATHS",
  1807. linkDirs.c_str(), configName);
  1808. // now add the link libraries
  1809. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1810. {
  1811. for(std::vector<cmStdString>::iterator lib = libNames.begin();
  1812. lib != libNames.end(); ++lib)
  1813. {
  1814. this->AppendBuildSettingAttribute(target, "OTHER_LDFLAGS",
  1815. lib->c_str(), configName);
  1816. }
  1817. }
  1818. }
  1819. }
  1820. //----------------------------------------------------------------------------
  1821. void cmGlobalXCodeGenerator::CreateGroups(cmLocalGenerator* root,
  1822. std::vector<cmLocalGenerator*>&
  1823. generators)
  1824. {
  1825. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  1826. i != generators.end(); ++i)
  1827. {
  1828. if(this->IsExcluded(root, *i))
  1829. {
  1830. continue;
  1831. }
  1832. cmMakefile* mf = (*i)->GetMakefile();
  1833. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  1834. cmTargets &tgts = mf->GetTargets();
  1835. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1836. {
  1837. cmTarget& cmtarget = l->second;
  1838. // add the soon to be generated Info.plist file as a source for a
  1839. // MACOSX_BUNDLE file
  1840. if(cmtarget.GetPropertyAsBool("MACOSX_BUNDLE"))
  1841. {
  1842. cmSourceFile file;
  1843. file.SetName("Info",
  1844. this->CurrentMakefile->GetCurrentOutputDirectory(),
  1845. "plist", false);
  1846. cmtarget.GetSourceFiles().push_back
  1847. (this->CurrentMakefile->AddSource(file));
  1848. }
  1849. std::vector<cmSourceFile*> & classes = cmtarget.GetSourceFiles();
  1850. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  1851. s != classes.end(); s++)
  1852. {
  1853. cmSourceFile* sf = *s;
  1854. // Add the file to the list of sources.
  1855. std::string const& source = sf->GetFullPath();
  1856. cmSourceGroup& sourceGroup =
  1857. mf->FindSourceGroup(source.c_str(), sourceGroups);
  1858. cmXCodeObject* pbxgroup =
  1859. this->CreateOrGetPBXGroup(cmtarget, &sourceGroup);
  1860. this->GroupMap[sf] = pbxgroup;
  1861. }
  1862. }
  1863. }
  1864. }
  1865. //----------------------------------------------------------------------------
  1866. cmXCodeObject* cmGlobalXCodeGenerator
  1867. ::CreateOrGetPBXGroup(cmTarget& cmtarget, cmSourceGroup* sg)
  1868. {
  1869. cmStdString s = cmtarget.GetName();
  1870. s += "/";
  1871. s += sg->GetName();
  1872. std::map<cmStdString, cmXCodeObject* >::iterator i =
  1873. this->GroupNameMap.find(s);
  1874. if(i != this->GroupNameMap.end())
  1875. {
  1876. return i->second;
  1877. }
  1878. i = this->TargetGroup.find(cmtarget.GetName());
  1879. cmXCodeObject* tgroup = 0;
  1880. if(i != this->TargetGroup.end())
  1881. {
  1882. tgroup = i->second;
  1883. }
  1884. else
  1885. {
  1886. tgroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1887. this->TargetGroup[cmtarget.GetName()] = tgroup;
  1888. cmXCodeObject* tgroupChildren =
  1889. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1890. tgroup->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1891. tgroup->AddAttribute("children", tgroupChildren);
  1892. if(this->XcodeVersion == 15)
  1893. {
  1894. tgroup->AddAttribute("refType", this->CreateString("4"));
  1895. }
  1896. tgroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1897. this->SourcesGroupChildren->AddObject(tgroup);
  1898. }
  1899. cmXCodeObject* tgroupChildren = tgroup->GetObject("children");
  1900. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  1901. cmXCodeObject* groupChildren =
  1902. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1903. group->AddAttribute("name", this->CreateString(sg->GetName()));
  1904. group->AddAttribute("children", groupChildren);
  1905. if(this->XcodeVersion == 15)
  1906. {
  1907. group->AddAttribute("refType", this->CreateString("4"));
  1908. }
  1909. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  1910. tgroupChildren->AddObject(group);
  1911. this->GroupNameMap[s] = group;
  1912. return group;
  1913. }
  1914. //----------------------------------------------------------------------------
  1915. void cmGlobalXCodeGenerator
  1916. ::CreateXCodeObjects(cmLocalGenerator* root,
  1917. std::vector<cmLocalGenerator*>&
  1918. generators)
  1919. {
  1920. this->ClearXCodeObjects();
  1921. this->RootObject = 0;
  1922. this->SourcesGroupChildren = 0;
  1923. this->MainGroupChildren = 0;
  1924. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1925. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  1926. cmXCodeObject* developBuildStyle =
  1927. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1928. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1929. if(this->XcodeVersion == 15)
  1930. {
  1931. developBuildStyle->AddAttribute("name",
  1932. this->CreateString("Development"));
  1933. developBuildStyle->AddAttribute("buildSettings", group);
  1934. listObjs->AddObject(developBuildStyle);
  1935. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1936. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  1937. cmXCodeObject* deployBuildStyle =
  1938. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1939. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  1940. deployBuildStyle->AddAttribute("buildSettings", group);
  1941. listObjs->AddObject(deployBuildStyle);
  1942. }
  1943. else
  1944. {
  1945. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  1946. {
  1947. cmXCodeObject* buildStyle =
  1948. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1949. const char* name = this->CurrentConfigurationTypes[i].c_str();
  1950. buildStyle->AddAttribute("name", this->CreateString(name));
  1951. buildStyle->SetComment(name);
  1952. cmXCodeObject* sgroup =
  1953. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1954. sgroup->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  1955. buildStyle->AddAttribute("buildSettings", sgroup);
  1956. listObjs->AddObject(buildStyle);
  1957. }
  1958. }
  1959. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1960. this->MainGroupChildren =
  1961. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1962. mainGroup->AddAttribute("children", this->MainGroupChildren);
  1963. if(this->XcodeVersion == 15)
  1964. {
  1965. mainGroup->AddAttribute("refType", this->CreateString("4"));
  1966. }
  1967. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1968. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1969. this->SourcesGroupChildren =
  1970. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1971. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  1972. sourcesGroup->AddAttribute("children", this->SourcesGroupChildren);
  1973. if(this->XcodeVersion == 15)
  1974. {
  1975. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  1976. }
  1977. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1978. this->MainGroupChildren->AddObject(sourcesGroup);
  1979. // now create the cmake groups
  1980. this->CreateGroups(root, generators);
  1981. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1982. productGroup->AddAttribute("name", this->CreateString("Products"));
  1983. if(this->XcodeVersion == 15)
  1984. {
  1985. productGroup->AddAttribute("refType", this->CreateString("4"));
  1986. }
  1987. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1988. cmXCodeObject* productGroupChildren =
  1989. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1990. productGroup->AddAttribute("children", productGroupChildren);
  1991. this->MainGroupChildren->AddObject(productGroup);
  1992. this->RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  1993. this->RootObject->SetComment("Project object");
  1994. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1995. this->RootObject->AddAttribute("mainGroup",
  1996. this->CreateObjectReference(mainGroup));
  1997. this->RootObject->AddAttribute("buildSettings", group);
  1998. this->RootObject->AddAttribute("buildStyles", listObjs);
  1999. this->RootObject->AddAttribute("hasScannedForEncodings",
  2000. this->CreateString("0"));
  2001. cmXCodeObject* configlist =
  2002. this->CreateObject(cmXCodeObject::XCConfigurationList);
  2003. cmXCodeObject* buildConfigurations =
  2004. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2005. std::vector<cmXCodeObject*> configs;
  2006. if(this->XcodeVersion == 15)
  2007. {
  2008. cmXCodeObject* configDebug =
  2009. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2010. configDebug->AddAttribute("name", this->CreateString("Debug"));
  2011. configs.push_back(configDebug);
  2012. cmXCodeObject* configRelease =
  2013. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2014. configRelease->AddAttribute("name", this->CreateString("Release"));
  2015. configs.push_back(configRelease);
  2016. }
  2017. else
  2018. {
  2019. for(unsigned int i = 0; i < this->CurrentConfigurationTypes.size(); ++i)
  2020. {
  2021. const char* name = this->CurrentConfigurationTypes[i].c_str();
  2022. cmXCodeObject* config =
  2023. this->CreateObject(cmXCodeObject::XCBuildConfiguration);
  2024. config->AddAttribute("name", this->CreateString(name));
  2025. configs.push_back(config);
  2026. }
  2027. }
  2028. for(std::vector<cmXCodeObject*>::iterator c = configs.begin();
  2029. c != configs.end(); ++c)
  2030. {
  2031. buildConfigurations->AddObject(*c);
  2032. }
  2033. configlist->AddAttribute("buildConfigurations", buildConfigurations);
  2034. std::string comment = "Build configuration list for PBXProject ";
  2035. comment += " \"";
  2036. comment += this->CurrentProject;
  2037. comment += "\"";
  2038. configlist->SetComment(comment.c_str());
  2039. configlist->AddAttribute("defaultConfigurationIsVisible",
  2040. this->CreateString("0"));
  2041. configlist->AddAttribute("defaultConfigurationName",
  2042. this->CreateString("Debug"));
  2043. cmXCodeObject* buildSettings =
  2044. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  2045. const char* osxArch =
  2046. this->CurrentMakefile->GetDefinition("CMAKE_OSX_ARCHITECTURES");
  2047. const char* sysroot =
  2048. this->CurrentMakefile->GetDefinition("CMAKE_OSX_SYSROOT");
  2049. if(osxArch && sysroot)
  2050. {
  2051. this->Architectures.clear();
  2052. cmSystemTools::ExpandListArgument(std::string(osxArch),
  2053. this->Architectures);
  2054. if(this->Architectures.size() > 1)
  2055. {
  2056. buildSettings->AddAttribute("SDKROOT",
  2057. this->CreateString(sysroot));
  2058. std::string archString;
  2059. for( std::vector<std::string>::iterator i =
  2060. this->Architectures.begin();
  2061. i != this->Architectures.end(); ++i)
  2062. {
  2063. archString += *i;
  2064. archString += " ";
  2065. }
  2066. buildSettings->AddAttribute("ARCHS",
  2067. this->CreateString(archString.c_str()));
  2068. }
  2069. }
  2070. for( std::vector<cmXCodeObject*>::iterator i = configs.begin();
  2071. i != configs.end(); ++i)
  2072. {
  2073. (*i)->AddAttribute("buildSettings", buildSettings);
  2074. }
  2075. this->RootObject->AddAttribute("buildConfigurationList",
  2076. this->CreateObjectReference(configlist));
  2077. std::vector<cmXCodeObject*> targets;
  2078. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  2079. i != generators.end(); ++i)
  2080. {
  2081. if(!this->IsExcluded(root, *i))
  2082. {
  2083. this->CreateXCodeTargets(*i, targets);
  2084. }
  2085. }
  2086. // loop over all targets and add link and depend info
  2087. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2088. i != targets.end(); ++i)
  2089. {
  2090. cmXCodeObject* t = *i;
  2091. this->AddDependAndLinkInformation(t);
  2092. }
  2093. // now create xcode depend hack makefile
  2094. this->CreateXCodeDependHackTarget(targets);
  2095. // now add all targets to the root object
  2096. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  2097. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2098. i != targets.end(); ++i)
  2099. {
  2100. cmXCodeObject* t = *i;
  2101. allTargets->AddObject(t);
  2102. cmXCodeObject* productRef = t->GetObject("productReference");
  2103. if(productRef)
  2104. {
  2105. productGroupChildren->AddObject(productRef->GetObject());
  2106. }
  2107. }
  2108. this->RootObject->AddAttribute("targets", allTargets);
  2109. }
  2110. //----------------------------------------------------------------------------
  2111. void
  2112. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  2113. std::vector<cmXCodeObject*>& targets)
  2114. {
  2115. cmGeneratedFileStream
  2116. makefileStream(this->CurrentXCodeHackMakefile.c_str());
  2117. if(!makefileStream)
  2118. {
  2119. cmSystemTools::Error("Could not create",
  2120. this->CurrentXCodeHackMakefile.c_str());
  2121. return;
  2122. }
  2123. makefileStream.SetCopyIfDifferent(true);
  2124. // one more pass for external depend information not handled
  2125. // correctly by xcode
  2126. makefileStream << "# DO NOT EDIT\n";
  2127. makefileStream << "# This makefile makes sure all linkable targets are \n";
  2128. makefileStream << "# up-to-date with anything they link to,avoiding a "
  2129. "bug in XCode 1.5\n";
  2130. for(std::vector<std::string>::const_iterator
  2131. ct = this->CurrentConfigurationTypes.begin();
  2132. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2133. {
  2134. if(this->XcodeVersion < 21 || ct->empty())
  2135. {
  2136. makefileStream << "all: ";
  2137. }
  2138. else
  2139. {
  2140. makefileStream << "all." << *ct << ": ";
  2141. }
  2142. const char* configName = 0;
  2143. if(!ct->empty())
  2144. {
  2145. configName = ct->c_str();
  2146. }
  2147. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2148. i != targets.end(); ++i)
  2149. {
  2150. cmXCodeObject* target = *i;
  2151. cmTarget* t =target->GetTarget();
  2152. if(t->GetType() == cmTarget::EXECUTABLE ||
  2153. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2154. t->GetType() == cmTarget::MODULE_LIBRARY)
  2155. {
  2156. makefileStream << "\\\n\t" <<
  2157. this->ConvertToRelativeForMake(
  2158. t->GetFullPath(configName).c_str());
  2159. }
  2160. }
  2161. makefileStream << "\n\n";
  2162. }
  2163. makefileStream
  2164. << "# For each target create a dummy rule "
  2165. "so the target does not have to exist\n";
  2166. std::set<cmStdString> emitted;
  2167. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2168. i != targets.end(); ++i)
  2169. {
  2170. cmXCodeObject* target = *i;
  2171. std::map<cmStdString, cmXCodeObject::StringVec> const& deplibs =
  2172. target->GetDependLibraries();
  2173. for(std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator ci
  2174. = deplibs.begin(); ci != deplibs.end(); ++ci)
  2175. {
  2176. for(cmXCodeObject::StringVec::const_iterator d = ci->second.begin();
  2177. d != ci->second.end(); ++d)
  2178. {
  2179. if(emitted.insert(*d).second)
  2180. {
  2181. makefileStream <<
  2182. this->ConvertToRelativeForMake(d->c_str()) << ":\n";
  2183. }
  2184. }
  2185. }
  2186. }
  2187. makefileStream << "\n\n";
  2188. // Write rules to help Xcode relink things at the right time.
  2189. makefileStream <<
  2190. "# Rules to remove targets that are older than anything to which they\n"
  2191. "# link. This forces Xcode to relink the targets from scratch. It\n"
  2192. "# does not seem to check these dependencies itself.\n";
  2193. for(std::vector<std::string>::const_iterator
  2194. ct = this->CurrentConfigurationTypes.begin();
  2195. ct != this->CurrentConfigurationTypes.end(); ++ct)
  2196. {
  2197. const char* configName = 0;
  2198. if(!ct->empty())
  2199. {
  2200. configName = ct->c_str();
  2201. }
  2202. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  2203. i != targets.end(); ++i)
  2204. {
  2205. cmXCodeObject* target = *i;
  2206. cmTarget* t =target->GetTarget();
  2207. if(t->GetType() == cmTarget::EXECUTABLE ||
  2208. t->GetType() == cmTarget::SHARED_LIBRARY ||
  2209. t->GetType() == cmTarget::MODULE_LIBRARY)
  2210. {
  2211. // Create a rule for this target.
  2212. std::string tfull = t->GetFullPath(configName);
  2213. makefileStream << this->ConvertToRelativeForMake(tfull.c_str())
  2214. << ":";
  2215. // List dependencies if any exist.
  2216. std::map<cmStdString, cmXCodeObject::StringVec>::const_iterator
  2217. x = target->GetDependLibraries().find(*ct);
  2218. if(x != target->GetDependLibraries().end())
  2219. {
  2220. std::vector<cmStdString> const& deplibs = x->second;
  2221. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  2222. d != deplibs.end(); ++d)
  2223. {
  2224. makefileStream << "\\\n\t" <<
  2225. this->ConvertToRelativeForMake(d->c_str());
  2226. }
  2227. }
  2228. // Write the action to remove the target if it is out of date.
  2229. makefileStream << "\n";
  2230. makefileStream << "\t/bin/rm -f "
  2231. << this->ConvertToRelativeForMake(tfull.c_str())
  2232. << "\n";
  2233. // if building for more than one architecture
  2234. // then remove those exectuables as well
  2235. if(this->Architectures.size() > 1)
  2236. {
  2237. std::string universal = t->GetDirectory();
  2238. universal += "/";
  2239. universal += this->CurrentMakefile->GetProjectName();
  2240. universal += ".build/";
  2241. universal += configName;
  2242. universal += "/";
  2243. universal += t->GetName();
  2244. universal += ".build/Objects-normal/";
  2245. for( std::vector<std::string>::iterator arch =
  2246. this->Architectures.begin();
  2247. arch != this->Architectures.end(); ++arch)
  2248. {
  2249. std::string universalFile = universal;
  2250. universalFile += *arch;
  2251. universalFile += "/";
  2252. universalFile += t->GetName();
  2253. makefileStream << "\t/bin/rm -f "
  2254. <<
  2255. this->ConvertToRelativeForMake(universalFile.c_str())
  2256. << "\n";
  2257. }
  2258. }
  2259. makefileStream << "\n\n";
  2260. }
  2261. }
  2262. }
  2263. }
  2264. //----------------------------------------------------------------------------
  2265. void
  2266. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  2267. std::vector<cmLocalGenerator*>&
  2268. generators)
  2269. {
  2270. if(generators.size() == 0)
  2271. {
  2272. return;
  2273. }
  2274. #if 1
  2275. // TODO: This block should be moved to a central location for all
  2276. // generators. It is duplicated in every generator.
  2277. for(std::vector<cmLocalGenerator*>::iterator g = generators.begin();
  2278. g != generators.end(); ++g)
  2279. {
  2280. if(this->IsExcluded(root, *g))
  2281. {
  2282. continue;
  2283. }
  2284. cmMakefile* mf = (*g)->GetMakefile();
  2285. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  2286. cmTargets &tgts = mf->GetTargets();
  2287. // Call TraceVSDependencies on all targets
  2288. for(cmTargets::iterator l = tgts.begin();
  2289. l != tgts.end(); l++)
  2290. {
  2291. // INCLUDE_EXTERNAL_MSPROJECT command only affects the workspace
  2292. // so don't build a projectfile for it
  2293. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  2294. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  2295. && (strncmp(l->first.c_str(), "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0))
  2296. {
  2297. cmTarget& target = l->second;
  2298. target.TraceVSDependencies(target.GetName(), mf);
  2299. }
  2300. }
  2301. // now for all custom commands that are not used directly in a
  2302. // target, add them to all targets in the current directory or
  2303. // makefile
  2304. std::set<cmStdString> banned;
  2305. banned.insert("ALL_BUILD");
  2306. banned.insert("XCODE_DEPEND_HELPER");
  2307. banned.insert("install");
  2308. std::vector<cmSourceFile*> & classesmf = mf->GetSourceFiles();
  2309. for(std::vector<cmSourceFile*>::const_iterator i = classesmf.begin();
  2310. i != classesmf.end(); i++)
  2311. {
  2312. if(cmCustomCommand* cc = (*i)->GetCustomCommand())
  2313. {
  2314. if(!cc->IsUsed())
  2315. {
  2316. for(cmTargets::iterator l = tgts.begin();
  2317. l != tgts.end(); l++)
  2318. {
  2319. if ((l->second.GetType() != cmTarget::INSTALL_FILES)
  2320. && (l->second.GetType() != cmTarget::INSTALL_PROGRAMS)
  2321. && (strncmp(l->first.c_str(),
  2322. "INCLUDE_EXTERNAL_MSPROJECT", 26) != 0)
  2323. && banned.find(l->second.GetName()) == banned.end())
  2324. {
  2325. cmTarget& target = l->second;
  2326. bool sameAsTarget = false;
  2327. // make sure we don't add a custom command that depends on
  2328. // this target
  2329. for(unsigned int k =0; k < cc->GetDepends().size(); k++)
  2330. {
  2331. if(cmSystemTools::GetFilenameName
  2332. (cc->GetDepends()[k]) == target.GetFullName())
  2333. {
  2334. sameAsTarget = true;
  2335. }
  2336. }
  2337. if(!sameAsTarget)
  2338. {
  2339. target.GetSourceFiles().push_back(*i);
  2340. }
  2341. }
  2342. }
  2343. }
  2344. }
  2345. }
  2346. }
  2347. #endif
  2348. this->CreateXCodeObjects(root,
  2349. generators);
  2350. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  2351. xcodeDir += "/";
  2352. xcodeDir += root->GetMakefile()->GetProjectName();
  2353. xcodeDir += ".xcode";
  2354. if(this->XcodeVersion > 20)
  2355. {
  2356. xcodeDir += "proj";
  2357. }
  2358. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  2359. std::string xcodeProjFile = xcodeDir + "/project.pbxproj";
  2360. cmGeneratedFileStream fout(xcodeProjFile.c_str());
  2361. fout.SetCopyIfDifferent(true);
  2362. if(!fout)
  2363. {
  2364. return;
  2365. }
  2366. this->WriteXCodePBXProj(fout, root, generators);
  2367. this->ClearXCodeObjects();
  2368. }
  2369. //----------------------------------------------------------------------------
  2370. void
  2371. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  2372. cmLocalGenerator* ,
  2373. std::vector<cmLocalGenerator*>& )
  2374. {
  2375. fout << "// !$*UTF8*$!\n";
  2376. fout << "{\n";
  2377. cmXCodeObject::Indent(1, fout);
  2378. fout << "archiveVersion = 1;\n";
  2379. cmXCodeObject::Indent(1, fout);
  2380. fout << "classes = {\n";
  2381. cmXCodeObject::Indent(1, fout);
  2382. fout << "};\n";
  2383. cmXCodeObject::Indent(1, fout);
  2384. fout << "objectVersion = 39;\n";
  2385. cmXCodeObject::PrintList(this->XCodeObjects, fout);
  2386. cmXCodeObject::Indent(1, fout);
  2387. fout << "rootObject = " << this->RootObject->GetId() << ";\n";
  2388. fout << "}\n";
  2389. }
  2390. //----------------------------------------------------------------------------
  2391. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  2392. const
  2393. {
  2394. entry.name = this->GetName();
  2395. entry.brief = "Generate XCode project files.";
  2396. entry.full = "";
  2397. }
  2398. //----------------------------------------------------------------------------
  2399. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  2400. {
  2401. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2402. {
  2403. return cmSystemTools::ConvertToOutputPath(p);
  2404. }
  2405. else
  2406. {
  2407. std::string ret =
  2408. this->ConvertToRelativePath(this->CurrentOutputDirectoryComponents, p);
  2409. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2410. }
  2411. }
  2412. //----------------------------------------------------------------------------
  2413. std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
  2414. {
  2415. if ( !this->CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  2416. {
  2417. return cmSystemTools::ConvertToOutputPath(p);
  2418. }
  2419. else
  2420. {
  2421. std::string ret =
  2422. this->ConvertToRelativePath(this->ProjectOutputDirectoryComponents, p);
  2423. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  2424. }
  2425. }
  2426. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
  2427. {
  2428. std::string ret = p;
  2429. if(ret.find(' ') != ret.npos)
  2430. {
  2431. std::string t = ret;
  2432. ret = "\\\"";
  2433. ret += t;
  2434. ret += "\\\"";
  2435. }
  2436. return ret;
  2437. }
  2438. //----------------------------------------------------------------------------
  2439. void
  2440. cmGlobalXCodeGenerator
  2441. ::AppendDirectoryForConfig(const char* prefix,
  2442. const char* config,
  2443. const char* suffix,
  2444. std::string& dir)
  2445. {
  2446. if(this->XcodeVersion > 20)
  2447. {
  2448. if(config)
  2449. {
  2450. dir += prefix;
  2451. dir += config;
  2452. dir += suffix;
  2453. }
  2454. }
  2455. }
  2456. //----------------------------------------------------------------------------
  2457. std::string cmGlobalXCodeGenerator::LookupFlags(const char* varNamePrefix,
  2458. const char* varNameLang,
  2459. const char* varNameSuffix,
  2460. const char* default_flags)
  2461. {
  2462. if(varNameLang)
  2463. {
  2464. std::string varName = varNamePrefix;
  2465. varName += varNameLang;
  2466. varName += varNameSuffix;
  2467. if(const char* varValue =
  2468. this->CurrentMakefile->GetDefinition(varName.c_str()))
  2469. {
  2470. if(*varValue)
  2471. {
  2472. return varValue;
  2473. }
  2474. }
  2475. }
  2476. return default_flags;
  2477. }