xmake icon indicating copy to clipboard operation
xmake copied to clipboard

使用clang-cl并开启asan时,cxxflags里缺少-fsanitize=address,并且clang-cl不会主动链接clang_rt.asan-x86_64

Open Arteiimis opened this issue 1 year ago • 4 comments

Xmake 版本

v2.9.5

操作系统版本和架构

Windows10 22H2

描述问题

使用clang-cl并开启asan时,cxxflags里缺少-fsanitize=address,并且clang-cl不会主动链接clang_rt.asan-x86_64

经过测试,llvm自带的clang_rt.asan-x86_64路径: \path\to\llvm\version\lib\clang\19\lib\windows并不在系统路径中, 直接add_links("clang_rt.asan-x86_64")并不奏效, 并且只链接clang_rt.asan-x86_64, asan功能不完善.

期待的结果

正确处理cxxflags, 并且添加链接

工程配置

target("play_ground")
	set_kind("binary")
	set_rundir("$(projectdir)")
	
	set_policy("build.sanitizer.address", true)
	
	add_languages("c++23")
	add_files("src/*.cpp")
	add_includedirs("src")
	add_cxxflags("-std=c++23")
	
	-- add_linkdirs("D:\\scoop\\apps\\llvm\\19.1.2\\lib\\clang\\19\\lib\\windows")
	-- add_links("clang_rt.asan_dynamic-x86_64", "clang_rt.asan_dynamic_runtime_thunk-x86_64")
	-- add_links("clang_rt.asan-x86_64")

	add_packages(requirements)

	set_toolchains("clang-cl")
	-- set_toolset("ld", "lld-link")
target_end()

附加信息和错误日志

checking for flags (-DNDEBUG) ... ok
[ 60%]: cache compiling.release src\main.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 -std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\main.cpp.obj src\main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[ 60%]: cache compiling.release src\thread_pool.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 -std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj src\thread_pool.cpp
[ 80%]: linking.release play_ground.exe
"D:\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.41.34120\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat -machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\play_ground.pdb -out:build\windows\x64\release\play_ground.exe build\.objs\play_ground\windows\x64\release\src\main.cpp.obj build\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj
checking for clang-cl.exe ... D:\scoop\apps\llvm\current\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
checking for flags (cl_external_includedir) ... ok
create ok!
compile_commands.json updated!
[100%]: build ok, spent 1.563s

Arteiimis avatar Oct 17 '24 14:10 Arteiimis

Bot detected the issue body's language is not English, translate it automatically.


Title: When using clang-cl and turning on asan, -fsanitize=address is missing in cxxflags, and clang-cl will not actively link clang_rt.asan-x86_64

Xmake version

v2.9.5

Operating system version and architecture

Windows 10 22H2

Describe the problem

When using clang-cl and turning on asan, -fsanitize=address is missing in cxxflags, and clang-cl will not actively link clang_rt.asan-x86_64

After testing, the clang_rt.asan-x86_64 path that comes with llvm: \path\to\llvm\version\lib\clang\19\lib\windows is not in the system path, directly add_links("clang_rt.asan -x86_64") does not work, and only links clang_rt.asan-x86_64, and the asan function is incomplete.

Expected results

Correctly handle cxxflags and add links

Project configuration

target("play_ground")
set_kind("binary")
set_rundir("$(projectdir)")
	
set_policy("build.sanitizer.address", true)
	
add_languages("c++23")
add_files("src/*.cpp")
add_includedirs("src")
add_cxxflags("-std=c++23")
	
-- add_linkdirs("D:\\scoop\\apps\\llvm\\19.1.2\\lib\\clang\\19\\lib\\windows")
-- add_links("clang_rt.asan_dynamic-x86_64", "clang_rt.asan_dynamic_runtime_thunk-x86_64")
-- add_links("clang_rt.asan-x86_64")

add_packages(requirements)

set_toolchains("clang-cl")
-- set_toolset("ld", "lld-link")
target_end()

Additional information and error logs

checking for flags (-DNDEBUG) ... ok
[60%]: cache compiling.release src\main.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 - std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\main.cpp.obj src\ main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[60%]: cache compiling.release src\thread_pool.cpp
D:\scoop\apps\llvm\current\bin\clang-cl.exe -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.play_ground.pdb -O2 -std:c++20 - std:c++latest -MD -Isrc /EHsc /utf-8 -Xclang -std=c++23 -DNDEBUG -Fobuild\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj src\ thread_pool.cpp
[80%]: linking.release play_ground.exe
"D:\\Microsoft Visual Studio\\2022\\Community\\VC\\Tools\\MSVC\\14.41.34120\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat - machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\play_ground.pdb -out:build\windows\x64\release\play_ground.exe build\.objs\play_ground\windows\ x64\release\src\main.cpp.obj build\.objs\play_ground\windows\x64\release\src\thread_pool.cpp.obj
checking for clang-cl.exe ... D:\scoop\apps\llvm\current\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
checking for flags (cl_external_includedir) ... ok
Create OK!
compile_commands.json updated!
[100%]: build ok, spent 1.563s

