cmGlobalXCodeGenerator.cxx 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 "cmLocalXCodeGenerator.h"
  15. #include "cmMakefile.h"
  16. #include "cmXCodeObject.h"
  17. #include "cmake.h"
  18. #include "cmGeneratedFileStream.h"
  19. #include "cmSourceFile.h"
  20. //TODO
  21. // RUN_TESTS
  22. // add a group for Sources Headers, and other cmake group stuff
  23. // for each target create a custom build phase that is run first
  24. // it can have inputs and outputs should just be a makefile
  25. // per file flags howto
  26. // 115281011528101152810000 = {
  27. // fileEncoding = 4;
  28. // isa = PBXFileReference;
  29. // lastKnownFileType = sourcecode.cpp.cpp;
  30. // path = /Users/kitware/Bill/CMake/Source/cmakemain.cxx;
  31. // refType = 0;
  32. // sourceTree = "<absolute>";
  33. // };
  34. // 115285011528501152850000 = {
  35. // fileRef = 115281011528101152810000;
  36. // isa = PBXBuildFile;
  37. // settings = {
  38. // COMPILER_FLAGS = "-Dcmakemakeindefflag";
  39. // };
  40. // };
  41. // custom commands and clean up custom targets
  42. // do I need an ALL_BUILD yes
  43. // exe/lib output paths
  44. //----------------------------------------------------------------------------
  45. cmGlobalXCodeGenerator::cmGlobalXCodeGenerator()
  46. {
  47. m_FindMakeProgramFile = "CMakeFindXCode.cmake";
  48. m_RootObject = 0;
  49. m_MainGroupChildren = 0;
  50. m_SourcesGroupChildren = 0;
  51. m_CurrentMakefile = 0;
  52. m_CurrentLocalGenerator = 0;
  53. m_ExternalGroupChildren = 0;
  54. }
  55. //----------------------------------------------------------------------------
  56. void cmGlobalXCodeGenerator::EnableLanguage(std::vector<std::string>const&
  57. lang,
  58. cmMakefile * mf)
  59. {
  60. mf->AddDefinition("CMAKE_CFG_INTDIR",".");
  61. mf->AddDefinition("CMAKE_GENERATOR_CC", "gcc");
  62. mf->AddDefinition("CMAKE_GENERATOR_CXX", "g++");
  63. mf->AddDefinition("CMAKE_GENERATOR_NO_COMPILER_ENV", "1");
  64. this->cmGlobalGenerator::EnableLanguage(lang, mf);
  65. }
  66. //----------------------------------------------------------------------------
  67. int cmGlobalXCodeGenerator::Build(
  68. const char *,
  69. const char *bindir,
  70. const char *projectName,
  71. const char *targetName,
  72. std::string *output,
  73. const char *makeCommandCSTR,
  74. const char *,
  75. bool )
  76. {
  77. // now build the test
  78. if(makeCommandCSTR == 0 || !strlen(makeCommandCSTR))
  79. {
  80. cmSystemTools::Error(
  81. "Generator cannot find the appropriate make command.");
  82. return 1;
  83. }
  84. std::string makeCommand =
  85. cmSystemTools::ConvertToOutputPath(makeCommandCSTR);
  86. std::string lowerCaseCommand = makeCommand;
  87. cmSystemTools::LowerCase(lowerCaseCommand);
  88. /**
  89. * Run an executable command and put the stdout in output.
  90. */
  91. std::string cwd = cmSystemTools::GetCurrentWorkingDirectory();
  92. cmSystemTools::ChangeDirectory(bindir);
  93. makeCommand += " -project ";
  94. makeCommand += projectName;
  95. makeCommand += ".xcode";
  96. makeCommand += " build -target ";
  97. if (targetName)
  98. {
  99. makeCommand += targetName;
  100. }
  101. else
  102. {
  103. makeCommand += "ALL_BUILD";
  104. }
  105. makeCommand += " -buildstyle Development ";
  106. makeCommand += " SYMROOT=";
  107. makeCommand += cmSystemTools::ConvertToOutputPath(bindir);
  108. int retVal;
  109. int timeout = cmGlobalGenerator::s_TryCompileTimeout;
  110. if (!cmSystemTools::RunSingleCommand(makeCommand.c_str(), output, &retVal,
  111. 0, false, timeout))
  112. {
  113. cmSystemTools::Error("Generator: execution of xcodebuild failed.");
  114. // return to the original directory
  115. cmSystemTools::ChangeDirectory(cwd.c_str());
  116. return 1;
  117. }
  118. cmSystemTools::ChangeDirectory(cwd.c_str());
  119. return retVal;
  120. }
  121. //----------------------------------------------------------------------------
  122. void cmGlobalXCodeGenerator::ConfigureOutputPaths()
  123. {
  124. // Format the library and executable output paths.
  125. m_LibraryOutputPath =
  126. m_CurrentMakefile->GetSafeDefinition("LIBRARY_OUTPUT_PATH");
  127. if(m_LibraryOutputPath.size() == 0)
  128. {
  129. m_LibraryOutputPath = m_CurrentMakefile->GetCurrentOutputDirectory();
  130. }
  131. // make sure there is a trailing slash
  132. if(m_LibraryOutputPath.size() &&
  133. m_LibraryOutputPath[m_LibraryOutputPath.size()-1] != '/')
  134. {
  135. m_LibraryOutputPath += "/";
  136. if(!cmSystemTools::MakeDirectory(m_LibraryOutputPath.c_str()))
  137. {
  138. cmSystemTools::Error("Error creating directory ",
  139. m_LibraryOutputPath.c_str());
  140. }
  141. }
  142. m_CurrentMakefile->AddLinkDirectory(m_LibraryOutputPath.c_str());
  143. m_ExecutableOutputPath =
  144. m_CurrentMakefile->GetSafeDefinition("EXECUTABLE_OUTPUT_PATH");
  145. if(m_ExecutableOutputPath.size() == 0)
  146. {
  147. m_ExecutableOutputPath = m_CurrentMakefile->GetCurrentOutputDirectory();
  148. }
  149. // make sure there is a trailing slash
  150. if(m_ExecutableOutputPath.size() &&
  151. m_ExecutableOutputPath[m_ExecutableOutputPath.size()-1] != '/')
  152. {
  153. m_ExecutableOutputPath += "/";
  154. if(!cmSystemTools::MakeDirectory(m_ExecutableOutputPath.c_str()))
  155. {
  156. cmSystemTools::Error("Error creating directory ",
  157. m_ExecutableOutputPath.c_str());
  158. }
  159. }
  160. }
  161. //----------------------------------------------------------------------------
  162. ///! Create a local generator appropriate to this Global Generator
  163. cmLocalGenerator *cmGlobalXCodeGenerator::CreateLocalGenerator()
  164. {
  165. cmLocalGenerator *lg = new cmLocalXCodeGenerator;
  166. lg->SetGlobalGenerator(this);
  167. return lg;
  168. }
  169. //----------------------------------------------------------------------------
  170. void cmGlobalXCodeGenerator::Generate()
  171. {
  172. this->cmGlobalGenerator::Generate();
  173. std::map<cmStdString, std::vector<cmLocalGenerator*> >::iterator it;
  174. for(it = m_ProjectMap.begin(); it!= m_ProjectMap.end(); ++it)
  175. {
  176. cmLocalGenerator* root = it->second[0];
  177. m_CurrentMakefile = root->GetMakefile();
  178. m_OutputDir = m_CurrentMakefile->GetHomeOutputDirectory();
  179. m_OutputDir = cmSystemTools::CollapseFullPath(m_OutputDir.c_str());
  180. m_CurrentLocalGenerator = root;
  181. // add ALL_BUILD, INSTALL, etc
  182. this->AddExtraTargets(root, it->second);
  183. // now create the project
  184. this->OutputXCodeProject(root, it->second);
  185. }
  186. }
  187. //----------------------------------------------------------------------------
  188. void
  189. cmGlobalXCodeGenerator::AddExtraTargets(cmLocalGenerator* root,
  190. std::vector<cmLocalGenerator*>& gens)
  191. {
  192. std::vector<std::string> srcs; // dummy list
  193. cmMakefile* mf = root->GetMakefile();
  194. // Add ALL_BUILD
  195. mf->AddUtilityCommand("ALL_BUILD", "echo",
  196. "\"Build all projects\"",false,srcs);
  197. cmTarget* allbuild = mf->FindTarget("ALL_BUILD");
  198. // ADD install
  199. std::string cmake_command = mf->GetRequiredDefinition("CMAKE_COMMAND");
  200. mf->AddUtilityCommand("install", cmake_command.c_str(),
  201. "-P cmake_install.cmake",false,srcs);
  202. // Add RUN_TESTS target if testing has been enabled
  203. std::string fname;
  204. fname = mf->GetStartOutputDirectory();
  205. fname += "/";
  206. fname += "DartTestfile.txt";
  207. if (cmSystemTools::FileExists(fname.c_str()))
  208. {
  209. std::string ctest_command =
  210. mf->GetRequiredDefinition("CMAKE_CTEST_COMMAND");
  211. mf->AddUtilityCommand("RUN_TESTS", ctest_command.c_str(), "",false,srcs);
  212. }
  213. // Add install
  214. mf->AddUtilityCommand("install", cmake_command.c_str(),
  215. "-P cmake_install.cmake", false, srcs);
  216. // Add XCODE depend helper
  217. std::string dir = mf->GetCurrentOutputDirectory();
  218. m_CurrentXCodeHackMakefile = dir;
  219. m_CurrentXCodeHackMakefile += "/CMakeScripts";
  220. cmSystemTools::MakeDirectory(m_CurrentXCodeHackMakefile.c_str());
  221. m_CurrentXCodeHackMakefile += "/XCODE_DEPEND_HELPER.make";
  222. std::string makecommand = "make -C ";
  223. makecommand += this->ConvertToRelativeOutputPath(dir.c_str());
  224. makecommand += " -f ";
  225. makecommand += this->ConvertToRelativeOutputPath(
  226. m_CurrentXCodeHackMakefile.c_str());
  227. mf->AddUtilityCommand("XCODE_DEPEND_HELPER", makecommand.c_str(),
  228. "",
  229. false,srcs);
  230. // now make the allbuild depend on all the non-utility targets
  231. // in the project
  232. for(std::vector<cmLocalGenerator*>::iterator i = gens.begin();
  233. i != gens.end(); ++i)
  234. {
  235. cmLocalGenerator* lg = *i;
  236. if(this->IsExcluded(root, *i))
  237. {
  238. continue;
  239. }
  240. cmTargets& tgts = lg->GetMakefile()->GetTargets();
  241. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  242. {
  243. cmTarget& target = l->second;
  244. // make all exe, shared libs and modules depend
  245. // on the XCODE_DEPEND_HELPER target
  246. if((target.GetType() == cmTarget::EXECUTABLE ||
  247. target.GetType() == cmTarget::SHARED_LIBRARY ||
  248. target.GetType() == cmTarget::MODULE_LIBRARY))
  249. {
  250. target.AddUtility("XCODE_DEPEND_HELPER");
  251. }
  252. if(target.IsInAll())
  253. {
  254. allbuild->AddUtility(target.GetName());
  255. }
  256. }
  257. }
  258. }
  259. //----------------------------------------------------------------------------
  260. void cmGlobalXCodeGenerator::ClearXCodeObjects()
  261. {
  262. m_TargetDoneSet.clear();
  263. for(unsigned int i = 0; i < m_XCodeObjects.size(); ++i)
  264. {
  265. delete m_XCodeObjects[i];
  266. }
  267. m_XCodeObjects.clear();
  268. }
  269. //----------------------------------------------------------------------------
  270. cmXCodeObject*
  271. cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::PBXType ptype)
  272. {
  273. cmXCodeObject* obj = new cmXCodeObject(ptype, cmXCodeObject::OBJECT);
  274. m_XCodeObjects.push_back(obj);
  275. return obj;
  276. }
  277. //----------------------------------------------------------------------------
  278. cmXCodeObject*
  279. cmGlobalXCodeGenerator::CreateObject(cmXCodeObject::Type type)
  280. {
  281. cmXCodeObject* obj = new cmXCodeObject(cmXCodeObject::None, type);
  282. m_XCodeObjects.push_back(obj);
  283. return obj;
  284. }
  285. //----------------------------------------------------------------------------
  286. cmXCodeObject*
  287. cmGlobalXCodeGenerator::CreateString(const char* s)
  288. {
  289. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::STRING);
  290. obj->SetString(s);
  291. return obj;
  292. }
  293. //----------------------------------------------------------------------------
  294. cmXCodeObject* cmGlobalXCodeGenerator::CreateObjectReference(cmXCodeObject* ref)
  295. {
  296. cmXCodeObject* obj = this->CreateObject(cmXCodeObject::OBJECT_REF);
  297. obj->SetObject(ref);
  298. return obj;
  299. }
  300. //----------------------------------------------------------------------------
  301. cmXCodeObject*
  302. cmGlobalXCodeGenerator::CreateXCodeSourceFile(cmLocalGenerator* lg,
  303. cmSourceFile* sf)
  304. {
  305. std::string flags;
  306. // Add flags from source file properties.
  307. lg->AppendFlags(flags, sf->GetProperty("COMPILE_FLAGS"));
  308. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  309. m_SourcesGroupChildren->AddObject(fileRef);
  310. cmXCodeObject* buildFile = this->CreateObject(cmXCodeObject::PBXBuildFile);
  311. buildFile->AddAttribute("fileRef", this->CreateObjectReference(fileRef));
  312. cmXCodeObject* settings = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  313. settings->AddAttribute("COMPILER_FLAGS", this->CreateString(flags.c_str()));
  314. buildFile->AddAttribute("settings", settings);
  315. fileRef->AddAttribute("fileEncoding", this->CreateString("4"));
  316. const char* lang =
  317. this->GetLanguageFromExtension(sf->GetSourceExtension().c_str());
  318. std::string sourcecode = "sourcecode";
  319. if(sf->GetSourceExtension() == "o")
  320. {
  321. sourcecode = "compiled.mach-o.objfile";
  322. }
  323. else if(sf->GetSourceExtension() == "mm")
  324. {
  325. sourcecode += ".cpp.objcpp";
  326. }
  327. else if(!lang)
  328. {
  329. std::string ext = ".";
  330. ext = sf->GetSourceExtension();
  331. sourcecode += ext;
  332. sourcecode += ext;
  333. }
  334. else if(strcmp(lang, "C") == 0)
  335. {
  336. sourcecode += ".c.c";
  337. }
  338. else
  339. {
  340. sourcecode += ".cpp.cpp";
  341. }
  342. fileRef->AddAttribute("lastKnownFileType",
  343. this->CreateString(sourcecode.c_str()));
  344. std::string path =
  345. this->ConvertToRelativeOutputPath(sf->GetFullPath().c_str());
  346. fileRef->AddAttribute("path", this->CreateString(path.c_str()));
  347. fileRef->AddAttribute("refType", this->CreateString("4"));
  348. if(path.size() > 1 && path[0] == '.' && path[1] == '.')
  349. {
  350. fileRef->AddAttribute("sourceTree", this->CreateString("<group>"));
  351. }
  352. else
  353. {
  354. fileRef->AddAttribute("sourceTree", this->CreateString("<absolute>"));
  355. }
  356. return buildFile;
  357. }
  358. //----------------------------------------------------------------------------
  359. bool cmGlobalXCodeGenerator::SpecialTargetEmitted(std::string const& tname)
  360. {
  361. if(tname == "ALL_BUILD" || tname == "XCODE_DEPEND_HELPER" ||
  362. tname == "install" || tname == "RUN_TESTS")
  363. {
  364. if(m_TargetDoneSet.find(tname) != m_TargetDoneSet.end())
  365. {
  366. return true;
  367. }
  368. m_TargetDoneSet.insert(tname);
  369. return false;
  370. }
  371. return false;
  372. }
  373. //----------------------------------------------------------------------------
  374. void
  375. cmGlobalXCodeGenerator::CreateXCodeTargets(cmLocalGenerator* gen,
  376. std::vector<cmXCodeObject*>&
  377. targets)
  378. {
  379. m_CurrentLocalGenerator = gen;
  380. m_CurrentMakefile = gen->GetMakefile();
  381. cmTargets &tgts = gen->GetMakefile()->GetTargets();
  382. for(cmTargets::iterator l = tgts.begin(); l != tgts.end(); l++)
  383. {
  384. cmTarget& cmtarget = l->second;
  385. // make sure ALL_BUILD, INSTALL, etc are only done once
  386. if(this->SpecialTargetEmitted(l->first.c_str()))
  387. {
  388. continue;
  389. }
  390. if(cmtarget.GetType() == cmTarget::UTILITY ||
  391. cmtarget.GetType() == cmTarget::INSTALL_FILES ||
  392. cmtarget.GetType() == cmTarget::INSTALL_PROGRAMS)
  393. {
  394. if(cmtarget.GetType() == cmTarget::UTILITY)
  395. {
  396. targets.push_back(this->CreateUtilityTarget(cmtarget));
  397. }
  398. continue;
  399. }
  400. // create source build phase
  401. cmXCodeObject* sourceBuildPhase =
  402. this->CreateObject(cmXCodeObject::PBXSourcesBuildPhase);
  403. sourceBuildPhase->AddAttribute("buildActionMask",
  404. this->CreateString("2147483647"));
  405. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  406. sourceBuildPhase->AddAttribute("files", buildFiles);
  407. sourceBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  408. this->CreateString("0"));
  409. std::vector<cmSourceFile*> &classes = l->second.GetSourceFiles();
  410. // add all the sources
  411. std::vector<cmXCodeObject*> externalObjFiles;
  412. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  413. i != classes.end(); ++i)
  414. {
  415. cmXCodeObject* xsf = this->CreateXCodeSourceFile(gen, *i);
  416. cmXCodeObject* fr = xsf->GetObject("fileRef");
  417. cmXCodeObject* filetype =
  418. fr->GetObject()->GetObject("lastKnownFileType");
  419. if(strcmp(filetype->GetString(), "\"compiled.mach-o.objfile\"") == 0)
  420. {
  421. externalObjFiles.push_back(xsf);
  422. }
  423. else
  424. {
  425. buildFiles->AddObject(xsf);
  426. }
  427. }
  428. // create header build phase
  429. cmXCodeObject* headerBuildPhase =
  430. this->CreateObject(cmXCodeObject::PBXHeadersBuildPhase);
  431. headerBuildPhase->AddAttribute("buildActionMask",
  432. this->CreateString("2147483647"));
  433. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  434. headerBuildPhase->AddAttribute("files", buildFiles);
  435. headerBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  436. this->CreateString("0"));
  437. // create framework build phase
  438. cmXCodeObject* frameworkBuildPhase =
  439. this->CreateObject(cmXCodeObject::PBXFrameworksBuildPhase);
  440. frameworkBuildPhase->AddAttribute("buildActionMask",
  441. this->CreateString("2147483647"));
  442. buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  443. frameworkBuildPhase->AddAttribute("files", buildFiles);
  444. for(std::vector<cmXCodeObject*>::iterator i = externalObjFiles.begin();
  445. i != externalObjFiles.end(); ++i)
  446. {
  447. buildFiles->AddObject(*i);
  448. }
  449. frameworkBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  450. this->CreateString("0"));
  451. cmXCodeObject* buildPhases =
  452. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  453. this->CreateCustomCommands(buildPhases, sourceBuildPhase,
  454. headerBuildPhase, frameworkBuildPhase,
  455. cmtarget);
  456. targets.push_back(this->CreateXCodeTarget(l->second, buildPhases));
  457. }
  458. }
  459. //----------------------------------------------------------------------------
  460. void cmGlobalXCodeGenerator::CreateCustomCommands(cmXCodeObject* buildPhases,
  461. cmXCodeObject*
  462. sourceBuildPhase,
  463. cmXCodeObject*
  464. headerBuildPhase,
  465. cmXCodeObject*
  466. frameworkBuildPhase,
  467. cmTarget& cmtarget)
  468. {
  469. std::vector<cmCustomCommand> const & prebuild
  470. = cmtarget.GetPreBuildCommands();
  471. std::vector<cmCustomCommand> const & prelink
  472. = cmtarget.GetPreLinkCommands();
  473. std::vector<cmCustomCommand> const & postbuild
  474. = cmtarget.GetPostBuildCommands();
  475. cmtarget.TraceVSDependencies(cmtarget.GetName(), m_CurrentMakefile);
  476. std::vector<cmSourceFile*> &classes = cmtarget.GetSourceFiles();
  477. // add all the sources
  478. std::vector<cmCustomCommand> commands;
  479. for(std::vector<cmSourceFile*>::iterator i = classes.begin();
  480. i != classes.end(); ++i)
  481. {
  482. if((*i)->GetCustomCommand())
  483. {
  484. commands.push_back(*(*i)->GetCustomCommand());
  485. }
  486. }
  487. // create prebuild phase
  488. cmXCodeObject* cmakeRulesBuildPhase = 0;
  489. if(commands.size())
  490. {
  491. cmakeRulesBuildPhase =
  492. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  493. cmakeRulesBuildPhase->AddAttribute("buildActionMask",
  494. this->CreateString("2147483647"));
  495. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  496. cmakeRulesBuildPhase->AddAttribute("files", buildFiles);
  497. cmakeRulesBuildPhase->AddAttribute("name",
  498. this->CreateString("CMake Rules"));
  499. cmakeRulesBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  500. this->CreateString("0"));
  501. cmakeRulesBuildPhase->AddAttribute("shellPath",
  502. this->CreateString("/bin/sh"));
  503. this->AddCommandsToBuildPhase(cmakeRulesBuildPhase, cmtarget, commands,
  504. "cmakeRulesCommands");
  505. }
  506. // create prebuild phase
  507. cmXCodeObject* preBuildPhase = 0;
  508. if(prebuild.size())
  509. {
  510. preBuildPhase =
  511. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  512. preBuildPhase->AddAttribute("buildActionMask",
  513. this->CreateString("2147483647"));
  514. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  515. preBuildPhase->AddAttribute("files", buildFiles);
  516. preBuildPhase->AddAttribute("name",
  517. this->CreateString("CMake PreBuild Rules"));
  518. preBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  519. this->CreateString("0"));
  520. preBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  521. this->AddCommandsToBuildPhase(preBuildPhase, cmtarget, prebuild,
  522. "preBuildCommands");
  523. }
  524. // create prebuild phase
  525. cmXCodeObject* preLinkPhase = 0;
  526. if(prelink.size())
  527. {
  528. preLinkPhase =
  529. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  530. preLinkPhase->AddAttribute("buildActionMask",
  531. this->CreateString("2147483647"));
  532. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  533. preLinkPhase->AddAttribute("files", buildFiles);
  534. preLinkPhase->AddAttribute("name",
  535. this->CreateString("CMake PreLink Rules"));
  536. preLinkPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  537. this->CreateString("0"));
  538. preLinkPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  539. this->AddCommandsToBuildPhase(preLinkPhase, cmtarget, prelink,
  540. "preLinkCommands");
  541. }
  542. // create prebuild phase
  543. cmXCodeObject* postBuildPhase = 0;
  544. if(postbuild.size())
  545. {
  546. postBuildPhase =
  547. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  548. postBuildPhase->AddAttribute("buildActionMask",
  549. this->CreateString("2147483647"));
  550. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  551. postBuildPhase->AddAttribute("files", buildFiles);
  552. postBuildPhase->AddAttribute("name",
  553. this->CreateString("CMake PostBuild Rules"));
  554. postBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  555. this->CreateString("0"));
  556. postBuildPhase->AddAttribute("shellPath", this->CreateString("/bin/sh"));
  557. this->AddCommandsToBuildPhase(postBuildPhase, cmtarget, postbuild,
  558. "postBuildCommands");
  559. }
  560. // the order here is the order they will be built in
  561. if(preBuildPhase)
  562. {
  563. buildPhases->AddObject(preBuildPhase);
  564. }
  565. if(cmakeRulesBuildPhase)
  566. {
  567. buildPhases->AddObject(cmakeRulesBuildPhase);
  568. }
  569. if(sourceBuildPhase)
  570. {
  571. buildPhases->AddObject(sourceBuildPhase);
  572. }
  573. if(headerBuildPhase)
  574. {
  575. buildPhases->AddObject(headerBuildPhase);
  576. }
  577. if(preLinkPhase)
  578. {
  579. buildPhases->AddObject(preLinkPhase);
  580. }
  581. if(frameworkBuildPhase)
  582. {
  583. buildPhases->AddObject(frameworkBuildPhase);
  584. }
  585. if(postBuildPhase)
  586. {
  587. buildPhases->AddObject(postBuildPhase);
  588. }
  589. }
  590. //----------------------------------------------------------------------------
  591. void
  592. cmGlobalXCodeGenerator::AddCommandsToBuildPhase(cmXCodeObject* buildphase,
  593. cmTarget& target,
  594. std::vector<cmCustomCommand>
  595. const & commands,
  596. const char* name)
  597. {
  598. std::string dir = m_CurrentMakefile->GetCurrentOutputDirectory();
  599. dir += "/CMakeScripts";
  600. cmSystemTools::MakeDirectory(dir.c_str());
  601. std::string makefile = dir;
  602. makefile += "/";
  603. makefile += target.GetName();
  604. makefile += "_";
  605. makefile += name;
  606. makefile += ".make";
  607. cmGeneratedFileStream makefileStream(makefile.c_str());
  608. if(!makefileStream)
  609. {
  610. return;
  611. }
  612. makefileStream << "# Generated by CMake, DO NOT EDIT\n";
  613. makefileStream << "# Custom rules for " << target.GetName() << "\n";
  614. // have all depend on all outputs
  615. makefileStream << "all: ";
  616. std::map<const cmCustomCommand*, cmStdString> tname;
  617. int count = 0;
  618. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  619. i != commands.end(); ++i)
  620. {
  621. cmCustomCommand const& cc = *i;
  622. if(cc.GetCommand().size())
  623. {
  624. if(cc.GetOutput().size())
  625. {
  626. makefileStream << "\\\n\t" << this->
  627. ConvertToRelativeOutputPath(cc.GetOutput().c_str());
  628. }
  629. else
  630. {
  631. char c = '1' + count++;
  632. tname[&cc] = std::string(target.GetName()) + c;
  633. makefileStream << "\\\n\t" << tname[&cc];
  634. }
  635. }
  636. }
  637. makefileStream << "\n\n";
  638. for(std::vector<cmCustomCommand>::const_iterator i = commands.begin();
  639. i != commands.end(); ++i)
  640. {
  641. cmCustomCommand const& cc = *i;
  642. if(cc.GetCommand().size())
  643. {
  644. makefileStream << "\n#" << "Custom command rule: " <<
  645. cc.GetComment() << "\n";
  646. if(cc.GetOutput().size())
  647. {
  648. makefileStream << this
  649. ->ConvertToRelativeOutputPath(cc.GetOutput().c_str()) << ": ";
  650. }
  651. else
  652. {
  653. makefileStream << tname[&cc] << ": ";
  654. }
  655. for(std::vector<std::string>::const_iterator d = cc.GetDepends().begin();
  656. d != cc.GetDepends().end(); ++d)
  657. {
  658. if(!this->FindTarget(d->c_str()))
  659. {
  660. makefileStream << "\\\n" << *d;
  661. }
  662. else
  663. {
  664. // if the depend is a target then make
  665. // the target with the source that is a custom command
  666. // depend on the that target via a AddUtility call
  667. target.AddUtility(d->c_str());
  668. }
  669. }
  670. makefileStream << "\n";
  671. makefileStream << "\t" << cc.GetCommand() << " "
  672. << cc.GetArguments() << "\n";
  673. }
  674. }
  675. std::string cdir = m_CurrentMakefile->GetCurrentOutputDirectory();
  676. cdir = this->ConvertToRelativeOutputPath(cdir.c_str());
  677. std::string makecmd = "make -C ";
  678. makecmd += cdir;
  679. makecmd += " -f ";
  680. makecmd += this->ConvertToRelativeOutputPath(makefile.c_str());
  681. cmSystemTools::ReplaceString(makecmd, "\\ ", "\\\\ ");
  682. buildphase->AddAttribute("shellScript", this->CreateString(makecmd.c_str()));
  683. }
  684. //----------------------------------------------------------------------------
  685. void cmGlobalXCodeGenerator::CreateBuildSettings(cmTarget& target,
  686. cmXCodeObject* buildSettings,
  687. std::string& fileType,
  688. std::string& productType,
  689. std::string& productName)
  690. {
  691. this->ConfigureOutputPaths();
  692. std::string flags;
  693. bool shared = ((target.GetType() == cmTarget::SHARED_LIBRARY) ||
  694. (target.GetType() == cmTarget::MODULE_LIBRARY));
  695. if(shared)
  696. {
  697. flags += "-D";
  698. if(const char* custom_export_name = target.GetProperty("DEFINE_SYMBOL"))
  699. {
  700. flags += custom_export_name;
  701. }
  702. else
  703. {
  704. std::string in = target.GetName();
  705. in += "_EXPORTS";
  706. flags += cmSystemTools::MakeCindentifier(in.c_str());
  707. }
  708. }
  709. const char* lang = target.GetLinkerLanguage(this);
  710. if(lang)
  711. {
  712. // Add language-specific flags.
  713. m_CurrentLocalGenerator->AddLanguageFlags(flags, lang);
  714. // Add shared-library flags if needed.
  715. m_CurrentLocalGenerator->AddSharedFlags(flags, lang, shared);
  716. }
  717. // Add define flags
  718. m_CurrentLocalGenerator->AppendFlags(flags,
  719. m_CurrentMakefile->GetDefineFlags());
  720. cmSystemTools::ReplaceString(flags, "\"", "\\\"");
  721. productName = target.GetName();
  722. bool needLinkDirs = true;
  723. switch(target.GetType())
  724. {
  725. case cmTarget::STATIC_LIBRARY:
  726. {
  727. needLinkDirs = false;
  728. if(m_LibraryOutputPath.size())
  729. {
  730. buildSettings->AddAttribute("SYMROOT",
  731. this->CreateString
  732. (m_LibraryOutputPath.c_str()));
  733. }
  734. productName += ".a";
  735. std::string t = "lib";
  736. t += productName;
  737. productName = t;
  738. productType = "com.apple.product-type.library.static";
  739. fileType = "archive.ar";
  740. buildSettings->AddAttribute("LIBRARY_STYLE",
  741. this->CreateString("STATIC"));
  742. break;
  743. }
  744. case cmTarget::MODULE_LIBRARY:
  745. {
  746. if(m_LibraryOutputPath.size())
  747. {
  748. buildSettings->AddAttribute("SYMROOT",
  749. this->CreateString
  750. (m_LibraryOutputPath.c_str()));
  751. }
  752. buildSettings->AddAttribute("EXECUTABLE_PREFIX",
  753. this->CreateString("lib"));
  754. buildSettings->AddAttribute("EXECUTABLE_EXTENSION",
  755. this->CreateString("so"));
  756. buildSettings->AddAttribute("LIBRARY_STYLE",
  757. this->CreateString("BUNDLE"));
  758. productName += ".so";
  759. std::string t = "lib";
  760. t += productName;
  761. productName = t;
  762. buildSettings->AddAttribute("OTHER_LDFLAGS",
  763. this->CreateString("-bundle"));
  764. productType = "com.apple.product-type.library.dynamic";
  765. fileType = "compiled.mach-o.dylib";
  766. break;
  767. }
  768. case cmTarget::SHARED_LIBRARY:
  769. {
  770. if(m_LibraryOutputPath.size())
  771. {
  772. buildSettings->AddAttribute("SYMROOT",
  773. this->CreateString
  774. (m_LibraryOutputPath.c_str()));
  775. }
  776. buildSettings->AddAttribute("LIBRARY_STYLE",
  777. this->CreateString("DYNAMIC"));
  778. productName += ".dylib";
  779. std::string t = "lib";
  780. t += productName;
  781. productName = t;
  782. buildSettings->AddAttribute("DYLIB_COMPATIBILITY_VERSION",
  783. this->CreateString("1"));
  784. buildSettings->AddAttribute("DYLIB_CURRENT_VERSION",
  785. this->CreateString("1"));
  786. buildSettings->AddAttribute("OTHER_LDFLAGS",
  787. this->CreateString("-dynamiclib"));
  788. productType = "com.apple.product-type.library.dynamic";
  789. fileType = "compiled.mach-o.dylib";
  790. break;
  791. }
  792. case cmTarget::EXECUTABLE:
  793. if(m_ExecutableOutputPath.size())
  794. {
  795. buildSettings->AddAttribute("SYMROOT",
  796. this->CreateString
  797. (m_ExecutableOutputPath.c_str()));
  798. }
  799. fileType = "compiled.mach-o.executable";
  800. productType = "com.apple.product-type.tool";
  801. break;
  802. case cmTarget::UTILITY:
  803. break;
  804. case cmTarget::INSTALL_FILES:
  805. break;
  806. case cmTarget::INSTALL_PROGRAMS:
  807. break;
  808. }
  809. std::string dirs;
  810. if(needLinkDirs)
  811. {
  812. // Try to emit each search path once
  813. std::set<cmStdString> emitted;
  814. // Some search paths should never be emitted
  815. emitted.insert("");
  816. emitted.insert("/usr/lib");
  817. std::vector<std::string> const& linkdirs =
  818. target.GetLinkDirectories();
  819. for(std::vector<std::string>::const_iterator l = linkdirs.begin();
  820. l != linkdirs.end(); ++l)
  821. {
  822. std::string libpath =
  823. this->ConvertToRelativeOutputPath(l->c_str());
  824. if(emitted.insert(libpath).second)
  825. {
  826. dirs += libpath + " ";
  827. }
  828. }
  829. if(dirs.size())
  830. {
  831. buildSettings->AddAttribute("LIBRARY_SEARCH_PATHS",
  832. this->CreateString(dirs.c_str()));
  833. }
  834. }
  835. dirs = "";
  836. std::vector<std::string>& includes =
  837. m_CurrentMakefile->GetIncludeDirectories();
  838. std::vector<std::string>::iterator i = includes.begin();
  839. for(;i != includes.end(); ++i)
  840. {
  841. std::string incpath =
  842. this->ConvertToRelativeOutputPath(i->c_str());
  843. dirs += incpath + " ";
  844. }
  845. if(dirs.size())
  846. {
  847. buildSettings->AddAttribute("HEADER_SEARCH_PATHS",
  848. this->CreateString(dirs.c_str()));
  849. }
  850. buildSettings->AddAttribute("GCC_OPTIMIZATION_LEVEL",
  851. this->CreateString("0"));
  852. buildSettings->AddAttribute("INSTALL_PATH",
  853. this->CreateString(""));
  854. buildSettings->AddAttribute("OPTIMIZATION_CFLAGS",
  855. this->CreateString(""));
  856. buildSettings->AddAttribute("OTHER_CFLAGS",
  857. this->CreateString(flags.c_str()));
  858. buildSettings->AddAttribute("OTHER_LDFLAGS",
  859. this->CreateString(""));
  860. buildSettings->AddAttribute("OTHER_REZFLAGS",
  861. this->CreateString(""));
  862. buildSettings->AddAttribute("SECTORDER_FLAGS",
  863. this->CreateString(""));
  864. buildSettings->AddAttribute("WARNING_CFLAGS",
  865. this->CreateString(
  866. "-Wmost -Wno-four-char-constants"
  867. " -Wno-unknown-pragmas"));
  868. std::string pname;
  869. if(target.GetType() == cmTarget::SHARED_LIBRARY)
  870. {
  871. pname = "lib";
  872. }
  873. pname += target.GetName();
  874. buildSettings->AddAttribute("PRODUCT_NAME",
  875. this->CreateString(pname.c_str()));
  876. }
  877. //----------------------------------------------------------------------------
  878. cmXCodeObject*
  879. cmGlobalXCodeGenerator::CreateUtilityTarget(cmTarget& cmtarget)
  880. {
  881. cmXCodeObject* shellBuildPhase =
  882. this->CreateObject(cmXCodeObject::PBXShellScriptBuildPhase);
  883. shellBuildPhase->AddAttribute("buildActionMask",
  884. this->CreateString("2147483647"));
  885. cmXCodeObject* buildFiles = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  886. shellBuildPhase->AddAttribute("files", buildFiles);
  887. cmXCodeObject* inputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  888. shellBuildPhase->AddAttribute("inputPaths", inputPaths);
  889. cmXCodeObject* outputPaths = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  890. shellBuildPhase->AddAttribute("outputPaths", outputPaths);
  891. shellBuildPhase->AddAttribute("runOnlyForDeploymentPostprocessing",
  892. this->CreateString("0"));
  893. shellBuildPhase->AddAttribute("shellPath",
  894. this->CreateString("/bin/sh"));
  895. shellBuildPhase->AddAttribute("shellScript",
  896. this->CreateString(
  897. "# shell script goes here\nexit 0"));
  898. cmXCodeObject* target =
  899. this->CreateObject(cmXCodeObject::PBXAggregateTarget);
  900. cmXCodeObject* buildPhases =
  901. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  902. this->CreateCustomCommands(buildPhases, 0, 0, 0, cmtarget);
  903. target->AddAttribute("buildPhases", buildPhases);
  904. cmXCodeObject* buildSettings =
  905. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  906. std::string fileTypeString;
  907. std::string productTypeString;
  908. std::string productName;
  909. this->CreateBuildSettings(cmtarget,
  910. buildSettings, fileTypeString,
  911. productTypeString, productName);
  912. target->AddAttribute("buildSettings", buildSettings);
  913. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  914. target->AddAttribute("dependencies", dependencies);
  915. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  916. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  917. target->SetcmTarget(&cmtarget);
  918. return target;
  919. }
  920. //----------------------------------------------------------------------------
  921. cmXCodeObject*
  922. cmGlobalXCodeGenerator::CreateXCodeTarget(cmTarget& cmtarget,
  923. cmXCodeObject* buildPhases)
  924. {
  925. cmXCodeObject* target =
  926. this->CreateObject(cmXCodeObject::PBXNativeTarget);
  927. target->AddAttribute("buildPhases", buildPhases);
  928. cmXCodeObject* buildRules = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  929. target->AddAttribute("buildRules", buildRules);
  930. cmXCodeObject* buildSettings =
  931. this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  932. std::string fileTypeString;
  933. std::string productTypeString;
  934. std::string productName;
  935. this->CreateBuildSettings(cmtarget,
  936. buildSettings, fileTypeString,
  937. productTypeString, productName);
  938. target->AddAttribute("buildSettings", buildSettings);
  939. cmXCodeObject* dependencies = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  940. target->AddAttribute("dependencies", dependencies);
  941. target->AddAttribute("name", this->CreateString(cmtarget.GetName()));
  942. target->AddAttribute("productName",this->CreateString(cmtarget.GetName()));
  943. cmXCodeObject* fileRef = this->CreateObject(cmXCodeObject::PBXFileReference);
  944. fileRef->AddAttribute("explicitFileType",
  945. this->CreateString(fileTypeString.c_str()));
  946. fileRef->AddAttribute("path", this->CreateString(productName.c_str()));
  947. fileRef->AddAttribute("refType", this->CreateString("0"));
  948. fileRef->AddAttribute("sourceTree",
  949. this->CreateString("BUILT_PRODUCTS_DIR"));
  950. target->AddAttribute("productReference",
  951. this->CreateObjectReference(fileRef));
  952. target->AddAttribute("productType",
  953. this->CreateString(productTypeString.c_str()));
  954. target->SetcmTarget(&cmtarget);
  955. return target;
  956. }
  957. //----------------------------------------------------------------------------
  958. cmXCodeObject* cmGlobalXCodeGenerator::FindXCodeTarget(cmTarget* t)
  959. {
  960. if(!t)
  961. {
  962. return 0;
  963. }
  964. for(std::vector<cmXCodeObject*>::iterator i = m_XCodeObjects.begin();
  965. i != m_XCodeObjects.end(); ++i)
  966. {
  967. cmXCodeObject* o = *i;
  968. if(o->GetcmTarget() == t)
  969. {
  970. return o;
  971. }
  972. }
  973. return 0;
  974. }
  975. //----------------------------------------------------------------------------
  976. void cmGlobalXCodeGenerator::AddDependTarget(cmXCodeObject* target,
  977. cmXCodeObject* dependTarget)
  978. {
  979. // make sure a target does not depend on itself
  980. if(target == dependTarget)
  981. {
  982. return;
  983. }
  984. // now avoid circular references if dependTarget already
  985. // depends on target then skip it. Circular references crashes
  986. // xcode
  987. cmXCodeObject* dependTargetDepends = dependTarget->GetObject("dependencies");
  988. if(dependTargetDepends)
  989. {
  990. if(dependTargetDepends->HasObject(target->GetPBXTargetDependency()))
  991. {
  992. return;
  993. }
  994. }
  995. cmXCodeObject* targetdep = dependTarget->GetPBXTargetDependency();
  996. if(!targetdep)
  997. {
  998. cmXCodeObject* container =
  999. this->CreateObject(cmXCodeObject::PBXContainerItemProxy);
  1000. container->AddAttribute("containerPortal",
  1001. this->CreateObjectReference(m_RootObject));
  1002. container->AddAttribute("proxyType", this->CreateString("1"));
  1003. container->AddAttribute("remoteGlobalIDString",
  1004. this->CreateObjectReference(dependTarget));
  1005. container->AddAttribute("remoteInfo",
  1006. this->CreateString(
  1007. dependTarget->GetcmTarget()->GetName()));
  1008. targetdep =
  1009. this->CreateObject(cmXCodeObject::PBXTargetDependency);
  1010. targetdep->AddAttribute("target",
  1011. this->CreateObjectReference(dependTarget));
  1012. targetdep->AddAttribute("targetProxy",
  1013. this->CreateObjectReference(container));
  1014. dependTarget->SetPBXTargetDependency(targetdep);
  1015. }
  1016. cmXCodeObject* depends = target->GetObject("dependencies");
  1017. if(!depends)
  1018. {
  1019. std::cerr << "target does not have dependencies attribute error...\n";
  1020. }
  1021. else
  1022. {
  1023. depends->AddUniqueObject(targetdep);
  1024. }
  1025. }
  1026. //----------------------------------------------------------------------------
  1027. void cmGlobalXCodeGenerator::AddLinkLibrary(cmXCodeObject* target,
  1028. const char* library,
  1029. cmTarget* dtarget)
  1030. {
  1031. // if the library is a full path then create a file reference
  1032. // and build file and add them to the PBXFrameworksBuildPhase
  1033. // for the target
  1034. std::string file = library;
  1035. if(cmSystemTools::FileIsFullPath(library))
  1036. {
  1037. target->AddDependLibrary(library);
  1038. std::string dir;
  1039. cmSystemTools::SplitProgramPath(library, dir, file);
  1040. // add the library path to the search path for the target
  1041. cmXCodeObject* bset = target->GetObject("buildSettings");
  1042. if(bset)
  1043. {
  1044. cmXCodeObject* spath = bset->GetObject("LIBRARY_SEARCH_PATHS");
  1045. if(spath)
  1046. {
  1047. std::string libs = spath->GetString();
  1048. // remove double quotes
  1049. libs = libs.substr(1, libs.size()-2);
  1050. libs += " ";
  1051. libs += this->ConvertToRelativeOutputPath(dir.c_str());
  1052. spath->SetString(libs.c_str());
  1053. }
  1054. else
  1055. {
  1056. std::string libs = this->ConvertToRelativeOutputPath(dir.c_str());
  1057. bset->AddAttribute("LIBRARY_SEARCH_PATHS",
  1058. this->CreateString(libs.c_str()));
  1059. }
  1060. }
  1061. cmsys::RegularExpression libname("^lib([^/]*)(\\.a|\\.dylib).*");
  1062. if(libname.find(file))
  1063. {
  1064. file = libname.match(1);
  1065. }
  1066. }
  1067. else
  1068. {
  1069. if(dtarget)
  1070. {
  1071. target->AddDependLibrary(this->GetTargetFullPath(dtarget).c_str());
  1072. }
  1073. }
  1074. // if the library is not a full path then add it with a -l flag
  1075. // to the settings of the target
  1076. cmXCodeObject* settings = target->GetObject("buildSettings");
  1077. cmXCodeObject* ldflags = settings->GetObject("OTHER_LDFLAGS");
  1078. std::string link = ldflags->GetString();
  1079. cmSystemTools::ReplaceString(link, "\"", "");
  1080. cmsys::RegularExpression reg("^([ \t]*\\-[lWRB])|([ \t]*\\-framework)|(\\${)|([ \t]*\\-pthread)|([ \t]*`)");
  1081. // if the library is not already in the form required by the compiler
  1082. // add a -l infront of the name
  1083. link += " ";
  1084. if(!reg.find(file))
  1085. {
  1086. link += "-l";
  1087. }
  1088. link += file;
  1089. ldflags->SetString(link.c_str());
  1090. }
  1091. //----------------------------------------------------------------------------
  1092. std::string cmGlobalXCodeGenerator::GetTargetFullPath(cmTarget* target)
  1093. {
  1094. std::string libPath;
  1095. cmXCodeObject* xtarget = this->FindXCodeTarget(target);
  1096. cmXCodeObject* bset = xtarget->GetObject("buildSettings");
  1097. cmXCodeObject* spath = bset->GetObject("SYMROOT");
  1098. libPath = spath->GetString();
  1099. libPath = libPath.substr(1, libPath.size()-2);
  1100. if(target->GetType() == cmTarget::STATIC_LIBRARY)
  1101. {
  1102. libPath += "lib";
  1103. libPath += target->GetName();
  1104. libPath += ".a";
  1105. }
  1106. else if(target->GetType() == cmTarget::SHARED_LIBRARY)
  1107. {
  1108. libPath += "lib";
  1109. libPath += target->GetName();
  1110. libPath += ".dylib";
  1111. }
  1112. else
  1113. {
  1114. libPath += target->GetName();
  1115. }
  1116. return libPath;
  1117. }
  1118. //----------------------------------------------------------------------------
  1119. void cmGlobalXCodeGenerator::AddDependAndLinkInformation(cmXCodeObject* target)
  1120. {
  1121. cmTarget* cmtarget = target->GetcmTarget();
  1122. if(!cmtarget)
  1123. {
  1124. std::cerr << "Error no target on xobject\n";
  1125. return;
  1126. }
  1127. cmTarget::LinkLibraries::const_iterator j, jend;
  1128. j = cmtarget->GetLinkLibraries().begin();
  1129. jend = cmtarget->GetLinkLibraries().end();
  1130. for(;j!= jend; ++j)
  1131. {
  1132. cmTarget* t = this->FindTarget(j->first.c_str());
  1133. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1134. if(dptarget)
  1135. {
  1136. this->AddDependTarget(target, dptarget);
  1137. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1138. {
  1139. this->AddLinkLibrary(target, t->GetName(), t);
  1140. }
  1141. }
  1142. else
  1143. {
  1144. if(cmtarget->GetType() != cmTarget::STATIC_LIBRARY)
  1145. {
  1146. this->AddLinkLibrary(target, j->first.c_str());
  1147. }
  1148. }
  1149. }
  1150. std::set<cmStdString>::const_iterator i, end;
  1151. // write utility dependencies.
  1152. i = cmtarget->GetUtilities().begin();
  1153. end = cmtarget->GetUtilities().end();
  1154. for(;i!= end; ++i)
  1155. {
  1156. cmTarget* t = this->FindTarget(i->c_str());
  1157. cmXCodeObject* dptarget = this->FindXCodeTarget(t);
  1158. if(dptarget)
  1159. {
  1160. this->AddDependTarget(target, dptarget);
  1161. }
  1162. else
  1163. {
  1164. std::cerr << "Error Utility: " << i->c_str() << "\n";
  1165. std::cerr << "Is on the target " << cmtarget->GetName() << "\n";
  1166. std::cerr << "But it has no xcode target created yet??\n";
  1167. std::cerr << "Current project is "
  1168. << m_CurrentMakefile->GetProjectName() << "\n";
  1169. }
  1170. }
  1171. }
  1172. //----------------------------------------------------------------------------
  1173. void cmGlobalXCodeGenerator::CreateXCodeObjects(cmLocalGenerator* root,
  1174. std::vector<cmLocalGenerator*>&
  1175. generators
  1176. )
  1177. {
  1178. this->ClearXCodeObjects();
  1179. m_RootObject = 0;
  1180. m_ExternalGroupChildren = 0;
  1181. m_SourcesGroupChildren = 0;
  1182. m_MainGroupChildren = 0;
  1183. cmXCodeObject* group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1184. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("NO"));
  1185. cmXCodeObject* developBuildStyle =
  1186. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1187. developBuildStyle->AddAttribute("name", this->CreateString("Development"));
  1188. developBuildStyle->AddAttribute("buildSettings", group);
  1189. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1190. group->AddAttribute("COPY_PHASE_STRIP", this->CreateString("YES"));
  1191. cmXCodeObject* deployBuildStyle =
  1192. this->CreateObject(cmXCodeObject::PBXBuildStyle);
  1193. deployBuildStyle->AddAttribute("name", this->CreateString("Deployment"));
  1194. deployBuildStyle->AddAttribute("buildSettings", group);
  1195. cmXCodeObject* listObjs = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1196. listObjs->AddObject(developBuildStyle);
  1197. listObjs->AddObject(deployBuildStyle);
  1198. cmXCodeObject* mainGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1199. m_MainGroupChildren =
  1200. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1201. mainGroup->AddAttribute("children", m_MainGroupChildren);
  1202. mainGroup->AddAttribute("refType", this->CreateString("4"));
  1203. mainGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1204. cmXCodeObject* sourcesGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1205. m_SourcesGroupChildren =
  1206. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1207. sourcesGroup->AddAttribute("name", this->CreateString("Sources"));
  1208. sourcesGroup->AddAttribute("children", m_SourcesGroupChildren);
  1209. sourcesGroup->AddAttribute("refType", this->CreateString("4"));
  1210. sourcesGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1211. m_MainGroupChildren->AddObject(sourcesGroup);
  1212. cmXCodeObject* externalGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1213. m_ExternalGroupChildren =
  1214. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1215. externalGroup->AddAttribute("name",
  1216. this->CreateString("External Libraries and Frameworks"));
  1217. externalGroup->AddAttribute("children", m_ExternalGroupChildren);
  1218. externalGroup->AddAttribute("refType", this->CreateString("4"));
  1219. externalGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1220. m_MainGroupChildren->AddObject(externalGroup);
  1221. cmXCodeObject* productGroup = this->CreateObject(cmXCodeObject::PBXGroup);
  1222. productGroup->AddAttribute("name", this->CreateString("Products"));
  1223. productGroup->AddAttribute("refType", this->CreateString("4"));
  1224. productGroup->AddAttribute("sourceTree", this->CreateString("<group>"));
  1225. cmXCodeObject* productGroupChildren =
  1226. this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1227. productGroup->AddAttribute("children", productGroupChildren);
  1228. m_MainGroupChildren->AddObject(productGroup);
  1229. m_RootObject = this->CreateObject(cmXCodeObject::PBXProject);
  1230. group = this->CreateObject(cmXCodeObject::ATTRIBUTE_GROUP);
  1231. m_RootObject->AddAttribute("mainGroup",
  1232. this->CreateObjectReference(mainGroup));
  1233. m_RootObject->AddAttribute("buildSettings", group);
  1234. m_RootObject->AddAttribute("buildSyles", listObjs);
  1235. m_RootObject->AddAttribute("hasScannedForEncodings",
  1236. this->CreateString("0"));
  1237. std::vector<cmXCodeObject*> targets;
  1238. for(std::vector<cmLocalGenerator*>::iterator i = generators.begin();
  1239. i != generators.end(); ++i)
  1240. {
  1241. if(!this->IsExcluded(root, *i))
  1242. {
  1243. this->CreateXCodeTargets(*i, targets);
  1244. }
  1245. }
  1246. // loop over all targets and add link and depend info
  1247. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1248. i != targets.end(); ++i)
  1249. {
  1250. cmXCodeObject* t = *i;
  1251. this->AddDependAndLinkInformation(t);
  1252. }
  1253. // now create xcode depend hack makefile
  1254. this->CreateXCodeDependHackTarget(targets);
  1255. // now add all targets to the root object
  1256. cmXCodeObject* allTargets = this->CreateObject(cmXCodeObject::OBJECT_LIST);
  1257. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1258. i != targets.end(); ++i)
  1259. {
  1260. cmXCodeObject* t = *i;
  1261. allTargets->AddObject(t);
  1262. cmXCodeObject* productRef = t->GetObject("productReference");
  1263. if(productRef)
  1264. {
  1265. productGroupChildren->AddObject(productRef->GetObject());
  1266. }
  1267. }
  1268. m_RootObject->AddAttribute("targets", allTargets);
  1269. }
  1270. //----------------------------------------------------------------------------
  1271. void
  1272. cmGlobalXCodeGenerator::CreateXCodeDependHackTarget(
  1273. std::vector<cmXCodeObject*>& targets)
  1274. {
  1275. cmGeneratedFileStream makefileStream(m_CurrentXCodeHackMakefile.c_str());
  1276. if(!makefileStream)
  1277. {
  1278. cmSystemTools::Error("Could not create",
  1279. m_CurrentXCodeHackMakefile.c_str());
  1280. return;
  1281. }
  1282. // one more pass for external depend information not handled
  1283. // correctly by xcode
  1284. makefileStream << "# DO NOT EDIT\n";
  1285. makefileStream << "# This makefile makes sure all linkable targets are \n";
  1286. makefileStream
  1287. << "# up-to-date with anything they link to,avoiding a bug in XCode 1.5\n";
  1288. makefileStream << "all: ";
  1289. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1290. i != targets.end(); ++i)
  1291. {
  1292. cmXCodeObject* target = *i;
  1293. cmTarget* t =target->GetcmTarget();
  1294. if(t->GetType() == cmTarget::EXECUTABLE ||
  1295. t->GetType() == cmTarget::SHARED_LIBRARY ||
  1296. t->GetType() == cmTarget::MODULE_LIBRARY)
  1297. {
  1298. makefileStream << "\\\n\t"
  1299. << this->GetTargetFullPath(target->GetcmTarget());
  1300. }
  1301. }
  1302. makefileStream << "\n\n";
  1303. makefileStream
  1304. << "# For each target create a dummy rule "
  1305. "so the target does not have to exist\n";
  1306. std::set<cmStdString> emitted;
  1307. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1308. i != targets.end(); ++i)
  1309. {
  1310. cmXCodeObject* target = *i;
  1311. std::vector<cmStdString> const& deplibs = target->GetDependLibraries();
  1312. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  1313. d != deplibs.end(); ++d)
  1314. {
  1315. if(emitted.insert(*d).second)
  1316. {
  1317. makefileStream << *d << ":\n";
  1318. }
  1319. }
  1320. }
  1321. makefileStream << "\n\n";
  1322. makefileStream <<
  1323. "# Each linkable target depends on everything it links to.\n";
  1324. makefileStream
  1325. << "#And the target is removed if it is older than what it linkes to\n";
  1326. for(std::vector<cmXCodeObject*>::iterator i = targets.begin();
  1327. i != targets.end(); ++i)
  1328. {
  1329. cmXCodeObject* target = *i;
  1330. cmTarget* t =target->GetcmTarget();
  1331. if(t->GetType() == cmTarget::EXECUTABLE ||
  1332. t->GetType() == cmTarget::SHARED_LIBRARY ||
  1333. t->GetType() == cmTarget::MODULE_LIBRARY)
  1334. {
  1335. std::vector<cmStdString> const& deplibs = target->GetDependLibraries();
  1336. std::string tfull = this->GetTargetFullPath(target->GetcmTarget());
  1337. makefileStream << tfull << ": ";
  1338. for(std::vector<cmStdString>::const_iterator d = deplibs.begin();
  1339. d != deplibs.end(); ++d)
  1340. {
  1341. makefileStream << "\\\n\t" << *d;
  1342. }
  1343. makefileStream << "\n";
  1344. makefileStream << "\t/bin/rm -f " << tfull << "\n";
  1345. makefileStream << "\n\n";
  1346. }
  1347. }
  1348. }
  1349. //----------------------------------------------------------------------------
  1350. void
  1351. cmGlobalXCodeGenerator::OutputXCodeProject(cmLocalGenerator* root,
  1352. std::vector<cmLocalGenerator*>&
  1353. generators)
  1354. {
  1355. if(generators.size() == 0)
  1356. {
  1357. return;
  1358. }
  1359. this->CreateXCodeObjects(root,
  1360. generators);
  1361. std::string xcodeDir = root->GetMakefile()->GetStartOutputDirectory();
  1362. xcodeDir += "/";
  1363. xcodeDir += root->GetMakefile()->GetProjectName();
  1364. xcodeDir += ".xcode";
  1365. cmSystemTools::MakeDirectory(xcodeDir.c_str());
  1366. xcodeDir += "/project.pbxproj";
  1367. cmGeneratedFileStream fout(xcodeDir.c_str());
  1368. fout.SetCopyIfDifferent(true);
  1369. if(!fout)
  1370. {
  1371. return;
  1372. }
  1373. this->WriteXCodePBXProj(fout, root, generators);
  1374. this->ClearXCodeObjects();
  1375. }
  1376. //----------------------------------------------------------------------------
  1377. void
  1378. cmGlobalXCodeGenerator::WriteXCodePBXProj(std::ostream& fout,
  1379. cmLocalGenerator* ,
  1380. std::vector<cmLocalGenerator*>& )
  1381. {
  1382. fout << "// !$*UTF8*$!\n";
  1383. fout << "{\n";
  1384. cmXCodeObject::Indent(1, fout);
  1385. fout << "archiveVersion = 1;\n";
  1386. cmXCodeObject::Indent(1, fout);
  1387. fout << "classes = {\n";
  1388. cmXCodeObject::Indent(1, fout);
  1389. fout << "};\n";
  1390. cmXCodeObject::Indent(1, fout);
  1391. fout << "objectVersion = 39;\n";
  1392. cmXCodeObject::PrintList(m_XCodeObjects, fout);
  1393. cmXCodeObject::Indent(1, fout);
  1394. fout << "rootObject = " << m_RootObject->GetId() << ";\n";
  1395. fout << "}\n";
  1396. }
  1397. //----------------------------------------------------------------------------
  1398. void cmGlobalXCodeGenerator::GetDocumentation(cmDocumentationEntry& entry)
  1399. const
  1400. {
  1401. entry.name = this->GetName();
  1402. entry.brief = "Generate XCode project files.";
  1403. entry.full = "";
  1404. }
  1405. //----------------------------------------------------------------------------
  1406. std::string cmGlobalXCodeGenerator::ConvertToRelativeOutputPath(const char* p)
  1407. {
  1408. if ( !m_CurrentMakefile->IsOn("CMAKE_USE_RELATIVE_PATHS") )
  1409. {
  1410. return cmSystemTools::ConvertToOutputPath(p);
  1411. }
  1412. // NOTE, much of this was copied from
  1413. // cmLocalGenerator::ConvertToRelativeOutputPath
  1414. // fixes here should be made there as well.
  1415. //
  1416. // copy to a string class
  1417. std::string pathIn = p;
  1418. // check to see if the path is already relative, it is
  1419. // considered relative if one of the following is true
  1420. // - has no / in it at all
  1421. // - does not start with / or drive leter :
  1422. // - starts with a ".."
  1423. if(pathIn.find('/') == pathIn.npos ||
  1424. (pathIn[0] != '/' && pathIn[1] != ':') ||
  1425. pathIn.find("..") == 0)
  1426. {
  1427. return cmSystemTools::ConvertToOutputPath(p);
  1428. }
  1429. // Given that we are in m_CurrentOutputDirectory how to we
  1430. // get to pathIn with a relative path, store in ret
  1431. std::string ret =
  1432. cmSystemTools::RelativePath(m_OutputDir.c_str(), pathIn.c_str());
  1433. // If the path is 0 sized make it a .
  1434. // this happens when pathIn is the same as m_CurrentOutputDirectory
  1435. if(ret.size() == 0)
  1436. {
  1437. ret = ".";
  1438. }
  1439. // if there was a trailing / there still is one, and
  1440. // if there was not one, there still is not one
  1441. if(ret[ret.size()-1] == '/' &&
  1442. pathIn[pathIn.size()-1] != '/')
  1443. {
  1444. ret.erase(ret.size()-1, 1);
  1445. }
  1446. if(ret[ret.size()-1] != '/' &&
  1447. pathIn[pathIn.size()-1] == '/')
  1448. {
  1449. ret += "/";
  1450. }
  1451. // Now convert the relative path to an output path
  1452. ret = cmSystemTools::ConvertToOutputPath(ret.c_str());
  1453. // finally return the path
  1454. // at this point it should be relative and in the correct format
  1455. // for the native build system. (i.e. \ for windows and / for unix,
  1456. // and correct escaping/quoting of spaces in the path
  1457. return ret;
  1458. }