cmGlobalXCodeGenerator.cxx 55 KB

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