diff options
| author | ruki <[email protected]> | 2023-09-21 22:38:18 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-09-21 22:38:18 +0800 |
| commit | 3d9cf6af8a3fae36dbb5e8fe055a54000ddcadfb (patch) | |
| tree | 6ecc187316c099057cdad9cdeeaa8d93ef73913e | |
| parent | 5c619b17b5847b18d284323830dedaeff9d5d6ed (diff) | |
add asan support for package and policy
| -rw-r--r-- | xmake/core/package/package.lua | 27 | ||||
| -rw-r--r-- | xmake/core/project/policy.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/private/action/require/impl/package.lua | 7 | ||||
| -rw-r--r-- | xmake/rules/c++/build_sanitizer/config.lua | 47 | ||||
| -rw-r--r-- | xmake/rules/c++/build_sanitizer/xmake.lua | 32 | ||||
| -rw-r--r-- | xmake/rules/c++/xmake.lua | 4 |
6 files changed, 116 insertions, 3 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 074e35b29..10d32a0ca 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -2111,6 +2111,24 @@ function _instance:_generate_lto_configs(sourcekind) return configs end +-- generate sanitizer configs +function _instance:_generate_sanitizer_configs(sourcekind, checkmode) + + -- add cflags + local configs = {} + if self:has_tool(sourcekind, "cl", "clang", "clangxx", "gcc", "gxx") then + local cflag = sourcekind == "cxx" and "cxxflags" or "cflags" + configs[cflag] = "-fsanitize=" .. checkmode + end + + -- add ldflags and shflags + if self:has_tool("ld", "link", "clang", "clangxx", "gcc", "gxx") then + configs.ldflags = "-fsanitize=" .. checkmode + configs.shflags = "-fsanitize=" .. checkmode + end + return configs +end + -- generate building configs for has_xxx/check_xxx function _instance:_generate_build_configs(configs, opt) opt = opt or {} @@ -2146,6 +2164,15 @@ function _instance:_generate_build_configs(configs, opt) end end end + if self:config("asan") then + local configs_asan = self:_generate_sanitizer_configs(opt.sourcekind or "cxx", "address") + if configs_asan then + for k, v in pairs(configs_asan) do + configs[k] = table.wrap(configs[k] or {}) + table.join2(configs[k], v) + end + end + end -- enable exceptions for msvc by default if opt.sourcekind == "cxx" and configs.exceptions == nil and self:has_tool("cxx", "cl") then configs.exceptions = "cxx" diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 96ed1adaf..c5d9be573 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -52,6 +52,8 @@ function policy.policies() ["build.warning"] = {description = "Enable build warning output.", type = "boolean"}, -- Enable LTO linker-time optimization for c/c++ building. ["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 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/modules/private/action/require/impl/package.lua b/xmake/modules/private/action/require/impl/package.lua index ea7d8d621..a7377d41b 100644 --- a/xmake/modules/private/action/require/impl/package.lua +++ b/xmake/modules/private/action/require/impl/package.lua @@ -328,6 +328,9 @@ function _add_package_configurations(package) if package:extraconf("configs", "lto", "default") == nil then package:add("configs", "lto", {builtin = true, description = "Enable the link-time build optimization.", type = "boolean"}) end + if package:extraconf("configs", "asan", "default") == nil then + package:add("configs", "asan", {builtin = true, description = "Enable the address sanitizer.", type = "boolean"}) + end if package:extraconf("configs", "vs_runtime", "default") == nil then package:add("configs", "vs_runtime", {builtin = true, description = "Set vs compiler runtime.", values = {"MT", "MTd", "MD", "MDd"}}) end @@ -500,11 +503,12 @@ function _init_requireinfo(requireinfo, package, opt) requireinfo.configs.vs_runtime = requireinfo.configs.vs_runtime or get_config("vs_runtime") end requireinfo.configs.lto = requireinfo.configs.lto or project.policy("build.optimization.lto") + requireinfo.configs.asan = requireinfo.configs.asan or project.policy("build.sanitizer.address") end -- but we will ignore some configs for buildhash in the headeronly and host/binary package -- @note on_test still need these configs, @see https://github.com/xmake-io/xmake/issues/4124 if package:is_headeronly() or (package:is_binary() and not package:is_cross()) then - requireinfo.ignored_configs_for_buildhash = {"vs_runtime", "toolchains", "lto", "pic"} + requireinfo.ignored_configs_for_buildhash = {"vs_runtime", "toolchains", "lto", "asan", "pic"} end end @@ -652,6 +656,7 @@ function _inherit_parent_configs(requireinfo, package, parentinfo) requireinfo_configs.toolchains = requireinfo_configs.toolchains or parentinfo_configs.toolchains requireinfo_configs.vs_runtime = requireinfo_configs.vs_runtime or parentinfo_configs.vs_runtime requireinfo_configs.lto = requireinfo_configs.lto or parentinfo_configs.lto + requireinfo_configs.asan = requireinfo_configs.asan or parentinfo_configs.asan requireinfo.configs = requireinfo_configs end end diff --git a/xmake/rules/c++/build_sanitizer/config.lua b/xmake/rules/c++/build_sanitizer/config.lua new file mode 100644 index 000000000..52732e6a4 --- /dev/null +++ b/xmake/rules/c++/build_sanitizer/config.lua @@ -0,0 +1,47 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file config.lua +-- + +-- imports +import("core.tool.compiler") +import("core.project.project") + +-- 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) + end + + -- add ldflags and shflags + if target:has_tool("ld", "link", "clang", "clangxx", "gcc", "gxx") then + target:add("ldflags", "-fsanitize=" .. checkmode) + target:add("shflags", "-fsanitize=" .. checkmode) + end +end + +function main(target, sourcekind) + if target:policy("build.sanitizer.address") or + project.policy("build.sanitizer.address") then + _add_build_sanitizer(target, sourcekind, "address") + end +end diff --git a/xmake/rules/c++/build_sanitizer/xmake.lua b/xmake/rules/c++/build_sanitizer/xmake.lua new file mode 100644 index 000000000..cd96e9150 --- /dev/null +++ b/xmake/rules/c++/build_sanitizer/xmake.lua @@ -0,0 +1,32 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file xmake.lua +-- + +-- define rule: c.build.sanitizer +rule("c.build.sanitizer") + on_config(function (target) + import("config")(target, "cc") + end) + +-- define rule: c++.build.sanitizer +rule("c++.build.sanitizer") + on_config(function (target) + import("config")(target, "cxx") + end) + diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua index 19c943e77..e130df73b 100644 --- a/xmake/rules/c++/xmake.lua +++ b/xmake/rules/c++/xmake.lua @@ -20,12 +20,12 @@ rule("c.build") set_sourcekinds("cc") - add_deps("c.build.pcheader", "c.build.optimization") + add_deps("c.build.pcheader", "c.build.optimization", "c.build.sanitizer") on_build_files("private.action.build.object", {batch = true, distcc = true}) rule("c++.build") set_sourcekinds("cxx") - add_deps("c++.build.pcheader", "c++.build.modules", "c++.build.optimization") + add_deps("c++.build.pcheader", "c++.build.modules", "c++.build.optimization", "c++.build.sanitizer") on_build_files("private.action.build.object", {batch = true, distcc = true}) on_config(function (target) -- we enable c++ exceptions by default |
