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"
11 #include "cmGeneratedFileStream.h"
12 #include "cmGeneratorTarget.h"
13 #include "cmGlobalGenerator.h"
14 #include "cmLocalGenerator.h"
15 #include "cmMakefile.h"
16 #include "cmSourceFile.h"
17 #include "cmStateTypes.h"
18 #include "cmStringAlgorithms.h"
19 #include "cmSystemTools.h"
22 cmExtraKateGenerator::cmExtraKateGenerator() = default;
24 cmExternalMakefileProjectGeneratorFactory* cmExtraKateGenerator::GetFactory()
26 static cmExternalMakefileProjectGeneratorSimpleFactory<cmExtraKateGenerator>
27 factory("Kate", "Generates Kate project files.");
29 if (factory.GetSupportedGlobalGenerators().empty()) {
31 factory.AddSupportedGlobalGenerator("MinGW Makefiles");
32 factory.AddSupportedGlobalGenerator("NMake Makefiles");
33 // disable until somebody actually tests it:
34 // factory.AddSupportedGlobalGenerator("MSYS Makefiles");
36 factory.AddSupportedGlobalGenerator("Ninja");
37 factory.AddSupportedGlobalGenerator("Unix Makefiles");
43 void cmExtraKateGenerator::Generate()
45 const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
46 const cmMakefile* mf = lg->GetMakefile();
47 this->ProjectName = this->GenerateProjectName(
48 lg->GetProjectName(), mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
49 this->GetPathBasename(lg->GetBinaryDirectory()));
50 this->UseNinja = (this->GlobalGenerator->GetName() == "Ninja");
52 this->CreateKateProjectFile(*lg);
53 this->CreateDummyKateProjectFile(*lg);
56 void cmExtraKateGenerator::CreateKateProjectFile(
57 const cmLocalGenerator& lg) const
59 std::string filename = cmStrCat(lg.GetBinaryDirectory(), "/.kateproject");
60 cmGeneratedFileStream fout(filename);
65 /* clang-format off */
68 "\t\"name\": \"" << this->ProjectName << "\",\n"
69 "\t\"directory\": \"" << lg.GetSourceDirectory() << "\",\n"
70 "\t\"files\": [ { " << this->GenerateFilesString(lg) << "} ],\n";
72 this->WriteTargets(lg, fout);
76 void cmExtraKateGenerator::WriteTargets(const cmLocalGenerator& lg,
77 cmGeneratedFileStream& fout) const
79 cmMakefile const* mf = lg.GetMakefile();
80 const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
81 const std::string& makeArgs =
82 mf->GetSafeDefinition("CMAKE_KATE_MAKE_ARGUMENTS");
83 std::string const& homeOutputDir = lg.GetBinaryDirectory();
85 /* clang-format off */
88 "\t\t\"directory\": \"" << homeOutputDir << "\",\n"
89 "\t\t\"default_target\": \"all\",\n"
90 "\t\t\"clean_target\": \"clean\",\n";
93 // build, clean and quick are for the build plugin kate <= 4.12:
94 fout << "\t\t\"build\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
97 fout << "\t\t\"clean\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
100 fout << "\t\t\"quick\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
104 // this is for kate >= 4.13:
105 fout << "\t\t\"targets\":[\n";
107 this->AppendTarget(fout, "all", make, makeArgs, homeOutputDir,
109 this->AppendTarget(fout, "clean", make, makeArgs, homeOutputDir,
112 // add all executable and library targets and some of the GLOBAL
113 // and UTILITY targets
114 for (const auto& localGen : this->GlobalGenerator->GetLocalGenerators()) {
115 const auto& targets = localGen->GetGeneratorTargets();
116 std::string currentDir = localGen->GetCurrentBinaryDirectory();
117 bool topLevel = (currentDir == localGen->GetBinaryDirectory());
119 for (const auto& target : targets) {
120 std::string const& targetName = target->GetName();
121 switch (target->GetType()) {
122 case cmStateEnums::GLOBAL_TARGET: {
123 bool insertTarget = false;
124 // Only add the global targets from CMAKE_BINARY_DIR,
125 // not from the subdirs
128 // only add the "edit_cache" target if it's not ccmake, because
129 // this will not work within the IDE
130 if (targetName == "edit_cache") {
131 cmValue editCommand =
132 localGen->GetMakefile()->GetDefinition("CMAKE_EDIT_COMMAND");
134 strstr(editCommand->c_str(), "ccmake") != nullptr) {
135 insertTarget = false;
140 this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
144 case cmStateEnums::UTILITY:
145 // Add all utility targets, except the Nightly/Continuous/
146 // Experimental-"sub"targets as e.g. NightlyStart
147 if ((cmHasLiteralPrefix(targetName, "Nightly") &&
148 (targetName != "Nightly")) ||
149 (cmHasLiteralPrefix(targetName, "Continuous") &&
150 (targetName != "Continuous")) ||
151 (cmHasLiteralPrefix(targetName, "Experimental") &&
152 (targetName != "Experimental"))) {
156 this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
159 case cmStateEnums::EXECUTABLE:
160 case cmStateEnums::STATIC_LIBRARY:
161 case cmStateEnums::SHARED_LIBRARY:
162 case cmStateEnums::MODULE_LIBRARY:
163 case cmStateEnums::OBJECT_LIBRARY: {
164 this->AppendTarget(fout, targetName, make, makeArgs, currentDir,
166 std::string fastTarget = cmStrCat(targetName, "/fast");
167 this->AppendTarget(fout, fastTarget, make, makeArgs, currentDir,
176 // insert rules for compiling, preprocessing and assembling individual
178 std::vector<std::string> objectFileTargets;
179 localGen->GetIndividualFileTargets(objectFileTargets);
180 for (std::string const& f : objectFileTargets) {
181 this->AppendTarget(fout, f, make, makeArgs, currentDir, homeOutputDir);
188 void cmExtraKateGenerator::AppendTarget(cmGeneratedFileStream& fout,
189 const std::string& target,
190 const std::string& make,
191 const std::string& makeArgs,
192 const std::string& path,
193 const std::string& homeOutputDir) const
195 static char JsonSep = ' ';
197 fout << "\t\t\t" << JsonSep << R"({"name":")" << target
200 << make << " -C \\\"" << (this->UseNinja ? homeOutputDir : path)
201 << "\\\" " << makeArgs << " " << target << "\"}\n";
206 void cmExtraKateGenerator::CreateDummyKateProjectFile(
207 const cmLocalGenerator& lg) const
209 std::string filename =
210 cmStrCat(lg.GetBinaryDirectory(), '/', this->ProjectName, ".kateproject");
211 cmGeneratedFileStream fout(filename);
216 fout << "#Generated by " << cmSystemTools::GetCMakeCommand()
217 << ", do not edit.\n";
220 std::string cmExtraKateGenerator::GenerateFilesString(
221 const cmLocalGenerator& lg) const
223 std::string s = cmStrCat(lg.GetSourceDirectory(), "/.git");
224 if (cmSystemTools::FileExists(s)) {
225 return "\"git\": 1 ";
228 s = cmStrCat(lg.GetSourceDirectory(), "/.svn");
229 if (cmSystemTools::FileExists(s)) {
230 return "\"svn\": 1 ";
233 s = cmStrCat(lg.GetSourceDirectory(), '/');
235 std::set<std::string> files;
237 const auto& lgs = this->GlobalGenerator->GetLocalGenerators();
239 for (const auto& lgen : lgs) {
240 cmMakefile* makefile = lgen->GetMakefile();
241 const std::vector<std::string>& listFiles = makefile->GetListFiles();
242 for (std::string const& listFile : listFiles) {
249 for (const auto& sf : makefile->GetSourceFiles()) {
250 if (sf->GetIsGenerated()) {
254 tmp = sf->ResolveFullPath();
259 const char* sep = "";
261 for (std::string const& f : files) {
273 std::string cmExtraKateGenerator::GenerateProjectName(
274 const std::string& name, const std::string& type,
275 const std::string& path) const
277 return name + (type.empty() ? "" : "-") + type + '@' + path;
280 std::string cmExtraKateGenerator::GetPathBasename(
281 const std::string& path) const
283 std::string outputBasename = path;
284 while (!outputBasename.empty() &&
285 (outputBasename.back() == '/' || outputBasename.back() == '\\')) {
286 outputBasename.resize(outputBasename.size() - 1);
288 std::string::size_type loc = outputBasename.find_last_of("/\\");
289 if (loc != std::string::npos) {
290 outputBasename = outputBasename.substr(loc + 1);
293 return outputBasename;