cmExtraKateGenerator.cxx 9.5 KB

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