Repository: conan-io/conan

Issue:
Clang via Visual Studio results in error
To help us debug your issue please explain:

- [X] I've read the [CONTRIBUTING guide](https://raw.githubusercontent.com/conan-io/conan/develop/.github/CONTRIBUTING.md).
- [X] I've specified the Conan version, operating system version and any tool that can be relevant.
- [X] I've explained the steps to reproduce the error or the motivation/use case of the question/suggestion.

Conan 0.30.3

When using the `compiler` to `Visual Studio` and `compiler.toolset` setting set to `LLVM-vs2014` (and possibly others), the CMake generator's generated file creates an error condition:

```
-- Selecting Windows SDK version 10.0.15063.0 to target Windows 6.1.7601.
-- The C compiler identification is Clang 5.0.1
-- The CXX compiler identification is Clang 5.0.1
-- Check for working C compiler: C:/Program Files/LLVM/msbuild-bin/cl.exe
-- Check for working C compiler: C:/Program Files/LLVM/msbuild-bin/cl.exe -- works
...
-- Conan: called by CMake conan helper
CMake Error at D:/conan-cache/.../conanbuildinfo.cmake:416 (message):
  Incorrect 'Visual Studio', is not the one detected by CMake: 'Clang'
Call Stack (most recent call first):
  D:/conan-cache/.../conanbuildinfo.cmake:115 (conan_check_compiler)
  CMakeLists.txt:6 (conan_basic_setup)
```

Since this compiler.toolset option is present in the default settings.yml, I presume this is supposed to work. My guess at the right way to approach this is to teach the CMake generator about compiler.toolset, which it is currently blissfully ignorant of. The downside is that it also means teaching the generator about this setup (and would potentially not work for custom toolsets).
  


Discussion:
Actually, the CMake is passing the ``-T`` option, and it is being used, but it is the check in the ``conanbuildinfo.cmake`` that detects that the compiler being used is Clang, and not Visual Studio.

Certainly the support for Clang in Windows has to be improved.

Maybe @SSE4 can help further, has expertise in it.
Yeah, sorry if I wasn't clear - I recognize that the problem is in the generation of the conanbuildinfo.cmake, not in CMake itself (which is passing the correct -T option). I think I see the problem in the generator and have a patch, but I need to test it a little bit first; assuming it's good I'll submit a pull request.

Resolving patch:
diff --git a/conans/client/generators/cmake_common.py b/conans/client/generators/cmake_common.py
--- a/conans/client/generators/cmake_common.py
+++ b/conans/client/generators/cmake_common.py
@@ -376,10 +376,23 @@ def generate_targets_section(dependencies):
         set(CROSS_BUILDING 1)
     endif()
 
+    # If using VS, verify toolset
+    if (CONAN_COMPILER STREQUAL "Visual Studio")
+        if (CONAN_SETTINGS_COMPILER_TOOLSET MATCHES "LLVM" OR
+            CONAN_SETTINGS_COMPILER_TOOLSET MATCHES "clang")
+            set(EXPECTED_CMAKE_CXX_COMPILER_ID "Clang")
+        else()
+            set(EXPECTED_CMAKE_CXX_COMPILER_ID "MSVC")
+        endif()
+
+        if (NOT CMAKE_CXX_COMPILER_ID MATCHES ${EXPECTED_CMAKE_CXX_COMPILER_ID})
+            message(FATAL_ERROR "Incorrect '${CONAN_COMPILER}'. Toolset specifies compiler as '${EXPECTED_CMAKE_CXX_COMPILER_ID}' "
+                                "but CMake detected '${CMAKE_CXX_COMPILER_ID}'")
+        endif()
+
     # Avoid checks when cross compiling, apple-clang crashes because its APPLE but not apple-clang
     # Actually CMake is detecting "clang" when you are using apple-clang, only if CMP0025 is set to NEW will detect apple-clang
-    if( (CONAN_COMPILER STREQUAL "Visual Studio" AND NOT CMAKE_CXX_COMPILER_ID MATCHES MSVC) OR
-        (CONAN_COMPILER STREQUAL "gcc" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR
+    elseif((CONAN_COMPILER STREQUAL "gcc" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "GNU") OR
         (CONAN_COMPILER STREQUAL "apple-clang" AND NOT CROSS_BUILDING AND (NOT APPLE OR NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")) OR
         (CONAN_COMPILER STREQUAL "clang" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang") OR
         (CONAN_COMPILER STREQUAL "sun-cc" AND NOT CMAKE_CXX_COMPILER_ID MATCHES "SunPro") )


Test patch: