cmExtraKateGenerator.cxx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "cmExtraKateGenerator.h"
  4. #include <cstring>
  5. #include <memory>
  6. #include <ostream>
  7. #include <set>
  8. #include <vector>
  9. #include "cmGeneratedFileStream.h"
  10. #include "cmGeneratorTarget.h"
  11. #include "cmGlobalGenerator.h"
  12. #include "cmLocalGenerator.h"
  13. #include "cmMakefile.h"
  14. #include "cmSourceFile.h"
  15. #include "cmStateTypes.h"
  16. #include "cmStringAlgorithms.h"
  17. #include "cmSystemTools.h"
  18. cmExtraKateGenerator::cmExtraKateGenerator() = default;
  19. cmExternalMakefileProjectGeneratorFactory* cmExtraKateGenerator::GetFactory()
  20. {
  21. static cmExternalMakefileProjectGeneratorSimpleFactory<cmExtraKateGenerator>
  22. factory("Kate", "Generates Kate project files.");
  23. if (factory.GetSupportedGlobalGenerators().empty()) {
  24. #if defined(_WIN32)
  25. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  26. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  27. // disable until somebody actually tests it:
  28. // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
  29. #endif
  30. factory.AddSupportedGlobalGenerator("Ninja");
  31. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  32. }
  33. return &factory;
  34. }
  35. void cmExtraKateGenerator::Generate()
  36. {
  37. const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
  38. const cmMakefile* mf = lg->GetMakefile();
  39. this->ProjectName = this->GenerateProjectName(
  40. lg->GetProjectName(), mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  41. this->GetPathBasename(lg->GetBinaryDirectory()));
  42. this->UseNinja = (this->GlobalGenerator->GetName() == "Ninja");
  43. this->CreateKateProjectFile(*lg);
  44. this->CreateDummyKateProjectFile(*lg);
  45. }
  46. void cmExtraKateGenerator::CreateKateProjectFile(
  47. const cmLocalGenerator& lg) const
  48. {
  49. std::string filename = cmStrCat(lg.GetBinaryDirectory(), "/.kateproject");
  50. cmGeneratedFileStream fout(filename);
  51. if (!fout) {
  52. return;
  53. }
  54. /* clang-format off */
  55. fout <<
  56. "{\n"
  57. "\t\"name\": \"" << this->ProjectName << "\",\n"
  58. "\t\"directory\": \"" << lg.GetSourceDirectory() << "\",\n"
  59. "\t\"files\": [ { " << this->GenerateFilesString(lg) << "} ],\n";
  60. /* clang-format on */
  61. this->WriteTargets(lg, fout);
  62. fout << "}\n";
  63. }
  64. void cmExtraKateGenerator::WriteTargets(const cmLocalGenerator& lg,
  65. cmGeneratedFileStream& fout) const
  66. {
  67. cmMakefile const* mf = lg.GetMakefile();
  68. const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  69. const std::string& makeArgs =
  70. mf->GetSafeDefinition("CMAKE_KATE_MAKE_ARGUMENTS");
  71. std::string const& homeOutputDir = lg.GetBinaryDirectory();
  72. /* clang-format off */
  73. fout <<
  74. "\t\"build\": {\n"
  75. "\t\t\"directory\": \"" << homeOutputDir << "\",\n"
  76. "\t\t\"default_target\": \"all\",\n"
  77. "\t\t\"clean_target\": \"clean\",\n";
  78. /* clang-format on */
  79. // build, clean and quick are for the build plugin kate <= 4.12:
  80. fout << "\t\t\"build\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
  81. << makeArgs << " "
  82. << "all\",\n";
  83. fout << "\t\t\"clean\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
  84. << makeArgs << " "
  85. << "clean\",\n";
  86. fout << "\t\t\"quick\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
  87. << makeArgs << " "
  88. << "install\",\n";
  89. // this is for kate >= 4.13:
  90. fout << "\t\t\"targets\":[\n";
  91. this->AppendTarget(fout, "all", make, makeArgs, homeOutputDir,
  92. homeOutputDir);
  93. this->AppendTarget(fout, "clean", make, makeArgs, homeOutputDir,
  94. homeOutputDir);
  95. // add all executable and library targets and some of the GLOBAL
  96. // and UTILITY targets
  97. for (const auto& localGen : this->GlobalGenerator->GetLocalGenerators()) {
  98. const auto& targets = localGen->GetGeneratorTargets();
  99. std::string currentDir = localGen->GetCurrentBinaryDirectory();
  100. bool topLevel = (currentDir == localGen->GetBinaryDirectory());
  101. for (const auto& target : targets) {
  102. std::string const& targetName = target->GetName();
  103. switch (target->GetType()) {
  104. case cmStateEnums::GLOBAL_TARGET: {
  105. bool insertTarget = false;
  106. // Only add the global targets from CMAKE_BINARY_DIR,
  107. // not from the subdirs
  108. if (topLevel) {
  109. insertTarget = true;
  110. // only add the "edit_cache" target if it's not ccmake, because
  111. // this will not work within the IDE
  112. if (targetName == "edit_cache") {
  113. const char* editCommand =
  114. localGen->GetMakefile()->GetDefinition("CMAKE_EDIT_COMMAND");
  115. if (editCommand == nullptr) {
  116. insertTarget = false;
  117. } else if (strstr(editCommand, "ccmake") != nullptr) {
  118. insertTarget = false;
  119. }
  120. }
  121. }
  122. if (insertTarget) {
  123. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  124. homeOutputDir);
  125. }
  126. } break;
  127. case cmStateEnums::UTILITY:
  128. // Add all utility targets, except the Nightly/Continuous/
  129. // Experimental-"sub"targets as e.g. NightlyStart
  130. if ((cmHasLiteralPrefix(targetName, "Nightly") &&
  131. (targetName != "Nightly")) ||
  132. (cmHasLiteralPrefix(targetName, "Continuous") &&
  133. (targetName != "Continuous")) ||
  134. (cmHasLiteralPrefix(targetName, "Experimental") &&
  135. (targetName != "Experimental"))) {
  136. break;
  137. }
  138. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  139. homeOutputDir);
  140. break;
  141. case cmStateEnums::EXECUTABLE:
  142. case cmStateEnums::STATIC_LIBRARY:
  143. case cmStateEnums::SHARED_LIBRARY:
  144. case cmStateEnums::MODULE_LIBRARY:
  145. case cmStateEnums::OBJECT_LIBRARY: {
  146. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  147. homeOutputDir);
  148. std::string fastTarget = cmStrCat(targetName, "/fast");
  149. this->AppendTarget(fout, fastTarget, make, makeArgs, currentDir,
  150. homeOutputDir);
  151. } break;
  152. default:
  153. break;
  154. }
  155. }
  156. // insert rules for compiling, preprocessing and assembling individual
  157. // files
  158. std::vector<std::string> objectFileTargets;
  159. localGen->GetIndividualFileTargets(objectFileTargets);
  160. for (std::string const& f : objectFileTargets) {
  161. this->AppendTarget(fout, f, make, makeArgs, currentDir, homeOutputDir);
  162. }
  163. }
  164. fout << "\t] }\n";
  165. }
  166. void cmExtraKateGenerator::AppendTarget(cmGeneratedFileStream& fout,
  167. const std::string& target,
  168. const std::string& make,
  169. const std::string& makeArgs,
  170. const std::string& path,
  171. const std::string& homeOutputDir) const
  172. {
  173. static char JsonSep = ' ';
  174. fout << "\t\t\t" << JsonSep << R"({"name":")" << target
  175. << "\", "
  176. "\"build_cmd\":\""
  177. << make << " -C \\\"" << (this->UseNinja ? homeOutputDir : path)
  178. << "\\\" " << makeArgs << " " << target << "\"}\n";
  179. JsonSep = ',';
  180. }
  181. void cmExtraKateGenerator::CreateDummyKateProjectFile(
  182. const cmLocalGenerator& lg) const
  183. {
  184. std::string filename =
  185. cmStrCat(lg.GetBinaryDirectory(), '/', this->ProjectName, ".kateproject");
  186. cmGeneratedFileStream fout(filename);
  187. if (!fout) {
  188. return;
  189. }
  190. fout << "#Generated by " << cmSystemTools::GetCMakeCommand()
  191. << ", do not edit.\n";
  192. }
  193. std::string cmExtraKateGenerator::GenerateFilesString(
  194. const cmLocalGenerator& lg) const
  195. {
  196. std::string s = cmStrCat(lg.GetSourceDirectory(), "/.git");
  197. if (cmSystemTools::FileExists(s)) {
  198. return "\"git\": 1 ";
  199. }
  200. s = cmStrCat(lg.GetSourceDirectory(), "/.svn");
  201. if (cmSystemTools::FileExists(s)) {
  202. return "\"svn\": 1 ";
  203. }
  204. s = cmStrCat(lg.GetSourceDirectory(), '/');
  205. std::set<std::string> files;
  206. std::string tmp;
  207. const auto& lgs = this->GlobalGenerator->GetLocalGenerators();
  208. for (const auto& lgen : lgs) {
  209. cmMakefile* makefile = lgen->GetMakefile();
  210. const std::vector<std::string>& listFiles = makefile->GetListFiles();
  211. for (std::string const& listFile : listFiles) {
  212. tmp = listFile;
  213. {
  214. files.insert(tmp);
  215. }
  216. }
  217. for (const auto& sf : makefile->GetSourceFiles()) {
  218. if (sf->GetIsGenerated()) {
  219. continue;
  220. }
  221. tmp = sf->ResolveFullPath();
  222. files.insert(tmp);
  223. }
  224. }
  225. const char* sep = "";
  226. tmp = "\"list\": [";
  227. for (std::string const& f : files) {
  228. tmp += sep;
  229. tmp += " \"";
  230. tmp += f;
  231. tmp += "\"";
  232. sep = ",";
  233. }
  234. tmp += "] ";
  235. return tmp;
  236. }
  237. std::string cmExtraKateGenerator::GenerateProjectName(
  238. const std::string& name, const std::string& type,
  239. const std::string& path) const
  240. {
  241. return name + (type.empty() ? "" : "-") + type + "@" + path;
  242. }
  243. std::string cmExtraKateGenerator::GetPathBasename(
  244. const std::string& path) const
  245. {
  246. std::string outputBasename = path;
  247. while (!outputBasename.empty() &&
  248. (outputBasename.back() == '/' || outputBasename.back() == '\\')) {
  249. outputBasename.resize(outputBasename.size() - 1);
  250. }
  251. std::string::size_type loc = outputBasename.find_last_of("/\\");
  252. if (loc != std::string::npos) {
  253. outputBasename = outputBasename.substr(loc + 1);
  254. }
  255. return outputBasename;
  256. }