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"
9 #include <cm3p/json/value.h>
11 #include "cmFileAPI.h"
12 #include "cmGlobalGenerator.h"
14 #include "cmMakefile.h"
16 #include "cmStringAlgorithms.h"
22 struct ToolchainVariable
24 std::string ObjectKey
;
25 std::string VariableSuffix
;
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
);
44 Toolchains(cmFileAPI
& fileAPI
, unsigned long version
);
48 Toolchains::Toolchains(cmFileAPI
& fileAPI
, unsigned long version
)
52 static_cast<void>(this->Version
);
55 Json::Value
Toolchains::Dump()
57 Json::Value toolchains
= Json::objectValue
;
58 toolchains
["toolchains"] = this->DumpToolchains();
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
));
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",
88 { "linkLibraries", "IMPLICIT_LINK_LIBRARIES", true },
91 static const ToolchainVariable SourceFileExtensionsVariable
{
92 "sourceFileExtensions", "SOURCE_FILE_EXTENSIONS", true
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
);
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
);
119 void Toolchains::DumpToolchainVariable(cmMakefile
const* mf
,
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
);
131 Json::Value jsonArray
= Json::arrayValue
;
132 for (auto const& value
: values
) {
133 jsonArray
.append(value
);
135 object
[variable
.ObjectKey
] = jsonArray
;
138 cmValue def
= mf
->GetDefinition(variableName
);
140 object
[variable
.ObjectKey
] = *def
;
146 Json::Value
cmFileAPIToolchainsDump(cmFileAPI
& fileAPI
, unsigned long version
)
148 Toolchains
toolchains(fileAPI
, version
);
149 return toolchains
.Dump();