cmExtraKateGenerator.cxx 9.5 KB

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