cmExtraKateGenerator.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. const char* homeOutputDir = lg->GetBinaryDirectory();
  74. /* clang-format off */
  75. fout <<
  76. "\t\"build\": {\n"
  77. "\t\t\"directory\": \"" << lg->GetBinaryDirectory() << "\",\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 (std::vector<cmLocalGenerator*>::const_iterator it =
  100. this->GlobalGenerator->GetLocalGenerators().begin();
  101. it != this->GlobalGenerator->GetLocalGenerators().end(); ++it) {
  102. const std::vector<cmGeneratorTarget*>& targets =
  103. (*it)->GetGeneratorTargets();
  104. std::string currentDir = (*it)->GetCurrentBinaryDirectory();
  105. bool topLevel = (currentDir == (*it)->GetBinaryDirectory());
  106. for (std::vector<cmGeneratorTarget*>::const_iterator ti = targets.begin();
  107. ti != targets.end(); ++ti) {
  108. std::string targetName = (*ti)->GetName();
  109. switch ((*ti)->GetType()) {
  110. case cmStateEnums::GLOBAL_TARGET: {
  111. bool insertTarget = false;
  112. // Only add the global targets from CMAKE_BINARY_DIR,
  113. // not from the subdirs
  114. if (topLevel) {
  115. insertTarget = true;
  116. // only add the "edit_cache" target if it's not ccmake, because
  117. // this will not work within the IDE
  118. if (targetName == "edit_cache") {
  119. const char* editCommand =
  120. (*it)->GetMakefile()->GetDefinition("CMAKE_EDIT_COMMAND");
  121. if (editCommand == CM_NULLPTR) {
  122. insertTarget = false;
  123. } else if (strstr(editCommand, "ccmake") != CM_NULLPTR) {
  124. insertTarget = false;
  125. }
  126. }
  127. }
  128. if (insertTarget) {
  129. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  130. homeOutputDir);
  131. }
  132. } break;
  133. case cmStateEnums::UTILITY:
  134. // Add all utility targets, except the Nightly/Continuous/
  135. // Experimental-"sub"targets as e.g. NightlyStart
  136. if (((targetName.find("Nightly") == 0) &&
  137. (targetName != "Nightly")) ||
  138. ((targetName.find("Continuous") == 0) &&
  139. (targetName != "Continuous")) ||
  140. ((targetName.find("Experimental") == 0) &&
  141. (targetName != "Experimental"))) {
  142. break;
  143. }
  144. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  145. homeOutputDir);
  146. break;
  147. case cmStateEnums::EXECUTABLE:
  148. case cmStateEnums::STATIC_LIBRARY:
  149. case cmStateEnums::SHARED_LIBRARY:
  150. case cmStateEnums::MODULE_LIBRARY:
  151. case cmStateEnums::OBJECT_LIBRARY: {
  152. this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
  153. homeOutputDir);
  154. std::string fastTarget = targetName;
  155. fastTarget += "/fast";
  156. this->AppendTarget(fout, fastTarget, make, makeArgs, currentDir,
  157. homeOutputDir);
  158. } break;
  159. default:
  160. break;
  161. }
  162. }
  163. // insert rules for compiling, preprocessing and assembling individual
  164. // files
  165. std::vector<std::string> objectFileTargets;
  166. (*it)->GetIndividualFileTargets(objectFileTargets);
  167. for (std::vector<std::string>::const_iterator fit =
  168. objectFileTargets.begin();
  169. fit != objectFileTargets.end(); ++fit) {
  170. this->AppendTarget(fout, *fit, make, makeArgs, currentDir,
  171. homeOutputDir);
  172. }
  173. }
  174. fout << "\t] }\n";
  175. }
  176. void cmExtraKateGenerator::AppendTarget(cmGeneratedFileStream& fout,
  177. const std::string& target,
  178. const std::string& make,
  179. const std::string& makeArgs,
  180. const std::string& path,
  181. const char* homeOutputDir) const
  182. {
  183. static char JsonSep = ' ';
  184. fout << "\t\t\t" << JsonSep << "{\"name\":\"" << target << "\", "
  185. "\"build_cmd\":\""
  186. << make << " -C \\\"" << (this->UseNinja ? homeOutputDir : path.c_str())
  187. << "\\\" " << makeArgs << " " << target << "\"}\n";
  188. JsonSep = ',';
  189. }
  190. void cmExtraKateGenerator::CreateDummyKateProjectFile(
  191. const cmLocalGenerator* lg) const
  192. {
  193. std::string filename = lg->GetBinaryDirectory();
  194. filename += "/";
  195. filename += this->ProjectName;
  196. filename += ".kateproject";
  197. cmGeneratedFileStream fout(filename.c_str());
  198. if (!fout) {
  199. return;
  200. }
  201. fout << "#Generated by " << cmSystemTools::GetCMakeCommand()
  202. << ", do not edit.\n";
  203. }
  204. std::string cmExtraKateGenerator::GenerateFilesString(
  205. const cmLocalGenerator* lg) const
  206. {
  207. std::string s = lg->GetSourceDirectory();
  208. s += "/.git";
  209. if (cmSystemTools::FileExists(s.c_str())) {
  210. return std::string("\"git\": 1 ");
  211. }
  212. s = lg->GetSourceDirectory();
  213. s += "/.svn";
  214. if (cmSystemTools::FileExists(s.c_str())) {
  215. return std::string("\"svn\": 1 ");
  216. }
  217. s = lg->GetSourceDirectory();
  218. s += "/";
  219. std::set<std::string> files;
  220. std::string tmp;
  221. const std::vector<cmLocalGenerator*>& lgs =
  222. this->GlobalGenerator->GetLocalGenerators();
  223. for (std::vector<cmLocalGenerator*>::const_iterator it = lgs.begin();
  224. it != lgs.end(); it++) {
  225. cmMakefile* makefile = (*it)->GetMakefile();
  226. const std::vector<std::string>& listFiles = makefile->GetListFiles();
  227. for (std::vector<std::string>::const_iterator lt = listFiles.begin();
  228. lt != listFiles.end(); lt++) {
  229. tmp = *lt;
  230. {
  231. files.insert(tmp);
  232. }
  233. }
  234. const std::vector<cmSourceFile*>& sources = makefile->GetSourceFiles();
  235. for (std::vector<cmSourceFile*>::const_iterator sfIt = sources.begin();
  236. sfIt != sources.end(); sfIt++) {
  237. cmSourceFile* sf = *sfIt;
  238. if (sf->GetPropertyAsBool("GENERATED")) {
  239. continue;
  240. }
  241. tmp = sf->GetFullPath();
  242. files.insert(tmp);
  243. }
  244. }
  245. const char* sep = "";
  246. tmp = "\"list\": [";
  247. for (std::set<std::string>::const_iterator it = files.begin();
  248. it != files.end(); ++it) {
  249. tmp += sep;
  250. tmp += " \"";
  251. tmp += *it;
  252. tmp += "\"";
  253. sep = ",";
  254. }
  255. tmp += "] ";
  256. return tmp;
  257. }
  258. std::string cmExtraKateGenerator::GenerateProjectName(
  259. const std::string& name, const std::string& type,
  260. const std::string& path) const
  261. {
  262. return name + (type.empty() ? "" : "-") + type + "@" + path;
  263. }
  264. std::string cmExtraKateGenerator::GetPathBasename(
  265. const std::string& path) const
  266. {
  267. std::string outputBasename = path;
  268. while (!outputBasename.empty() &&
  269. (outputBasename[outputBasename.size() - 1] == '/' ||
  270. outputBasename[outputBasename.size() - 1] == '\\')) {
  271. outputBasename.resize(outputBasename.size() - 1);
  272. }
  273. std::string::size_type loc = outputBasename.find_last_of("/\\");
  274. if (loc != std::string::npos) {
  275. outputBasename = outputBasename.substr(loc + 1);
  276. }
  277. return outputBasename;
  278. }