Repository: conan-io/conan
Pull request #16844 (2024-08-19 09:44:14)

Use ClangCL platform toolset in MSBuildToolchain generator when compi…
Changelog: Bugfix: when using Visual Studio's llvm-clang, set the correct Platform Toolset in `MSBuildToolchain`
Docs: Omit

When building msvc-compatible binaries using the Clang distribution in Visual Studio, set the correct Platform toolset when using MSBuildToolchain.

From: https://learn.microsoft.com/en-us/gaming/gdk/_content/gc/tools-pc/visualstudio/gr-vs-clang
<img width="873" alt="Screenshot 2024-08-19 at 10 43 32" src="https://github.com/user-attachments/assets/ad4bb46b-3923-4adf-8fdf-6762b7eb2eaf">


memsharded: I have double checked conan-center-index, and all usages of ``msvs_toolset`` are protected by ``is_msvc()``, so we are safe there.

Docs in https://docs.conan.io/2/reference/tools/microsoft/helpers.html#msvs-toolset would need an update probably, they are not very clear, I suggest updating the docstring of ``msvs_toolset`` in this PR


Patch:
diff --git a/conan/tools/microsoft/visual.py b/conan/tools/microsoft/visual.py
index 0455ae45e..ab4a71293 100644
--- a/conan/tools/microsoft/visual.py
+++ b/conan/tools/microsoft/visual.py
@@ -367,11 +367,12 @@ def is_msvc_static_runtime(conanfile):
 
 def msvs_toolset(conanfile):
     """
-    Returns the corresponding platform toolset based on the compiler of the given conanfile.
+    Returns the corresponding platform toolset based on the compiler setting.
     In case no toolset is configured in the profile, it will return a toolset based on the
     compiler version, otherwise, it will return the toolset from the profile.
     When there is no compiler version neither toolset configured, it will return None
-    It supports Visual Studio, msvc and Intel.
+    It supports msvc, intel-cc and clang compilers. For clang, is assumes the ClangCl toolset,
+    as provided by the Visual Studio installer.
 
     :param conanfile: Conanfile instance to access settings.compiler
     :return: A toolset when compiler.version is valid or compiler.toolset is configured. Otherwise, None.
@@ -384,5 +385,7 @@ def msvs_toolset(conanfile):
         if subs_toolset:
             return subs_toolset
         return msvc_version_to_toolset_version(compiler_version)
+    if compiler == "clang":
+        return "ClangCl"
     if compiler == "intel-cc":
         return IntelCC(conanfile).ms_toolset


Test patch:
diff --git a/test/unittests/tools/microsoft/test_msvs_toolset.py b/test/unittests/tools/microsoft/test_msvs_toolset.py
index e7b70a3f9..e1c7f3098 100644
--- a/test/unittests/tools/microsoft/test_msvs_toolset.py
+++ b/test/unittests/tools/microsoft/test_msvs_toolset.py
@@ -81,3 +81,10 @@ def test_intel_cc_default(compiler_version, compiler_mode, expected_toolset):
                                        "compiler.version": compiler_version,
                                        "compiler.mode": compiler_mode})
     assert msvs_toolset(conanfile) == expected_toolset
+
+def test_clang_platform_toolset():
+    conanfile = ConanFileMock()
+    conanfile.settings = MockSettings({"compiler": "clang",
+                             "compiler.version": "17",
+                             "compiler.runtime": "dynamic"})
+    assert msvs_toolset(conanfile) == "ClangCl"