cmGlobalXCodeGenerator.cxx 74 KB

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