diff options
| author | ruki <[email protected]> | 2025-09-26 10:06:12 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-09-26 10:06:12 +0800 |
| commit | 7297d451335f489c614b4470cad7beb5890b8f34 (patch) | |
| tree | 1c70142f0ccec6bfa8bcd88b5bc4cd2e9dce4039 | |
| parent | 729a9e250b90201a9b580909e16f43e2bbc4c23c (diff) | |
| parent | adf16d85e19b8d9eb6bfce0feb4cd3509d7c95fb (diff) | |
Merge pull request #6859 from xmake-io/tools
improve to check target flags
| -rw-r--r-- | xmake/core/package/package.lua | 13 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 13 | ||||
| -rw-r--r-- | xmake/core/tool/builder.lua | 56 | ||||
| -rw-r--r-- | xmake/modules/private/check/checkers/api/api_checker.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/private/utils/target.lua | 37 |
5 files changed, 66 insertions, 55 deletions
diff --git a/xmake/core/package/package.lua b/xmake/core/package/package.lua index 3caa0ac2f..4b0ddd036 100644 --- a/xmake/core/package/package.lua +++ b/xmake/core/package/package.lua @@ -1408,14 +1408,13 @@ end -- ... -- end function _instance:has_tool(toolkind, ...) - local _, toolname = self:tool(toolkind) - if toolname then - for _, v in ipairs(table.join(...)) do - if v and toolname:find("^" .. v:gsub("%-", "%%-") .. "$") then - return true - end - end + local target_utils = package._target_utils + if target_utils == nil then + target_utils = sandbox_module.import("private.utils.target", {anonymous = true}) + package._target_utils = target_utils end + local _, toolname = self:tool(toolkind) + return target_utils.has_tool(toolname, table.pack(...)) end -- get the user private data diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 3b08223a6..3839eb01f 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -2678,14 +2678,13 @@ end -- ... -- end function _instance:has_tool(toolkind, ...) - local _, toolname = self:tool(toolkind) - if toolname then - for _, v in ipairs(table.pack(...)) do - if v and toolname:find("^" .. v:gsub("%-", "%%-") .. "$") then - return true - end - end + local target_utils = target._target_utils + if target_utils == nil then + target_utils = sandbox_module.import("private.utils.target", {anonymous = true}) + target._target_utils = target_utils end + local _, toolname = self:tool(toolkind) + return target_utils.has_tool(toolname, table.pack(...)) end -- has the given c funcs? diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua index cca77b7a9..bc7e995a4 100644 --- a/xmake/core/tool/builder.lua +++ b/xmake/core/tool/builder.lua @@ -22,19 +22,21 @@ local builder = builder or {} -- load modules -local io = require("base/io") -local path = require("base/path") -local utils = require("base/utils") -local table = require("base/table") -local string = require("base/string") -local option = require("base/option") -local hashset = require("base/hashset") -local graph = require("base/graph") -local tool = require("tool/tool") -local config = require("project/config") -local sandbox = require("sandbox/sandbox") -local language = require("language/language") -local platform = require("platform/platform") +local io = require("base/io") +local path = require("base/path") +local utils = require("base/utils") +local table = require("base/table") +local string = require("base/string") +local option = require("base/option") +local hashset = require("base/hashset") +local graph = require("base/graph") +local tool = require("tool/tool") +local config = require("project/config") +local sandbox = require("sandbox/sandbox") +local language = require("language/language") +local platform = require("platform/platform") +local sandbox_module = require("sandbox/modules/import/core/sandbox/module") +local target_utils = nil -- lazy import("private.utils.target") -- get the tool of builder function builder:_tool() @@ -157,32 +159,14 @@ end -- add flags from the flagkind function builder:_add_flags_from_flagkind(flags, target, flagkind, opt) + if target_utils == nil then + target_utils = sandbox_module.import("private.utils.target", {anonymous = true}) + end local targetflags = target:get(flagkind, opt) local extraconf = target:extraconf(flagkind) for _, flag in ipairs(table.wrap(targetflags)) do - -- does this flag belong to this tool? - -- @see https://github.com/xmake-io/xmake/issues/3022 - -- - -- e.g. - -- for all: add_cxxflags("-g") - -- only for clang: add_cxxflags("clang::-stdlib=libc++") - -- only for clang and multiple flags: add_cxxflags("-stdlib=libc++", "-DFOO", {tools = "clang"}) - -- - local for_this_tool = true - local flagconf = extraconf and extraconf[flag] - if type(flag) == "string" and flag:find("::", 1, true) then - for_this_tool = false - local splitinfo = flag:split("::", {plain = true}) - local toolname = splitinfo[1] - if toolname == self:name() then - flag = splitinfo[2] - for_this_tool = true - end - elseif flagconf and flagconf.tools then - for_this_tool = table.contains(table.wrap(flagconf.tools), self:name()) - end - - if for_this_tool then + flag = target_utils.flag_belong_to_tool(flag, self, extraconf) + if flag then if extraconf then -- @note we need join the single flag with shallow mode, aboid expand table values -- e.g. add_cflags({"-I", "/tmp/xxx foo"}, {force = true, expand = false}) diff --git a/xmake/modules/private/check/checkers/api/api_checker.lua b/xmake/modules/private/check/checkers/api/api_checker.lua index 811865745..805030421 100644 --- a/xmake/modules/private/check/checkers/api/api_checker.lua +++ b/xmake/modules/private/check/checkers/api/api_checker.lua @@ -143,7 +143,7 @@ end -- @see https://github.com/xmake-io/xmake/issues/3594 function check_flag(target, toolinst, flagkind, flag) local extraconf = target:extraconf(flagkind) - flag = target_utils.flag_belong_to_tool(target, flag, toolinst, extraconf) + flag = target_utils.flag_belong_to_tool(flag, toolinst, extraconf) if flag then extraconf = extraconf and extraconf[flag] if not extraconf or not extraconf.force then diff --git a/xmake/modules/private/utils/target.lua b/xmake/modules/private/utils/target.lua index a0cd8fa6a..ad6046b44 100644 --- a/xmake/modules/private/utils/target.lua +++ b/xmake/modules/private/utils/target.lua @@ -22,6 +22,28 @@ import("core.base.option") import("core.project.project") +-- Is this target has these tools? +function has_tool(toolname, tools) + if toolname then + -- We need compatibility with gcc/g++, clang/clang++ for c++ compiler/linker + -- @see https://github.com/xmake-io/xmake/issues/6852 + local trim_xx = false + if toolname == "clangxx" or toolname == "gxx" then + toolname = toolname:rtrim("xx") + trim_xx = true + end + for _, v in ipairs(tools) do + if trim_xx then + v = v:rtrim("xx") + end + if v and toolname:find("^" .. v:gsub("%-", "%%-") .. "$") then + return true + end + end + end + return false +end + -- does this flag belong to this tool? -- @see https://github.com/xmake-io/xmake/issues/3022 -- @@ -30,19 +52,26 @@ import("core.project.project") -- only for clang: add_cxxflags("clang::-stdlib=libc++") -- only for clang and multiple flags: add_cxxflags("-stdlib=libc++", "-DFOO", {tools = "clang"}) -- -function flag_belong_to_tool(target, flag, toolinst, extraconf) +function flag_belong_to_tool(flag, toolinst, extraconf) local for_this_tool = true local flagconf = extraconf and extraconf[flag] if type(flag) == "string" and flag:find("::", 1, true) then for_this_tool = false local splitinfo = flag:split("::", {plain = true}) local toolname = splitinfo[1] - if toolname == toolinst:name() then + local realname = toolinst:name() + -- We need compatibility with gcc/g++, clang/clang++ for c++ compiler/linker + -- @see https://github.com/xmake-io/xmake/issues/6852 + if realname == "clangxx" or realname == "gxx" then + toolname = toolname:rtrim("xx") + realname = realname:rtrim("xx") + end + if toolname == realname then flag = splitinfo[2] for_this_tool = true end elseif flagconf and flagconf.tools then - for_this_tool = table.contains(table.wrap(flagconf.tools), toolinst:name()) + for_this_tool = has_tool(toolinst:name(), flagconf.tools) end if for_this_tool then return flag @@ -79,7 +108,7 @@ function translate_flags_in_tool(target, flagkind, flags) -- local result = {} for _, flag in ipairs(flags) do - flag = flag_belong_to_tool(target, flag, toolinst, extraconf) + flag = flag_belong_to_tool(flag, toolinst, extraconf) if flag then table.insert(result, flag) end |
