Commit bb069c08 authored by Ben McMorran's avatar Ben McMorran
Browse files

cmFileAPI: Add "toolchains" object kind.

Fixes #19514
parent 1c9b61c2
......@@ -268,6 +268,8 @@ set(SRCS
cmFileAPICodemodel.h
cmFileAPICMakeFiles.cxx
cmFileAPICMakeFiles.h
cmFileAPIToolchains.cxx
cmFileAPIToolchains.h
cmFileCopier.cxx
cmFileCopier.h
cmFileInstaller.cxx
......
......@@ -18,6 +18,7 @@
#include "cmFileAPICMakeFiles.h"
#include "cmFileAPICache.h"
#include "cmFileAPICodemodel.h"
#include "cmFileAPIToolchains.h"
#include "cmGlobalGenerator.h"
#include "cmStringAlgorithms.h"
#include "cmSystemTools.h"
......@@ -262,6 +263,17 @@ bool cmFileAPI::ReadQuery(std::string const& query,
objects.push_back(o);
return true;
}
if (kindName == ObjectKindName(ObjectKind::Toolchains)) {
Object o;
o.Kind = ObjectKind::Toolchains;
if (verStr == "v1") {
o.Version = 1;
} else {
return false;
}
objects.push_back(o);
return true;
}
if (kindName == ObjectKindName(ObjectKind::InternalTest)) {
Object o;
o.Kind = ObjectKind::InternalTest;
......@@ -402,6 +414,7 @@ const char* cmFileAPI::ObjectKindName(ObjectKind kind)
"codemodel", //
"cache", //
"cmakeFiles", //
"toolchains", //
"__test" //
};
return objectKindNames[size_t(kind)];
......@@ -435,6 +448,9 @@ Json::Value cmFileAPI::BuildObject(Object const& object)
case ObjectKind::CMakeFiles:
value = this->BuildCMakeFiles(object);
break;
case ObjectKind::Toolchains:
value = this->BuildToolchains(object);
break;
case ObjectKind::InternalTest:
value = this->BuildInternalTest(object);
break;
......@@ -491,6 +507,8 @@ cmFileAPI::ClientRequest cmFileAPI::BuildClientRequest(
r.Kind = ObjectKind::Cache;
} else if (kindName == this->ObjectKindName(ObjectKind::CMakeFiles)) {
r.Kind = ObjectKind::CMakeFiles;
} else if (kindName == this->ObjectKindName(ObjectKind::Toolchains)) {
r.Kind = ObjectKind::Toolchains;
} else if (kindName == this->ObjectKindName(ObjectKind::InternalTest)) {
r.Kind = ObjectKind::InternalTest;
} else {
......@@ -518,6 +536,9 @@ cmFileAPI::ClientRequest cmFileAPI::BuildClientRequest(
case ObjectKind::CMakeFiles:
this->BuildClientRequestCMakeFiles(r, versions);
break;
case ObjectKind::Toolchains:
this->BuildClientRequestToolchains(r, versions);
break;
case ObjectKind::InternalTest:
this->BuildClientRequestInternalTest(r, versions);
break;
......@@ -765,6 +786,40 @@ Json::Value cmFileAPI::BuildCMakeFiles(Object const& object)
return cmakeFiles;
}
// The "toolchains" object kind.
static unsigned int const ToolchainsV1Minor = 0;
void cmFileAPI::BuildClientRequestToolchains(
ClientRequest& r, std::vector<RequestVersion> const& versions)
{
// Select a known version from those requested.
for (RequestVersion const& v : versions) {
if ((v.Major == 1 && v.Minor <= ToolchainsV1Minor)) {
r.Version = v.Major;
break;
}
}
if (!r.Version) {
r.Error = NoSupportedVersion(versions);
}
}
Json::Value cmFileAPI::BuildToolchains(Object const& object)
{
Json::Value toolchains = cmFileAPIToolchainsDump(*this, object.Version);
toolchains["kind"] = this->ObjectKindName(object.Kind);
Json::Value& version = toolchains["version"];
if (object.Version == 1) {
version = BuildVersion(1, ToolchainsV1Minor);
} else {
return toolchains; // should be unreachable
}
return toolchains;
}
// The "__test" object kind is for internal testing of CMake.
static unsigned int const InternalTestV1Minor = 3;
......@@ -828,5 +883,13 @@ Json::Value cmFileAPI::ReportCapabilities()
requests.append(std::move(request)); // NOLINT(*)
}
{
Json::Value request = Json::objectValue;
request["kind"] = ObjectKindName(ObjectKind::Toolchains);
Json::Value& versions = request["version"] = Json::arrayValue;
versions.append(BuildVersion(1, ToolchainsV1Minor));
requests.append(std::move(request)); // NOLINT(*)
}
return capabilities;
}
......@@ -56,6 +56,7 @@ private:
CodeModel,
Cache,
CMakeFiles,
Toolchains,
InternalTest
};
......@@ -200,6 +201,10 @@ private:
ClientRequest& r, std::vector<RequestVersion> const& versions);
Json::Value BuildCMakeFiles(Object const& object);
void BuildClientRequestToolchains(
ClientRequest& r, std::vector<RequestVersion> const& versions);
Json::Value BuildToolchains(Object const& object);
void BuildClientRequestInternalTest(
ClientRequest& r, std::vector<RequestVersion> const& versions);
Json::Value BuildInternalTest(Object const& object);
......
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmFileAPIToolchains.h"
#include <memory>
#include <string>
#include <vector>
#include <cm3p/json/value.h>
#include "cmFileAPI.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
#include "cmProperty.h"
#include "cmState.h"
#include "cmStringAlgorithms.h"
#include "cmake.h"
namespace {
struct ToolchainVariable
{
std::string ObjectKey;
std::string VariableSuffix;
bool IsList;
};
class Toolchains
{
cmFileAPI& FileAPI;
unsigned long Version;
static const std::vector<ToolchainVariable> CompilerVariables;
static const std::vector<ToolchainVariable> CompilerImplicitVariables;
static const ToolchainVariable SourceFileExtensionsVariable;
Json::Value DumpToolchains();
Json::Value DumpToolchain(std::string const& lang);
Json::Value DumpToolchainVariables(
cmMakefile const* mf, std::string const& lang,
std::vector<ToolchainVariable> const& variables);
void DumpToolchainVariable(cmMakefile const* mf, Json::Value& object,
std::string const& lang,
ToolchainVariable const& variable);
public:
Toolchains(cmFileAPI& fileAPI, unsigned long version);
Json::Value Dump();
};
const std::vector<ToolchainVariable> Toolchains::CompilerVariables{
{ "path", "COMPILER", false },
{ "id", "COMPILER_ID", false },
{ "version", "COMPILER_VERSION", false },
{ "target", "COMPILER_TARGET", false },
};
const std::vector<ToolchainVariable> Toolchains::CompilerImplicitVariables{
{ "includeDirectories", "IMPLICIT_INCLUDE_DIRECTORIES", true },
{ "linkDirectories", "IMPLICIT_LINK_DIRECTORIES", true },
{ "linkFrameworkDirectories", "IMPLICIT_LINK_FRAMEWORK_DIRECTORIES", true },
{ "linkLibraries", "IMPLICIT_LINK_LIBRARIES", true },
};
const ToolchainVariable Toolchains::SourceFileExtensionsVariable{
"sourceFileExtensions", "SOURCE_FILE_EXTENSIONS", true
};
Toolchains::Toolchains(cmFileAPI& fileAPI, unsigned long version)
: FileAPI(fileAPI)
, Version(version)
{
static_cast<void>(this->Version);
}
Json::Value Toolchains::Dump()
{
Json::Value toolchains = Json::objectValue;
toolchains["toolchains"] = this->DumpToolchains();
return toolchains;
}
Json::Value Toolchains::DumpToolchains()
{
Json::Value toolchains = Json::arrayValue;
for (std::string const& lang :
this->FileAPI.GetCMakeInstance()->GetState()->GetEnabledLanguages()) {
toolchains.append(this->DumpToolchain(lang));
}
return toolchains;
}
Json::Value Toolchains::DumpToolchain(std::string const& lang)
{
const auto& mf =
this->FileAPI.GetCMakeInstance()->GetGlobalGenerator()->GetMakefiles()[0];
Json::Value toolchain = Json::objectValue;
toolchain["language"] = lang;
toolchain["compiler"] =
this->DumpToolchainVariables(mf.get(), lang, CompilerVariables);
toolchain["compiler"]["implicit"] =
this->DumpToolchainVariables(mf.get(), lang, CompilerImplicitVariables);
this->DumpToolchainVariable(mf.get(), toolchain, lang,
SourceFileExtensionsVariable);
return toolchain;
}
Json::Value Toolchains::DumpToolchainVariables(
cmMakefile const* mf, std::string const& lang,
std::vector<ToolchainVariable> const& variables)
{
Json::Value object = Json::objectValue;
for (const auto& variable : variables) {
this->DumpToolchainVariable(mf, object, lang, variable);
}
return object;
}
void Toolchains::DumpToolchainVariable(cmMakefile const* mf,
Json::Value& object,
std::string const& lang,
ToolchainVariable const& variable)
{
std::string const variableName =
cmStrCat("CMAKE_", lang, "_", variable.VariableSuffix);
if (variable.IsList) {
std::vector<std::string> values;
if (mf->GetDefExpandList(variableName, values)) {
Json::Value jsonArray = Json::arrayValue;
for (std::string const& value : values) {
jsonArray.append(value);
}
object[variable.ObjectKey] = jsonArray;
}
} else {
cmProp def = mf->GetDefinition(variableName);
if (def) {
object[variable.ObjectKey] = *def;
}
}
}
}
Json::Value cmFileAPIToolchainsDump(cmFileAPI& fileAPI, unsigned long version)
{
Toolchains toolchains(fileAPI, version);
return toolchains.Dump();
}
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#pragma once
#include "cmConfigure.h" // IWYU pragma: keep
#include <cm3p/json/value.h>
class cmFileAPI;
extern Json::Value cmFileAPIToolchainsDump(cmFileAPI& fileAPI,
unsigned long version);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment