diff options
| author | star9029 <[email protected]> | 2025-12-22 11:42:39 +0800 |
|---|---|---|
| committer | star9029 <[email protected]> | 2025-12-22 11:42:39 +0800 |
| commit | a9e51834621aedeb6e8aa5aec0880af28ac4200d (patch) | |
| tree | 2d558cc78ce31cc0a6132684010d0b10a8c6b8e3 | |
| parent | 3c6c162da56f3fe7b9b99dd3f1194e0a545c68f7 (diff) | |
clean code to toolchain_utils
| -rw-r--r-- | xmake/core/package/package.lua | 39 | ||||
| -rw-r--r-- | xmake/modules/private/utils/toolchain.lua | 40 | ||||
| -rw-r--r-- | xmake/rules/c++/config/sanitizer.lua | 40 | ||||
| -rw-r--r-- | xmake/toolchains/clang-cl/load.lua | 3 | ||||
| -rw-r--r-- | xmake/toolchains/clang/load.lua | 2 |
5 files changed, 51 insertions, 73 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index b658b09f6..bdd09ed5a 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -2457,8 +2457,6 @@ end -- generate sanitizer configs function _instance:_generate_sanitizer_configs(checkmode, sourcekind) - - -- add cflags local configs = {} if sourcekind and self:has_tool(sourcekind, "cl", "clang", "clangxx", "clang_cl", "gcc", "gxx") then local cflag = sourcekind == "cxx" and "cxxflags" or "cflags" @@ -2466,45 +2464,20 @@ function _instance:_generate_sanitizer_configs(checkmode, sourcekind) 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 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 + local toolchain_utils = sandbox_module.import("private.utils.toolchain", {anonymous = true}) + table.join2(ldflags, toolchain_utils.add_llvm_asan_flags(self)) end - configs.ldflags = ldflags - configs.shflags = ldflags + if #ldflags ~= 0 then + configs.ldflags = ldflags + configs.shflags = ldflags + end return configs end diff --git a/xmake/modules/private/utils/toolchain.lua b/xmake/modules/private/utils/toolchain.lua index c0cec7f74..864d2d76e 100644 --- a/xmake/modules/private/utils/toolchain.lua +++ b/xmake/modules/private/utils/toolchain.lua @@ -467,7 +467,9 @@ function add_llvm_runenvs(toolchain) for _, dir in ipairs({dirs.libdir or false, dirs.cxxlibdir or false, dirs.rtlibdir or false}) do if dir then if toolchain:is_plat("windows") or is_host("windows") then - toolchain:add("runenvs", "PATH", dir) + -- clang asan path must be first + local envs = toolchain:get("runenvs")["PATH"] + table.insert(envs, 1 , dir) elseif toolchain:is_plat("linux", "bsd") then toolchain:add("runenvs", "LD_LIBRARY_PATH", dir) elseif toolchain:is_plat("macosx") then @@ -481,3 +483,39 @@ function add_llvm_runenvs(toolchain) end end end + +-- add address sanitizer flags for llvm +function add_llvm_asan_flags(target) + assert(target:has_runtime("MD", "MT"), "clang asan only support MD/MT runtime on windows") + + local ldflags = {} + if target:has_tool("cxx", "clang", "clangxx") then + if target:has_runtime("MT") then + table.insert(ldflags, "-D_MT") + elseif target:has_runtime("MD") then + table.join2(ldflags, {"-D_MT", "-D_DLL"}) + end + elseif target: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 toolchain = target:toolchain("clang-cl") or target:toolchain("clang") + local libdir = assert(get_llvm_dirs(toolchain).rtlibdir, "clang resource directory not found") + + local kind + if target:has_runtime("MD") then + kind = "dynamic" + elseif target:has_runtime("MT") then + kind = "static" + end + + local driver = target: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 + + return ldflags +end diff --git a/xmake/rules/c++/config/sanitizer.lua b/xmake/rules/c++/config/sanitizer.lua index 1862d3e9b..646197c0c 100644 --- a/xmake/rules/c++/config/sanitizer.lua +++ b/xmake/rules/c++/config/sanitizer.lua @@ -24,11 +24,6 @@ import("lib.detect.find_tool") import("core.base.semver") import("private.utils.toolchain", {alias = "toolchain_utils"}) -function _get_clang_asan_library_dir(target) - local toolchain = target:toolchain("clang-cl") or target:toolchain("clang") - return path.join(toolchain_utils.get_llvm_resourcedir(toolchain), "lib/windows") -end - -- add build sanitizer function _add_build_sanitizer(target, sourcekind, checkmode) -- add cflags @@ -52,36 +47,11 @@ function _add_build_sanitizer(target, sourcekind, checkmode) end if target:is_plat("windows") and checkmode == "address" and not target:has_tool("cxx", "cl") then - assert(target:has_runtime("MD", "MT"), "clang asan only support MD/MT runtime on windows") - - local ldflags = {} - if target:has_tool("ld", "clang", "clangxx") then - if target:has_runtime("MT") then - table.insert(ldflags, "-D_MT") - elseif target:has_runtime("MD") then - table.join2(ldflags, {"-D_MT", "-D_DLL"}) - end - else - -- cmake unsupported use clang++ as linker with clang-cl compiler, so we keep using lld-link/link as linker - -- @see https://gitlab.kitware.com/cmake/cmake/-/issues/26430 - local kind - if target:has_runtime("MD") then - kind = "dynamic" - elseif target:has_runtime("MT") then - kind = "static" - end - - local libdir = _get_clang_asan_library_dir(target) - local driver = target: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", - }) + local ldflags = toolchain_utils.add_llvm_asan_flags(target) + if #ldflags ~= 0 then + target:add("ldflags", ldflags, {force = true}) + target:add("shflags", ldflags, {force = true}) end - target:add("ldflags", ldflags, {force = true}) - target:add("shflags", ldflags, {force = true}) end end @@ -117,8 +87,6 @@ function main(target, sourcekind) target:add("runenvs", "PATH", path.directory(cl.program)) end end - else - target:add("runenvs", "PATH", _get_clang_asan_library_dir(target)) end end end diff --git a/xmake/toolchains/clang-cl/load.lua b/xmake/toolchains/clang-cl/load.lua index fb95724b9..908f31be9 100644 --- a/xmake/toolchains/clang-cl/load.lua +++ b/xmake/toolchains/clang-cl/load.lua @@ -47,9 +47,10 @@ function main(toolchain) toolchain:set("toolset", "ar", "link.exe") end - -- add vs environments toolchain_utils.add_vsenvs(toolchain) + toolchain_utils.add_llvm_runenvs(toolchain) + local flags = toolchain_utils.get_clang_target_flags(toolchain) if flags then toolchain:add("cxflags", flags) diff --git a/xmake/toolchains/clang/load.lua b/xmake/toolchains/clang/load.lua index 4b66ef5d6..80123d5c5 100644 --- a/xmake/toolchains/clang/load.lua +++ b/xmake/toolchains/clang/load.lua @@ -57,9 +57,7 @@ function main(toolchain, suffix) _load_windows(toolchain, suffix) end - -- set llvm runtimes toolchain_utils.set_llvm_runtimes(toolchain) - -- add llvm runenvs toolchain_utils.add_llvm_runenvs(toolchain) end |
