From 197b4cbe186cd31a490fc3cfcaace55bd7123829 Mon Sep 17 00:00:00 2001
From: Julien Jemine <julien.jemine@gmail.com>
Date: Tue, 28 Apr 2020 16:55:03 +0200
Subject: VS: Add option for per-target PlatformToolset

Add a `VS_PLATFORM_TOOLSET` target property to set `PlatformToolset` in
the `.vcxproj` file for specific targets.  Document that this is safe
only when the named toolset uses the same underlying compiler as the
primary toolset.

Fixes: #17429
---
 Help/manual/cmake-properties.7.rst                 |  1 +
 Help/prop_tgt/VS_PLATFORM_TOOLSET.rst              | 10 ++++++
 Help/release/dev/vs-platform-toolset.rst           |  6 ++++
 Source/cmVisualStudio10TargetGenerator.cxx         | 10 ++++--
 Tests/RunCMake/VS10Project/RunCMakeTest.cmake      |  1 +
 .../VS10Project/VsPlatformToolset-check.cmake      | 36 ++++++++++++++++++++++
 Tests/RunCMake/VS10Project/VsPlatformToolset.cmake |  6 ++++
 7 files changed, 68 insertions(+), 2 deletions(-)
 create mode 100644 Help/prop_tgt/VS_PLATFORM_TOOLSET.rst
 create mode 100644 Help/release/dev/vs-platform-toolset.rst
 create mode 100644 Tests/RunCMake/VS10Project/VsPlatformToolset-check.cmake
 create mode 100644 Tests/RunCMake/VS10Project/VsPlatformToolset.cmake

diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst
index 4966f86..9031e9c 100644
--- a/Help/manual/cmake-properties.7.rst
+++ b/Help/manual/cmake-properties.7.rst
@@ -372,6 +372,7 @@ Properties on Targets
    /prop_tgt/VS_MOBILE_EXTENSIONS_VERSION
    /prop_tgt/VS_NO_SOLUTION_DEPLOY
    /prop_tgt/VS_PACKAGE_REFERENCES
+   /prop_tgt/VS_PLATFORM_TOOLSET
    /prop_tgt/VS_PROJECT_IMPORT
    /prop_tgt/VS_SCC_AUXPATH
    /prop_tgt/VS_SCC_LOCALPATH
diff --git a/Help/prop_tgt/VS_PLATFORM_TOOLSET.rst b/Help/prop_tgt/VS_PLATFORM_TOOLSET.rst
new file mode 100644
index 0000000..f8f2e8e
--- /dev/null
+++ b/Help/prop_tgt/VS_PLATFORM_TOOLSET.rst
@@ -0,0 +1,10 @@
+VS_PLATFORM_TOOLSET
+-------------------
+
+Overrides the platform toolset used to build a target.
+
+Only supported when the compiler used by the given toolset is the
+same as the compiler used to build the whole source tree.
+
+This is especially useful to create driver projects with the toolsets
+"WindowsUserModeDriver10.0" or "WindowsKernelModeDriver10.0".
diff --git a/Help/release/dev/vs-platform-toolset.rst b/Help/release/dev/vs-platform-toolset.rst
new file mode 100644
index 0000000..c5062c7
--- /dev/null
+++ b/Help/release/dev/vs-platform-toolset.rst
@@ -0,0 +1,6 @@
+vs-platform-toolset
+-------------------
+
+* The :prop_tgt:`VS_PLATFORM_TOOLSET` target property was added to tell
+  :ref:`Visual Studio Generators` for VS 2010 and above to override
+  the platform toolset.
diff --git a/Source/cmVisualStudio10TargetGenerator.cxx b/Source/cmVisualStudio10TargetGenerator.cxx
index 930db41..5f79eb0 100644
--- a/Source/cmVisualStudio10TargetGenerator.cxx
+++ b/Source/cmVisualStudio10TargetGenerator.cxx
@@ -1236,7 +1236,10 @@ void cmVisualStudio10TargetGenerator::WriteMSToolConfigurationValues(
   } else {
     e1.Element("CharacterSet", "MultiByte");
   }
