cmExtraSublimeTextGenerator.cxx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 "cmGeneratedFileStream.h"
  13. #include "cmGeneratorTarget.h"
  14. #include "cmGlobalGenerator.h"
  15. #include "cmLocalGenerator.h"
  16. #include "cmMakefile.h"
  17. #include "cmSourceFile.h"
  18. #include "cmState.h"
  19. #include "cmSystemTools.h"
  20. #include <cmsys/RegularExpression.hxx>
  21. #include <ostream>
  22. #include <set>
  23. #include <string.h>
  24. #include <utility>
  25. /*
  26. Sublime Text 2 Generator
  27. Author: Morné Chamberlain
  28. This generator was initially based off of the CodeBlocks generator.
  29. Some useful URLs:
  30. Homepage:
  31. http://www.sublimetext.com/
  32. File format docs:
  33. http://www.sublimetext.com/docs/2/projects.html
  34. http://sublimetext.info/docs/en/reference/build_systems.html
  35. */
  36. cmExternalMakefileProjectGeneratorFactory*
  37. cmExtraSublimeTextGenerator::GetFactory()
  38. {
  39. static cmExternalMakefileProjectGeneratorSimpleFactory<
  40. cmExtraSublimeTextGenerator>
  41. factory("Sublime Text 2", "Generates Sublime Text 2 project files.");
  42. if (factory.GetSupportedGlobalGenerators().empty()) {
  43. #if defined(_WIN32)
  44. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  45. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  46. // disable until somebody actually tests it:
  47. // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
  48. #endif
  49. factory.AddSupportedGlobalGenerator("Ninja");
  50. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  51. }
  52. return &factory;
  53. }
  54. cmExtraSublimeTextGenerator::cmExtraSublimeTextGenerator()
  55. : cmExternalMakefileProjectGenerator()
  56. {
  57. }
  58. void cmExtraSublimeTextGenerator::Generate()
  59. {
  60. // for each sub project in the project create a sublime text 2 project
  61. for (std::map<std::string, std::vector<cmLocalGenerator*> >::const_iterator
  62. it = this->GlobalGenerator->GetProjectMap().begin();
  63. it != this->GlobalGenerator->GetProjectMap().end(); ++it) {
  64. // create a project file
  65. this->CreateProjectFile(it->second);
  66. }
  67. }
  68. void cmExtraSublimeTextGenerator::CreateProjectFile(
  69. const std::vector<cmLocalGenerator*>& lgs)
  70. {
  71. std::string outputDir = lgs[0]->GetCurrentBinaryDirectory();
  72. std::string projectName = lgs[0]->GetProjectName();
  73. const std::string filename =
  74. outputDir + "/" + projectName + ".sublime-project";
  75. this->CreateNewProjectFile(lgs, filename);
  76. }
  77. void cmExtraSublimeTextGenerator::CreateNewProjectFile(
  78. const std::vector<cmLocalGenerator*>& lgs, const std::string& filename)
  79. {
  80. const cmMakefile* mf = lgs[0]->GetMakefile();
  81. cmGeneratedFileStream fout(filename.c_str());
  82. if (!fout) {
  83. return;
  84. }
  85. const std::string& sourceRootRelativeToOutput = cmSystemTools::RelativePath(
  86. lgs[0]->GetBinaryDirectory(), lgs[0]->GetSourceDirectory());
  87. // Write the folder entries to the project file
  88. fout << "{\n";
  89. fout << "\t\"folders\":\n\t[\n\t";
  90. if (!sourceRootRelativeToOutput.empty()) {
  91. fout << "\t{\n\t\t\t\"path\": \"" << sourceRootRelativeToOutput << "\"";
  92. const std::string& outputRelativeToSourceRoot =
  93. cmSystemTools::RelativePath(lgs[0]->GetSourceDirectory(),
  94. lgs[0]->GetBinaryDirectory());
  95. if ((!outputRelativeToSourceRoot.empty()) &&
  96. ((outputRelativeToSourceRoot.length() < 3) ||
  97. (outputRelativeToSourceRoot.substr(0, 3) != "../"))) {
  98. fout << ",\n\t\t\t\"folder_exclude_patterns\": [\""
  99. << outputRelativeToSourceRoot << "\"]";
  100. }
  101. } else {
  102. fout << "\t{\n\t\t\t\"path\": \"./\"";
  103. }
  104. fout << "\n\t\t}";
  105. // End of the folders section
  106. fout << "\n\t]";
  107. // Write the beginning of the build systems section to the project file
  108. fout << ",\n\t\"build_systems\":\n\t[\n\t";
  109. // Set of include directories over all targets (sublime text/sublimeclang
  110. // doesn't currently support these settings per build system, only project
  111. // wide
  112. MapSourceFileFlags sourceFileFlags;
  113. AppendAllTargets(lgs, mf, fout, sourceFileFlags);
  114. // End of build_systems
  115. fout << "\n\t]";
  116. fout << "\n\t}";
  117. }
  118. void cmExtraSublimeTextGenerator::AppendAllTargets(
  119. const std::vector<cmLocalGenerator*>& lgs, const cmMakefile* mf,
  120. cmGeneratedFileStream& fout, MapSourceFileFlags& sourceFileFlags)
  121. {
  122. std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  123. std::string compiler = "";
  124. if (!lgs.empty()) {
  125. this->AppendTarget(fout, "all", lgs[0], CM_NULLPTR, make.c_str(), mf,
  126. compiler.c_str(), sourceFileFlags, true);
  127. this->AppendTarget(fout, "clean", lgs[0], CM_NULLPTR, make.c_str(), mf,
  128. compiler.c_str(), sourceFileFlags, false);
  129. }
  130. // add all executable and library targets and some of the GLOBAL
  131. // and UTILITY targets
  132. for (std::vector<cmLocalGenerator*>::const_iterator lg = lgs.begin();
  133. lg != lgs.end(); lg++) {
  134. cmMakefile* makefile = (*lg)->GetMakefile();
  135. std::vector<cmGeneratorTarget*> targets = (*lg)->GetGeneratorTargets();
  136. for (std::vector<cmGeneratorTarget*>::iterator ti = targets.begin();
  137. ti != targets.end(); ti++) {
  138. std::string targetName = (*ti)->GetName();
  139. switch ((*ti)->GetType()) {
  140. case cmState::GLOBAL_TARGET: {
  141. // Only add the global targets from CMAKE_BINARY_DIR,
  142. // not from the subdirs
  143. if (strcmp((*lg)->GetCurrentBinaryDirectory(),
  144. (*lg)->GetBinaryDirectory()) == 0) {
  145. this->AppendTarget(fout, targetName, *lg, CM_NULLPTR, make.c_str(),
  146. makefile, compiler.c_str(), sourceFileFlags,
  147. false);
  148. }
  149. } break;
  150. case cmState::UTILITY:
  151. // Add all utility targets, except the Nightly/Continuous/
  152. // Experimental-"sub"targets as e.g. NightlyStart
  153. if (((targetName.find("Nightly") == 0) &&
  154. (targetName != "Nightly")) ||
  155. ((targetName.find("Continuous") == 0) &&
  156. (targetName != "Continuous")) ||
  157. ((targetName.find("Experimental") == 0) &&
  158. (targetName != "Experimental"))) {
  159. break;
  160. }
  161. this->AppendTarget(fout, targetName, *lg, CM_NULLPTR, make.c_str(),
  162. makefile, compiler.c_str(), sourceFileFlags,
  163. false);
  164. break;
  165. case cmState::EXECUTABLE:
  166. case cmState::STATIC_LIBRARY:
  167. case cmState::SHARED_LIBRARY:
  168. case cmState::MODULE_LIBRARY:
  169. case cmState::OBJECT_LIBRARY: {
  170. this->AppendTarget(fout, targetName, *lg, *ti, make.c_str(),
  171. makefile, compiler.c_str(), sourceFileFlags,
  172. false);
  173. std::string fastTarget = targetName;
  174. fastTarget += "/fast";
  175. this->AppendTarget(fout, fastTarget, *lg, *ti, make.c_str(),
  176. makefile, compiler.c_str(), sourceFileFlags,
  177. false);
  178. } break;
  179. default:
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. void cmExtraSublimeTextGenerator::AppendTarget(
  186. cmGeneratedFileStream& fout, const std::string& targetName,
  187. cmLocalGenerator* lg, cmGeneratorTarget* target, const char* make,
  188. const cmMakefile* makefile, const char* /*compiler*/,
  189. MapSourceFileFlags& sourceFileFlags, bool firstTarget)
  190. {
  191. if (target != CM_NULLPTR) {
  192. std::vector<cmSourceFile*> sourceFiles;
  193. target->GetSourceFiles(sourceFiles,
  194. makefile->GetSafeDefinition("CMAKE_BUILD_TYPE"));
  195. std::vector<cmSourceFile*>::const_iterator sourceFilesEnd =
  196. sourceFiles.end();
  197. for (std::vector<cmSourceFile*>::const_iterator iter = sourceFiles.begin();
  198. iter != sourceFilesEnd; ++iter) {
  199. cmSourceFile* sourceFile = *iter;
  200. MapSourceFileFlags::iterator sourceFileFlagsIter =
  201. sourceFileFlags.find(sourceFile->GetFullPath());
  202. if (sourceFileFlagsIter == sourceFileFlags.end()) {
  203. sourceFileFlagsIter =
  204. sourceFileFlags
  205. .insert(MapSourceFileFlags::value_type(sourceFile->GetFullPath(),
  206. std::vector<std::string>()))
  207. .first;
  208. }
  209. std::vector<std::string>& flags = sourceFileFlagsIter->second;
  210. std::string flagsString = this->ComputeFlagsForObject(*iter, lg, target);
  211. std::string definesString = this->ComputeDefines(*iter, lg, target);
  212. flags.clear();
  213. cmsys::RegularExpression flagRegex;
  214. // Regular expression to extract compiler flags from a string
  215. // https://gist.github.com/3944250
  216. const char* regexString =
  217. "(^|[ ])-[DIOUWfgs][^= ]+(=\\\"[^\"]+\\\"|=[^\"][^ ]+)?";
  218. flagRegex.compile(regexString);
  219. std::string workString = flagsString + " " + definesString;
  220. while (flagRegex.find(workString)) {
  221. std::string::size_type start = flagRegex.start();
  222. if (workString[start] == ' ') {
  223. start++;
  224. }
  225. flags.push_back(workString.substr(start, flagRegex.end() - start));
  226. if (flagRegex.end() < workString.size()) {
  227. workString = workString.substr(flagRegex.end());
  228. } else {
  229. workString = "";
  230. }
  231. }
  232. }
  233. }
  234. // Ninja uses ninja.build files (look for a way to get the output file name
  235. // from cmMakefile or something)
  236. std::string makefileName;
  237. if (this->GlobalGenerator->GetName() == "Ninja") {
  238. makefileName = "build.ninja";
  239. } else {
  240. makefileName = "Makefile";
  241. }
  242. if (!firstTarget) {
  243. fout << ",\n\t";
  244. }
  245. fout << "\t{\n\t\t\t\"name\": \"" << lg->GetProjectName() << " - "
  246. << targetName << "\",\n";
  247. fout << "\t\t\t\"cmd\": ["
  248. << this->BuildMakeCommand(make, makefileName.c_str(), targetName)
  249. << "],\n";
  250. fout << "\t\t\t\"working_dir\": \"${project_path}\",\n";
  251. fout << "\t\t\t\"file_regex\": \"^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$\"\n";
  252. fout << "\t\t}";
  253. }
  254. // Create the command line for building the given target using the selected
  255. // make
  256. std::string cmExtraSublimeTextGenerator::BuildMakeCommand(
  257. const std::string& make, const char* makefile, const std::string& target)
  258. {
  259. std::string command = "\"";
  260. command += make + "\"";
  261. std::string generator = this->GlobalGenerator->GetName();
  262. if (generator == "NMake Makefiles") {
  263. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  264. command += ", \"/NOLOGO\", \"/f\", \"";
  265. command += makefileName + "\"";
  266. command += ", \"VERBOSE=1\", \"";
  267. command += target;
  268. command += "\"";
  269. } else if (generator == "Ninja") {
  270. std::string makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  271. command += ", \"-f\", \"";
  272. command += makefileName + "\"";
  273. command += ", \"-v\", \"";
  274. command += target;
  275. command += "\"";
  276. } else {
  277. std::string makefileName;
  278. if (generator == "MinGW Makefiles") {
  279. // no escaping of spaces in this case, see
  280. // https://gitlab.kitware.com/cmake/cmake/issues/10014
  281. makefileName = makefile;
  282. } else {
  283. makefileName = cmSystemTools::ConvertToOutputPath(makefile);
  284. }
  285. command += ", \"-f\", \"";
  286. command += makefileName + "\"";
  287. command += ", \"VERBOSE=1\", \"";
  288. command += target;
  289. command += "\"";
  290. }
  291. return command;
  292. }
  293. // TODO: Most of the code is picked up from the Ninja generator, refactor it.
  294. std::string cmExtraSublimeTextGenerator::ComputeFlagsForObject(
  295. cmSourceFile* source, cmLocalGenerator* lg, cmGeneratorTarget* gtgt)
  296. {
  297. std::string flags;
  298. std::string language = source->GetLanguage();
  299. if (language.empty()) {
  300. language = "C";
  301. }
  302. std::string const& config =
  303. lg->GetMakefile()->GetSafeDefinition("CMAKE_BUILD_TYPE");
  304. lg->GetTargetCompileFlags(gtgt, config, language, flags);
  305. // Add include directory flags.
  306. {
  307. std::vector<std::string> includes;
  308. lg->GetIncludeDirectories(includes, gtgt, language, config);
  309. std::string includeFlags = lg->GetIncludeFlags(includes, gtgt, language,
  310. true); // full include paths
  311. lg->AppendFlags(flags, includeFlags);
  312. }
  313. // Add source file specific flags.
  314. lg->AppendFlags(flags, source->GetProperty("COMPILE_FLAGS"));
  315. return flags;
  316. }
  317. // TODO: Refactor with
  318. // void cmMakefileTargetGenerator::WriteTargetLanguageFlags().
  319. std::string cmExtraSublimeTextGenerator::ComputeDefines(
  320. cmSourceFile* source, cmLocalGenerator* lg, cmGeneratorTarget* target)
  321. {
  322. std::set<std::string> defines;
  323. cmMakefile* makefile = lg->GetMakefile();
  324. const std::string& language = source->GetLanguage();
  325. const std::string& config = makefile->GetSafeDefinition("CMAKE_BUILD_TYPE");
  326. // Add the export symbol definition for shared library objects.
  327. if (const char* exportMacro = target->GetExportMacro()) {
  328. lg->AppendDefines(defines, exportMacro);
  329. }
  330. // Add preprocessor definitions for this target and configuration.
  331. lg->AddCompileDefinitions(defines, target, config, language);
  332. lg->AppendDefines(defines, source->GetProperty("COMPILE_DEFINITIONS"));
  333. {
  334. std::string defPropName = "COMPILE_DEFINITIONS_";
  335. defPropName += cmSystemTools::UpperCase(config);
  336. lg->AppendDefines(defines, source->GetProperty(defPropName));
  337. }
  338. std::string definesString;
  339. lg->JoinDefines(defines, definesString, language);
  340. return definesString;
  341. }