cmGlobalXCodeGenerator.cxx 63 KB

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