summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2023-03-01 00:42:31 +0800
committerruki <[email protected]>2023-03-05 21:59:26 +0800
commit85dc110f9126b4662d56c2d96c9627532056a3ca (patch)
treedfe100106e8b900df298c2867303df6437af600a
parent70f1a5ff1850f8138759500295702eb2541c227b (diff)
improve has_flags
-rw-r--r--xmake/core/tool/tool.lua70
-rw-r--r--xmake/modules/core/tools/gcc.lua2
2 files changed, 51 insertions, 21 deletions
diff --git a/xmake/core/tool/tool.lua b/xmake/core/tool/tool.lua
index 9d94bc375..7dc305980 100644
--- a/xmake/core/tool/tool.lua
+++ b/xmake/core/tool/tool.lua
@@ -32,6 +32,7 @@ local config = require("project/config")
local sandbox = require("sandbox/sandbox")
local toolchain = require("tool/toolchain")
local platform = require("platform/platform")
+local language = require("language/language")
local import = require("sandbox/modules/import")
-- new an instance
@@ -70,20 +71,6 @@ function _instance.new(kind, name, program, plat, arch, toolchain_inst)
return instance
end
--- load tool
-function _instance:_load()
- if not self._LOADED then
- if self.load then
- local ok, errors = sandbox.load(self.load, self)
- if not ok then
- return false, errors
- end
- end
- self._LOADED = true
- end
- return true
-end
-
-- get the tool name
function _instance:name()
return self._NAME
@@ -169,12 +156,7 @@ function _instance:has_flags(flags, flagkind, opt)
opt.program = opt.program or self:program()
opt.toolkind = opt.toolkind or self:kind()
opt.flagkind = opt.flagkind or flagkind
-
- -- get system flags
- opt.sysflags = opt.sysflags or self:get(self:kind() .. 'flags')
- if not opt.sysflags and opt.flagkind then
- opt.sysflags = self:get(opt.flagkind)
- end
+ opt.sysflags = opt.sysflags or self:_sysflags(opt.toolkind, opt.flagkind)
-- import has_flags()
self._has_flags = self._has_flags or import("lib.detect.has_flags", {anonymous = true})
@@ -186,6 +168,54 @@ function _instance:has_flags(flags, flagkind, opt)
return self._has_flags(self:name(), flags, opt)
end
+-- load tool
+function _instance:_load()
+ if not self._LOADED then
+ if self.load then
+ local ok, errors = sandbox.load(self.load, self)
+ if not ok then
+ return false, errors
+ end
+ end
+ self._LOADED = true
+ end
+ return true
+end
+
+-- get system flags from toolchains
+-- @see https://github.com/xmake-io/xmake/issues/3429
+function _instance:_sysflags(toolkind, flagkind)
+ local sysflags = {}
+ local sourceflags = language.sourceflags()[toolkind]
+ if not sourceflags and flagkind then
+ sourceflags = {flagkind}
+ if flagkind == "cflags" or flagkind == "cxxflags" then
+ table.insert(sourceflags, "cxflags")
+ elseif flagkind == "cxflags" then
+ table.insert(sourceflags, "cxxflags")
+ elseif flagkind == "mflags" or flagkind == "mxxflags" then
+ table.insert(sourceflags, "mxflags")
+ elseif flagkind == "mxflags" then
+ table.insert(sourceflags, "mxxflags")
+ end
+ end
+ if sourceflags then
+ for _, flagname in ipairs(sourceflags) do
+ local flags = self:get(flagname)
+ if flags then
+ table.join2(sysflags, flags)
+ end
+ end
+ end
+ -- maybe it's linker flags, ld -> ldflags, dcld -> dcldflags
+ if #sysflags == 0 then
+ table.join2(sysflags, self:get(toolkind .. "flags"))
+ end
+ if #sysflags > 0 then
+ return sysflags
+ end
+end
+
-- load the given tool from the given kind
--
-- @param kind the tool kind e.g. cc, cxx, mm, mxx, as, ar, ld, sh, ..
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua
index 0d33decb4..f6dd6f55b 100644
--- a/xmake/modules/core/tools/gcc.lua
+++ b/xmake/modules/core/tools/gcc.lua
@@ -74,7 +74,7 @@ function load(self)
-- we need check it for clang/gcc with window target
-- @see https://github.com/xmake-io/xmake/issues/1392
--
- if not self:is_plat("windows", "mingw") and self:has_flags("-fPIC", "cxflags") then
+ if not self:is_plat("windows", "mingw") and self:has_flags("-fPIC") then
self:add("shflags", "-fPIC")
self:add("shared.cxflags", "-fPIC")
end