summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2023-01-30 16:09:56 -0500
committerBrad King <brad.king@kitware.com>2023-02-01 09:32:42 -0500
commit5252c885693ce35467f6c4c7fdc8deb6406df149 (patch)
tree35704c91a461029b32411307789d096df0a52a10
parent0a48d8fe5ccf8a44bcada7b528f4cf4dd591b18e (diff)
downloadcmake-5252c885693ce35467f6c4c7fdc8deb6406df149.tar.gz
try_compile: Record propagated CMake variables in configure log
These provide more detailed information about how the test project was configured. Issue: #23200
-rw-r--r--Help/manual/cmake-configure-log.7.rst8
-rw-r--r--Source/cmConfigureLog.cxx24
-rw-r--r--Source/cmConfigureLog.h3
-rw-r--r--Source/cmCoreTryCompile.cxx21
-rw-r--r--Source/cmCoreTryCompile.h1
-rw-r--r--Tests/RunCMake/try_compile/ConfigureLog-config.txt13
-rw-r--r--Tests/RunCMake/try_compile/ConfigureLog.cmake13
-rw-r--r--Tests/RunCMake/try_compile/Inspect-config.txt6
-rw-r--r--Tests/RunCMake/try_run/ConfigureLog-config.txt15
9 files changed, 104 insertions, 0 deletions
diff --git a/Help/manual/cmake-configure-log.7.rst b/Help/manual/cmake-configure-log.7.rst
index a9c185dd3b..72d4093cba 100644
--- a/Help/manual/cmake-configure-log.7.rst
+++ b/Help/manual/cmake-configure-log.7.rst
@@ -190,6 +190,8 @@ A ``try_compile-v1`` event is a YAML mapping:
directories:
source: "/path/to/.../TryCompile-01234"
binary: "/path/to/.../TryCompile-01234"
+ cmakeVariables:
+ SOME_VARIABLE: "Some Value"
buildResult:
variable: "COMPILE_RESULT"
cached: true
@@ -217,6 +219,12 @@ The keys specific to ``try_compile-v1`` mappings are:
For non-project invocations, this is often the same as
the source directory.
+``cmakeVariables``
+ An optional key that is present when CMake propagates variables
+ into the test project, either automatically or due to the
+ :variable:`CMAKE_TRY_COMPILE_PLATFORM_VARIABLES` variable.
+ Its value is a mapping from variable names to their values.
+
``buildResult``
A mapping describing the result of compiling the test code.
It has the following keys:
diff --git a/Source/cmConfigureLog.cxx b/Source/cmConfigureLog.cxx
index 1cf785a4e4..a6658e2fa3 100644
--- a/Source/cmConfigureLog.cxx
+++ b/Source/cmConfigureLog.cxx
@@ -193,6 +193,30 @@ void cmConfigureLog::WriteValue(cm::string_view key,
this->EndObject();
}
+void cmConfigureLog::WriteValue(cm::string_view key,
+ std::map<std::string, std::string> const& map)
+{
+ static const std::string rawKeyChars = //
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" //
+ "abcdefghijklmnopqrstuvwxyz" //
+ "0123456789" //
+ "-_" //
+ ;
+ this->BeginObject(key);
+ for (auto const& entry : map) {
+ if (entry.first.find_first_not_of(rawKeyChars) == std::string::npos) {
+ this->WriteValue(entry.first, entry.second);
+ } else {
+ this->BeginLine();
+ this->Encoder->write(entry.first, &this->Stream);
+ this->Stream << ": ";
+ this->Encoder->write(entry.second, &this->Stream);
+ this->EndLine();
+ }
+ }
+ this->EndObject();
+}
+
void cmConfigureLog::WriteLiteralTextBlock(cm::string_view key,
cm::string_view text)
{
diff --git a/Source/cmConfigureLog.h b/Source/cmConfigureLog.h
index eb25702f4d..7edc3ed657 100644
--- a/Source/cmConfigureLog.h
+++ b/Source/cmConfigureLog.h
@@ -2,6 +2,7 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#pragma once
+#include <map>
#include <memory>
#include <string>
#include <vector>
@@ -42,6 +43,8 @@ public:
void WriteValue(cm::string_view key, int value);
void WriteValue(cm::string_view key, std::string const& value);
void WriteValue(cm::string_view key, std::vector<std::string> const& list);
+ void WriteValue(cm::string_view key,
+ std::map<std::string, std::string> const& map);
void WriteTextBlock(cm::string_view key, cm::string_view text);
void WriteLiteralTextBlock(cm::string_view key, cm::string_view text);
diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx
index 618c794aaa..acf1c206be 100644
--- a/Source/cmCoreTryCompile.cxx
+++ b/Source/cmCoreTryCompile.cxx
@@ -471,6 +471,8 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
return cm::nullopt;
}
+ std::map<std::string, std::string> cmakeVariables;
+
std::string outFileName = this->BinaryDirectory + "/CMakeLists.txt";
// which signature are we using? If we are using var srcfile bindir
if (this->SrcFileSignature) {
@@ -592,6 +594,7 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
cmVersion::GetPatchVersion(), cmVersion::GetTweakVersion());
if (def) {
fprintf(fout, "set(CMAKE_MODULE_PATH \"%s\")\n", def->c_str());
+ cmakeVariables.emplace("CMAKE_MODULE_PATH", *def);
}
/* Set MSVC runtime library policy to match our selection. */
@@ -653,10 +656,12 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
this->Makefile->GetDefinition(rulesOverrideLang)) {
fprintf(fout, "set(%s \"%s\")\n", rulesOverrideLang.c_str(),
rulesOverridePath->c_str());
+ cmakeVariables.emplace(rulesOverrideLang, *rulesOverridePath);
} else if (cmValue rulesOverridePath2 =
this->Makefile->GetDefinition(rulesOverrideBase)) {
fprintf(fout, "set(%s \"%s\")\n", rulesOverrideBase.c_str(),
rulesOverridePath2->c_str());
+ cmakeVariables.emplace(rulesOverrideBase, *rulesOverridePath2);
}
}
fprintf(fout, "project(CMAKE_TRY_COMPILE%s)\n", projectLangs.c_str());
@@ -687,6 +692,9 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
"set(CMAKE_%s_FLAGS \"${CMAKE_%s_FLAGS}"
" ${COMPILE_DEFINITIONS}\")\n",
li.c_str(), li.c_str());
+ if (flags) {
+ cmakeVariables.emplace(langFlags, *flags);
+ }
}
switch (this->Makefile->GetPolicyStatus(cmPolicies::CMP0066)) {
case cmPolicies::WARN:
@@ -723,6 +731,9 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
cmValue flagsCfg = this->Makefile->GetDefinition(langFlagsCfg);
fprintf(fout, "set(%s %s)\n", langFlagsCfg.c_str(),
cmOutputConverter::EscapeForCMake(*flagsCfg).c_str());
+ if (flagsCfg) {
+ cmakeVariables.emplace(langFlagsCfg, *flagsCfg);
+ }
}
} break;
}
@@ -757,6 +768,9 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
this->Makefile->GetDefinition("CMAKE_EXE_LINKER_FLAGS");
fprintf(fout, "set(CMAKE_EXE_LINKER_FLAGS %s)\n",
cmOutputConverter::EscapeForCMake(*exeLinkFlags).c_str());
+ if (exeLinkFlags) {
+ cmakeVariables.emplace("CMAKE_EXE_LINKER_FLAGS", *exeLinkFlags);
+ }
}
break;
}
@@ -1044,12 +1058,14 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
vars.erase(kCMAKE_OSX_ARCHITECTURES);
std::string flag = "-DCMAKE_OSX_ARCHITECTURES=" + *tcArchs;
arguments.CMakeFlags.emplace_back(std::move(flag));
+ cmakeVariables.emplace("CMAKE_OSX_ARCHITECTURES", *tcArchs);
}
for (std::string const& var : vars) {
if (cmValue val = this->Makefile->GetDefinition(var)) {
std::string flag = "-D" + var + "=" + *val;
arguments.CMakeFlags.emplace_back(std::move(flag));
+ cmakeVariables.emplace(var, *val);
}
}
}
@@ -1060,6 +1076,7 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
if (cmValue val = this->Makefile->GetDefinition(var)) {
std::string flag = "-D" + var + "=" + "'" + *val + "'";
arguments.CMakeFlags.emplace_back(std::move(flag));
+ cmakeVariables.emplace(var, *val);
}
}
}
@@ -1145,6 +1162,7 @@ cm::optional<cmTryCompileResult> cmCoreTryCompile::TryCompileCode(
if (arguments.LogDescription) {
result.LogDescription = *arguments.LogDescription;
}
+ result.CMakeVariables = std::move(cmakeVariables);
result.SourceDirectory = sourceDirectory;
result.BinaryDirectory = this->BinaryDirectory;
result.Variable = *arguments.CompileResultVariable;
@@ -1304,6 +1322,9 @@ void cmCoreTryCompile::WriteTryCompileEventFields(
log.WriteValue("source"_s, compileResult.SourceDirectory);
log.WriteValue("binary"_s, compileResult.BinaryDirectory);
log.EndObject();
+ if (!compileResult.CMakeVariables.empty()) {
+ log.WriteValue("cmakeVariables"_s, compileResult.CMakeVariables);
+ }
log.BeginObject("buildResult"_s);
log.WriteValue("variable"_s, compileResult.Variable);
log.WriteValue("cached"_s, compileResult.VariableCached);
diff --git a/Source/cmCoreTryCompile.h b/Source/cmCoreTryCompile.h
index 1ec440500f..ba38c196b8 100644
--- a/Source/cmCoreTryCompile.h
+++ b/Source/cmCoreTryCompile.h
@@ -22,6 +22,7 @@ class cmRange;
struct cmTryCompileResult
{
cm::optional<std::string> LogDescription;
+ std::map<std::string, std::string> CMakeVariables;
std::string SourceDirectory;
std::string BinaryDirectory;
diff --git a/Tests/RunCMake/try_compile/ConfigureLog-config.txt b/Tests/RunCMake/try_compile/ConfigureLog-config.txt
index 42c12781a3..ef5c73e2a3 100644
--- a/Tests/RunCMake/try_compile/ConfigureLog-config.txt
+++ b/Tests/RunCMake/try_compile/ConfigureLog-config.txt
@@ -20,6 +20,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_compile/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_compile/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "CMAKE_C_ABI_COMPILED"
cached: true
@@ -41,6 +44,13 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_compile/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_compile/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:
+ ABCDEFGHIJKLMNOPQRSTUVWXYZ: "Upper case"(
+ CMAKE_[^
+]*)+
+ "WITH SPACE": "Space"
+ _-0123456789: "Other chars"
+ abcdefghijklmnopqrstuvwxyz: "Lower case"
buildResult:
variable: "COMPILE_RESULT"
cached: true
@@ -58,6 +68,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_compile/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_compile/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "COMPILE_RESULT"
cached: true
diff --git a/Tests/RunCMake/try_compile/ConfigureLog.cmake b/Tests/RunCMake/try_compile/ConfigureLog.cmake
index 294e0f9cf8..a897719755 100644
--- a/Tests/RunCMake/try_compile/ConfigureLog.cmake
+++ b/Tests/RunCMake/try_compile/ConfigureLog.cmake
@@ -1,10 +1,23 @@
enable_language(C)
+set(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES
+ ABCDEFGHIJKLMNOPQRSTUVWXYZ
+ abcdefghijklmnopqrstuvwxyz
+ _-0123456789
+ "WITH SPACE"
+ )
+set(ABCDEFGHIJKLMNOPQRSTUVWXYZ "Upper case")
+set(abcdefghijklmnopqrstuvwxyz "Lower case")
+set(_-0123456789 "Other chars")
+set("WITH SPACE" "Space")
+
try_compile(COMPILE_RESULT
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ConfigureLog-bad.c
LOG_DESCRIPTION "Source that should not compile."
)
+unset(CMAKE_TRY_COMPILE_PLATFORM_VARIABLES)
+
try_compile(COMPILE_RESULT
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/ConfigureLog-test.c
NO_LOG
diff --git a/Tests/RunCMake/try_compile/Inspect-config.txt b/Tests/RunCMake/try_compile/Inspect-config.txt
index b0f90e56ef..44bd443307 100644
--- a/Tests/RunCMake/try_compile/Inspect-config.txt
+++ b/Tests/RunCMake/try_compile/Inspect-config.txt
@@ -20,6 +20,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_compile/Inspect-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_compile/Inspect-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "CMAKE_C_ABI_COMPILED"
cached: true
@@ -44,6 +47,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_compile/Inspect-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_compile/Inspect-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "CMAKE_CXX_ABI_COMPILED"
cached: true
diff --git a/Tests/RunCMake/try_run/ConfigureLog-config.txt b/Tests/RunCMake/try_run/ConfigureLog-config.txt
index a68472c9c5..e1d5fa77ad 100644
--- a/Tests/RunCMake/try_run/ConfigureLog-config.txt
+++ b/Tests/RunCMake/try_run/ConfigureLog-config.txt
@@ -19,6 +19,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "CMAKE_C_ABI_COMPILED"
cached: true
@@ -40,6 +43,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "COMPILE_RESULT"
cached: true
@@ -59,6 +65,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "COMPILE_RESULT"
cached: true
@@ -83,6 +92,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "COMPILE_RESULT"
cached: true
@@ -103,6 +115,9 @@ events:(
directories:
source: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
binary: "[^"]*/Tests/RunCMake/try_run/ConfigureLog-build/CMakeFiles/CMakeScratch/TryCompile-[^/"]+"
+ cmakeVariables:(
+ CMAKE_[^
+]*)+
buildResult:
variable: "COMPILE_RESULT"
cached: true