Merge branch 'release-3.29'
[kiteware-cmake.git] / Source / cmFileAPIToolchains.cxx
bloba51ae2048e9568e27f01672d49079c6edc89f24f
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 "cmFileAPIToolchains.h"
5 #include <memory>
6 #include <string>
7 #include <vector>
9 #include <cm3p/json/value.h>
11 #include "cmFileAPI.h"
12 #include "cmGlobalGenerator.h"
13 #include "cmList.h"
14 #include "cmMakefile.h"
15 #include "cmState.h"
16 #include "cmStringAlgorithms.h"
17 #include "cmValue.h"
18 #include "cmake.h"
20 namespace {
22 struct ToolchainVariable
24 std::string ObjectKey;
25 std::string VariableSuffix;
26 bool IsList;
29 class Toolchains
31 cmFileAPI& FileAPI;
32 unsigned long Version;
34 Json::Value DumpToolchains();
35 Json::Value DumpToolchain(std::string const& lang);
36 Json::Value DumpToolchainVariables(
37 cmMakefile const* mf, std::string const& lang,
38 std::vector<ToolchainVariable> const& variables);
39 void DumpToolchainVariable(cmMakefile const* mf, Json::Value& object,
40 std::string const& lang,
41 ToolchainVariable const& variable);
43 public:
44 Toolchains(cmFileAPI& fileAPI, unsigned long version);
45 Json::Value Dump();
48 Toolchains::Toolchains(cmFileAPI& fileAPI, unsigned long version)
49 : FileAPI(fileAPI)
50 , Version(version)
52 static_cast<void>(this->Version);
55 Json::Value Toolchains::Dump()
57 Json::Value toolchains = Json::objectValue;
58 toolchains["toolchains"] = this->DumpToolchains();
59 return toolchains;
62 Json::Value Toolchains::DumpToolchains()
64 Json::Value toolchains = Json::arrayValue;
66 for (std::string const& lang :
67 this->FileAPI.GetCMakeInstance()->GetState()->GetEnabledLanguages()) {
68 toolchains.append(this->DumpToolchain(lang));
71 return toolchains;
74 Json::Value Toolchains::DumpToolchain(std::string const& lang)
76 static const std::vector<ToolchainVariable> CompilerVariables{
77 { "path", "COMPILER", false },
78 { "id", "COMPILER_ID", false },
79 { "version", "COMPILER_VERSION", false },
80 { "target", "COMPILER_TARGET", false },
83 static const std::vector<ToolchainVariable> CompilerImplicitVariables{
84 { "includeDirectories", "IMPLICIT_INCLUDE_DIRECTORIES", true },
85 { "linkDirectories", "IMPLICIT_LINK_DIRECTORIES", true },
86 { "linkFrameworkDirectories", "IMPLICIT_LINK_FRAMEWORK_DIRECTORIES",
87 true },
88 { "linkLibraries", "IMPLICIT_LINK_LIBRARIES", true },
91 static const ToolchainVariable SourceFileExtensionsVariable{
92 "sourceFileExtensions", "SOURCE_FILE_EXTENSIONS", true
95 const auto& mf =
96 this->FileAPI.GetCMakeInstance()->GetGlobalGenerator()->GetMakefiles()[0];
97 Json::Value toolchain = Json::objectValue;
98 toolchain["language"] = lang;
99 toolchain["compiler"] =
100 this->DumpToolchainVariables(mf.get(), lang, CompilerVariables);
101 toolchain["compiler"]["implicit"] =
102 this->DumpToolchainVariables(mf.get(), lang, CompilerImplicitVariables);
103 this->DumpToolchainVariable(mf.get(), toolchain, lang,
104 SourceFileExtensionsVariable);
105 return toolchain;
108 Json::Value Toolchains::DumpToolchainVariables(
109 cmMakefile const* mf, std::string const& lang,
110 std::vector<ToolchainVariable> const& variables)
112 Json::Value object = Json::objectValue;
113 for (const auto& variable : variables) {
114 this->DumpToolchainVariable(mf, object, lang, variable);
116 return object;
119 void Toolchains::DumpToolchainVariable(cmMakefile const* mf,
120 Json::Value& object,
121 std::string const& lang,
122 ToolchainVariable const& variable)
124 std::string const variableName =
125 cmStrCat("CMAKE_", lang, "_", variable.VariableSuffix);
127 if (variable.IsList) {
128 cmValue data = mf->GetDefinition(variableName);
129 if (data) {
130 cmList values(data);
131 Json::Value jsonArray = Json::arrayValue;
132 for (auto const& value : values) {
133 jsonArray.append(value);
135 object[variable.ObjectKey] = jsonArray;
137 } else {
138 cmValue def = mf->GetDefinition(variableName);
139 if (def) {
140 object[variable.ObjectKey] = *def;
146 Json::Value cmFileAPIToolchainsDump(cmFileAPI& fileAPI, unsigned long version)
148 Toolchains toolchains(fileAPI, version);
149 return toolchains.Dump();