cmGlobalXCodeGenerator.cxx 70 KB

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