-  if (const char* toolset = gg->GetPlatformToolset()) {
+  if (const char* projectToolsetOverride =
+        this->GeneratorTarget->GetProperty("VS_PLATFORM_TOOLSET")) {
+    e1.Element("PlatformToolset", projectToolsetOverride);
+  } else if (const char* toolset = gg->GetPlatformToolset()) {
     e1.Element("PlatformToolset", toolset);
   }
   if (this->GeneratorTarget->GetPropertyAsBool("VS_WINRT_COMPONENT") ||
@@ -1279,7 +1282,10 @@ void cmVisualStudio10TargetGenerator::WriteMSToolConfigurationValuesManaged(
     o.RemoveFlag("Platform");
   }
 
-  if (const char* toolset = gg->GetPlatformToolset()) {
+  if (const char* projectToolsetOverride =
+        this->GeneratorTarget->GetProperty("VS_PLATFORM_TOOLSET")) {
+    e1.Element("PlatformToolset", projectToolsetOverride);
+  } else if (const char* toolset = gg->GetPlatformToolset()) {
     e1.Element("PlatformToolset", toolset);
   }
 
diff --git a/Tests/RunCMake/VS10Project/RunCMakeTest.cmake b/Tests/RunCMake/VS10Project/RunCMakeTest.cmake
index 5ccca01..06ccaae 100644
--- a/Tests/RunCMake/VS10Project/RunCMakeTest.cmake
+++ b/Tests/RunCMake/VS10Project/RunCMakeTest.cmake
@@ -33,6 +33,7 @@ run_cmake(VsPrecompileHeadersReuseFromCompilePDBName)
 run_cmake(VsDeployEnabled)
 run_cmake(VsSettings)
 run_cmake(VsSourceSettingsTool)
+run_cmake(VsPlatformToolset)
 
 run_cmake(VsWinRTByDefault)
 
diff --git a/Tests/RunCMake/VS10Project/VsPlatformToolset-check.cmake b/Tests/RunCMake/VS10Project/VsPlatformToolset-check.cmake
new file mode 100644
index 0000000..416220b
--- /dev/null
+++ b/Tests/RunCMake/VS10Project/VsPlatformToolset-check.cmake
@@ -0,0 +1,36 @@
+macro(ReadPlatformToolset tgt outvar)
+  set(vcProjectFile "${RunCMake_TEST_BINARY_DIR}/${tgt}.vcxproj")
+  if(NOT EXISTS "${vcProjectFile}")
+    set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not exist.")
+    return()
+  endif()
+
+  set(HAVE_PlatformToolset 0)
+
+  file(STRINGS "${vcProjectFile}" lines)
+  foreach(line IN LISTS lines)
+    if(line MATCHES "^ *<PlatformToolset>([^<>]+)</PlatformToolset>")
+      set(${outvar} "${CMAKE_MATCH_1}")
+      set(HAVE_PlatformToolset 1)
+      break()
+    endif()
+  endforeach()
+
+  if(NOT HAVE_PlatformToolset)
+    set(RunCMake_TEST_FAILED "Project file ${tgt}.vcxproj does not have a <PlatformToolset> field.")
+    return()
+  endif()
+endmacro()
+
+ReadPlatformToolset(NormalPlatformToolset NORMAL_TOOLSET)
+ReadPlatformToolset(OverridenPlatformToolset OVERRIDEN_TOOLSET)
+
+if (NOT "${OVERRIDEN_TOOLSET}" STREQUAL "MyCustomToolset")
+  set(RunCMake_TEST_FAILED "Failed to override the platform toolset")
+  return()
+endif()
+
+if ("${NORMAL_TOOLSET}" STREQUAL "MyCustomToolset")
+  set(RunCMake_TEST_FAILED "Main toolset was overriden (it shouldn't)")
+  return()
+endif()
diff --git a/Tests/RunCMake/VS10Project/VsPlatformToolset.cmake b/Tests/RunCMake/VS10Project/VsPlatformToolset.cmake
new file mode 100644
index 0000000..dce9717
--- /dev/null
+++ b/Tests/RunCMake/VS10Project/VsPlatformToolset.cmake
@@ -0,0 +1,6 @@
+enable_language(CXX)
+
+add_library(NormalPlatformToolset foo.cpp)
+add_library(OverridenPlatformToolset foo.cpp)
+set_target_properties(OverridenPlatformToolset
+                      PROPERTIES VS_PLATFORM_TOOLSET MyCustomToolset)
-- 
cgit v0.12