cmGlobalXCodeGenerator.cxx 78 KB

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