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