mirror of
https://github.com/Kitware/CMake.git
synced 2026-01-08 14:50:10 -06:00
try_compile: Do not use CMAKE_BUILD_TYPE or CMAKE_CONFIGURATION_TYPES env vars
Since: * commite216b9bbd3(cmake: Allow CMAKE_BUILD_TYPE to be set by environment variable, 2021-06-29, v3.22.0-rc1~503^2~1) * commitef56eefc9b(cmake: Allow CMAKE_CONFIGURATION_TYPES to be set by environment variable, 2021-06-29, v3.22.0-rc1~503^2) the environment variables are supposed to provide defaults for settings the user otherwise can control via cache entries. However, they accidentally affect `try_compile` projects too, which are supposed to be programmatically controlled. Fixes: #22935
This commit is contained in:
@@ -498,7 +498,8 @@ bool cmGlobalGenerator::CheckLanguages(
|
||||
void cmGlobalGenerator::EnableLanguage(
|
||||
std::vector<std::string> const& languages, cmMakefile* mf, bool optional)
|
||||
{
|
||||
if (!this->IsMultiConfig()) {
|
||||
if (!this->IsMultiConfig() &&
|
||||
!this->GetCMakeInstance()->GetIsInTryCompile()) {
|
||||
std::string envBuildType;
|
||||
if (!mf->GetDefinition("CMAKE_BUILD_TYPE") &&
|
||||
cmSystemTools::GetEnv("CMAKE_BUILD_TYPE", envBuildType)) {
|
||||
|
||||
@@ -3167,7 +3167,8 @@ void cmMakefile::InitCMAKE_CONFIGURATION_TYPES(std::string const& genDefault)
|
||||
return;
|
||||
}
|
||||
std::string initConfigs;
|
||||
if (!cmSystemTools::GetEnv("CMAKE_CONFIGURATION_TYPES", initConfigs)) {
|
||||
if (this->GetCMakeInstance()->GetIsInTryCompile() ||
|
||||
!cmSystemTools::GetEnv("CMAKE_CONFIGURATION_TYPES", initConfigs)) {
|
||||
initConfigs = genDefault;
|
||||
}
|
||||
this->AddCacheDefinition(
|
||||
|
||||
7
Tests/RunCMake/try_compile/EnvConfig.c
Normal file
7
Tests/RunCMake/try_compile/EnvConfig.c
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifdef TC_CONFIG_BAD
|
||||
# error "Built in 'Bad' config"
|
||||
#endif
|
||||
int main(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
18
Tests/RunCMake/try_compile/EnvConfig.cmake
Normal file
18
Tests/RunCMake/try_compile/EnvConfig.cmake
Normal file
@@ -0,0 +1,18 @@
|
||||
enable_language(C)
|
||||
|
||||
set(ENV{CMAKE_BUILD_TYPE} "Bad")
|
||||
set(ENV{CMAKE_CONFIGURATION_TYPES} "Bad;Debug")
|
||||
|
||||
add_library(tc_defs INTERFACE IMPORTED)
|
||||
target_compile_definitions(tc_defs INTERFACE "TC_CONFIG_$<UPPER_CASE:$<CONFIG>>")
|
||||
|
||||
try_compile(ENV_CONFIG_RESULT "${CMAKE_BINARY_DIR}"
|
||||
SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/EnvConfig.c"
|
||||
COPY_FILE "${CMAKE_CURRENT_BINARY_DIR}/EnvConfig.bin"
|
||||
OUTPUT_VARIABLE tc_output
|
||||
LINK_LIBRARIES tc_defs
|
||||
)
|
||||
if(NOT ENV_CONFIG_RESULT)
|
||||
string(REPLACE "\n" "\n " tc_output " ${tc_output}")
|
||||
message(FATAL_ERROR "try_compile failed:\n${tc_output}")
|
||||
endif()
|
||||
@@ -16,6 +16,8 @@ run_cmake(BadSources2)
|
||||
run_cmake(NonSourceCopyFile)
|
||||
run_cmake(NonSourceCompileDefinitions)
|
||||
|
||||
run_cmake(EnvConfig)
|
||||
|
||||
set(RunCMake_TEST_OPTIONS --debug-trycompile)
|
||||
run_cmake(PlatformVariables)
|
||||
run_cmake(WarnDeprecated)
|
||||
|
||||
Reference in New Issue
Block a user