From 083cf7e8a2515d9f838f3edbb2724fd3b17d83cd Mon Sep 17 00:00:00 2001 From: Eicke Herbertz Date: Fri, 12 Apr 2019 00:16:46 +0200 Subject: cmake: Allow default generator to be set by environment variables When there is no Generator available in the Cache, this will read CMAKE_GENERATOR from environment before using the CMake platform default. If CMAKE_GENERATOR is empty, use the platform default. If a environment default generator is specified, subsequent variables CMAKE_GENERATOR_(INSTANCE,PLATFORM,TOOLSET) are also evaluated in the same way. --- Source/cmake.cxx | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) (limited to 'Source/cmake.cxx') diff --git a/Source/cmake.cxx b/Source/cmake.cxx index fc24ac08ca..c861f3bf09 100644 --- a/Source/cmake.cxx +++ b/Source/cmake.cxx @@ -159,6 +159,9 @@ cmake::cmake(Role role, cmState::Mode mode) #endif this->GlobalGenerator = nullptr; + this->GeneratorInstanceSet = false; + this->GeneratorPlatformSet = false; + this->GeneratorToolsetSet = false; this->CurrentWorkingMode = NORMAL_MODE; #ifdef CMAKE_BUILD_WITH_CMAKE @@ -174,6 +177,10 @@ cmake::cmake(Role role, cmState::Mode mode) this->AddProjectCommands(); } + if (mode == cmState::Project) { + this->LoadEnvironmentPresets(); + } + // Make sure we can capture the build tool output. cmSystemTools::EnableVSConsoleOutput(); @@ -612,6 +619,35 @@ bool cmake::FindPackage(const std::vector& args) return packageFound; } +void cmake::LoadEnvironmentPresets() +{ + std::string envGenVar; + bool hasEnvironmentGenerator = false; + if (cmSystemTools::GetEnv("CMAKE_GENERATOR", envGenVar)) { + hasEnvironmentGenerator = true; + this->EnvironmentGenerator = envGenVar; + } + + auto readGeneratorVar = [&](std::string name, std::string& key) { + std::string varValue; + if (cmSystemTools::GetEnv(name, varValue)) { + if (hasEnvironmentGenerator) { + key = varValue; + } else if (!this->GetIsInTryCompile()) { + std::string message = "Warning: Environment variable "; + message += name; + message += " will be ignored, because CMAKE_GENERATOR "; + message += "is not set."; + cmSystemTools::Message(message, "Warning"); + } + } + }; + + readGeneratorVar("CMAKE_GENERATOR_INSTANCE", this->GeneratorInstance); + readGeneratorVar("CMAKE_GENERATOR_PLATFORM", this->GeneratorPlatform); + readGeneratorVar("CMAKE_GENERATOR_TOOLSET", this->GeneratorToolset); +} + // Parse the args void cmake::SetArgs(const std::vector& args) { @@ -759,7 +795,7 @@ void cmake::SetArgs(const std::vector& args) cmSystemTools::Error("Multiple -A options not allowed"); return; } - this->GeneratorPlatform = value; + this->SetGeneratorPlatform(value); havePlatform = true; } else if (arg.find("-T", 0) == 0) { std::string value = arg.substr(2); @@ -775,7 +811,7 @@ void cmake::SetArgs(const std::vector& args) cmSystemTools::Error("Multiple -T options not allowed"); return; } - this->GeneratorToolset = value; + this->SetGeneratorToolset(value); haveToolset = true; } else if (arg.find("-G", 0) == 0) { std::string value = arg.substr(2); @@ -806,6 +842,16 @@ void cmake::SetArgs(const std::vector& args) else { this->SetDirectoriesFromFile(arg.c_str()); } + // Empty instance, platform and toolset if only a generator is specified + if (this->GlobalGenerator) { + this->GeneratorInstance = ""; + if (!this->GeneratorPlatformSet) { + this->GeneratorPlatform = ""; + } + if (!this->GeneratorToolsetSet) { + this->GeneratorToolset = ""; + } + } } const bool haveSourceDir = !this->GetHomeDirectory().empty(); @@ -1419,8 +1465,7 @@ int cmake::ActualConfigure() if (const std::string* instance = this->State->GetInitializedCacheValue("CMAKE_GENERATOR_INSTANCE")) { - if (!this->GeneratorInstance.empty() && - this->GeneratorInstance != *instance) { + if (this->GeneratorInstanceSet && this->GeneratorInstance != *instance) { std::string message = "Error: generator instance: "; message += this->GeneratorInstance; message += "\nDoes not match the instance used previously: "; @@ -1438,7 +1483,7 @@ int cmake::ActualConfigure() if (const std::string* platformName = this->State->GetInitializedCacheValue("CMAKE_GENERATOR_PLATFORM")) { - if (!this->GeneratorPlatform.empty() && + if (this->GeneratorPlatformSet && this->GeneratorPlatform != *platformName) { std::string message = "Error: generator platform: "; message += this->GeneratorPlatform; @@ -1457,7 +1502,7 @@ int cmake::ActualConfigure() if (const std::string* tsName = this->State->GetInitializedCacheValue("CMAKE_GENERATOR_TOOLSET")) { - if (!this->GeneratorToolset.empty() && this->GeneratorToolset != *tsName) { + if (this->GeneratorToolsetSet && this->GeneratorToolset != *tsName) { std::string message = "Error: generator toolset: "; message += this->GeneratorToolset; message += "\nDoes not match the toolset used previously: "; @@ -1535,6 +1580,16 @@ int cmake::ActualConfigure() std::unique_ptr cmake::EvaluateDefaultGlobalGenerator() { + if (!this->EnvironmentGenerator.empty()) { + cmGlobalGenerator* gen = + this->CreateGlobalGenerator(this->EnvironmentGenerator); + if (!gen) { + cmSystemTools::Error("CMAKE_GENERATOR was set but the specified " + "generator doesn't exist. Using CMake default."); + } else { + return std::unique_ptr(gen); + } + } #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(CMAKE_BOOT_MINGW) std::string found; // Try to find the newest VS installed on the computer and -- cgit v1.2.1