cmGlobalXCodeGenerator.cxx 55 KB

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