cmExtraSublimeTextGenerator.cxx 13 KB

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