1
0

cmGlobalXCodeGenerator.cxx 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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 "cmake.h"
  19. #include "cmGeneratedFileStream.h"
  20. #include "cmSourceFile.h"
  21. #include "cmOrderLinkDirectories.h"
  22. #if defined(CMAKE_BUILD_WITH_CMAKE)
  23. #include "cmXMLParser.h"
  24. // parse the xml file storing the installed version of Xcode on
  25. // the machine
  26. class cmXcodeVersionParser : public cmXMLParser
  27. {
  28. public:
  29. void StartElement(const char* , const char** )
  30. {
  31. m_Data = "";
  32. }
  33. void EndElement(const char* name)
  34. {
  35. if(strcmp(name, "key") == 0)
  36. {
  37. m_Key = m_Data;
  38. }
  39. else if(strcmp(name, "string") == 0)
  40. {
  41. if(m_Key == "CFBundleShortVersionString")
  42. {
  43. m_Version = (int)(10.0 * atof(m_Data.c_str()));
  44. }
  45. }
  46. }
  47. void CharacterDataHandler(const char* data, int length)
  48. {
  49. m_Data.append(data, length);
  50. }
  51. int m_Version;
  52. std::string m_Key;
  53. std::string m_Data;
  54. };
  55. #endif
  56. //TODO
  57. // add OSX application stuff
  58. //----------------------------------------------------------------------------
  59. cmGlobalXCodeGenerator::cmGlobalXCodeGenerator()
  60. {
  61. m_FindMakeProgramFile = "CMakeFindXCode.cmake";
  62. m_RootObject = 0;
  63. m_MainGroupChildren = 0;
  64. m_SourcesGroupChildren = 0;
  65. m_CurrentMakefile = 0;
  66. m_CurrentLocalGenerator = 0;
  67. m_XcodeVersion = 15;
  68. }
  69. //----------------------------------------------------------------------------
  70. cmGlobalGenerator* cmGlobalXCodeGenerator::New()
  71. {
  72. #if defined(CMAKE_BUILD_WITH_CMAKE)
  73. cmXcodeVersionParser parser;
  74. parser.ParseFile("/Developer/Applications/Xcode.app/Contents/version.plist");
  75. if(parser.m_Version == 15)
  76. {
  77. return new cmGlobalXCodeGenerator;
  78. }
  79. else if (parser.m_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. return new cmGlobalXCode21Generator;
  86. #else
  87. std::cerr
  88. << "CMake should be built with cmake to use XCode, default to Xcode 1.5\n";
  89. return new cmGlobalXCodeGenerator;
  90. #endif
  91. }
  92. //----------------------------------------------------------------------------
  93. void cmGlobalXCodeGenerator::EnableLanguage(std::vector<std::string>const&
  94. lang,
  95. cmMakefile * mf)
  96. {
  97. mf->AddDefinition("XCODE","1");
  98. if(m_XcodeVersion == 15)
  99. {
  100. mf->AddDefinition("CMAKE_CFG_INTDIR",".");
  101. }
  102. else
  103. {
  104. mf->AddDefinition("CMAKE_CFG_INTDIR","$(CONFIGURATION)");
  105. }
  106. mf->AddDefinition("CMAKE_GENERATOR_CC", "gcc");
  107. mf->AddDefinition("CMAKE_GENERATOR_CXX", "g++");
  108. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  109. this->cmGlobalGenerator::EnableLanguage(lang, mf);
  110. }
  111. //----------------------------------------------------------------------------
  112. std::string cmGlobalXCodeGenerator::GenerateBuildCommand(const char* makeProgram,
  113. const char *projectName, const char *targetName, const char* config,
  114. bool ignoreErrors)
  115. {
  116. // Config is not used yet
  117. (void) config;
  118. (void) ignoreErrors;
  119. // now build the test
  120. if(makeProgram == 0 || !strlen(makeProgram))
  121. {
  122. cmSystemTools::Error(
  123. "Generator cannot find the appropriate make command.");
  124. return "";
  125. }
  126. std::string makeCommand =
  127. cmSystemTools::ConvertToOutputPath(makeProgram);
  128. std::string lowerCaseCommand = makeCommand;
  129. cmSystemTools::LowerCase(lowerCaseCommand);
  130. makeCommand += " -project ";
  131. makeCommand += projectName;
  132. makeCommand += ".xcode";
  133. bool clean = false;
  134. if ( targetName && strcmp(targetName, "clean") == 0 )
  135. {
  136. clean = true;
  137. targetName = "ALL_BUILD";
  138. }
  139. if(clean)
  140. {
  141. makeCommand += " clean";
  142. }
  143. else
  144. {
  145. makeCommand += " build";
  146. }
  147. makeCommand += " -target ";
  148. if (targetName && strlen(targetName))
  149. {
  150. makeCommand += targetName;
  151. }
  152. else
  153. {
  154. makeCommand += "ALL_BUILD";
  155. }
  156. if(m_XcodeVersion == 15)
  157. {
  158. makeCommand += " -buildstyle Development ";
  159. }
  160. else
  161. {
  162. makeCommand += " -configuration Debug";
  163. }
  164. return makeCommand;
  165. }
  166. //----------------------------------------------------------------------------
  167. void cmGlobalXCodeGenerator::ConfigureOutputPaths()
  168. {
  169. // Format the library and executable output paths.
  170. m_LibraryOutputPath =
  171. m_CurrentMakefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  172. if(m_LibraryOutputPath.size() == 0)
  173. {
  174. m_LibraryOutputPath = m_CurrentMakefile->GetCurrentOutputDirectory();
  175. }
  176. // make sure there is a trailing slash
  177. if(m_LibraryOutputPath.size() &&
  178. m_LibraryOutputPath[m_LibraryOutputPath.size()-1] != '/')
  179. {
  180. m_LibraryOutputPath += "/";
  181. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  182. {
  183. cmSystemTools::Error("Error creating directory ",
  184. m_LibraryOutputPath.c_str());
  185. }
  186. }
  187. m_CurrentMakefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  188. m_ExecutableOutputPath =
  189. m_CurrentMakefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  190. if(m_ExecutableOutputPath.size() == 0)
  191. {
  192. m_ExecutableOutputPath = m_CurrentMakefile->GetCurrentOutputDirectory();
  193. }
  194. // make sure there is a trailing slash
  195. if(m_ExecutableOutputPath.size() &&
  196. m_ExecutableOutputPath[m_ExecutableOutputPath.size()-1] != '/')
  197. {
  198. m_ExecutableOutputPath += "/";
  199. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  200. {
  201. cmSystemTools::Error("Error creating directory ",
  202. m_ExecutableOutputPath.c_str());
  203. }
  204. }
  205. }
  206. //----------------------------------------------------------------------------
  207. ///! Create a local generator appropriate to this Global Generator
  208. cmLocalGenerator *cmGlobalXCodeGenerator::CreateLocalGenerator()
  209. {
  210. cmLocalGenerator *lg = new cmLocalXCodeGenerator;
  211. lg->SetGlobalGenerator(this);
  212. return lg;
  213. }
  214. //----------------------------------------------------------------------------
  215. void cmGlobalXCodeGenerator::Generate()
  216. {
  217. this->cmGlobalGenerator::Generate();
  218. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  219. for(it = m_ProjectMap.begin(); it!= m_ProjectMap.end(); ++it)
  220. {
  221. cmLocalGenerator* root = it->second[0];
  222. m_CurrentProject = root->GetMakefile()->GetProjectName();
  223. this->SetCurrentLocalGenerator(root);
  224. m_OutputDir = m_CurrentMakefile->GetHomeOutputDirectory();
  225. m_OutputDir = cmSystemTools::CollapseFullPath(m_OutputDir.c_str());
  226. cmSystemTools::SplitPath(m_OutputDir.c_str(),
  227. m_ProjectOutputDirectoryComponents);
  228. m_CurrentLocalGenerator = root;
  229. // add ALL_BUILD, INSTALL, etc
  230. this->AddExtraTargets(root, it->second);
  231. // now create the project
  232. this->OutputXCodeProject(root, it->second);
  233. }
  234. }
  235. //----------------------------------------------------------------------------
  236. void
  237. cmGlobalXCodeGenerator::AddExtraTargets(cmLocalGenerator* root,
  238. std::vector<cmLocalGenerator*>& gens)
  239. {
  240. cmMakefile* mf = root->GetMakefile();
  241. // Add ALL_BUILD
  242. const char* no_output = 0;
  243. std::vector<std::string> no_depends;
  244. mf->AddUtilityCommand("ALL_BUILD", false, no_output, no_depends,
  245. "echo", "Build all projects");
  246. cmTarget* allbuild = mf->FindTarget("ALL_BUILD");
  247. // ADD install
  248. std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
  249. if(m_XcodeVersion == 15)
  250. {
  251. mf->AddUtilityCommand("install", false, no_output, no_depends,
  252. cmake_command.c_str(),
  253. "-P", "cmake_install.cmake");
  254. }
  255. else
  256. {
  257. mf->AddUtilityCommand("install", false, no_output, no_depends,
  258. cmake_command.c_str(),
  259. "-DBUILD_TYPE=$(CONFIGURATION)",
  260. "-P", "cmake_install.cmake");
  261. }
  262. const char* noall =
  263. mf->GetDefinition("CMAKE_SKIP_INSTALL_ALL_DEPENDENCY");
  264. if(!noall || cmSystemTools::IsOff(noall))
  265. {
  266. cmTarget* install = mf->FindTarget("install");
  267. install->AddUtility("ALL_BUILD");
  268. }
  269. // Add RUN_TESTS target if testing has been enabled
  270. std::string fname;
  271. fname = mf->GetStartOutputDirectory();
  272. fname += "/";
  273. fname += "DartTestfile.txt";
  274. if (cmSystemTools::FileExists(fname.c_str()))
  275. {
  276. std::string ctest_command =
  277. mf->GetRequiredDefinition("CMAKE_CTEST_COMMAND");
  278. mf->AddUtilityCommand("RUN_TESTS", false, no_output, no_depends,
  279. ctest_command.c_str());
  280. }
  281. // Add XCODE depend helper
  282. std::string dir = mf->GetCurrentOutputDirectory();
  283. m_CurrentXCodeHackMakefile = dir;
  284. m_CurrentXCodeHackMakefile += "/CMakeScripts";
  285. cmSystemTools::MakeDirectory(m_CurrentXCodeHackMakefile.c_str());
  286. m_CurrentXCodeHackMakefile += "/XCODE_DEPEND_HELPER.make";
  287. cmCustomCommandLine makecommand;
  288. makecommand.push_back("make");
  289. makecommand.push_back("-C");
  290. makecommand.push_back(dir.c_str());
  291. makecommand.push_back("-f");
  292. makecommand.push_back(m_CurrentXCodeHackMakefile.c_str());
  293. cmCustomCommandLines commandLines;
  294. commandLines.push_back(makecommand);
  295. mf->AddUtilityCommand("XCODE_DEPEND_HELPER", false, no_output, no_depends,
  296. commandLines);
  297. // Add Re-Run CMake rules
  298. this->CreateReRunCMakeFile(root);
  299. // now make the allbuild depend on all the non-utility targets
  300. // in the project
  301. for(std::vector<cmLocalGenerator*>::iterator i = gens.begin();
  302. i != gens.end(); ++i)
  303. {
  304. cmLocalGenerator* lg = *i;
  305. if(this->IsExcluded(root, *i))
  306. {
  307. continue;
  308. }
  309. cmTargets& tgts = lg->GetMakefile()->GetTargets();
  310. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  311. {
  312. cmTarget& target = l->second;
  313. // make all exe, shared libs and modules depend
  314. // on the XCODE_DEPEND_HELPER target
  315. if((target.GetType() == cmTarget::EXECUTABLE ||
  316. target.GetType() == cmTarget::SHARED_LIBRARY ||
  317. target.GetType() == cmTarget::MODULE_LIBRARY))
  318. {
  319. target.AddUtility("XCODE_DEPEND_HELPER");
  320. }
  321. if(target.IsInAll())
  322. {
  323. allbuild->AddUtility(target.GetName());
  324. }
  325. }
  326. }
  327. }
  328. //----------------------------------------------------------------------------
  329. void cmGlobalXCodeGenerator::CreateReRunCMakeFile(cmLocalGenerator* root)
  330. {
  331. cmMakefile* mf = root->GetMakefile();
  332. std::vector<std::string> lfiles = mf->GetListFiles();
  333. // sort the array
  334. std::sort(lfiles.begin(), lfiles.end(), std::less<std::string>());
  335. std::vector<std::string>::iterator new_end =
  336. std::unique(lfiles.begin(), lfiles.end());
  337. lfiles.erase(new_end, lfiles.end());
  338. std::string dir = mf->GetHomeOutputDirectory();
  339. m_CurrentReRunCMakeMakefile = dir;
  340. m_CurrentReRunCMakeMakefile += "/CMakeScripts";
  341. cmSystemTools::MakeDirectory(m_CurrentReRunCMakeMakefile.c_str());
  342. m_CurrentReRunCMakeMakefile += "/ReRunCMake.make";
  343. cmGeneratedFileStream makefileStream(m_CurrentReRunCMakeMakefile.c_str());
  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. m_TargetDoneSet.clear();
  362. for(unsigned int i = 0; i < m_XCodeObjects.size(); ++i)
  363. {
  364. delete m_XCodeObjects[i];
  365. }
  366. m_XCodeObjects.clear();
  367. m_GroupMap.clear();
  368. m_GroupNameMap.clear();
  369. m_TargetGroup.clear();
  370. }
  371. //----------------------------------------------------------------------------
  372. cmXCodeObject*
  373. cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::PBXType ptype)
  374. {
  375. cmXCodeObject* obj = new cmXCodeObject(ptype, cmXCodeObject::OBJECT);
  376. m_XCodeObjects.push_back(obj);
  377. return obj;
  378. }
  379. //----------------------------------------------------------------------------
  380. cmXCodeObject*
  381. cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
  382. {
  383. cmXCodeObject* obj = new cmXCodeObject(cmXCodeObject::None, type);
  384. m_XCodeObjects.push_back(obj);
  385. return obj;
  386. }
  387. //----------------------------------------------------------------------------
  388. cmXCodeObject*
  389. cmGlobalXCodeGenerator::CreateString(const char* s)
  390. {
  391. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::STRING);
  392. obj->SetString(s);
  393. return obj;
  394. }
  395. //----------------------------------------------------------------------------
  396. cmXCodeObject* cmGlobalXCodeGenerator::CreateObjectReference(cmXCodeObject* ref)
  397. {
  398. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::OBJECT_REF);
  399. obj->SetObject(ref);
  400. return obj;
  401. }
  402. //----------------------------------------------------------------------------
  403. cmXCodeObject*
  404. cmGlobalXCodeGenerator::CreateXCodeSourceFile(cmLocalGenerator* lg,
  405. cmSourceFile* sf)
  406. {
  407. std::string flags;
  408. // Add flags from source file properties.
  409. lg->AppendFlags(flags, sf->GetProperty("COMPILE_FLAGS"));
  410. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  411. cmXCodeObject* group = m_GroupMap[sf];
  412. cmXCodeObject* children = group->GetObject("children");
  413. children->AddObject(fileRef);
  414. // m_SourcesGroupChildren->AddObject(fileRef);
  415. cmXCodeObject* buildFile = this->CreateObject(cmXCodeObject::PBXBuildFile);
  416. buildFile->AddAttribute("fileRef", this->CreateObjectReference(fileRef));
  417. cmXCodeObject* settings = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  418. settings->AddAttribute("COMPILER_FLAGS", this->CreateString(flags.c_str()));
  419. buildFile->AddAttribute("settings", settings);
  420. fileRef->AddAttribute("fileEncoding", this->CreateString("4"));
  421. const char* lang =
  422. this->GetLanguageFromExtension(sf->GetSourceExtension().c_str());
  423. std::string sourcecode = "sourcecode";
  424. std::string ext = sf->GetSourceExtension();
  425. ext = cmSystemTools::LowerCase(ext);
  426. if(ext == "o")
  427. {
  428. sourcecode = "compiled.mach-o.objfile";
  429. }
  430. else if(ext == "mm")
  431. {
  432. sourcecode += ".cpp.objcpp";
  433. }
  434. else if(ext == "m")
  435. {
  436. sourcecode += ".cpp.objc";
  437. }
  438. else if(!lang)
  439. {
  440. sourcecode += ext;
  441. sourcecode += ".";
  442. sourcecode += ext;
  443. }
  444. else if(strcmp(lang, "C") == 0)
  445. {
  446. sourcecode += ".c.c";
  447. }
  448. else
  449. {
  450. sourcecode += ".cpp.cpp";
  451. }
  452. fileRef->AddAttribute("lastKnownFileType",
  453. this->CreateString(sourcecode.c_str()));
  454. std::string path =
  455. this->ConvertToRelativeForXCode(sf->GetFullPath().c_str());
  456. // std::string file =
  457. // cmSystemTools::RelativePath(m_CurrentMakefile->GetHomeDirectory(),
  458. // sf->GetFullPath().c_str());
  459. std::string dir;
  460. std::string file;
  461. cmSystemTools::SplitProgramPath(sf->GetFullPath().c_str(),
  462. dir, file);
  463. fileRef->AddAttribute("name", this->CreateString(file.c_str()));
  464. fileRef->AddAttribute("path", this->CreateString(path.c_str()));
  465. fileRef->AddAttribute("refType", this->CreateString("4"));
  466. if(path.size() > 1 && path[0] == '.' && path[1] == '.')
  467. {
  468. fileRef->AddAttribute("sourceTree", this->CreateString("<group>"));
  469. }
  470. else
  471. {
  472. fileRef->AddAttribute("sourceTree", this->CreateString("<absolute>"));
  473. }
  474. return buildFile;
  475. }
  476. //----------------------------------------------------------------------------
  477. bool cmGlobalXCodeGenerator::SpecialTargetEmitted(std::string const& tname)
  478. {
  479. if(tname == "ALL_BUILD" || tname == "XCODE_DEPEND_HELPER" ||
  480. tname == "install" || tname == "RUN_TESTS" )
  481. {
  482. if(m_TargetDoneSet.find(tname) != m_TargetDoneSet.end())
  483. {
  484. return true;
  485. }
  486. m_TargetDoneSet.insert(tname);
  487. return false;
  488. }
  489. return false;
  490. }
  491. void cmGlobalXCodeGenerator::SetCurrentLocalGenerator(cmLocalGenerator* gen)
  492. {
  493. m_CurrentLocalGenerator = gen;
  494. m_CurrentMakefile = gen->GetMakefile();
  495. std::string outdir =
  496. cmSystemTools::CollapseFullPath(m_CurrentMakefile->
  497. GetCurrentOutputDirectory());
  498. cmSystemTools::SplitPath(outdir.c_str(), m_CurrentOutputDirectoryComponents);
  499. }
  500. //----------------------------------------------------------------------------
  501. void
  502. cmGlobalXCodeGenerator::CreateXCodeTargets(cmLocalGenerator* gen,
  503. std::vector<cmXCodeObject*>&
  504. targets)
  505. {
  506. this->SetCurrentLocalGenerator(gen);
  507. cmTargets &tgts = gen->GetMakefile()->GetTargets();
  508. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  509. {
  510. cmTarget& cmtarget = l->second;
  511. // make sure ALL_BUILD, INSTALL, etc are only done once
  512. if(this->SpecialTargetEmitted(l->first.c_str()))
  513. {
  514. continue;
  515. }
  516. if(cmtarget.GetType() == cmTarget::UTILITY ||
  517. cmtarget.GetType() == cmTarget::INSTALL_FILES ||
  518. cmtarget.GetType() == cmTarget::INSTALL_PROGRAMS)
  519. {
  520. if(cmtarget.GetType() == cmTarget::UTILITY)
  521. {
  522. targets.push_back(this->CreateUtilityTarget(cmtarget));
  523. }
  524. continue;
  525. }
  526. // create source build phase
  527. cmXCodeObject* sourceBuildPhase =
  528. this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  529. sourceBuildPhase->AddAttribute("buildActionMask",
  530. this->CreateString("2147483647"));
  531. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  532. sourceBuildPhase->AddAttribute("files", buildFiles);
  533. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  534. this->CreateString("0"));
  535. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  536. // add all the sources
  537. std::vector<cmXCodeObject*> externalObjFiles;
  538. std::vector<cmXCodeObject*> headerFiles;
  539. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  540. i != classes.end(); ++i)
  541. {
  542. cmXCodeObject* xsf = this->CreateXCodeSourceFile(gen, *i);
  543. cmXCodeObject* fr = xsf->GetObject("fileRef");
  544. cmXCodeObject* filetype =
  545. fr->GetObject()->GetObject("lastKnownFileType");
  546. if(strcmp(filetype->GetString(), "\"compiled.mach-o.objfile\"") == 0)
  547. {
  548. externalObjFiles.push_back(xsf);
  549. }
  550. else if((*i)->GetPropertyAsBool("HEADER_FILE_ONLY"))
  551. {
  552. headerFiles.push_back(xsf);
  553. }
  554. else
  555. {
  556. buildFiles->AddObject(xsf);
  557. }
  558. }
  559. // create header build phase
  560. cmXCodeObject* headerBuildPhase =
  561. this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  562. headerBuildPhase->AddAttribute("buildActionMask",
  563. this->CreateString("2147483647"));
  564. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  565. for(std::vector<cmXCodeObject*>::iterator i = headerFiles.begin();
  566. i != headerFiles.end(); ++i)
  567. {
  568. buildFiles->AddObject(*i);
  569. }
  570. headerBuildPhase->AddAttribute("files", buildFiles);
  571. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  572. this->CreateString("0"));
  573. // create framework build phase
  574. cmXCodeObject* frameworkBuildPhase =
  575. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  576. frameworkBuildPhase->AddAttribute("buildActionMask",
  577. this->CreateString("2147483647"));
  578. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  579. frameworkBuildPhase->AddAttribute("files", buildFiles);
  580. for(std::vector<cmXCodeObject*>::iterator i = externalObjFiles.begin();
  581. i != externalObjFiles.end(); ++i)
  582. {
  583. buildFiles->AddObject(*i);
  584. }
  585. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  586. this->CreateString("0"));
  587. cmXCodeObject* buildPhases =
  588. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  589. this->CreateCustomCommands(buildPhases, sourceBuildPhase,
  590. headerBuildPhase, frameworkBuildPhase,
  591. cmtarget);
  592. targets.push_back(this->CreateXCodeTarget(l->second, buildPhases));
  593. }
  594. }
  595. //----------------------------------------------------------------------------
  596. cmXCodeObject*
  597. cmGlobalXCodeGenerator::CreateBuildPhase(const char* name,
  598. const char* name2,
  599. cmTarget& cmtarget,
  600. const std::vector<cmCustomCommand>&
  601. commands)
  602. {
  603. if(commands.size() == 0 && strcmp(name, "CMake ReRun") != 0)
  604. {
  605. return 0;
  606. }
  607. cmXCodeObject* buildPhase =
  608. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  609. buildPhase->AddAttribute("buildActionMask",
  610. this->CreateString("2147483647"));
  611. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  612. buildPhase->AddAttribute("files", buildFiles);
  613. buildPhase->AddAttribute("name",
  614. this->CreateString(name));
  615. buildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  616. this->CreateString("0"));
  617. buildPhase->AddAttribute("shellPath",
  618. this->CreateString("/bin/sh"));
  619. this->AddCommandsToBuildPhase(buildPhase, cmtarget, commands,
  620. name2);
  621. return buildPhase;
  622. }
  623. //----------------------------------------------------------------------------
  624. void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
  625. cmXCodeObject*
  626. sourceBuildPhase,
  627. cmXCodeObject*
  628. headerBuildPhase,
  629. cmXCodeObject*
  630. frameworkBuildPhase,
  631. cmTarget& cmtarget)
  632. {
  633. std::vector<cmCustomCommand> const & prebuild
  634. = cmtarget.GetPreBuildCommands();
  635. std::vector<cmCustomCommand> const & prelink
  636. = cmtarget.GetPreLinkCommands();
  637. std::vector<cmCustomCommand> const & postbuild
  638. = cmtarget.GetPostBuildCommands();
  639. cmtarget.TraceVSDependencies(cmtarget.GetName(), m_CurrentMakefile);
  640. std::vector<cmSourceFile*> &classes = cmtarget.GetSourceFiles();
  641. // add all the sources
  642. std::vector<cmCustomCommand> commands;
  643. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  644. i != classes.end(); ++i)
  645. {
  646. if((*i)->GetCustomCommand())
  647. {
  648. commands.push_back(*(*i)->GetCustomCommand());
  649. }
  650. }
  651. std::vector<cmCustomCommand> reruncom;
  652. cmXCodeObject* cmakeReRunPhase = this->CreateBuildPhase("CMake ReRun",
  653. "cmakeReRunPhase",
  654. cmtarget, reruncom);
  655. buildPhases->AddObject(cmakeReRunPhase);
  656. // create prebuild phase
  657. cmXCodeObject* cmakeRulesBuildPhase =
  658. this->CreateBuildPhase("CMake Rules",
  659. "cmakeRulesBuildPhase",
  660. cmtarget, commands);
  661. // create prebuild phase
  662. cmXCodeObject* preBuildPhase = this->CreateBuildPhase("CMake PreBuild Rules",
  663. "preBuildCommands",
  664. cmtarget, prebuild);
  665. // create prebuild phase
  666. cmXCodeObject* preLinkPhase = this->CreateBuildPhase("CMake PreLink Rules",
  667. "preLinkCommands",
  668. cmtarget, prelink);
  669. // create prebuild phase
  670. cmXCodeObject* postBuildPhase =
  671. this->CreateBuildPhase("CMake PostBuild Rules",
  672. "postBuildPhase",
  673. cmtarget, postbuild);
  674. // the order here is the order they will be built in
  675. if(preBuildPhase)
  676. {
  677. buildPhases->AddObject(preBuildPhase);
  678. }
  679. if(cmakeRulesBuildPhase)
  680. {
  681. buildPhases->AddObject(cmakeRulesBuildPhase);
  682. }
  683. if(sourceBuildPhase)
  684. {
  685. buildPhases->AddObject(sourceBuildPhase);
  686. }
  687. if(headerBuildPhase)
  688. {
  689. buildPhases->AddObject(headerBuildPhase);
  690. }
  691. if(preLinkPhase)
  692. {
  693. buildPhases->AddObject(preLinkPhase);
  694. }
  695. if(frameworkBuildPhase)
  696. {
  697. buildPhases->AddObject(frameworkBuildPhase);
  698. }
  699. if(postBuildPhase)
  700. {
  701. buildPhases->AddObject(postBuildPhase);
  702. }
  703. }
  704. //----------------------------------------------------------------------------
  705. void
  706. cmGlobalXCodeGenerator::AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  707. cmTarget& target,
  708. std::vector<cmCustomCommand>
  709. const & commands,
  710. const char* name)
  711. {
  712. if(strcmp(name, "cmakeReRunPhase") == 0)
  713. {
  714. std::string cdir = m_CurrentMakefile->GetHomeOutputDirectory();
  715. cdir = this->ConvertToRelativeForMake(cdir.c_str());
  716. std::string makecmd = "make -C ";
  717. makecmd += cdir;
  718. makecmd += " -f ";
  719. makecmd +=
  720. this->ConvertToRelativeForMake(m_CurrentReRunCMakeMakefile.c_str());
  721. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  722. buildphase->AddAttribute("shellScript",
  723. this->CreateString(makecmd.c_str()));
  724. return;
  725. }
  726. std::string dir = m_CurrentMakefile->GetCurrentOutputDirectory();
  727. dir += "/CMakeScripts";
  728. cmSystemTools::MakeDirectory(dir.c_str());
  729. std::string makefile = dir;
  730. makefile += "/";
  731. makefile += target.GetName();
  732. makefile += "_";
  733. makefile += name;
  734. makefile += ".make";
  735. cmGeneratedFileStream makefileStream(makefile.c_str());
  736. if(!makefileStream)
  737. {
  738. return;
  739. }
  740. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  741. makefileStream << "# Custom rules for " << target.GetName() << "\n";
  742. // have all depend on all outputs
  743. makefileStream << "all: ";
  744. std::map<const cmCustomCommand*, cmStdString> tname;
  745. int count = 0;
  746. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  747. i != commands.end(); ++i)
  748. {
  749. cmCustomCommand const& cc = *i;
  750. if(!cc.GetCommandLines().empty())
  751. {
  752. if(cc.GetOutput()[0])
  753. {
  754. makefileStream << "\\\n\t" << this->
  755. ConvertToRelativeForMake(cc.GetOutput());
  756. }
  757. else
  758. {
  759. char c = '1' + count++;
  760. tname[&cc] = std::string(target.GetName()) + c;
  761. makefileStream << "\\\n\t" << tname[&cc];
  762. }
  763. }
  764. }
  765. makefileStream << "\n\n";
  766. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  767. i != commands.end(); ++i)
  768. {
  769. cmCustomCommand const& cc = *i;
  770. if(!cc.GetCommandLines().empty())
  771. {
  772. makefileStream << "\n#" << "Custom command rule: " <<
  773. cc.GetComment() << "\n";
  774. if(cc.GetOutput()[0])
  775. {
  776. makefileStream << this
  777. ->ConvertToRelativeForMake(cc.GetOutput()) << ": ";
  778. }
  779. else
  780. {
  781. makefileStream << tname[&cc] << ": ";
  782. }
  783. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  784. d != cc.GetDepends().end(); ++d)
  785. {
  786. if(!this->FindTarget(m_CurrentProject.c_str(),
  787. d->c_str()))
  788. {
  789. // if the depend is not a target but
  790. // is a full path then use it, if not then
  791. // just skip it
  792. if(cmSystemTools::FileIsFullPath(d->c_str()))
  793. {
  794. makefileStream << "\\\n" << this
  795. ->ConvertToRelativeForMake(d->c_str());
  796. }
  797. }
  798. else
  799. {
  800. // if the depend is a target then make
  801. // the target with the source that is a custom command
  802. // depend on the that target via a AddUtility call
  803. target.AddUtility(d->c_str());
  804. }
  805. }
  806. makefileStream << "\n";
  807. // Add each command line to the set of commands.
  808. for(cmCustomCommandLines::const_iterator cl =
  809. cc.GetCommandLines().begin();
  810. cl != cc.GetCommandLines().end(); ++cl)
  811. {
  812. // Build the command line in a single string.
  813. const cmCustomCommandLine& commandLine = *cl;
  814. std::string cmd = commandLine[0];
  815. cmSystemTools::ReplaceString(cmd, "/./", "/");
  816. cmd = this->ConvertToRelativeForMake(cmd.c_str());
  817. for(unsigned int j=1; j < commandLine.size(); ++j)
  818. {
  819. cmd += " ";
  820. cmd += cmSystemTools::EscapeSpaces(commandLine[j].c_str());
  821. }
  822. makefileStream << "\t" << cmd.c_str() << "\n";
  823. }
  824. }
  825. }
  826. std::string cdir = m_CurrentMakefile->GetCurrentOutputDirectory();
  827. cdir = this->ConvertToRelativeForXCode(cdir.c_str());
  828. std::string makecmd = "make -C ";
  829. makecmd += cdir;
  830. makecmd += " -f ";
  831. makecmd += this->ConvertToRelativeForMake(makefile.c_str());
  832. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  833. buildphase->AddAttribute("shellScript", this->CreateString(makecmd.c_str()));
  834. }
  835. //----------------------------------------------------------------------------
  836. void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
  837. cmXCodeObject* buildSettings,
  838. std::string& fileType,
  839. std::string& productType,
  840. std::string& productName)
  841. {
  842. this->ConfigureOutputPaths();
  843. std::string flags;
  844. std::string defFlags;
  845. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  846. (target.GetType() == cmTarget::MODULE_LIBRARY));
  847. if(shared)
  848. {
  849. defFlags += "-D";
  850. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  851. {
  852. defFlags += custom_export_name;
  853. }
  854. else
  855. {
  856. std::string in = target.GetName();
  857. in += "_EXPORTS";
  858. defFlags += cmSystemTools::MakeCindentifier(in.c_str());
  859. }
  860. }
  861. const char* lang = target.GetLinkerLanguage(this);
  862. std::string cflags;
  863. if(lang)
  864. {
  865. // for c++ projects get the c flags as well
  866. if(strcmp(lang, "CXX") == 0)
  867. {
  868. m_CurrentLocalGenerator->AddLanguageFlags(cflags, "C");
  869. m_CurrentLocalGenerator->AddSharedFlags(cflags, lang, shared);
  870. }
  871. // Add language-specific flags.
  872. m_CurrentLocalGenerator->AddLanguageFlags(flags, lang);
  873. // Add shared-library flags if needed.
  874. m_CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
  875. }
  876. // Add define flags
  877. m_CurrentLocalGenerator->AppendFlags(defFlags,
  878. m_CurrentMakefile->GetDefineFlags());
  879. cmSystemTools::ReplaceString(defFlags, "\"", "\\\"");
  880. cmSystemTools::ReplaceString(flags, "\"", "\\\"");
  881. cmSystemTools::ReplaceString(cflags, "\"", "\\\"");
  882. if(m_XcodeVersion == 21)
  883. {
  884. defFlags += " -DCMAKE_INTDIR=\\\\\\\"$(CONFIGURATION)\\\\\\\" ";
  885. }
  886. productName = target.GetName();
  887. switch(target.GetType())
  888. {
  889. case cmTarget::STATIC_LIBRARY:
  890. {
  891. if(m_LibraryOutputPath.size())
  892. {
  893. buildSettings->AddAttribute("SYMROOT",
  894. this->CreateString
  895. (m_LibraryOutputPath.c_str()));
  896. }
  897. productName += ".a";
  898. std::string t = "lib";
  899. t += productName;
  900. productName = t;
  901. productType = "com.apple.product-type.library.static";
  902. fileType = "archive.ar";
  903. buildSettings->AddAttribute("LIBRARY_STYLE",
  904. this->CreateString("STATIC"));
  905. break;
  906. }
  907. case cmTarget::MODULE_LIBRARY:
  908. {
  909. if(m_LibraryOutputPath.size())
  910. {
  911. buildSettings->AddAttribute("SYMROOT",
  912. this->CreateString
  913. (m_LibraryOutputPath.c_str()));
  914. }
  915. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  916. this->CreateString("lib"));
  917. buildSettings->AddAttribute("EXECUTABLE_EXTENSION",
  918. this->CreateString("so"));
  919. buildSettings->AddAttribute("LIBRARY_STYLE",
  920. this->CreateString("BUNDLE"));
  921. productName += ".so";
  922. std::string t = "lib";
  923. t += productName;
  924. productName = t;
  925. buildSettings->AddAttribute("OTHER_LDFLAGS",
  926. this->CreateString("-bundle"));
  927. productType = "com.apple.product-type.library.dynamic";
  928. fileType = "compiled.mach-o.dylib";
  929. break;
  930. }
  931. case cmTarget::SHARED_LIBRARY:
  932. {
  933. if(m_LibraryOutputPath.size())
  934. {
  935. buildSettings->AddAttribute("SYMROOT",
  936. this->CreateString
  937. (m_LibraryOutputPath.c_str()));
  938. }
  939. buildSettings->AddAttribute("LIBRARY_STYLE",
  940. this->CreateString("DYNAMIC"));
  941. productName += ".dylib";
  942. std::string t = "lib";
  943. t += productName;
  944. productName = t;
  945. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  946. this->CreateString("1"));
  947. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  948. this->CreateString("1"));
  949. buildSettings->AddAttribute("OTHER_LDFLAGS",
  950. this->CreateString("-dynamiclib"));
  951. productType = "com.apple.product-type.library.dynamic";
  952. fileType = "compiled.mach-o.dylib";
  953. break;
  954. }
  955. case cmTarget::EXECUTABLE:
  956. {
  957. std::string symRoot;
  958. if(m_ExecutableOutputPath.size())
  959. {
  960. std::string path = m_ExecutableOutputPath;
  961. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  962. {
  963. path += target.GetName();
  964. path += ".app/Contents/MacOS/";
  965. }
  966. symRoot = path;
  967. }
  968. fileType = "compiled.mach-o.executable";
  969. if(target.GetPropertyAsBool("MACOSX_BUNDLE"))
  970. {
  971. if(symRoot.size() == 0)
  972. {
  973. symRoot = target.GetName();
  974. symRoot += ".app/Contents/MacOS/";
  975. }
  976. productType = "com.apple.product-type.tool";
  977. }
  978. else
  979. {
  980. productType = "com.apple.product-type.tool";
  981. }
  982. if(symRoot.size())
  983. {
  984. buildSettings->AddAttribute("SYMROOT",
  985. this->CreateString
  986. (symRoot.c_str()));
  987. }
  988. }
  989. break;
  990. case cmTarget::UTILITY:
  991. break;
  992. case cmTarget::INSTALL_FILES:
  993. break;
  994. case cmTarget::INSTALL_PROGRAMS:
  995. break;
  996. }
  997. std::string dirs;
  998. std::vector<std::string>& includes =
  999. m_CurrentMakefile->GetIncludeDirectories();
  1000. std::vector<std::string>::iterator i = includes.begin();
  1001. for(;i != includes.end(); ++i)
  1002. {
  1003. std::string incpath =
  1004. this->XCodeEscapePath(i->c_str());
  1005. dirs += incpath + " ";
  1006. }
  1007. if(dirs.size())
  1008. {
  1009. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  1010. this->CreateString(dirs.c_str()));
  1011. }
  1012. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  1013. this->CreateString("0"));
  1014. buildSettings->AddAttribute("INSTALL_PATH",
  1015. this->CreateString(""));
  1016. buildSettings->AddAttribute("OPTIMIZATION_CFLAGS",
  1017. this->CreateString(""));
  1018. if(lang && strcmp(lang, "CXX") == 0)
  1019. {
  1020. flags += " ";
  1021. flags += defFlags;
  1022. buildSettings->AddAttribute("OTHER_CPLUSPLUSFLAGS",
  1023. this->CreateString(flags.c_str()));
  1024. cflags += " ";
  1025. cflags += defFlags;
  1026. buildSettings->AddAttribute("OTHER_CFLAGS",
  1027. this->CreateString(cflags.c_str()));
  1028. }
  1029. else
  1030. {
  1031. flags += " ";
  1032. flags += defFlags;
  1033. buildSettings->AddAttribute("OTHER_CFLAGS",
  1034. this->CreateString(flags.c_str()));
  1035. }
  1036. buildSettings->AddAttribute("OTHER_LDFLAGS",
  1037. this->CreateString(""));
  1038. buildSettings->AddAttribute("OTHER_REZFLAGS",
  1039. this->CreateString(""));
  1040. buildSettings->AddAttribute("SECTORDER_FLAGS",
  1041. this->CreateString(""));
  1042. buildSettings->AddAttribute("USE_HEADERMAP",
  1043. this->CreateString("NO"));
  1044. buildSettings->AddAttribute("WARNING_CFLAGS",
  1045. this->CreateString(
  1046. "-Wmost -Wno-four-char-constants"
  1047. " -Wno-unknown-pragmas"));
  1048. std::string pname;
  1049. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  1050. {
  1051. pname = "lib";
  1052. }
  1053. pname += target.GetName();
  1054. buildSettings->AddAttribute("PRODUCT_NAME",
  1055. this->CreateString(pname.c_str()));
  1056. }
  1057. //----------------------------------------------------------------------------
  1058. cmXCodeObject*
  1059. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  1060. {
  1061. cmXCodeObject* shellBuildPhase =
  1062. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  1063. shellBuildPhase->AddAttribute("buildActionMask",
  1064. this->CreateString("2147483647"));
  1065. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1066. shellBuildPhase->AddAttribute("files", buildFiles);
  1067. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1068. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  1069. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1070. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  1071. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  1072. this->CreateString("0"));
  1073. shellBuildPhase->AddAttribute("shellPath",
  1074. this->CreateString("/bin/sh"));
  1075. shellBuildPhase->AddAttribute("shellScript",
  1076. this->CreateString(
  1077. "# shell script goes here\nexit 0"));
  1078. cmXCodeObject* target =
  1079. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  1080. cmXCodeObject* buildPhases =
  1081. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1082. this->CreateCustomCommands(buildPhases, 0, 0, 0, cmtarget);
  1083. target->AddAttribute("buildPhases", buildPhases);
  1084. cmXCodeObject* buildSettings =
  1085. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1086. std::string fileTypeString;
  1087. std::string productTypeString;
  1088. std::string productName;
  1089. this->CreateBuildSettings(cmtarget,
  1090. buildSettings, fileTypeString,
  1091. productTypeString, productName);
  1092. target->AddAttribute("buildSettings", buildSettings);
  1093. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1094. target->AddAttribute("dependencies", dependencies);
  1095. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1096. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1097. target->SetcmTarget(&cmtarget);
  1098. return target;
  1099. }
  1100. //----------------------------------------------------------------------------
  1101. cmXCodeObject*
  1102. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  1103. cmXCodeObject* buildPhases)
  1104. {
  1105. cmXCodeObject* target =
  1106. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  1107. target->AddAttribute("buildPhases", buildPhases);
  1108. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1109. target->AddAttribute("buildRules", buildRules);
  1110. cmXCodeObject* buildSettings =
  1111. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1112. std::string fileTypeString;
  1113. std::string productTypeString;
  1114. std::string productName;
  1115. this->CreateBuildSettings(cmtarget,
  1116. buildSettings, fileTypeString,
  1117. productTypeString, productName);
  1118. target->AddAttribute("buildSettings", buildSettings);
  1119. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1120. target->AddAttribute("dependencies", dependencies);
  1121. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1122. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  1123. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  1124. fileRef->AddAttribute("explicitFileType",
  1125. this->CreateString(fileTypeString.c_str()));
  1126. fileRef->AddAttribute("path", this->CreateString(productName.c_str()));
  1127. fileRef->AddAttribute("refType", this->CreateString("0"));
  1128. fileRef->AddAttribute("sourceTree",
  1129. this->CreateString("BUILT_PRODUCTS_DIR"));
  1130. target->AddAttribute("productReference",
  1131. this->CreateObjectReference(fileRef));
  1132. target->AddAttribute("productType",
  1133. this->CreateString(productTypeString.c_str()));
  1134. target->SetcmTarget(&cmtarget);
  1135. return target;
  1136. }
  1137. //----------------------------------------------------------------------------
  1138. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  1139. {
  1140. if(!t)
  1141. {
  1142. return 0;
  1143. }
  1144. for(std::vector<cmXCodeObject*>::iterator i = m_XCodeObjects.begin();
  1145. i != m_XCodeObjects.end(); ++i)
  1146. {
  1147. cmXCodeObject* o = *i;
  1148. if(o->GetcmTarget() == t)
  1149. {
  1150. return o;
  1151. }
  1152. }
  1153. return 0;
  1154. }
  1155. //----------------------------------------------------------------------------
  1156. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  1157. cmXCodeObject* dependTarget)
  1158. {
  1159. // make sure a target does not depend on itself
  1160. if(target == dependTarget)
  1161. {
  1162. return;
  1163. }
  1164. // now avoid circular references if dependTarget already
  1165. // depends on target then skip it. Circular references crashes
  1166. // xcode
  1167. cmXCodeObject* dependTargetDepends = dependTarget->GetObject("dependencies");
  1168. if(dependTargetDepends)
  1169. {
  1170. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  1171. {
  1172. return;
  1173. }
  1174. }
  1175. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  1176. if(!targetdep)
  1177. {
  1178. cmXCodeObject* container =
  1179. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  1180. container->AddAttribute("containerPortal",
  1181. this->CreateObjectReference(m_RootObject));
  1182. container->AddAttribute("proxyType", this->CreateString("1"));
  1183. container->AddAttribute("remoteGlobalIDString",
  1184. this->CreateObjectReference(dependTarget));
  1185. container->AddAttribute("remoteInfo",
  1186. this->CreateString(
  1187. dependTarget->GetcmTarget()->GetName()));
  1188. targetdep =
  1189. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  1190. targetdep->AddAttribute("target",
  1191. this->CreateObjectReference(dependTarget));
  1192. targetdep->AddAttribute("targetProxy",
  1193. this->CreateObjectReference(container));
  1194. dependTarget->SetPBXTargetDependency(targetdep);
  1195. }
  1196. cmXCodeObject* depends = target->GetObject("dependencies");
  1197. if(!depends)
  1198. {
  1199. cmSystemTools::
  1200. Error("target does not have dependencies attribute error..");
  1201. }
  1202. else
  1203. {
  1204. depends->AddUniqueObject(targetdep);
  1205. }
  1206. }
  1207. //----------------------------------------------------------------------------
  1208. void cmGlobalXCodeGenerator::AddLinkLibrary(cmXCodeObject* target,
  1209. const char* library,
  1210. cmTarget* dtarget)
  1211. {
  1212. if(dtarget)
  1213. {
  1214. target->AddDependLibrary(this->GetTargetFullPath(dtarget).c_str());
  1215. }
  1216. // if the library is not a full path then add it with a -l flag
  1217. // to the settings of the target
  1218. cmXCodeObject* settings = target->GetObject("buildSettings");
  1219. cmXCodeObject* ldflags = settings->GetObject("OTHER_LDFLAGS");
  1220. std::string link = ldflags->GetString();
  1221. cmSystemTools::ReplaceString(link, "\"", "");
  1222. cmsys::RegularExpression reg("^([ \t]*\\-[lLWRB])|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)|([ \t]*`)");
  1223. // if the library is not already in the form required by the compiler
  1224. // add a -l infront of the name
  1225. link += " ";
  1226. if(!reg.find(library))
  1227. {
  1228. link += "-l";
  1229. }
  1230. link += library;
  1231. ldflags->SetString(link.c_str());
  1232. }
  1233. //----------------------------------------------------------------------------
  1234. std::string cmGlobalXCodeGenerator::GetTargetFullPath(cmTarget* target)
  1235. {
  1236. std::string libPath;
  1237. cmXCodeObject* xtarget = this->FindXCodeTarget(target);
  1238. cmXCodeObject* bset = xtarget->GetObject("buildSettings");
  1239. cmXCodeObject* spath = bset->GetObject("SYMROOT");
  1240. if(m_XcodeVersion == 21)
  1241. {
  1242. libPath += "$(CONFIGURATION)/";
  1243. }
  1244. libPath = spath->GetString();
  1245. libPath = libPath.substr(1, libPath.size()-2);
  1246. if(target->GetType() == cmTarget::STATIC_LIBRARY)
  1247. {
  1248. libPath += "lib";
  1249. libPath += target->GetName();
  1250. libPath += ".a";
  1251. }
  1252. else if(target->GetType() == cmTarget::SHARED_LIBRARY)
  1253. {
  1254. libPath += "lib";
  1255. libPath += target->GetName();
  1256. libPath += ".dylib";
  1257. }
  1258. else
  1259. {
  1260. libPath += target->GetName();
  1261. }
  1262. return libPath;
  1263. }
  1264. //----------------------------------------------------------------------------
  1265. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  1266. {
  1267. cmTarget* cmtarget = target->GetcmTarget();
  1268. if(!cmtarget)
  1269. {
  1270. cmSystemTools::Error("Error no target on xobject\n");
  1271. return;
  1272. }
  1273. // compute the correct order for link libraries
  1274. cmOrderLinkDirectories orderLibs;
  1275. std::string ext =
  1276. m_CurrentMakefile->GetSafeDefinition("CMAKE_STATIC_LIBRARY_SUFFIX");
  1277. if(ext.size())
  1278. {
  1279. orderLibs.AddLinkExtension(ext.c_str());
  1280. }
  1281. ext =
  1282. m_CurrentMakefile->GetSafeDefinition("CMAKE_SHARED_LIBRARY_SUFFIX");
  1283. if(ext.size())
  1284. {
  1285. orderLibs.AddLinkExtension(ext.c_str());
  1286. }
  1287. ext =
  1288. m_CurrentMakefile->GetSafeDefinition("CMAKE_LINK_LIBRARY_SUFFIX");
  1289. if(ext.size())
  1290. {
  1291. orderLibs.AddLinkExtension(ext.c_str());
  1292. }
  1293. const char* targetLibrary = cmtarget->GetName();
  1294. if(cmtarget->GetType() == cmTarget::EXECUTABLE)
  1295. {
  1296. targetLibrary = 0;
  1297. }
  1298. orderLibs.SetLinkInformation(*cmtarget, cmTarget::GENERAL, targetLibrary);
  1299. orderLibs.DetermineLibraryPathOrder();
  1300. std::vector<cmStdString> libdirs;
  1301. std::vector<cmStdString> linkItems;
  1302. orderLibs.GetLinkerInformation(libdirs, linkItems);
  1303. std::string linkDirs;
  1304. // add the library search paths
  1305. for(std::vector<cmStdString>::const_iterator libDir = libdirs.begin();
  1306. libDir != libdirs.end(); ++libDir)
  1307. {
  1308. if(libDir->size() && *libDir != "/usr/lib")
  1309. {
  1310. if(m_XcodeVersion == 21)
  1311. {
  1312. // now add the same one but append $(CONFIGURATION) to it:
  1313. linkDirs += " ";
  1314. linkDirs += this->XCodeEscapePath(libDir->c_str());
  1315. linkDirs += "/$(CONFIGURATION)";
  1316. }
  1317. linkDirs += " ";
  1318. linkDirs += this->XCodeEscapePath(libDir->c_str());
  1319. }
  1320. }
  1321. cmXCodeObject* bset = target->GetObject("buildSettings");
  1322. if(bset)
  1323. {
  1324. bset->AddAttribute("LIBRARY_SEARCH_PATHS",
  1325. this->CreateString(linkDirs.c_str()));
  1326. }
  1327. // now add the link libraries
  1328. for(std::vector<cmStdString>::iterator lib = linkItems.begin();
  1329. lib != linkItems.end(); ++lib)
  1330. {
  1331. cmTarget* t = this->FindTarget(m_CurrentProject.c_str(),
  1332. lib->c_str());
  1333. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1334. if(dptarget)
  1335. {
  1336. this->AddDependTarget(target, dptarget);
  1337. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1338. {
  1339. this->AddLinkLibrary(target, t->GetName(), t);
  1340. }
  1341. }
  1342. else
  1343. {
  1344. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1345. {
  1346. this->AddLinkLibrary(target, lib->c_str());
  1347. }
  1348. }
  1349. }
  1350. // write utility dependencies.
  1351. for(std::set<cmStdString>::const_iterator i
  1352. = cmtarget->GetUtilities().begin();
  1353. i != cmtarget->GetUtilities().end(); ++i)
  1354. {
  1355. cmTarget* t = this->FindTarget(m_CurrentProject.c_str(),
  1356. i->c_str());
  1357. // if the target is in this project then make target depend
  1358. // on it. It may not be in this project if this is a sub
  1359. // project from the top.
  1360. if(t)
  1361. {
  1362. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1363. if(dptarget)
  1364. {
  1365. this->AddDependTarget(target, dptarget);
  1366. }
  1367. else
  1368. {
  1369. std::string m = "Error Utility: ";
  1370. m += i->c_str();
  1371. m += "\n";
  1372. m += "cmtarget ";
  1373. if(t)
  1374. {
  1375. m += t->GetName();
  1376. }
  1377. m += "\n";
  1378. m += "Is on the target ";
  1379. m += cmtarget->GetName();
  1380. m += "\n";
  1381. m += "But it has no xcode target created yet??\n";
  1382. m += "Current project is ";
  1383. m += m_CurrentProject.c_str();
  1384. cmSystemTools::Error(m.c_str());
  1385. }
  1386. }
  1387. }
  1388. std::vector<cmStdString> fullPathLibs;
  1389. orderLibs.GetFullPathLibraries(fullPathLibs);
  1390. for(std::vector<cmStdString>::iterator i = fullPathLibs.begin();
  1391. i != fullPathLibs.end(); ++i)
  1392. {
  1393. target->AddDependLibrary(i->c_str());
  1394. }
  1395. }
  1396. //----------------------------------------------------------------------------
  1397. void cmGlobalXCodeGenerator::CreateGroups(cmLocalGenerator* root,
  1398. std::vector<cmLocalGenerator*>&
  1399. generators)
  1400. {
  1401. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  1402. i != generators.end(); ++i)
  1403. {
  1404. if(this->IsExcluded(root, *i))
  1405. {
  1406. continue;
  1407. }
  1408. cmMakefile* mf = (*i)->GetMakefile();
  1409. std::vector<cmSourceGroup> sourceGroups = mf->GetSourceGroups();
  1410. cmTargets &tgts = mf->GetTargets();
  1411. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  1412. {
  1413. cmTarget& cmtarget = l->second;
  1414. std::vector<cmSourceFile*> & classes = cmtarget.GetSourceFiles();
  1415. for(std::vector<cmSourceFile*>::const_iterator s = classes.begin();
  1416. s != classes.end(); s++)
  1417. {
  1418. cmSourceFile* sf = *s;
  1419. // Add the file to the list of sources.
  1420. std::string const& source = sf->GetFullPath();
  1421. cmSourceGroup& sourceGroup =
  1422. mf->FindSourceGroup(source.c_str(), sourceGroups);
  1423. cmXCodeObject* pbxgroup = this->CreateOrGetPBXGroup(cmtarget, &sourceGroup);
  1424. m_GroupMap[sf] = pbxgroup;
  1425. }
  1426. }
  1427. }
  1428. }
  1429. //----------------------------------------------------------------------------
  1430. cmXCodeObject* cmGlobalXCodeGenerator::CreateOrGetPBXGroup(cmTarget& cmtarget,
  1431. cmSourceGroup* sg)
  1432. {
  1433. cmStdString s = cmtarget.GetName();
  1434. s += "/";
  1435. s += sg->GetName();
  1436. std::map<cmStdString, cmXCodeObject* >::iterator i = m_GroupNameMap.find(s);
  1437. if(i != m_GroupNameMap.end())
  1438. {
  1439. return i->second;
  1440. }
  1441. i = m_TargetGroup.find(cmtarget.GetName());
  1442. cmXCodeObject* tgroup = 0;
  1443. if(i != m_TargetGroup.end())
  1444. {
  1445. tgroup = i->second;
  1446. }
  1447. else
  1448. {
  1449. tgroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1450. m_TargetGroup[cmtarget.GetName()] = tgroup;
  1451. cmXCodeObject* tgroupChildren =
  1452. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1453. tgroup->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  1454. tgroup->AddAttribute("children", tgroupChildren);
  1455. tgroup->AddAttribute("refType", this->CreateString("4"));
  1456. tgroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1457. m_SourcesGroupChildren->AddObject(tgroup);
  1458. }
  1459. cmXCodeObject* tgroupChildren = tgroup->GetObject("children");
  1460. cmXCodeObject* group = this->CreateObject(cmXCodeObject::PBXGroup);
  1461. cmXCodeObject* groupChildren =
  1462. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1463. group->AddAttribute("name", this->CreateString(sg->GetName()));
  1464. group->AddAttribute("children", groupChildren);
  1465. group->AddAttribute("refType", this->CreateString("4"));
  1466. group->AddAttribute("sourceTree", this->CreateString("<group>"));
  1467. tgroupChildren->AddObject(group);
  1468. m_GroupNameMap[s] = group;
  1469. return group;
  1470. }
  1471. //----------------------------------------------------------------------------
  1472. void cmGlobalXCodeGenerator::CreateXCodeObjects(cmLocalGenerator* root,
  1473. std::vector<cmLocalGenerator*>&
  1474. generators
  1475. )
  1476. {
  1477. this->ClearXCodeObjects();
  1478. m_RootObject = 0;
  1479. m_SourcesGroupChildren = 0;
  1480. m_MainGroupChildren = 0;
  1481. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1482. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  1483. cmXCodeObject* developBuildStyle =
  1484. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1485. developBuildStyle->AddAttribute("name", this->CreateString("Development"));
  1486. developBuildStyle->AddAttribute("buildSettings", group);
  1487. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1488. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  1489. cmXCodeObject* deployBuildStyle =
  1490. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1491. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  1492. deployBuildStyle->AddAttribute("buildSettings", group);
  1493. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1494. listObjs->AddObject(developBuildStyle);
  1495. listObjs->AddObject(deployBuildStyle);
  1496. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1497. m_MainGroupChildren =
  1498. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1499. mainGroup->AddAttribute("children", m_MainGroupChildren);
  1500. mainGroup->AddAttribute("refType", this->CreateString("4"));
  1501. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1502. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1503. m_SourcesGroupChildren =
  1504. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1505. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  1506. sourcesGroup->AddAttribute("children", m_SourcesGroupChildren);
  1507. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  1508. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1509. m_MainGroupChildren->AddObject(sourcesGroup);
  1510. // now create the cmake groups
  1511. this->CreateGroups(root, generators);
  1512. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1513. productGroup->AddAttribute("name", this->CreateString("Products"));
  1514. productGroup->AddAttribute("refType", this->CreateString("4"));
  1515. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1516. cmXCodeObject* productGroupChildren =
  1517. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1518. productGroup->AddAttribute("children", productGroupChildren);
  1519. m_MainGroupChildren->AddObject(productGroup);
  1520. m_RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  1521. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1522. m_RootObject->AddAttribute("mainGroup",
  1523. this->CreateObjectReference(mainGroup));
  1524. m_RootObject->AddAttribute("buildSettings", group);
  1525. m_RootObject->AddAttribute("buildSyles", listObjs);
  1526. m_RootObject->AddAttribute("hasScannedForEncodings",
  1527. this->CreateString("0"));
  1528. std::vector<cmXCodeObject*> targets;
  1529. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  1530. i != generators.end(); ++i)
  1531. {
  1532. if(!this->IsExcluded(root, *i))
  1533. {
  1534. this->CreateXCodeTargets(*i, targets);
  1535. }
  1536. }
  1537. // loop over all targets and add link and depend info
  1538. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1539. i != targets.end(); ++i)
  1540. {
  1541. cmXCodeObject* t = *i;
  1542. this->AddDependAndLinkInformation(t);
  1543. }
  1544. // now create xcode depend hack makefile
  1545. this->CreateXCodeDependHackTarget(targets);
  1546. // now add all targets to the root object
  1547. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1548. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1549. i != targets.end(); ++i)
  1550. {
  1551. cmXCodeObject* t = *i;
  1552. allTargets->AddObject(t);
  1553. cmXCodeObject* productRef = t->GetObject("productReference");
  1554. if(productRef)
  1555. {
  1556. productGroupChildren->AddObject(productRef->GetObject());
  1557. }
  1558. }
  1559. m_RootObject->AddAttribute("targets", allTargets);
  1560. }
  1561. //----------------------------------------------------------------------------
  1562. void
  1563. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  1564. std::vector<cmXCodeObject*>& targets)
  1565. {
  1566. cmGeneratedFileStream makefileStream(m_CurrentXCodeHackMakefile.c_str());
  1567. if(!makefileStream)
  1568. {
  1569. cmSystemTools::Error("Could not create",
  1570. m_CurrentXCodeHackMakefile.c_str());
  1571. return;
  1572. }
  1573. // one more pass for external depend information not handled
  1574. // correctly by xcode
  1575. makefileStream << "# DO NOT EDIT\n";
  1576. makefileStream << "# This makefile makes sure all linkable targets are \n";
  1577. makefileStream
  1578. << "# up-to-date with anything they link to,avoiding a bug in XCode 1.5\n";
  1579. makefileStream << "all: ";
  1580. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1581. i != targets.end(); ++i)
  1582. {
  1583. cmXCodeObject* target = *i;
  1584. cmTarget* t =target->GetcmTarget();
  1585. if(t->GetType() == cmTarget::EXECUTABLE ||
  1586. t->GetType() == cmTarget::SHARED_LIBRARY ||
  1587. t->GetType() == cmTarget::MODULE_LIBRARY)
  1588. {
  1589. makefileStream << "\\\n\t"
  1590. << this->
  1591. ConvertToRelativeForMake(this->GetTargetFullPath(target->GetcmTarget()).c_str());
  1592. }
  1593. }
  1594. makefileStream << "\n\n";
  1595. makefileStream
  1596. << "# For each target create a dummy rule "
  1597. "so the target does not have to exist\n";
  1598. std::set<cmStdString> emitted;
  1599. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1600. i != targets.end(); ++i)
  1601. {
  1602. cmXCodeObject* target = *i;
  1603. std::vector<cmStdString> const& deplibs = target->GetDependLibraries();
  1604. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  1605. d != deplibs.end(); ++d)
  1606. {
  1607. if(emitted.insert(*d).second)
  1608. {
  1609. makefileStream << this->ConvertToRelativeForMake(d->c_str()) << ":\n";
  1610. }
  1611. }
  1612. }
  1613. makefileStream << "\n\n";
  1614. makefileStream <<
  1615. "# Each linkable target depends on everything it links to.\n";
  1616. makefileStream
  1617. << "#And the target is removed if it is older than what it linkes to\n";
  1618. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1619. i != targets.end(); ++i)
  1620. {
  1621. cmXCodeObject* target = *i;
  1622. cmTarget* t =target->GetcmTarget();
  1623. if(t->GetType() == cmTarget::EXECUTABLE ||
  1624. t->GetType() == cmTarget::SHARED_LIBRARY ||
  1625. t->GetType() == cmTarget::MODULE_LIBRARY)
  1626. {
  1627. std::vector<cmStdString> const& deplibs = target->GetDependLibraries();
  1628. std::string tfull = this->GetTargetFullPath(target->GetcmTarget());
  1629. makefileStream << this->ConvertToRelativeForMake(tfull.c_str()) << ": ";
  1630. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  1631. d != deplibs.end(); ++d)
  1632. {
  1633. makefileStream << "\\\n\t" << this->ConvertToRelativeForMake(d->c_str());
  1634. }
  1635. makefileStream << "\n";
  1636. makefileStream << "\t/bin/rm -f "
  1637. << this->ConvertToRelativeForMake(tfull.c_str()) << "\n";
  1638. makefileStream << "\n\n";
  1639. }
  1640. }
  1641. }
  1642. //----------------------------------------------------------------------------
  1643. void
  1644. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  1645. std::vector<cmLocalGenerator*>&
  1646. generators)
  1647. {
  1648. if(generators.size() == 0)
  1649. {
  1650. return;
  1651. }
  1652. this->CreateXCodeObjects(root,
  1653. generators);
  1654. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  1655. xcodeDir += "/";
  1656. xcodeDir += root->GetMakefile()->GetProjectName();
  1657. xcodeDir += ".xcode";
  1658. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  1659. xcodeDir += "/project.pbxproj";
  1660. cmGeneratedFileStream fout(xcodeDir.c_str());
  1661. fout.SetCopyIfDifferent(true);
  1662. if(!fout)
  1663. {
  1664. return;
  1665. }
  1666. this->WriteXCodePBXProj(fout, root, generators);
  1667. this->ClearXCodeObjects();
  1668. }
  1669. //----------------------------------------------------------------------------
  1670. void
  1671. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  1672. cmLocalGenerator* ,
  1673. std::vector<cmLocalGenerator*>& )
  1674. {
  1675. fout << "// !$*UTF8*$!\n";
  1676. fout << "{\n";
  1677. cmXCodeObject::Indent(1, fout);
  1678. fout << "archiveVersion = 1;\n";
  1679. cmXCodeObject::Indent(1, fout);
  1680. fout << "classes = {\n";
  1681. cmXCodeObject::Indent(1, fout);
  1682. fout << "};\n";
  1683. cmXCodeObject::Indent(1, fout);
  1684. fout << "objectVersion = 39;\n";
  1685. cmXCodeObject::PrintList(m_XCodeObjects, fout);
  1686. cmXCodeObject::Indent(1, fout);
  1687. fout << "rootObject = " << m_RootObject->GetId() << ";\n";
  1688. fout << "}\n";
  1689. }
  1690. //----------------------------------------------------------------------------
  1691. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  1692. const
  1693. {
  1694. entry.name = this->GetName();
  1695. entry.brief = "Generate XCode project files.";
  1696. entry.full = "";
  1697. }
  1698. //----------------------------------------------------------------------------
  1699. std::string cmGlobalXCodeGenerator::ConvertToRelativeForMake(const char* p)
  1700. {
  1701. if ( !m_CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  1702. {
  1703. return cmSystemTools::ConvertToOutputPath(p);
  1704. }
  1705. else
  1706. {
  1707. std::string ret =
  1708. this->ConvertToRelativePath(m_CurrentOutputDirectoryComponents, p);
  1709. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  1710. }
  1711. }
  1712. //----------------------------------------------------------------------------
  1713. std::string cmGlobalXCodeGenerator::ConvertToRelativeForXCode(const char* p)
  1714. {
  1715. if ( !m_CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  1716. {
  1717. return cmSystemTools::ConvertToOutputPath(p);
  1718. }
  1719. else
  1720. {
  1721. std::string ret =
  1722. this->ConvertToRelativePath(m_ProjectOutputDirectoryComponents, p);
  1723. return cmSystemTools::ConvertToOutputPath(ret.c_str());
  1724. }
  1725. }
  1726. std::string cmGlobalXCodeGenerator::XCodeEscapePath(const char* p)
  1727. {
  1728. std::string ret = p;
  1729. if(ret.find(' ') != ret.npos)
  1730. {
  1731. std::string t = ret;
  1732. ret = "\\\"";
  1733. ret += t;
  1734. ret += "\\\"";
  1735. }
  1736. return ret;
  1737. }