diff options
| author | ruki <[email protected]> | 2023-09-21 22:46:24 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-09-21 22:46:24 +0800 |
| commit | a46eb50bda5710a6be361faff204c0f699545c5b (patch) | |
| tree | c1580749f19604f4648f020ece4945b4661d11d0 | |
| parent | b607622a00a3bd4d31a6a7feb2ca1b49dd1108cf (diff) | |
add tsan, lsan, msan and ubsan policy
| -rw-r--r-- | xmake/core/project/policy.lua | 8 | ||||
| -rw-r--r-- | xmake/rules/c++/build_sanitizer/config.lua | 40 | ||||
| -rw-r--r-- | xmake/rules/mode/xmake.lua | 42 |
3 files changed, 53 insertions, 37 deletions
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index c5d9be573..3ce75ca27 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -54,6 +54,14 @@ function policy.policies() ["build.optimization.lto"] = {description = "Enable LTO linker-time optimization for c/c++ building.", type = "boolean"}, -- Enable address sanitizer for c/c++ building. ["build.sanitizer.address"] = {description = "Enable address sanitizer for c/c++ building.", type = "boolean"}, + -- Enable thread sanitizer for c/c++ building. + ["build.sanitizer.thread"] = {description = "Enable thread sanitizer for c/c++ building.", type = "boolean"}, + -- Enable memort sanitizer for c/c++ building. + ["build.sanitizer.memory"] = {description = "Enable memory sanitizer for c/c++ building.", type = "boolean"}, + -- Enable leak sanitizer for c/c++ building. + ["build.sanitizer.leak"] = {description = "Enable leak sanitizer for c/c++ building.", type = "boolean"}, + -- Enable undefined sanitizer for c/c++ building. + ["build.sanitizer.undefined"] = {description = "Enable undefined sanitizer for c/c++ building.", type = "boolean"}, -- Enable C++ modules for C++ building, even if no .mpp is involved in the compilation ["build.c++.modules"] = {description = "Enable C++ modules for C++ building.", type = "boolean"}, -- Enable clang std modulemap diff --git a/xmake/rules/c++/build_sanitizer/config.lua b/xmake/rules/c++/build_sanitizer/config.lua index 4f974ef4b..7c8d4f0da 100644 --- a/xmake/rules/c++/build_sanitizer/config.lua +++ b/xmake/rules/c++/build_sanitizer/config.lua @@ -46,28 +46,36 @@ function _add_build_sanitizer(target, sourcekind, checkmode) target:add("shflags", "-fsanitize=" .. checkmode) end - -- enable the debug symbols - if not target:get("symbols") then - target:set("symbols", "debug") - end end function main(target, sourcekind) local sanitizer = false - if target:policy("build.sanitizer.address") or - project.policy("build.sanitizer.address") then - _add_build_sanitizer(target, sourcekind, "address") - sanitizer = true + for _, checkmode in ipairs({"address", "thread", "memory", "leak", "undefined"}) do + if target:policy("build.sanitizer." .. checkmode) or + project.policy("build.sanitizer." .. checkmode) then + _add_build_sanitizer(target, sourcekind, checkmode) + sanitizer = true + end end - if sanitizer and target:is_plat("windows") and target:is_binary() then - local msvc = target:toolchain("msvc") - if msvc then - local envs = msvc:runenvs() - local vscmd_ver = envs and envs.VSCMD_VER - if vscmd_ver and semver.match(vscmd_ver):ge("17.7") then - local cl = assert(find_tool("cl", {envs = envs}), "cl not found!") - target:add("runenvs", "PATH", path.directory(cl.program)) + if sanitizer then + + -- enable the debug symbols for sanitizer + if not target:get("symbols") then + target:set("symbols", "debug") + end + + -- we need to load runenvs for msvc + -- @see https://github.com/xmake-io/xmake/issues/4176 + if target:is_plat("windows") and target:is_binary() then + local msvc = target:toolchain("msvc") + if msvc then + local envs = msvc:runenvs() + local vscmd_ver = envs and envs.VSCMD_VER + if vscmd_ver and semver.match(vscmd_ver):ge("17.7") then + local cl = assert(find_tool("cl", {envs = envs}), "cl not found!") + target:add("runenvs", "PATH", path.directory(cl.program)) + end end end end diff --git a/xmake/rules/mode/xmake.lua b/xmake/rules/mode/xmake.lua index d9ba61e0d..6ac40c487 100644 --- a/xmake/rules/mode/xmake.lua +++ b/xmake/rules/mode/xmake.lua @@ -215,7 +215,7 @@ rule("mode.asan") -- define rule: tsan mode rule("mode.tsan") - on_config(function (target) + after_load(function (target) -- is tsan mode now? xmake f -m tsan if is_mode("tsan") then @@ -235,16 +235,16 @@ rule("mode.tsan") end -- enable tsan checker - target:add("cxflags", "-fsanitize=thread") - target:add("mxflags", "-fsanitize=thread") - target:add("ldflags", "-fsanitize=thread") - target:add("shflags", "-fsanitize=thread") + target:set("policy", "build.sanitizer.thread", true) + + -- we should use "build.sanitizer.thread" instead of it. + wprint("deprecated: please use set_policy(\"build.sanitizer.thread\", true) instead of \"mode.tsan\".") end end) -- define rule: msan mode rule("mode.msan") - on_config(function (target) + after_load(function (target) -- is msan mode now? xmake f -m msan if is_mode("msan") then @@ -264,16 +264,16 @@ rule("mode.msan") end -- enable msan checker - target:add("cxflags", "-fsanitize=memory") - target:add("mxflags", "-fsanitize=memory") - target:add("ldflags", "-fsanitize=memory") - target:add("shflags", "-fsanitize=memory") + target:set("policy", "build.sanitizer.memory", true) + + -- we should use "build.sanitizer.memory" instead of it. + wprint("deprecated: please use set_policy(\"build.sanitizer.memory\", true) instead of \"mode.msan\".") end end) -- define rule: lsan mode rule("mode.lsan") - on_config(function (target) + after_load(function (target) -- is lsan mode now? xmake f -m lsan if is_mode("lsan") then @@ -293,16 +293,16 @@ rule("mode.lsan") end -- enable lsan checker - target:add("cxflags", "-fsanitize=leak") - target:add("mxflags", "-fsanitize=leak") - target:add("ldflags", "-fsanitize=leak") - target:add("shflags", "-fsanitize=leak") + target:set("policy", "build.sanitizer.leak", true) + + -- we should use "build.sanitizer.leak" instead of it. + wprint("deprecated: please use set_policy(\"build.sanitizer.leak\", true) instead of \"mode.lsan\".") end end) -- define rule: ubsan mode rule("mode.ubsan") - on_config(function (target) + after_load(function (target) -- is ubsan mode now? xmake f -m ubsan if is_mode("ubsan") then @@ -321,11 +321,11 @@ rule("mode.ubsan") end end - -- enable tsan checker - target:add("cxflags", "-fsanitize=undefined") - target:add("mxflags", "-fsanitize=undefined") - target:add("ldflags", "-fsanitize=undefined") - target:add("shflags", "-fsanitize=undefined") + -- enable ubsan checker + target:set("policy", "build.sanitizer.undefined", true) + + -- we should use "build.sanitizer.undefined" instead of it. + wprint("deprecated: please use set_policy(\"build.sanitizer.undefined\", true) instead of \"mode.ubsan\".") end end) |
