cmExtraKateGenerator.cxx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 "cmSystemTools.h"
  12. #include <ostream>
  13. #include <set>
  14. #include <string.h>
  15. #include <vector>
  16. cmExtraKateGenerator::cmExtraKateGenerator() = default;
  17. cmExternalMakefileProjectGeneratorFactory* cmExtraKateGenerator::GetFactory()
  18. {
  19. static cmExternalMakefileProjectGeneratorSimpleFactory<cmExtraKateGenerator>
  20. factory("Kate", "Generates Kate project files.");
  21. if (factory.GetSupportedGlobalGenerators().empty()) {
  22. #if defined(_WIN32)
  23. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  24. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  25. // disable until somebody actually tests it:
  26. // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
  27. #endif
  28. factory.AddSupportedGlobalGenerator("Ninja");
  29. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  30. }
  31. return &factory;
  32. }
  33. void cmExtraKateGenerator::Generate()
  34. {
  35. cmLocalGenerator* lg = this->GlobalGenerator->GetLocalGenerators()[0];
  36. const cmMakefile* mf = lg->GetMakefile();
  37. this->ProjectName = this->GenerateProjectName(
  38. lg->GetProjectName(), mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  39. this->GetPathBasename(lg->GetBinaryDirectory()));
  40. this->UseNinja = (this->GlobalGenerator->GetName() == "Ninja");
  41. this->CreateKateProjectFile(lg);
  42. this->CreateDummyKateProjectFile(lg);
  43. }
  44. void cmExtraKateGenerator::CreateKateProjectFile(
  45. const cmLocalGenerator* lg) const
  46. {
  47. std::string filename = lg->GetBinaryDirectory();
  48. filename += "/.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 = targetName;
  150. fastTarget += "/fast";
  151. this->AppendTarget(fout, fastTarget, make, makeArgs, currentDir,
  152. homeOutputDir);
  153. } break;
  154. default:
  155. break;
  156. }
  157. }
  158. // insert rules for compiling, preprocessing and assembling individual
  159. // files
  160. std::vector<std::string> objectFileTargets;
  161. localGen->GetIndividualFileTargets(objectFileTargets);
  162. for (std::string const& f : objectFileTargets) {
  163. this->AppendTarget(fout, f, make, makeArgs, currentDir, homeOutputDir);
  164. }
  165. }
  166. fout << "\t] }\n";
  167. }
  168. void cmExtraKateGenerator::AppendTarget(cmGeneratedFileStream& fout,
  169. const std::string& target,
  170. const std::string& make,
  171. const std::string& makeArgs,
  172. const std::string& path,
  173. const std::string& homeOutputDir) const
  174. {
  175. static char JsonSep = ' ';
  176. fout << "\t\t\t" << JsonSep << "{\"name\":\"" << target
  177. << "\", "
  178. "\"build_cmd\":\""
  179. << make << " -C \\\"" << (this->UseNinja ? homeOutputDir : path)
  180. << "\\\" " << makeArgs << " " << target << "\"}\n";
  181. JsonSep = ',';
  182. }
  183. void cmExtraKateGenerator::CreateDummyKateProjectFile(
  184. const cmLocalGenerator* lg) const
  185. {
  186. std::string filename = lg->GetBinaryDirectory();
  187. filename += "/";
  188. filename += this->ProjectName;
  189. filename += ".kateproject";
  190. cmGeneratedFileStream fout(filename);
  191. if (!fout) {
  192. return;
  193. }
  194. fout << "#Generated by " << cmSystemTools::GetCMakeCommand()
  195. << ", do not edit.\n";
  196. }
  197. std::string cmExtraKateGenerator::GenerateFilesString(
  198. const cmLocalGenerator* lg) const
  199. {
  200. std::string s = lg->GetSourceDirectory();
  201. s += "/.git";
  202. if (cmSystemTools::FileExists(s)) {
  203. return "\"git\": 1 ";
  204. }
  205. s = lg->GetSourceDirectory();
  206. s += "/.svn";
  207. if (cmSystemTools::FileExists(s)) {
  208. return "\"svn\": 1 ";
  209. }
  210. s = lg->GetSourceDirectory();
  211. s += "/";
  212. std::set<std::string> files;
  213. std::string tmp;
  214. const std::vector<cmLocalGenerator*>& lgs =
  215. this->GlobalGenerator->GetLocalGenerators();
  216. for (cmLocalGenerator* lgen : lgs) {
  217. cmMakefile* makefile = lgen->GetMakefile();
  218. const std::vector<std::string>& listFiles = makefile->GetListFiles();
  219. for (std::string const& listFile : listFiles) {
  220. tmp = listFile;
  221. {
  222. files.insert(tmp);
  223. }
  224. }
  225. const std::vector<cmSourceFile*>& sources = makefile->GetSourceFiles();
  226. for (cmSourceFile* sf : sources) {
  227. if (sf->GetIsGenerated()) {
  228. continue;
  229. }
  230. tmp = sf->GetFullPath();
  231. files.insert(tmp);
  232. }
  233. }
  234. const char* sep = "";
  235. tmp = "\"list\": [";
  236. for (std::string const& f : files) {
  237. tmp += sep;
  238. tmp += " \"";
  239. tmp += f;
  240. tmp += "\"";
  241. sep = ",";
  242. }
  243. tmp += "] ";
  244. return tmp;
  245. }
  246. std::string cmExtraKateGenerator::GenerateProjectName(
  247. const std::string& name, const std::string& type,
  248. const std::string& path) const
  249. {
  250. return name + (type.empty() ? "" : "-") + type + "@" + path;
  251. }
  252. std::string cmExtraKateGenerator::GetPathBasename(
  253. const std::string& path) const
  254. {
  255. std::string outputBasename = path;
  256. while (!outputBasename.empty() &&
  257. (outputBasename.back() == '/' || outputBasename.back() == '\\')) {
  258. outputBasename.resize(outputBasename.size() - 1);
  259. }
  260. std::string::size_type loc = outputBasename.find_last_of("/\\");
  261. if (loc != std::string::npos) {
  262. outputBasename = outputBasename.substr(loc + 1);
  263. }
  264. return outputBasename;
  265. }