cmExtraKateGenerator.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. : cmExternalMakefileProjectGenerator()
  18. {
  19. }
  20. cmExternalMakefileProjectGeneratorFactory* cmExtraKateGenerator::GetFactory()
  21. {
  22. static cmExternalMakefileProjectGeneratorSimpleFactory<cmExtraKateGenerator>
  23. factory("Kate", "Generates Kate project files.");
  24. if (factory.GetSupportedGlobalGenerators().empty()) {
  25. #if defined(_WIN32)
  26. factory.AddSupportedGlobalGenerator("MinGW Makefiles");
  27. factory.AddSupportedGlobalGenerator("NMake Makefiles");
  28. // disable until somebody actually tests it:
  29. // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
  30. #endif
  31. factory.AddSupportedGlobalGenerator("Ninja");
  32. factory.AddSupportedGlobalGenerator("Unix Makefiles");
  33. }
  34. return &factory;
  35. }
  36. void cmExtraKateGenerator::Generate()
  37. {
  38. cmLocalGenerator* lg = this->GlobalGenerator->GetLocalGenerators()[0];
  39. const cmMakefile* mf = lg->GetMakefile();
  40. this->ProjectName = this->GenerateProjectName(
  41. lg->GetProjectName(), mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
  42. this->GetPathBasename(lg->GetBinaryDirectory()));
  43. this->UseNinja = (this->GlobalGenerator->GetName() == "Ninja");
  44. this->CreateKateProjectFile(lg);
  45. this->CreateDummyKateProjectFile(lg);
  46. }
  47. void cmExtraKateGenerator::CreateKateProjectFile(
  48. const cmLocalGenerator* lg) const
  49. {
  50. std::string filename = lg->GetBinaryDirectory();
  51. filename += "/.kateproject";
  52. cmGeneratedFileStream fout(filename.c_str());
  53. if (!fout) {
  54. return;
  55. }
  56. /* clang-format off */
  57. fout <<
  58. "{\n"
  59. "\t\"name\": \"" << this->ProjectName << "\",\n"
  60. "\t\"directory\": \"" << lg->GetSourceDirectory() << "\",\n"
  61. "\t\"files\": [ { " << this->GenerateFilesString(lg) << "} ],\n";
  62. /* clang-format on */
  63. this->WriteTargets(lg, fout);
  64. fout << "}\n";
  65. }
  66. void cmExtraKateGenerator::WriteTargets(const cmLocalGenerator* lg,
  67. cmGeneratedFileStream& fout) const
  68. {
  69. cmMakefile const* mf = lg->GetMakefile();
  70. const std::string make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
  71. const std::string makeArgs =
  72. mf->GetSafeDefinition("CMAKE_KATE_MAKE_ARGUMENTS");
  73. std::string const& homeOutputDir = lg->GetBinaryDirectory();
  74. /* clang-format off */
  75. fout <<
  76. "\t\"build\": {\n"
  77. "\t\t\"directory\": \"" << homeOutputDir << "\",\n"
  78. "\t\t\"default_target\": \"all\",\n"
  79. "\t\t\"clean_target\": \"clean\",\n";
  80. /* clang-format on */
  81. // build, clean and quick are for the build plugin kate <= 4.12:
  82. fout << "\t\t\"build\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
  83. << makeArgs << " "
  84. << "all\",\n";
  85. fout << "\t\t\"clean\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
  86. << makeArgs << " "
  87. << "clean\",\n";
  88. fout << "\t\t\"quick\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
  89. << makeArgs << " "
  90. << "install\",\n";
  91. // this is for kate >= 4.13:
  92. fout << "\t\t\"targets\":[\n";
  93. this->AppendTarget(fout, "all", make, makeArgs, homeOutputDir,
  94. homeOutputDir);
  95. this->AppendTarget(fout, "clean", make, makeArgs, homeOutputDir,
  96. homeOutputDir);
  97. // add all executable and library targets and some of the GLOBAL
  98. // and UTILITY targets
  99. for (cmLocalGenerator* localGen :
  100. this->GlobalGenerator->GetLocalGenerators()) {
  101. const std::vector<cmGeneratorTarget*>& targets =
  102. localGen->GetGeneratorTargets();
  103. std::string currentDir = localGen->GetCurrentBinaryDirectory();
  104. bool topLevel = (currentDir == localGen->GetBinaryDirectory());
  105. for (cmGeneratorTarget* target : targets) {
  106. std::string const& targetName = target->GetName();
  107. switch (target->GetType()) {
  108. case cmStateEnums::GLOBAL_TARGET: {
  109. bool insertTarget = false;
  110. // Only add the global targets from CMAKE_BINARY_DIR,
  111. // not from the subdirs
  112. if (topLevel) {
  113. insertTarget = true;
  114. // only add the "edit_cache" target if it's not ccmake, because
  115. // this will not work within the IDE
  116. if (targetName == "edit_cache") {
  117. const char* editCommand =
  118. localGen->GetMakefile()->GetDefinition("CMAKE_EDIT_COMMAND");
  119. if (editCommand == nullptr) {
  120. insertTarget = false;
  121. } else if (strstr(editCommand, "ccmake") != nullptr) {
  122. insertTarget = false;
  123. }
  124. }
  125. }
  126. if (insertTarget) {
  127. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  128. homeOutputDir);
  129. }
  130. } break;
  131. case cmStateEnums::UTILITY:
  132. // Add all utility targets, except the Nightly/Continuous/
  133. // Experimental-"sub"targets as e.g. NightlyStart
  134. if (((targetName.find("Nightly") == 0) &&
  135. (targetName != "Nightly")) ||
  136. ((targetName.find("Continuous") == 0) &&
  137. (targetName != "Continuous")) ||
  138. ((targetName.find("Experimental") == 0) &&
  139. (targetName != "Experimental"))) {
  140. break;
  141. }
  142. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  143. homeOutputDir);
  144. break;
  145. case cmStateEnums::EXECUTABLE:
  146. case cmStateEnums::STATIC_LIBRARY:
  147. case cmStateEnums::SHARED_LIBRARY:
  148. case cmStateEnums::MODULE_LIBRARY:
  149. case cmStateEnums::OBJECT_LIBRARY: {
  150. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  151. homeOutputDir);
  152. std::string fastTarget = targetName;
  153. fastTarget += "/fast";
  154. this->AppendTarget(fout, fastTarget, make, makeArgs, currentDir,
  155. homeOutputDir);
  156. } break;
  157. default:
  158. break;
  159. }
  160. }
  161. // insert rules for compiling, preprocessing and assembling individual
  162. // files
  163. std::vector<std::string> objectFileTargets;
  164. localGen->GetIndividualFileTargets(objectFileTargets);
  165. for (std::string const& f : objectFileTargets) {
  166. this->AppendTarget(fout, f, make, makeArgs, currentDir, homeOutputDir);
  167. }
  168. }
  169. fout << "\t] }\n";
  170. }
  171. void cmExtraKateGenerator::AppendTarget(cmGeneratedFileStream& fout,
  172. const std::string& target,
  173. const std::string& make,
  174. const std::string& makeArgs,
  175. const std::string& path,
  176. const std::string& homeOutputDir) const
  177. {
  178. static char JsonSep = ' ';
  179. fout << "\t\t\t" << JsonSep << "{\"name\":\"" << target << "\", "
  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.c_str());
  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[outputBasename.size() - 1] == '/' ||
  260. outputBasename[outputBasename.size() - 1] == '\\')) {
  261. outputBasename.resize(outputBasename.size() - 1);
  262. }
  263. std::string::size_type loc = outputBasename.find_last_of("/\\");
  264. if (loc != std::string::npos) {
  265. outputBasename = outputBasename.substr(loc + 1);
  266. }
  267. return outputBasename;
  268. }