Ver código fonte

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
Brad King 4 anos atrás
pai
commit
ef56eefc9b

+ 11 - 0
Help/envvar/CMAKE_CONFIGURATION_TYPES.rst

@@ -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.

+ 1 - 0
Help/manual/cmake-env-variables.7.rst

@@ -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

+ 4 - 0
Help/release/dev/env-init-configs.rst

@@ -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.

+ 3 - 1
Help/variable/CMAKE_CONFIGURATION_TYPES.rst

@@ -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.

+ 5 - 1
Source/cmMakefile.cxx

@@ -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.",

+ 2 - 0
Tests/RunCMake/CommandLine/EnvConfigTypes-stdout.txt

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

+ 2 - 0
Tests/RunCMake/CommandLine/EnvConfigTypes.cmake

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

+ 2 - 0
Tests/RunCMake/CommandLine/EnvConfigTypesIgnore-stdout.txt

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

+ 1 - 0
Tests/RunCMake/CommandLine/EnvConfigTypesIgnore.cmake

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

+ 9 - 0
Tests/RunCMake/CommandLine/RunCMakeTest.cmake

@@ -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)