summaryrefslogtreecommitdiff
path: root/xmake/core/package/package.lua
diff options
context:
space:
mode:
authorstar9029 <[email protected]>2025-12-20 23:30:23 +0800
committerstar9029 <[email protected]>2025-12-20 23:30:23 +0800
commitd3a3a4d45e92a4b6af0438e0df81dce9d0bd4dc4 (patch)
tree837e489367a9b546737b9e4dda2d98857985f790 /xmake/core/package/package.lua
parent95363af8cefe003ed1599b124081a0d009f9fb8d (diff)
Refactor windows asan
Diffstat (limited to 'xmake/core/package/package.lua')
-rw-r--r--xmake/core/package/package.lua40
1 files changed, 37 insertions, 3 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua
index 4a32bbdf7..75e134b2a 100644
--- a/xmake/core/package/package.lua
+++ b/xmake/core/package/package.lua
@@ -2460,17 +2460,51 @@ function _instance:_generate_sanitizer_configs(checkmode, sourcekind)
-- add cflags
local configs = {}
- if sourcekind and self:has_tool(sourcekind, "cl", "clang", "clangxx", "gcc", "gxx") then
+ if sourcekind and self:has_tool(sourcekind, "cl", "clang", "clangxx", "clang_cl", "gcc", "gxx") then
local cflag = sourcekind == "cxx" and "cxxflags" or "cflags"
configs[cflag] = "-fsanitize=" .. checkmode
end
+ local ldflags = {}
-- add ldflags and shflags
-- msvc does not have an fsanitize linker flag, so the 'link' tool is excluded
if self:has_tool("ld", "clang", "clangxx", "gcc", "gxx") then
- configs.ldflags = "-fsanitize=" .. checkmode
- configs.shflags = "-fsanitize=" .. checkmode
+ table.insert(ldflags, "-fsanitize=" .. checkmode)
end
+
+ if self:is_plat("windows") and checkmode == "address" and not self:has_tool("cxx", "cl") then
+ assert(self:has_runtime("MD", "MT"), "clang asan only support MD/MT runtime on windows")
+ if self:has_tool("cxx", "clang", "clangxx") then
+ if self:has_runtime("MT") then
+ table.insert(ldflags, "-D_MT")
+ elseif self:has_runtime("MD") then
+ table.join2(ldflags, {"-D_MT", "-D_DLL"})
+ end
+ elseif self:has_tool("cxx", "clang_cl") then
+ -- TODO: This is hack, try to find a way to let cmake use clang++ for link
+ -- @see https://gitlab.kitware.com/cmake/cmake/-/issues/26430
+ local outdata, errdata = assert(os.iorunv(self:build_getenv("cc"), {"-print-resource-dir"}))
+ local libdir = path.join(errdata:trim(), "lib/windows")
+
+ local kind
+ if self:has_runtime("MD") then
+ kind = "dynamic"
+ elseif self:has_runtime("MT") then
+ kind = "static"
+ end
+
+ local driver = self:has_tool("ld", "lld-link", "link") and "" or "-Wl,"
+ local thunk = path.join(libdir, string.format("clang_rt.asan_%s_runtime_thunk-x86_64.lib", kind))
+ table.join2(ldflags, {
+ path.unix(path.join(libdir, "clang_rt.asan_dynamic-x86_64.lib")),
+ driver .. "/WHOLEARCHIVE:" .. path.unix(thunk),
+ driver .. "/INFERASANLIBS:NO",
+ })
+ end
+ end
+
+ configs.ldflags = ldflags
+ configs.shflags = ldflags
return configs
end