Repository: conan-io/conan

Issue:
MSBuild should allow "do not adjust PlatformToolset" with an argument
Hey,

In https://github.com/conan-io/conan/pull/4250 the default behavior of VisualStudio's toolset selection was changed to compile the whole solution using the same toolset (the `/p:PlatformToolset` is always set - see discussion here https://github.com/conan-io/conan/pull/4250#pullrequestreview-193994771).

This change prevents using two different toolsets in the same solution. In my case building a driver using the `WindowsKernelModeDriver10.0` (which is not even a valid option right now) toolset and user mode library using the `v141` toolset.

I will be happy to write the code if this behavior change is accepted.


Discussion:
Hi, thanks for opening the issue.

How the call to `msbuild` would look like to solve that situation? I mean, what would be the patch to Conan? 
Thanks
Unfortunately I don't see a good option without adding a toolset option.
The options I thought about:
1. Adding a `from-compiler` toolset option that will determine the toolset from the `settings.compiler` field (using the current implementation for when `None` is passed as the toolset), and changing back the `None` behavior to use the solution configuration.
2. Adding `use-default` toolset option which will use the the solution configured toolset.

I think 2 is the way to go.
It will not break the behavior of `toolset=None` (again) and the implementation will be simpler (changing an `if` statement in `msbuild.get_command`).

I understand, So you need a way to say "do not adjust the toolset, please" to the `MSBuild` build helper. I think that reasonable. We will think about the best way to implement it. Thanks again for opening the issue!

Resolving patch:
diff --git a/conans/client/build/msbuild.py b/conans/client/build/msbuild.py
--- a/conans/client/build/msbuild.py
+++ b/conans/client/build/msbuild.py
@@ -104,7 +104,8 @@ def get_command(self, project_file, props_file_path=None, targets=None, upgrade_
 
         build_type = build_type or self._settings.get_safe("build_type")
         arch = arch or self._settings.get_safe("arch")
-        toolset = toolset or tools.msvs_toolset(self._settings)
+        if toolset is None:  # False value to skip adjusting
+            toolset = tools.msvs_toolset(self._settings)
         verbosity = os.getenv("CONAN_MSBUILD_VERBOSITY") or verbosity or "minimal"
         if not build_type:
             raise ConanException("Cannot build_sln_command, build_type not defined")


Test patch:
diff --git a/conans/test/unittests/client/build/msbuild_test.py b/conans/test/unittests/client/build/msbuild_test.py
--- a/conans/test/unittests/client/build/msbuild_test.py
+++ b/conans/test/unittests/client/build/msbuild_test.py
@@ -8,6 +8,7 @@
 from parameterized import parameterized
 
 from conans.client import tools
+from conans.client.tools.files import chdir
 from conans.client.build.msbuild import MSBuild
 from conans.errors import ConanException
 from conans.model.version import Version
@@ -152,6 +153,36 @@ def default_toolset_test(self, compiler_version, expected_toolset):
         command = msbuild.get_command("project_should_flags_test_file.sln")
         self.assertIn('/p:PlatformToolset="%s"' % expected_toolset, command)
 
+    @unittest.skipUnless(platform.system() == "Windows", "Requires MSBuild")
+    def skip_toolset_test(self):
+        settings = MockSettings({"build_type": "Debug",
+                                 "compiler": "Visual Studio",
+                                 "compiler.version": "15",
+                                 "arch": "x86_64"})
+
+        class Runner(object):
+
+            def __init__(self):
+                self.commands = []
+
+            def __call__(self, *args, **kwargs):
+                self.commands.append(args[0])
+
+        with chdir(tools.mkdir_tmp()):
+            runner = Runner()
+            conanfile = MockConanfile(settings, runner=runner)
+            msbuild = MSBuild(conanfile)
+            msbuild.build("myproject", toolset=False)
+            self.assertEqual(len(runner.commands), 1)
+            self.assertNotIn("PlatformToolset", runner.commands[0])
+
+            runner = Runner()
+            conanfile = MockConanfile(settings, runner=runner)
+            msbuild = MSBuild(conanfile)
+            msbuild.build("myproject", toolset="mytoolset")
+            self.assertEqual(len(runner.commands), 1)
+            self.assertIn('/p:PlatformToolset="mytoolset"', runner.commands[0])
+
     @parameterized.expand([("v142",),
                            ("v141",),
                            ("v140",),