cmake/Source/cmExtraKateGenerator.cxx

349 lines
11 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2014-08-03 19:52:23 +02:00
#include "cmExtraKateGenerator.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <cstring>
2020-08-30 11:54:41 +02:00
#include <memory>
2020-02-01 23:06:01 +01:00
#include <ostream>
#include <set>
#include <vector>
2023-07-02 19:51:09 +02:00
#include "cmCMakePath.h"
2016-07-09 11:21:54 +02:00
#include "cmGeneratedFileStream.h"
2016-10-30 18:24:19 +01:00
#include "cmGeneratorTarget.h"
#include "cmGlobalGenerator.h"
#include "cmLocalGenerator.h"
2014-08-03 19:52:23 +02:00
#include "cmMakefile.h"
#include "cmSourceFile.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2014-08-03 19:52:23 +02:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2014-08-03 19:52:23 +02:00
2019-11-11 23:01:05 +01:00
cmExtraKateGenerator::cmExtraKateGenerator() = default;
2014-08-03 19:52:23 +02:00
2016-10-30 18:24:19 +01:00
cmExternalMakefileProjectGeneratorFactory* cmExtraKateGenerator::GetFactory()
2014-08-03 19:52:23 +02:00
{
2016-10-30 18:24:19 +01:00
static cmExternalMakefileProjectGeneratorSimpleFactory<cmExtraKateGenerator>
2023-07-02 19:51:09 +02:00
factory("Kate", "Generates Kate project files (deprecated).");
2016-10-30 18:24:19 +01:00
if (factory.GetSupportedGlobalGenerators().empty()) {
2014-08-03 19:52:23 +02:00
#if defined(_WIN32)
2016-10-30 18:24:19 +01:00
factory.AddSupportedGlobalGenerator("MinGW Makefiles");
factory.AddSupportedGlobalGenerator("NMake Makefiles");
2014-08-03 19:52:23 +02:00
// disable until somebody actually tests it:
2016-10-30 18:24:19 +01:00
// factory.AddSupportedGlobalGenerator("MSYS Makefiles");
2014-08-03 19:52:23 +02:00
#endif
2016-10-30 18:24:19 +01:00
factory.AddSupportedGlobalGenerator("Ninja");
2023-07-02 19:51:09 +02:00
factory.AddSupportedGlobalGenerator("Ninja Multi-Config");
2016-10-30 18:24:19 +01:00
factory.AddSupportedGlobalGenerator("Unix Makefiles");
}
return &factory;
2014-08-03 19:52:23 +02:00
}
void cmExtraKateGenerator::Generate()
{
2020-08-30 11:54:41 +02:00
const auto& lg = this->GlobalGenerator->GetLocalGenerators()[0];
2016-03-13 13:35:51 +01:00
const cmMakefile* mf = lg->GetMakefile();
2016-07-09 11:21:54 +02:00
this->ProjectName = this->GenerateProjectName(
lg->GetProjectName(), mf->GetSafeDefinition("CMAKE_BUILD_TYPE"),
this->GetPathBasename(lg->GetBinaryDirectory()));
2023-07-02 19:51:09 +02:00
this->UseNinja =
((this->GlobalGenerator->GetName() == "Ninja") ||
(this->GlobalGenerator->GetName() == "Ninja Multi-Config"));
2014-08-03 19:52:23 +02:00
2020-08-30 11:54:41 +02:00
this->CreateKateProjectFile(*lg);
this->CreateDummyKateProjectFile(*lg);
2014-08-03 19:52:23 +02:00
}
2016-03-13 13:35:51 +01:00
void cmExtraKateGenerator::CreateKateProjectFile(
2020-08-30 11:54:41 +02:00
const cmLocalGenerator& lg) const
2014-08-03 19:52:23 +02:00
{
2020-08-30 11:54:41 +02:00
std::string filename = cmStrCat(lg.GetBinaryDirectory(), "/.kateproject");
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream fout(filename);
2016-07-09 11:21:54 +02:00
if (!fout) {
2014-08-03 19:52:23 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
/* clang-format off */
2014-08-03 19:52:23 +02:00
fout <<
"{\n"
"\t\"name\": \"" << this->ProjectName << "\",\n"
2020-08-30 11:54:41 +02:00
"\t\"directory\": \"" << lg.GetSourceDirectory() << "\",\n"
2016-03-13 13:35:51 +01:00
"\t\"files\": [ { " << this->GenerateFilesString(lg) << "} ],\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
2016-03-13 13:35:51 +01:00
this->WriteTargets(lg, fout);
2014-08-03 19:52:23 +02:00
fout << "}\n";
}
2020-08-30 11:54:41 +02:00
void cmExtraKateGenerator::WriteTargets(const cmLocalGenerator& lg,
2016-07-09 11:21:54 +02:00
cmGeneratedFileStream& fout) const
2014-08-03 19:52:23 +02:00
{
2020-08-30 11:54:41 +02:00
cmMakefile const* mf = lg.GetMakefile();
2019-11-11 23:01:05 +01:00
const std::string& make = mf->GetRequiredDefinition("CMAKE_MAKE_PROGRAM");
const std::string& makeArgs =
2016-07-09 11:21:54 +02:00
mf->GetSafeDefinition("CMAKE_KATE_MAKE_ARGUMENTS");
2020-08-30 11:54:41 +02:00
std::string const& homeOutputDir = lg.GetBinaryDirectory();
2023-07-02 19:51:09 +02:00
const auto configs = mf->GetGeneratorConfigs(cmMakefile::IncludeEmptyConfig);
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
/* clang-format off */
2014-08-03 19:52:23 +02:00
fout <<
"\t\"build\": {\n"
2018-04-23 21:13:27 +02:00
"\t\t\"directory\": \"" << homeOutputDir << "\",\n"
2014-08-03 19:52:23 +02:00
"\t\t\"default_target\": \"all\",\n"
"\t\t\"clean_target\": \"clean\",\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
2014-08-03 19:52:23 +02:00
// build, clean and quick are for the build plugin kate <= 4.12:
2016-07-09 11:21:54 +02:00
fout << "\t\t\"build\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
<< makeArgs << " "
<< "all\",\n";
fout << "\t\t\"clean\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
<< makeArgs << " "
<< "clean\",\n";
fout << "\t\t\"quick\": \"" << make << " -C \\\"" << homeOutputDir << "\\\" "
<< makeArgs << " "
<< "install\",\n";
2014-08-03 19:52:23 +02:00
// this is for kate >= 4.13:
2016-07-09 11:21:54 +02:00
fout << "\t\t\"targets\":[\n";
2014-08-03 19:52:23 +02:00
2023-07-02 19:51:09 +02:00
this->AppendTarget(fout, "all", configs, make, makeArgs, homeOutputDir,
2016-07-09 11:21:54 +02:00
homeOutputDir);
2023-07-02 19:51:09 +02:00
this->AppendTarget(fout, "clean", configs, make, makeArgs, homeOutputDir,
2016-07-09 11:21:54 +02:00
homeOutputDir);
2014-08-03 19:52:23 +02:00
// add all executable and library targets and some of the GLOBAL
// and UTILITY targets
2020-08-30 11:54:41 +02:00
for (const auto& localGen : this->GlobalGenerator->GetLocalGenerators()) {
const auto& targets = localGen->GetGeneratorTargets();
2023-07-02 19:51:09 +02:00
const std::string currentDir = localGen->GetCurrentBinaryDirectory();
2018-01-26 17:06:56 +01:00
bool topLevel = (currentDir == localGen->GetBinaryDirectory());
2020-08-30 11:54:41 +02:00
for (const auto& target : targets) {
2018-01-26 17:06:56 +01:00
std::string const& targetName = target->GetName();
switch (target->GetType()) {
2017-04-14 19:02:05 +02:00
case cmStateEnums::GLOBAL_TARGET: {
2014-08-03 19:52:23 +02:00
bool insertTarget = false;
// Only add the global targets from CMAKE_BINARY_DIR,
// not from the subdirs
2016-07-09 11:21:54 +02:00
if (topLevel) {
2014-08-03 19:52:23 +02:00
insertTarget = true;
// only add the "edit_cache" target if it's not ccmake, because
// this will not work within the IDE
2016-07-09 11:21:54 +02:00
if (targetName == "edit_cache") {
2021-11-20 13:41:27 +01:00
cmValue editCommand =
2018-01-26 17:06:56 +01:00
localGen->GetMakefile()->GetDefinition("CMAKE_EDIT_COMMAND");
2021-09-14 00:13:48 +02:00
if (!editCommand ||
strstr(editCommand->c_str(), "ccmake") != nullptr) {
2014-08-03 19:52:23 +02:00
insertTarget = false;
}
}
2016-07-09 11:21:54 +02:00
}
if (insertTarget) {
2023-07-02 19:51:09 +02:00
this->AppendTarget(fout, targetName, configs, make, makeArgs,
currentDir, homeOutputDir);
2016-07-09 11:21:54 +02:00
}
} break;
2017-04-14 19:02:05 +02:00
case cmStateEnums::UTILITY:
2014-08-03 19:52:23 +02:00
// Add all utility targets, except the Nightly/Continuous/
// Experimental-"sub"targets as e.g. NightlyStart
2020-08-30 11:54:41 +02:00
if ((cmHasLiteralPrefix(targetName, "Nightly") &&
2016-07-09 11:21:54 +02:00
(targetName != "Nightly")) ||
2020-08-30 11:54:41 +02:00
(cmHasLiteralPrefix(targetName, "Continuous") &&
2016-07-09 11:21:54 +02:00
(targetName != "Continuous")) ||
2020-08-30 11:54:41 +02:00
(cmHasLiteralPrefix(targetName, "Experimental") &&
2016-07-09 11:21:54 +02:00
(targetName != "Experimental"))) {
break;
}
2023-07-02 19:51:09 +02:00
this->AppendTarget(fout, targetName, configs, make, makeArgs,
currentDir, homeOutputDir);
2014-08-03 19:52:23 +02:00
break;
2017-04-14 19:02:05 +02:00
case cmStateEnums::EXECUTABLE:
case cmStateEnums::STATIC_LIBRARY:
case cmStateEnums::SHARED_LIBRARY:
case cmStateEnums::MODULE_LIBRARY:
case cmStateEnums::OBJECT_LIBRARY: {
2023-07-02 19:51:09 +02:00
this->AppendTarget(fout, targetName, configs, make, makeArgs,
currentDir, homeOutputDir);
if (!this->UseNinja) {
std::string fastTarget = cmStrCat(targetName, "/fast");
this->AppendTarget(fout, fastTarget, configs, make, makeArgs,
currentDir, homeOutputDir);
}
2014-08-03 19:52:23 +02:00
2016-07-09 11:21:54 +02:00
} break;
2014-08-03 19:52:23 +02:00
default:
break;
}
}
2016-07-09 11:21:54 +02:00
// insert rules for compiling, preprocessing and assembling individual
// files
2014-08-03 19:52:23 +02:00
std::vector<std::string> objectFileTargets;
2018-01-26 17:06:56 +01:00
localGen->GetIndividualFileTargets(objectFileTargets);
for (std::string const& f : objectFileTargets) {
2023-07-02 19:51:09 +02:00
this->AppendTarget(fout, f, configs, make, makeArgs, currentDir,
homeOutputDir);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
fout << "\t] }\n";
2014-08-03 19:52:23 +02:00
}
2023-07-02 19:51:09 +02:00
void cmExtraKateGenerator::AppendTarget(
cmGeneratedFileStream& fout, const std::string& target,
const std::vector<std::string>& configs, const std::string& make,
const std::string& makeArgs, const std::string& path,
const std::string& homeOutputDir) const
2014-08-03 19:52:23 +02:00
{
static char JsonSep = ' ';
2023-07-02 19:51:09 +02:00
for (const std::string& conf : configs) {
fout << "\t\t\t" << JsonSep << R"({"name":")" << target
<< ((configs.size() > 1) ? (std::string(":") + conf) : std::string())
<< "\", "
"\"build_cmd\":\""
<< make << " -C \\\"" << (this->UseNinja ? homeOutputDir : path)
<< "\\\" "
<< ((this->UseNinja && configs.size() > 1)
? std::string(" -f build-") + conf + ".ninja"
: std::string())
<< makeArgs << " " << target << "\"}\n";
JsonSep = ',';
}
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
void cmExtraKateGenerator::CreateDummyKateProjectFile(
2020-08-30 11:54:41 +02:00
const cmLocalGenerator& lg) const
2014-08-03 19:52:23 +02:00
{
2020-02-01 23:06:01 +01:00
std::string filename =
2020-08-30 11:54:41 +02:00
cmStrCat(lg.GetBinaryDirectory(), '/', this->ProjectName, ".kateproject");
2018-10-28 12:09:07 +01:00
cmGeneratedFileStream fout(filename);
2016-07-09 11:21:54 +02:00
if (!fout) {
2014-08-03 19:52:23 +02:00
return;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2015-08-17 11:37:30 +02:00
fout << "#Generated by " << cmSystemTools::GetCMakeCommand()
2014-08-03 19:52:23 +02:00
<< ", do not edit.\n";
}
2016-07-09 11:21:54 +02:00
std::string cmExtraKateGenerator::GenerateFilesString(
2020-08-30 11:54:41 +02:00
const cmLocalGenerator& lg) const
2014-08-03 19:52:23 +02:00
{
2023-07-02 19:51:09 +02:00
const cmMakefile* mf = lg.GetMakefile();
std::string mode =
cmSystemTools::UpperCase(mf->GetSafeDefinition("CMAKE_KATE_FILES_MODE"));
static const std::string gitString = "\"git\": 1 ";
static const std::string svnString = "\"svn\": 1 ";
static const std::string hgString = "\"hg\": 1 ";
static const std::string fossilString = "\"fossil\": 1 ";
if (mode == "SVN") {
return svnString;
2014-08-03 19:52:23 +02:00
}
2023-07-02 19:51:09 +02:00
if (mode == "GIT") {
return gitString;
2014-08-03 19:52:23 +02:00
}
2023-07-02 19:51:09 +02:00
if (mode == "HG") {
return hgString;
}
if (mode == "FOSSIL") {
return fossilString;
}
// check for the VCS files except when "forced" to "FILES" mode:
if (mode != "LIST") {
cmCMakePath startDir(lg.GetSourceDirectory(), cmCMakePath::auto_format);
// move the directories up to the root directory to see whether we are in
// a subdir of a svn, git, hg or fossil checkout
for (;;) {
std::string s = startDir.String() + "/.git";
if (cmSystemTools::FileExists(s)) {
return gitString;
}
2014-08-03 19:52:23 +02:00
2023-07-02 19:51:09 +02:00
s = startDir.String() + "/.svn";
if (cmSystemTools::FileExists(s)) {
return svnString;
}
s = startDir.String() + "/.hg";
if (cmSystemTools::FileExists(s)) {
return hgString;
}
s = startDir.String() + "/.fslckout";
if (cmSystemTools::FileExists(s)) {
return fossilString;
}
if (!startDir.HasRelativePath()) { // have we reached the root dir ?
break;
}
startDir = startDir.GetParentPath();
}
}
2014-08-03 19:52:23 +02:00
std::set<std::string> files;
std::string tmp;
2020-08-30 11:54:41 +02:00
const auto& lgs = this->GlobalGenerator->GetLocalGenerators();
2016-07-09 11:21:54 +02:00
2020-08-30 11:54:41 +02:00
for (const auto& lgen : lgs) {
2018-01-26 17:06:56 +01:00
cmMakefile* makefile = lgen->GetMakefile();
2016-07-09 11:21:54 +02:00
const std::vector<std::string>& listFiles = makefile->GetListFiles();
2018-01-26 17:06:56 +01:00
for (std::string const& listFile : listFiles) {
2023-07-02 19:51:09 +02:00
if (listFile.find("/CMakeFiles/") == std::string::npos) {
files.insert(listFile);
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2020-08-30 11:54:41 +02:00
for (const auto& sf : makefile->GetSourceFiles()) {
2019-11-11 23:01:05 +01:00
if (sf->GetIsGenerated()) {
2014-08-03 19:52:23 +02:00
continue;
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
2020-02-01 23:06:01 +01:00
tmp = sf->ResolveFullPath();
2014-08-03 19:52:23 +02:00
files.insert(tmp);
}
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
const char* sep = "";
tmp = "\"list\": [";
2018-01-26 17:06:56 +01:00
for (std::string const& f : files) {
2014-08-03 19:52:23 +02:00
tmp += sep;
tmp += " \"";
2018-01-26 17:06:56 +01:00
tmp += f;
2014-08-03 19:52:23 +02:00
tmp += "\"";
sep = ",";
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
tmp += "] ";
return tmp;
}
2016-07-09 11:21:54 +02:00
std::string cmExtraKateGenerator::GenerateProjectName(
const std::string& name, const std::string& type,
const std::string& path) const
2014-08-03 19:52:23 +02:00
{
2020-08-30 11:54:41 +02:00
return name + (type.empty() ? "" : "-") + type + '@' + path;
2014-08-03 19:52:23 +02:00
}
2016-07-09 11:21:54 +02:00
std::string cmExtraKateGenerator::GetPathBasename(
const std::string& path) const
2014-08-03 19:52:23 +02:00
{
std::string outputBasename = path;
2015-04-27 22:25:09 +02:00
while (!outputBasename.empty() &&
2019-11-11 23:01:05 +01:00
(outputBasename.back() == '/' || outputBasename.back() == '\\')) {
2014-08-03 19:52:23 +02:00
outputBasename.resize(outputBasename.size() - 1);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
std::string::size_type loc = outputBasename.find_last_of("/\\");
2016-07-09 11:21:54 +02:00
if (loc != std::string::npos) {
2014-08-03 19:52:23 +02:00
outputBasename = outputBasename.substr(loc + 1);
2016-07-09 11:21:54 +02:00
}
2014-08-03 19:52:23 +02:00
return outputBasename;
}