Clang: Detection of frontent-variant versus --driver-mode
The detection of Clang's frontent-variant, the logic that calculates CMAKE_<LANG>_COMPILER_FRONTENT_VARIANT
, is not working reliable in all situations.
On Windows it seems to work when using just clang
(respective clang++
) or clang-cl
for CMAKE_C_COMPILER
and CMAKE_CXX_COMPILER
.
But when also using Clang's --driver-mode
option the detection fails (or succeeds out of sheer luck).
For example when using the following CMakeLists.txt
file:
cmake_minimum_required(VERSION 3.19...3.21)
project( DetectCompiler
VERSION 1.0.0
LANGUAGES C CXX
)
message(STATUS "CMAKE_C_COMPILER_ID = ${CMAKE_C_COMPILER_ID}")
message(STATUS "CMAKE_C_SIMULATE_ID = ${CMAKE_C_SIMULATE_ID}")
message(STATUS "CMAKE_C_COMPILER FRONTEND_VARIANT = ${CMAKE_C_COMPILER_FRONTEND_VARIANT}")
message(STATUS "CMAKE_CXX_COMPILER_ID = ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "CMAKE_CXX_SIMULATE_ID = ${CMAKE_CXX_SIMULATE_ID}")
message(STATUS "CMAKE_CXX_COMPILER FRONTEND_VARIANT = ${CMAKE_CXX_COMPILER_FRONTEND_VARIANT}")
It fails for clang --driver-mode=cl
/ clang++ --driver-mode=cl
:
cmake -G "Ninja" -DCMAKE_C_COMPILER=clang;--driver-mode=cl -DCMAKE_CXX_COMPILER=clang++;--driver-mode=cl ../source
-- The C compiler identification is Clang 12.0.0 with GNU-like command-line
-- The CXX compiler identification is Clang 12.0.0 with GNU-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang.exe - broken
CMake Error at C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake/share/cmake-3.20/Modules/CMakeTestCCompiler.cmake:66 (message):
The C compiler
"C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/Users/dbahadir/cmake-tests/build/CMakeFiles/CMakeTmp
Run Build Command(s):C:/PROGRA~2/MICROS~2/2019/PROFES~1/Common7/IDE/COMMON~1/MICROS~1/CMake/Ninja/ninja.exe cmTC_d60bd && [1/2] Building C object CMakeFiles/cmTC_d60bd.dir/testCCompiler.c.obj
FAILED: CMakeFiles/cmTC_d60bd.dir/testCCompiler.c.obj
C:\PROGRA~2\MICROS~2\2019\PROFES~1\VC\Tools\Llvm\x64\bin\clang.exe --driver-mode=cl -g -Xclang -gcodeview -O0 -D_DEBUG -D_DLL -D_MT -Xclang --dependent-lib=msvcrtd -MD -MT CMakeFiles/cmTC_d60bd.dir/testCCompiler.c.obj -MF CMakeFiles\cmTC_d60bd.dir\testCCompiler.c.obj.d -o CMakeFiles/cmTC_d60bd.dir/testCCompiler.c.obj -c testCCompiler.c
clang: warning: unknown argument ignored in clang-cl: '-g' [-Wunknown-argument]
clang: warning: unknown argument ignored in clang-cl: '-MF' [-Wunknown-argument]
clang: error: no such file or directory: 'CMakeFiles/cmTC_d60bd.dir/testCCompiler.c.obj'
clang: error: no such file or directory: 'CMakeFiles\cmTC_d60bd.dir\testCCompiler.c.obj.d'
ninja: build stopped: subcommand failed.
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:3 (project)
-- Configuring incomplete, errors occurred!
See also "C:/Users/dbahadir/cmake-tests/build/CMakeFiles/CMakeOutput.log".
See also "C:/Users/dbahadir/cmake-tests/build/CMakeFiles/CMakeError.log".
For completeness, it succeeds (out of sheer luck) for clang --driver-mode=gcc
/ clang++ --driver-mode=g++
:
cmake -G "Ninja" -DCMAKE_C_COMPILER=clang;--driver-mode=gcc -DCMAKE_CXX_COMPILER=clang++;--driver-mode=g++ ../compiler-detection-tests/
-- The C compiler identification is Clang 12.0.0 with GNU-like command-line
-- The CXX compiler identification is Clang 12.0.0 with GNU-like command-line
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/VC/Tools/Llvm/x64/bin/clang++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - failed
-- CMAKE_C_COMPILER_ID = Clang
-- CMAKE_C_SIMULATE_ID = MSVC
-- CMAKE_C_COMPILER FRONTEND_VARIANT = GNU
-- CMAKE_CXX_COMPILER_ID = Clang
-- CMAKE_CXX_SIMULATE_ID = MSVC
-- CMAKE_CXX_COMPILER FRONTEND_VARIANT = GNU
-- Configuring done
-- Generating done
Edited by Brad King