1
0

cmExtraSublimeTextGenerator.cxx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2004-2009 Kitware, Inc.
  4. Copyright 2004 Alexander Neundorf ([email protected])
  5. Distributed under the OSI-approved BSD License (the "License");
  6. see accompanying file Copyright.txt for details.
  7. This software is distributed WITHOUT ANY WARRANTY; without even the
  8. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. See the License for more information.
  10. ============================================================================*/
  11. #include "cmExtraSublimeTextGenerator.h"
  12. #include "cmGlobalUnixMakefileGenerator3.h"
  13. #include "cmLocalUnixMakefileGenerator3.h"
  14. #include "cmMakefile.h"
  15. #include "cmake.h"
  16. #include "cmSourceFile.h"
  17. #include "cmGeneratedFileStream.h"
  18. #include "cmTarget.h"
  19. #include "cmSystemTools.h"
  20. #include "cmXMLSafe.h"
  21. #include <cmsys/SystemTools.hxx>
  22. /*
  23. Sublime Text 2 Generator
  24. Author: Morné Chamberlain
  25. This generator was initially based off of the CodeBlocks generator.
  26. Some useful URLs:
  27. Homepage:
  28. http://www.sublimetext.com/
  29. File format docs:
  30. http://www.sublimetext.com/docs/2/projects.html
  31. http://sublimetext.info/docs/en/reference/build_systems.html
  32. */
  33. //----------------------------------------------------------------------------
  34. void cmExtraSublimeTextGenerator
  35. ::GetDocumentation(cmDocumentationEntry& entry, const char*) const
  36. {
  37. entry.Name = this->GetName();
  38. entry.Brief = "Generates Sublime Text 2 project files.";
  39. entry.Full =
  40. "Project files for Sublime Text 2 will be created in the top directory "
  41. "and in every subdirectory which features a CMakeLists.txt file "
  42. "containing a PROJECT() call. "
  43. "Additionally a hierarchy of makefiles is generated into the "
  44. "build tree. The appropriate make program can build the project through "
  45. "the default make target. A \"make install\" target is also provided.";
  46. }
  47. cmExtraSublimeTextGenerator::cmExtraSublimeTextGenerator()
  48. :cmExternalMakefileProjectGenerator()
  49. {
  50. #if defined(_WIN32)
  51. this->SupportedGlobalGenerators.push_back("MinGW Makefiles");
  52. this->SupportedGlobalGenerators.push_back("NMake Makefiles");
  53. // disable until somebody actually tests it:
  54. // this->SupportedGlobalGenerators.push_back("MSYS Makefiles");
  55. #endif
  56. this->SupportedGlobalGenerators.push_back("Ninja");
  57. this->SupportedGlobalGenerators.push_back("Unix Makefiles");
  58. }
  59. void cmExtraSublimeTextGenerator::Generate()
  60. {
  61. // for each sub project in the project create a sublime text 2 project
  62. for (std::map<cmStdString, std::vector<cmLocalGenerator*> >::const_iterator
  63. it = this->GlobalGenerator->GetProjectMap().begin();
  64. it!= this->GlobalGenerator->GetProjectMap().end();
  65. ++it)
  66. {
  67. // create a project file
  68. this->CreateProjectFile(it->second);
  69. }
  70. }
  71. void cmExtraSublimeTextGenerator::CreateProjectFile(
  72. const std::vector<cmLocalGenerator*>& lgs)
  73. {
  74. const cmMakefile* mf=lgs[0]->GetMakefile();
  75. std::string outputDir=mf->GetStartOutputDirectory();
  76. std::string projectName=mf->GetProjectName();
  77. std::string filename=outputDir+"/";
  78. filename+=projectName+".sublime-project";
  79. this->CreateNewProjectFile(lgs, filename);
  80. }
  81. void cmExtraSublimeTextGenerator
  82. ::CreateNewProjectFile(const std::vector<cmLocalGenerator*>& lgs,
  83. const std::string& filename)
  84. {
  85. const cmMakefile* mf=lgs[0]->GetMakefile();
  86. cmGeneratedFileStream fout(filename.c_str());
  87. if(!fout)
  88. {
  89. return;
  90. }
  91. const std::string &sourceRootRelativeToOutput = cmSystemTools::RelativePath(
  92. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  93. lgs[0]->GetMakefile()->GetHomeDirectory());
  94. const std::string &outputRelativeToSourceRoot = cmSystemTools::RelativePath(
  95. lgs[0]->GetMakefile()->GetHomeDirectory(),
  96. lgs[0]->GetMakefile()->GetHomeOutputDirectory());
  97. // Write the folder entries to the project file
  98. fout << "{\n";
  99. fout << "\t\"folders\":\n\t[\n\t";
  100. fout << "\t{\n\t\t\t\"path\": \"" << sourceRootRelativeToOutput << "\",\n";
  101. fout << "\t\t\t\"folder_exclude_patterns\": [\"" <<
  102. outputRelativeToSourceRoot << "\"],\n";
  103. fout << "\t\t\t\"file_exclude_patterns\": []\n";
  104. fout << "\t\t},\n\t";
  105. // In order for SublimeClang's path resolution to work, the directory that
  106. // contains the sublime-project file must be included here. We just ensure
  107. // that no files or subfolders are included
  108. fout << "\t{\n\t\t\t\"path\": \"./\",\n";
  109. fout << "\t\t\t\"folder_exclude_patterns\": [\"*\"],\n";
  110. fout << "\t\t\t\"file_exclude_patterns\": [\"*\"]\n";
  111. fout << "\t\t}\n\t";
  112. // End of the folders section
  113. fout << "]";
  114. // Write the beginning of the build systems section to the project file
  115. fout << ",\n\t\"build_systems\":\n\t[\n\t";
  116. // Set of include directories over all targets (sublime text/sublimeclang
  117. // doesn't currently support these settings per build system, only project
  118. // wide
  119. std::set<std::string> includeDirs;
  120. std::set<std::string> defines;
  121. AppendAllTargets(lgs, mf, fout, includeDirs, defines);
  122. // End of build_systems
  123. fout << "\n\t]";
  124. // Write the settings section with sublimeclang options
  125. fout << ",\n\t\"settings\":\n\t{\n\t";
  126. fout << "\t\"sublimeclang_options\":\n\t\t[\n\t\t";
  127. std::set<std::string>::const_iterator stringSetIter = includeDirs.begin();
  128. while (stringSetIter != includeDirs.end())
  129. {
  130. const std::string &includeDir = *stringSetIter;
  131. const std::string &relative = cmSystemTools::RelativePath(
  132. lgs[0]->GetMakefile()->GetHomeOutputDirectory(),
  133. includeDir.c_str());
  134. // It appears that a relative path to the sublime-project file doesn't
  135. // always work. So we use ${folder:${project_path:<project_filename>}}
  136. // that SublimeClang will expand to the correct path
  137. fout << "\t\"-I${folder:${project_path:" << mf->GetProjectName() <<
  138. ".sublime-project}}/" << relative << "\"";
  139. stringSetIter++;
  140. if ((stringSetIter != includeDirs.end()) || (!defines.empty()))
  141. {
  142. fout << ",";
  143. }
  144. fout << "\n\t\t";
  145. }
  146. stringSetIter = defines.begin();
  147. while (stringSetIter != defines.end())
  148. {
  149. fout << "\t\"-D" << *stringSetIter << "\"";
  150. stringSetIter++;
  151. if (stringSetIter != defines.end())
  152. {
  153. fout << ",";
  154. }
  155. fout << "\n\t\t";
  156. }
  157. // End of the sublimeclang_options section
  158. fout << "]\n\t";
  159. // End of the settings section
  160. fout << "}\n";
  161. // End of file
  162. fout << "}";
  163. }
  164. void cmExtraSublimeTextGenerator::
  165. AppendAllTargets(const std::vector<cmLocalGenerator*>& lgs,
  166. const cmMakefile* mf,
  167. cmGeneratedFileStream& fout,
  168. std::set<std::string>& includeDirs,
  169. std::set<std::string>& defines)
  170. {
  171. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  172. std::string compiler = "";
  173. this->AppendTarget(fout, "all", 0, make.c_str(), mf, compiler.c_str(),
  174. includeDirs, defines, true);
  175. this->AppendTarget(fout, "clean", 0, make.c_str(), mf, compiler.c_str(),
  176. includeDirs, defines, false);
  177. // add all executable and library targets and some of the GLOBAL
  178. // and UTILITY targets
  179. for (std::vector<cmLocalGenerator*>::const_iterator lg=lgs.begin();
  180. lg!=lgs.end(); lg++)
  181. {
  182. cmMakefile* makefile=(*lg)->GetMakefile();
  183. cmTargets& targets=makefile->GetTargets();
  184. for (cmTargets::iterator ti = targets.begin();
  185. ti != targets.end(); ti++)
  186. {
  187. switch(ti->second.GetType())
  188. {
  189. case cmTarget::GLOBAL_TARGET:
  190. {
  191. bool insertTarget = false;
  192. // Only add the global targets from CMAKE_BINARY_DIR,
  193. // not from the subdirs
  194. if (strcmp(makefile->GetStartOutputDirectory(),
  195. makefile->GetHomeOutputDirectory())==0)
  196. {
  197. insertTarget = true;
  198. // only add the "edit_cache" target if it's not ccmake, because
  199. // this will not work within the IDE
  200. if (ti->first == "edit_cache")
  201. {
  202. const char* editCommand = makefile->GetDefinition
  203. ("CMAKE_EDIT_COMMAND");
  204. if (editCommand == 0)
  205. {
  206. insertTarget = false;
  207. }
  208. else if (strstr(editCommand, "ccmake")!=NULL)
  209. {
  210. insertTarget = false;
  211. }
  212. }
  213. }
  214. if (insertTarget)
  215. {
  216. this->AppendTarget(fout, ti->first.c_str(), 0,
  217. make.c_str(), makefile, compiler.c_str(),
  218. includeDirs, defines, false);
  219. }
  220. }
  221. break;
  222. case cmTarget::UTILITY:
  223. // Add all utility targets, except the Nightly/Continuous/
  224. // Experimental-"sub"targets as e.g. NightlyStart
  225. if (((ti->first.find("Nightly")==0) &&(ti->first!="Nightly"))
  226. || ((ti->first.find("Continuous")==0)&&(ti->first!="Continuous"))
  227. || ((ti->first.find("Experimental")==0)
  228. && (ti->first!="Experimental")))
  229. {
  230. break;
  231. }
  232. this->AppendTarget(fout, ti->first.c_str(), 0,
  233. make.c_str(), makefile, compiler.c_str(),
  234. includeDirs, defines, false);
  235. break;
  236. case cmTarget::EXECUTABLE:
  237. case cmTarget::STATIC_LIBRARY:
  238. case cmTarget::SHARED_LIBRARY:
  239. case cmTarget::MODULE_LIBRARY:
  240. case cmTarget::OBJECT_LIBRARY:
  241. {
  242. this->AppendTarget(fout, ti->first.c_str(), &ti->second,
  243. make.c_str(), makefile, compiler.c_str(),
  244. includeDirs, defines, false);
  245. std::string fastTarget = ti->first;
  246. fastTarget += "/fast";
  247. this->AppendTarget(fout, fastTarget.c_str(), &ti->second,
  248. make.c_str(), makefile, compiler.c_str(),
  249. includeDirs, defines, false);
  250. }
  251. break;
  252. default:
  253. break;
  254. }
  255. }
  256. }
  257. }
  258. // Generate the build_system entry for one target
  259. void cmExtraSublimeTextGenerator::AppendTarget(cmGeneratedFileStream& fout,
  260. const char* targetName,
  261. cmTarget* target,
  262. const char* make,
  263. const cmMakefile* makefile,
  264. const char* compiler,
  265. std::set<std::string>&
  266. includeDirs,
  267. std::set<std::string>& defines,
  268. bool firstTarget)
  269. {
  270. if (target != 0)
  271. {
  272. // the compilerdefines for this target
  273. cmGeneratorTarget *gtgt = this->GlobalGenerator
  274. ->GetGeneratorTarget(target);
  275. std::string cdefs = gtgt->GetCompileDefinitions();
  276. if(!cdefs.empty())
  277. {
  278. // Expand the list.
  279. std::vector<std::string> defs;
  280. cmSystemTools::ExpandListArgument(cdefs.c_str(), defs);
  281. for(std::vector<std::string>::const_iterator di = defs.begin();
  282. di != defs.end(); ++di)
  283. {
  284. cmXMLSafe safedef(di->c_str());
  285. defines.insert(safedef.str());
  286. }
  287. }
  288. // the include directories for this target
  289. std::vector<std::string> includes;
  290. target->GetMakefile()->GetLocalGenerator()->
  291. GetIncludeDirectories(includes, gtgt);
  292. for(std::vector<std::string>::const_iterator dirIt=includes.begin();
  293. dirIt != includes.end();
  294. ++dirIt)
  295. {
  296. includeDirs.insert(*dirIt);
  297. }
  298. std::string systemIncludeDirs = makefile->GetSafeDefinition(
  299. "CMAKE_EXTRA_GENERATOR_C_SYSTEM_INCLUDE_DIRS");
  300. if (!systemIncludeDirs.empty())
  301. {
  302. std::vector<std::string> dirs;
  303. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  304. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  305. dirIt != dirs.end();
  306. ++dirIt)
  307. {
  308. includeDirs.insert(*dirIt);
  309. }
  310. }
  311. systemIncludeDirs = makefile->GetSafeDefinition(
  312. "CMAKE_EXTRA_GENERATOR_CXX_SYSTEM_INCLUDE_DIRS");
  313. if (!systemIncludeDirs.empty())
  314. {
  315. std::vector<std::string> dirs;
  316. cmSystemTools::ExpandListArgument(systemIncludeDirs.c_str(), dirs);
  317. for(std::vector<std::string>::const_iterator dirIt=dirs.begin();
  318. dirIt != dirs.end();
  319. ++dirIt)
  320. {
  321. includeDirs.insert(*dirIt);
  322. }
  323. }
  324. }
  325. // Ninja uses ninja.build files (look for a way to get the output file name
  326. // from cmMakefile or something)
  327. std::string makefileName;
  328. if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  329. {
  330. makefileName = "build.ninja";
  331. }
  332. else
  333. {
  334. makefileName = "Makefile";
  335. }
  336. if (!firstTarget)
  337. {
  338. fout << ",\n\t";
  339. }
  340. fout << "\t{\n\t\t\t\"name\": \"" << makefile->GetProjectName() << " - " <<
  341. targetName << "\",\n";
  342. fout << "\t\t\t\"cmd\": [" <<
  343. this->BuildMakeCommand(make, makefileName.c_str(), targetName) <<
  344. "],\n";
  345. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  346. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  347. fout << "\t\t}";
  348. }
  349. // Create the command line for building the given target using the selected
  350. // make
  351. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  352. const std::string& make, const char* makefile, const char* target)
  353. {
  354. std::string command = "\"";
  355. command += make + "\"";
  356. if (strcmp(this->GlobalGenerator->GetName(), "NMake Makefiles")==0)
  357. {
  358. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  359. command += ", \"/NOLOGO\", \"/f\", \"";
  360. command += makefileName + "\"";
  361. command += ", \"VERBOSE=1\", \"";
  362. command += target;
  363. command += "\"";
  364. }
  365. else if (strcmp(this->GlobalGenerator->GetName(), "Ninja")==0)
  366. {
  367. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  368. command += ", \"-f\", \"";
  369. command += makefileName + "\"";
  370. command += ", \"-v\", \"";
  371. command += target;
  372. command += "\"";
  373. }
  374. else
  375. {
  376. std::string makefileName;
  377. if (strcmp(this->GlobalGenerator->GetName(), "MinGW Makefiles")==0)
  378. {
  379. // no escaping of spaces in this case, see
  380. // http://public.kitware.com/Bug/view.php?id=10014
  381. makefileName = makefile;
  382. }
  383. else
  384. {
  385. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  386. }
  387. command += ", \"-f\", \"";
  388. command += makefileName + "\"";
  389. command += ", \"VERBOSE=1\", \"";
  390. command += target;
  391. command += "\"";
  392. }
  393. return command;
  394. }