VS: Add option to select the version of the toolset used by VS 2017

Add new `version=` parameter in the toolset setting to select the
version.  Add variable `CMAKE_VS_PLATFORM_TOOLSET_VERSION` to hold the
version, if one is set (blank indicates default).

Fixes: #17549
This commit is contained in:
Basil Fierz
2018-05-19 13:23:21 +02:00
committed by Brad King
parent a543a233a2
commit 5f13168419
22 changed files with 191 additions and 0 deletions
@@ -231,9 +231,66 @@ bool cmGlobalVisualStudio10Generator::SetGeneratorToolset(
}
}
if (!this->GeneratorToolsetVersion.empty() &&
this->GeneratorToolsetVersion != "Test Toolset Version") {
// If a specific minor version of the toolset was requested, verify that it
// is compatible to the major version and that is exists on disk.
// If not clear the value.
std::string version = this->GeneratorToolsetVersion;
cmsys::RegularExpression regex("[0-9][0-9]\\.[0-9][0-9]");
if (regex.find(version)) {
version = "v" + version.erase(2, 1);
} else {
// Version not recognized. Clear it.
version.clear();
}
if (version.find(this->GetPlatformToolsetString()) != 0) {
std::ostringstream e;
/* clang-format off */
e <<
"Generator\n"
" " << this->GetName() << "\n"
"given toolset and version specification\n"
" " << this->GetPlatformToolsetString() << ",version=" <<
this->GeneratorToolsetVersion << "\n"
"contains an invalid version specification."
;
/* clang-format on */
mf->IssueMessage(cmake::FATAL_ERROR, e.str());
// Clear the configured tool-set
this->GeneratorToolsetVersion.clear();
}
std::string const toolsetPath = this->GetAuxiliaryToolset();
if (!toolsetPath.empty() && !cmSystemTools::FileExists(toolsetPath)) {
std::ostringstream e;
/* clang-format off */
e <<
"Generator\n"
" " << this->GetName() << "\n"
"given toolset and version specification\n"
" " << this->GetPlatformToolsetString() << ",version=" <<
this->GeneratorToolsetVersion << "\n"
"does not seem to be installed at\n" <<
" " << toolsetPath;
;
/* clang-format on */
mf->IssueMessage(cmake::FATAL_ERROR, e.str());
// Clear the configured tool-set
this->GeneratorToolsetVersion.clear();
}
}
if (const char* toolset = this->GetPlatformToolset()) {
mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET", toolset);
}
if (const char* version = this->GetPlatformToolsetVersion()) {
mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_VERSION", version);
}
if (const char* hostArch = this->GetPlatformToolsetHostArchitecture()) {
mf->AddDefinition("CMAKE_VS_PLATFORM_TOOLSET_HOST_ARCHITECTURE", hostArch);
}
@@ -319,6 +376,10 @@ bool cmGlobalVisualStudio10Generator::ProcessGeneratorToolsetField(
this->GeneratorToolsetCuda = value;
return true;
}
if (key == "version") {
this->GeneratorToolsetVersion = value;
return true;
}
return false;
}
@@ -512,6 +573,25 @@ std::string const& cmGlobalVisualStudio10Generator::GetPlatformToolsetString()
return empty;
}
const char* cmGlobalVisualStudio10Generator::GetPlatformToolsetVersion() const
{
std::string const& version = this->GetPlatformToolsetVersionString();
if (version.empty()) {
return nullptr;
}
return version.c_str();
}
std::string const&
cmGlobalVisualStudio10Generator::GetPlatformToolsetVersionString() const
{
if (!this->GeneratorToolsetVersion.empty()) {
return this->GeneratorToolsetVersion;
}
static std::string const empty;
return empty;
}
const char*
cmGlobalVisualStudio10Generator::GetPlatformToolsetHostArchitecture() const
{
@@ -535,6 +615,11 @@ cmGlobalVisualStudio10Generator::GetPlatformToolsetCudaString() const
return this->GeneratorToolsetCuda;
}
std::string cmGlobalVisualStudio10Generator::GetAuxiliaryToolset() const
{
return {};
}
bool cmGlobalVisualStudio10Generator::FindMakeProgram(cmMakefile* mf)
{
if (!this->cmGlobalVisualStudio8Generator::FindMakeProgram(mf)) {
+7
View File
@@ -54,6 +54,10 @@ public:
const char* GetPlatformToolset() const;
std::string const& GetPlatformToolsetString() const;
/** The toolset version. */
const char* GetPlatformToolsetVersion() const;
std::string const& GetPlatformToolsetVersionString() const;
/** The toolset host architecture name (e.g. x64 for 64-bit host tools). */
const char* GetPlatformToolsetHostArchitecture() const;
@@ -101,6 +105,8 @@ public:
std::string Encoding() override;
virtual const char* GetToolsVersion() { return "4.0"; }
virtual std::string GetAuxiliaryToolset() const;
bool FindMakeProgram(cmMakefile* mf) override;
static std::string GetInstalledNsightTegraVersion();
@@ -135,6 +141,7 @@ protected:
std::string const& GetMSBuildCommand();
std::string GeneratorToolset;
std::string GeneratorToolsetVersion;
std::string GeneratorToolsetHostArchitecture;
std::string GeneratorToolsetCuda;
std::string DefaultPlatformToolset;
@@ -158,6 +158,25 @@ bool cmGlobalVisualStudio15Generator::GetVSInstance(std::string& dir) const
return vsSetupAPIHelper.GetVSInstanceInfo(dir);
}
std::string cmGlobalVisualStudio15Generator::GetAuxiliaryToolset() const
{
const char* version = this->GetPlatformToolsetVersion();
if (version) {
std::string instancePath;
GetVSInstance(instancePath);
std::stringstream path;
path << instancePath;
path << "/VC/Auxiliary/Build/";
path << version;
path << "/Microsoft.VCToolsVersion." << version << ".props";
std::string toolsetPath = path.str();
cmSystemTools::ConvertToUnixSlashes(toolsetPath);
return toolsetPath;
}
return {};
}
bool cmGlobalVisualStudio15Generator::InitializeWindows(cmMakefile* mf)
{
// If the Win 8.1 SDK is installed then we can select a SDK matching
+2
View File
@@ -32,6 +32,8 @@ public:
bool GetVSInstance(std::string& dir) const;
std::string GetAuxiliaryToolset() const override;
protected:
bool InitializeWindows(cmMakefile* mf) override;
bool SelectWindowsStoreToolset(std::string& toolset) const override;
@@ -557,6 +557,11 @@ void cmVisualStudio10TargetGenerator::Generate()
switch (this->ProjectType) {
case vcxproj:
if (this->GlobalGenerator->GetPlatformToolsetVersion()) {
Elem(e0, "Import")
.Attribute("Project",
this->GlobalGenerator->GetAuxiliaryToolset());
}
Elem(e0, "Import").Attribute("Project", VS10_CXX_DEFAULT_PROPS);
break;
case csproj: