fileAPI: Expose CMAKE_<LANG>_COMPILER_ARG1

Compiler arguments coming from CC environment variables or multi-element
CMAKE_<LANG>_COMPILER variables set by toolchain files were previously not
exposed in the file API. Among other possible problems, this caused clients
to determine wrong system include paths and built-in preprocessor macros by
calling the compiler without these important arguments.

This is fixed by adding an optional "commandFragment" attribute to the
compiler description in the `toolchains` object, containing these arguments
as a command line fragment. This is already the form in which they are
internally stored in the CMAKE_<LANG>_COMPILER_ARG1 variable, so all that is
required is adding this variable to the set of exported variables, besides
some logic to omit it if empty.

Issue: #22568
This commit is contained in:
Christian Walther
2025-12-03 07:34:53 +11:00
committed by Craig Scott
parent 1e02926c9a
commit 71a4e34d97
9 changed files with 149 additions and 27 deletions
+19 -15
View File
@@ -24,6 +24,7 @@ struct ToolchainVariable
std::string ObjectKey;
std::string VariableSuffix;
bool IsList;
bool OmitEmpty;
};
class Toolchains
@@ -74,22 +75,23 @@ Json::Value Toolchains::DumpToolchains()
Json::Value Toolchains::DumpToolchain(std::string const& lang)
{
static std::vector<ToolchainVariable> const CompilerVariables{
{ "path", "COMPILER", false },
{ "id", "COMPILER_ID", false },
{ "version", "COMPILER_VERSION", false },
{ "target", "COMPILER_TARGET", false },
{ "path", "COMPILER", false, false },
{ "commandFragment", "COMPILER_ARG1", false, true },
{ "id", "COMPILER_ID", false, false },
{ "version", "COMPILER_VERSION", false, false },
{ "target", "COMPILER_TARGET", false, false },
};
static std::vector<ToolchainVariable> const CompilerImplicitVariables{
{ "includeDirectories", "IMPLICIT_INCLUDE_DIRECTORIES", true },
{ "linkDirectories", "IMPLICIT_LINK_DIRECTORIES", true },
{ "linkFrameworkDirectories", "IMPLICIT_LINK_FRAMEWORK_DIRECTORIES",
true },
{ "linkLibraries", "IMPLICIT_LINK_LIBRARIES", true },
{ "includeDirectories", "IMPLICIT_INCLUDE_DIRECTORIES", true, false },
{ "linkDirectories", "IMPLICIT_LINK_DIRECTORIES", true, false },
{ "linkFrameworkDirectories", "IMPLICIT_LINK_FRAMEWORK_DIRECTORIES", true,
false },
{ "linkLibraries", "IMPLICIT_LINK_LIBRARIES", true, false },
};
static ToolchainVariable const SourceFileExtensionsVariable{
"sourceFileExtensions", "SOURCE_FILE_EXTENSIONS", true
"sourceFileExtensions", "SOURCE_FILE_EXTENSIONS", true, false
};
auto const& mf =
@@ -128,15 +130,17 @@ void Toolchains::DumpToolchainVariable(cmMakefile const* mf,
cmValue data = mf->GetDefinition(variableName);
if (data) {
cmList values(data);
Json::Value jsonArray = Json::arrayValue;
for (auto const& value : values) {
jsonArray.append(value);
if (!variable.OmitEmpty || !values.empty()) {
Json::Value jsonArray = Json::arrayValue;
for (auto const& value : values) {
jsonArray.append(value);
}
object[variable.ObjectKey] = jsonArray;
}
object[variable.ObjectKey] = jsonArray;
}
} else {
cmValue def = mf->GetDefinition(variableName);
if (def) {
if (def && (!variable.OmitEmpty || !def.IsEmpty())) {
object[variable.ObjectKey] = *def;
}
}