cmGlobalXCodeGenerator.cxx 88 KB

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