Inspired by the “Ways to Compile with Clang on Windows” blog post from @Unspongeful and after some extended testing, the following command line worked for me (and yes, it’s one big command I just splitted into several lines for better readability):
> cmake -E env LDFLAGS="-fuse-ld=lld-link" PATH="<path\to\ninja>"
cmake -H. -G Ninja -Bbuild
-DCMAKE_C_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe"
-DCMAKE_CXX_COMPILER:PATH="%ProgramFiles(x86)%\LLVM\bin\clang.exe"
-DCMAKE_C_COMPILER_ID="Clang"
-DCMAKE_CXX_COMPILER_ID="Clang"
-DCMAKE_SYSTEM_NAME="Generic"
Here is some background information:
-
I injected your linker flags with the
LDFLAGS
environment variable -
I reduced the
PATH
environment variable to just point to whereninja
is located, because CMake was picking myMinGW
toolchain (which I didn’t want included in the build process)Related to Environment variable used by CMake to detect Visual C++ compiler tools for Ninja
-
Defining the compiler ids “bypasses the check for working compiler and basic compiler information tests”
See obsolete, but sometimes useful
CMakeForceCompiler
module -
And I set
CMAKE_SYSTEM_NAME
toGeneric
to avoid having any additional platform specific compiler/linker flags added by CMakeSee How to partially disabling cmake C/C++ custom compiler checking
It seems at the moment you have to bypass a lot of CMake’s automatic checks to get it working. So probably check with the CMake team or raise an issue to get this scenario officially supported.
And the last part with a Generic
system is probably not the best choice, because it will skip Windows specific settings like the .exe
suffix.
But it was the only constellation that actually worked:
-- The C compiler identification is Clang
-- The CXX compiler identification is Clang
-- Check for working C compiler: C:/Program Files (x86)/LLVM/bin/clang.exe
-- Check for working C compiler: C:/Program Files (x86)/LLVM/bin/clang.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/bin/clang.exe
-- Check for working CXX compiler: C:/Program Files (x86)/LLVM/bin/clang.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: build