cmake: Allow CMAKE_CONFIGURATION_TYPES to be set by environment variable

When no `CMAKE_CONFIGURATION_TYPES` is explicitly specified while
creating a new build tree, check for an environment variable of the same
name.

Issue: #20983
This commit is contained in:
Brad King
2021-06-29 16:58:16 -04:00
parent e216b9bbd3
commit ef56eefc9b
10 changed files with 40 additions and 2 deletions

View File

@@ -0,0 +1,11 @@
CMAKE_CONFIGURATION_TYPES
-------------------------
.. versionadded:: 3.22
.. include:: ENV_VAR.txt
The ``CMAKE_CONFIGURATION_TYPES`` environment variable specifies a
default value for the :variable:`CMAKE_CONFIGURATION_TYPES` variable
when there is no explicit configuration given on the first run while
creating a new build tree.

View File

@@ -31,6 +31,7 @@ Environment Variables that Control the Build
/envvar/CMAKE_APPLE_SILICON_PROCESSOR
/envvar/CMAKE_BUILD_PARALLEL_LEVEL
/envvar/CMAKE_BUILD_TYPE
/envvar/CMAKE_CONFIGURATION_TYPES
/envvar/CMAKE_CONFIG_TYPE
/envvar/CMAKE_EXPORT_COMPILE_COMMANDS
/envvar/CMAKE_GENERATOR

View File

@@ -3,3 +3,7 @@ env-init-configs
* The :envvar:`CMAKE_BUILD_TYPE` environment variable was added to
provide a default value for the :variable:`CMAKE_BUILD_TYPE` variable.
* The :envvar:`CMAKE_CONFIGURATION_TYPES` environment variable was added to
provide a default value for the :variable:`CMAKE_CONFIGURATION_TYPES`
variable.

View File

@@ -10,7 +10,9 @@ types.
This variable is initialized by the first :command:`project` or
:command:`enable_language` command called in a project when a new build
tree is first created. The default value is generator-specific.
tree is first created. If the :envvar:`CMAKE_CONFIGURATION_TYPES`
environment variable is set, its value is used. Otherwise, the default
value is generator-specific.
See :variable:`CMAKE_BUILD_TYPE` for specifying the configuration with
single-config generators.

View File

@@ -3187,8 +3187,12 @@ void cmMakefile::InitCMAKE_CONFIGURATION_TYPES(std::string const& genDefault)
if (this->GetDefinition("CMAKE_CONFIGURATION_TYPES")) {
return;
}
std::string initConfigs;
if (!cmSystemTools::GetEnv("CMAKE_CONFIGURATION_TYPES", initConfigs)) {
initConfigs = genDefault;
}
this->AddCacheDefinition(
"CMAKE_CONFIGURATION_TYPES", genDefault,
"CMAKE_CONFIGURATION_TYPES", initConfigs,
"Semicolon separated list of supported configuration types, "
"only supports Debug, Release, MinSizeRel, and RelWithDebInfo, "
"anything else will be ignored.",

View File

@@ -0,0 +1,2 @@
-- ENV{CMAKE_CONFIGURATION_TYPES}='ConfigTypesEnv'
-- CMAKE_CONFIGURATION_TYPES='ConfigTypesEnv'

View File

@@ -0,0 +1,2 @@
message(STATUS "ENV{CMAKE_CONFIGURATION_TYPES}='$ENV{CMAKE_CONFIGURATION_TYPES}'")
message(STATUS "CMAKE_CONFIGURATION_TYPES='${CMAKE_CONFIGURATION_TYPES}'")

View File

@@ -0,0 +1,2 @@
-- ENV{CMAKE_CONFIGURATION_TYPES}='ConfigTypesEnv'
-- CMAKE_CONFIGURATION_TYPES='ConfigTypesOpt'

View File

@@ -0,0 +1 @@
include(EnvConfigTypes.cmake)

View File

@@ -351,8 +351,17 @@ function(run_EnvironmentBuildType)
unset(ENV{CMAKE_BUILD_TYPE})
endfunction()
function(run_EnvironmentConfigTypes)
set(ENV{CMAKE_CONFIGURATION_TYPES} "ConfigTypesEnv")
run_cmake(EnvConfigTypes)
run_cmake_with_options(EnvConfigTypesIgnore -DCMAKE_CONFIGURATION_TYPES=ConfigTypesOpt)
unset(ENV{CMAKE_CONFIGURATION_TYPES})
endfunction()
if(RunCMake_GENERATOR MATCHES "Make|^Ninja$")
run_EnvironmentBuildType()
elseif(RunCMake_GENERATOR MATCHES "Ninja Multi-Config|Visual Studio|Xcode")
run_EnvironmentConfigTypes()
endif()
function(run_EnvironmentToolchain)