Using conan with clang-cl and CMake on Windows
See original GitHub issueClang provides the clang-cl
binary, which is compatible with MSVC’s cl
. It can also be used with the Ninja
CMake generator, for example, the following way:
call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\VC\Auxiliary\Build\vcvarsall.bat" x64
set CC=clang-cl
set CXX=clang-cl
set CFLAGS=-m64 -fmsc-version=1910
set CXXFLAGS=-m64 -fmsc-version=1910
cmake ../ -GNinja
CMake’s internal scripts correctly detect the compiler to be cl-compatible and use the cl style command line parameters in this case.
Conan itself has no knowledge about this compiler, but I assumed that if I compile the dependencies with MSVC (which is the default), I should be able to use them in my CMake project.
While this approach mostly works, the conanbuildinfo.cmake
script generated by conan detects the compiler as Clang instead of as MSVC, and displays an error, because of the following line:
if( (CONAN_COMPILER STREQUAL "Visual Studio" AND NOT CMAKE_CXX_COMPILER_ID MATCHES MSVC) OR
While the Ninja generator sets the compiler id to Clang, it also sets the MSVC variable to 1 and sets the MSVC_VERSION correctly to 1910.
I also know that ideally compilers shouldn’t be mixed, but I also assume that adding fully working clang/clang-cl support for conan on windows would be a significant work.
As a simpler solution, this check could be more permissive, for example:
- By changing that line to just
if( (CONAN_COMPILER STREQUAL "Visual Studio" AND NOT MSVC)
- By changing the MSVC version checks to use
MSVC_VERSION
instead of theCMAKE_CXX_COMPILER_VERSION
variable. (currently, this step isn’t necessary, because as I described in #1838, the MSVC version checks are incomplete and pass even when they shouldn’t)
This change would allow any compiler which claims to be compatible with the given MSVC version to be used. It would be also consistent with how most projects detect that they are compiled with MSVC.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:12
- Comments:26 (13 by maintainers)
Re: GNU command line clang. I have a merge request for CMake to support it on windows - after that gets merged, both clang and clang-cl can be real use cases on windows. In the current version, CMake gets a new information variable (CMAKE_lang_COMPILER_FRONTEND_VARIANT) which lets users differentiate between these. (https://gitlab.kitware.com/cmake/cmake/merge_requests/2992)
This PR https://github.com/conan-io/conan/pull/11492, merged for next 1.53 contains a few changes to better support clang in Windows, mainly for the new
CMakeToolchain
integration, but some minor changes for others too.There are some other pending issues about the Windows subsystems environment management, we are also trying to improve them in https://github.com/conan-io/conan/pull/12178, so if you are using Clang in some subsystem and depend on the environment, you might want to track this PR too.
Closing this issue now, but we know that there might still be some gaps, so please try to update to the new integration (this is necessary for 2.0 anyway), and report what might still be failing against this new integration. The best starting point would be the tests in https://github.com/conan-io/conan/blob/develop/conans/test/functional/toolchains/cmake/test_cmake_toolchain_win_clang.py, or using any of the predefined templates
conan new hello/0.1 -m=cmake_lib|autotools_lib|msbuild_lib|meson_lib
, and open a new issue. Many thanks!