cmGlobalXCodeGenerator.cxx 73 KB

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