Clang/Windows: vs_link_exe produces incorrect binary
I have the following setup:
OS: Windows 10
Arch: x86_64
Windows SDK: 10.0.19041.0 (Visual Studio 16 2019 toolchain only install)
MS Visual Studio: None
Additional LLVM Installation: 14.0.6
CMake: 3.20.3 and 3.21.1 (edit:) as well as 3.23.2 and 3.24.0-rc4
When I build a project with source coverage like the attached sample project with code foo.cc
"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64
cmake .. -G Ninja -DCMAKE_CXX_COMPILER:FILEPATH="C:\Program Files\LLVM\bin\clang-cl.exe"
ninja
and run the resulting executable foo.exe
the created profile file default.profraw
is of format version 0x05 (second 64bit integer in the file) and cannot be processed with the installed tools of the LLVM installation which expect version 0x08 of the file format.
If I replace the linker rule command in CMakeFiles\rules.ninja
cmd.exe /C "$PRE_LINK && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=$OBJECT_DIR --rc=C:\PROGRA~1\LLVM\bin\llvm-rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x86\mt.exe --manifests $MANIFESTS -- C:\PROGRA~1\LLVM\bin\lld-link.exe /nologo $in /out:$TARGET_FILE /implib:$TARGET_IMPLIB /pdb:$TARGET_PDB /version:0.0 $LINK_FLAGS $LINK_PATH $LINK_LIBRARIES && $POST_BUILD"
with the corresponding clang-cl.exe call
command = cmd.exe /C "$PRE_LINK && "C:\Program Files\LLVM\bin\clang-cl.exe" /nologo $in /o $TARGET_FILE /INCREMENTAL -fprofile-instr-generate -fcoverage-mapping /link /implib:$TARGET_IMPLIB /pdb:$TARGET_PDB /version:0.0 $LINK_FLAGS $LINK_PATH $LINK_LIBRARIES && $POST_BUILD"
the correct file format is generated.
Suspicion:
The following is just a guess because cmake -E vs_link_exe is not documented and does not support --trace or --debug-output options.
-E vs_link_exe somehow links against the much older C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\lib\x64\clang_rt.profile-x86_64.lib
and not C:\Program Files\LLVM\lib\clang\14.0.6\lib\windows\clang_rt.profile-x86_64.lib
which would be correct for the toolchain, as C:\PROGRA~1\LLVM\bin\clang-cl.exe
is used for compilation.