summaryrefslogtreecommitdiff
path: root/xmake
diff options
context:
space:
mode:
authorruki <[email protected]>2023-09-21 22:44:08 +0800
committerruki <[email protected]>2023-09-21 22:44:08 +0800
commitefee9f525906e48699c4bf0eb10cce74d4e7b468 (patch)
tree37892b254262d928d05c6f62a1a22aa3832a04c9 /xmake
parent8afc0e7ed5d14918fa4fafd450b23263f62ea147 (diff)
enable asan for objc
Diffstat (limited to 'xmake')
-rw-r--r--xmake/rules/c++/build_sanitizer/config.lua28
-rw-r--r--xmake/rules/c++/build_sanitizer/xmake.lua12
-rw-r--r--xmake/rules/mode/xmake.lua23
-rw-r--r--xmake/rules/objc++/xmake.lua4
4 files changed, 43 insertions, 24 deletions
diff --git a/xmake/rules/c++/build_sanitizer/config.lua b/xmake/rules/c++/build_sanitizer/config.lua
index 52732e6a4..38d548004 100644
--- a/xmake/rules/c++/build_sanitizer/config.lua
+++ b/xmake/rules/c++/build_sanitizer/config.lua
@@ -21,15 +21,23 @@
-- imports
import("core.tool.compiler")
import("core.project.project")
+import("lib.detect.find_tool")
+import("core.base.semver")
-- add build sanitizer
function _add_build_sanitizer(target, sourcekind, checkmode)
-- add cflags
local _, cc = target:tool(sourcekind)
- local cflag = sourcekind == "cxx" and "cxxflags" or "cflags"
- if target:has_tool(sourcekind, "cl", "clang", "clangxx", "gcc", "gxx") then
- target:add(cflag, "-fsanitize=" .. checkmode)
+ local flagnames = {
+ cc = "cflags",
+ cxx = "cxxflags",
+ mm = "mflags",
+ mxx = "mxflags"
+ }
+ local flagname = flagnames[sourcekind]
+ if flagname and target:has_tool(sourcekind, "cl", "clang", "clangxx", "gcc", "gxx") then
+ target:add(flagname, "-fsanitize=" .. checkmode)
end
-- add ldflags and shflags
@@ -40,8 +48,22 @@ function _add_build_sanitizer(target, sourcekind, checkmode)
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
+ 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))
+ end
+ end
end
end
diff --git a/xmake/rules/c++/build_sanitizer/xmake.lua b/xmake/rules/c++/build_sanitizer/xmake.lua
index cd96e9150..e7f8dbb62 100644
--- a/xmake/rules/c++/build_sanitizer/xmake.lua
+++ b/xmake/rules/c++/build_sanitizer/xmake.lua
@@ -30,3 +30,15 @@ rule("c++.build.sanitizer")
import("config")(target, "cxx")
end)
+-- define rule: objc.build.sanitizer
+rule("objc.build.sanitizer")
+ on_config(function (target)
+ import("config")(target, "mm")
+ end)
+
+-- define rule: objc++.build.sanitizer
+rule("objc++.build.sanitizer")
+ on_config(function (target)
+ import("config")(target, "mxx")
+ end)
+
diff --git a/xmake/rules/mode/xmake.lua b/xmake/rules/mode/xmake.lua
index e2bfc298b..66c5e1911 100644
--- a/xmake/rules/mode/xmake.lua
+++ b/xmake/rules/mode/xmake.lua
@@ -184,9 +184,9 @@ rule("mode.coverage")
-- define rule: asan mode
rule("mode.asan")
- on_config(function (target)
- import("lib.detect.find_tool")
- import("core.base.semver")
+
+ -- we use after_load because c++.build.sanitizer rule/on_config need it
+ after_load(function (target)
-- is asan mode now? xmake f -m asan
if is_mode("asan") then
@@ -206,22 +206,7 @@ rule("mode.asan")
end
-- enable asan checker
- target:add("cxflags", "-fsanitize=address")
- target:add("mxflags", "-fsanitize=address")
- target:add("ldflags", "-fsanitize=address")
- target:add("shflags", "-fsanitize=address")
-
- 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
+ target:set("policy", "build.sanitizer.address", true)
end
end)
diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua
index 9fbf392e8..c653261e7 100644
--- a/xmake/rules/objc++/xmake.lua
+++ b/xmake/rules/objc++/xmake.lua
@@ -21,7 +21,7 @@
-- define rule: objc.build
rule("objc.build")
set_sourcekinds("mm")
- add_deps("objc.build.pcheader", "c.build.optimization")
+ add_deps("objc.build.pcheader", "c.build.optimization", "objc.build.sanitizer")
after_load(function (target)
-- deprecated, we only need to use `add_mflags("-fno-objc-arc")` to override it
if target:values("objc.build.arc") == false then
@@ -36,7 +36,7 @@ rule("objc.build")
-- define rule: objc++.build
rule("objc++.build")
set_sourcekinds("mxx")
- add_deps("objc++.build.pcheader", "c++.build.optimization")
+ add_deps("objc++.build.pcheader", "c++.build.optimization", "objc++.build.sanitizer")
after_load(function (target)
-- deprecated, we only need to use `add_mxxflags("-fno-objc-arc")` to override it
if target:values("objc++.build.arc") == false then