cmExtraSublimeTextGenerator.cxx 15 KB

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