Issues-translate-bot avatar Oct 17 '24 14:10 Issues-translate-bot

clang 相关的 toolchain 大概都需要处理 https://stackoverflow.com/questions/76702561/clang-with-address-sanitizer-on-windows-fails-at-link-time

star-hengxing avatar Oct 17 '24 14:10 star-hengxing

clang 原本就支持,clang-cl 还没处理,只支持了 cl 。。可以提个 pr 过来

https://github.com/xmake-io/xmake/blob/86627f20f80a50fece956b4e8394afc4b5a83cd1/xmake/rules/c%2B%2B/build_sanitizer/config.lua#L39

waruqi avatar Oct 18 '24 01:10 waruqi

Bot detected the issue body's language is not English, translate it automatically.


clang originally supported it, clang-cl has not been processed yet, and only cl is supported. . Can you submit a PR?

https://github.com/xmake-io/xmake/blob/86627f20f80a50fece956b4e8394afc4b5a83cd1/xmake/rules/c%2B%2B/build_sanitizer/config.lua#L39

Issues-translate-bot avatar Oct 18 '24 03:10 Issues-translate-bot

试下这个 patch https://github.com/xmake-io/xmake/pull/5742

clang-cl不会主动链接clang_rt.asan-x86_64

这边测试没这个问题,而且 clang-cl 是编译器 又不是链接器

PS C:\Users\wangrunqing\Downloads\test91> xmake f --toolchain=clang-cl -c
checking for platform ... windows
checking for architecture ... x64
checking for Microsoft Visual Studio (x64) version ... 2022
checking for LLVM Clang C/C++ Compiler (x64) version ... 18.1.6
PS C:\Users\wangrunqing\Downloads\test91> xmake -rv
checking for C:\Program Files\LLVM\bin\clang-cl.exe ... ok
checking for flags (-FS -Fd) ... ok
checking for flags (-O2) ... ok
checking for flags (-DNDEBUG) ... ok
[ 50%]: cache compiling.release src\main.cpp
"C:\\Program Files\\LLVM\\bin\\clang-cl.exe" -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.toytrain.pdb -O2 -std:c++latest /EHsc -fsanitize=address -DNDEBUG -Fobuild\.objs\toytrain\windows\x64\release\src\main.cpp.obj src\main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[ 75%]: linking.release toytrain.exe
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.41.33923\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat -machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\toytrain.pdb -fsanitize=address -out:build\windows\x64\release\toytrain.exe build\.objs\toytrain\windows\x64\release\src\main.cpp.obj
checking for clang-cl.exe ... C:\Program Files\LLVM\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
[100%]: build ok, spent 2.547s
PS C:\Users\wangrunqing\Downloads\test91> xmake r
hello world!

waruqi avatar Oct 21 '24 06:10 waruqi

试下这个 patch #5742

clang-cl不会主动链接clang_rt.asan-x86_64

这边测试没这个问题,而且 clang-cl 是编译器 又不是链接器

PS C:\Users\wangrunqing\Downloads\test91> xmake f --toolchain=clang-cl -c
checking for platform ... windows
checking for architecture ... x64
checking for Microsoft Visual Studio (x64) version ... 2022
checking for LLVM Clang C/C++ Compiler (x64) version ... 18.1.6
PS C:\Users\wangrunqing\Downloads\test91> xmake -rv
checking for C:\Program Files\LLVM\bin\clang-cl.exe ... ok
checking for flags (-FS -Fd) ... ok
checking for flags (-O2) ... ok
checking for flags (-DNDEBUG) ... ok
[ 50%]: cache compiling.release src\main.cpp
"C:\\Program Files\\LLVM\\bin\\clang-cl.exe" -c -m64 -Zi -FS -Fdbuild\windows\x64\release\compile.toytrain.pdb -O2 -std:c++latest /EHsc -fsanitize=address -DNDEBUG -Fobuild\.objs\toytrain\windows\x64\release\src\main.cpp.obj src\main.cpp
checking for flags (-fcolor-diagnostics) ... ok
checking for flags (-fansi-escape-codes) ... ok
[ 75%]: linking.release toytrain.exe
"C:\\Program Files\\Microsoft Visual Studio\\2022\\Preview\\VC\\Tools\\MSVC\\14.41.33923\\bin\\HostX64\\x64\\link.exe" -nologo -dynamicbase -nxcompat -machine:x64 /opt:ref /opt:icf -debug -pdb:build\windows\x64\release\toytrain.pdb -fsanitize=address -out:build\windows\x64\release\toytrain.exe build\.objs\toytrain\windows\x64\release\src\main.cpp.obj
checking for clang-cl.exe ... C:\Program Files\LLVM\bin\clang-cl.exe
checking for the c compiler (cc) ... clang-cl.exe
[100%]: build ok, spent 2.547s
PS C:\Users\wangrunqing\Downloads\test91> xmake r
hello world!

没问题了,感谢!

Arteiimis avatar Oct 21 '24 08:10 Arteiimis