diff options
| author | ruki <[email protected]> | 2024-02-05 12:39:31 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-05 12:39:31 +0800 |
| commit | f98063ec295a77a0847dcae15b49c7370d8b7294 (patch) | |
| tree | abbf6ffffe12dc064d9611f345349ce82757c41c | |
| parent | e4e204c9f165215ee755d621386b9a1a36adf279 (diff) | |
| parent | ef7fc4639a82257fd1672e66c1792177b3d0212e (diff) | |
Merge pull request #4698 from xmake-io/depinherit
Improve dep inherits
| -rw-r--r-- | xmake/core/base/private/instance_deps.lua | 34 | ||||
| -rw-r--r-- | xmake/core/project/target.lua | 52 | ||||
| -rw-r--r-- | xmake/core/tool/builder.lua | 27 | ||||
| -rw-r--r-- | xmake/rules/utils/inherit_links/inherit_links.lua | 7 |
4 files changed, 68 insertions, 52 deletions
diff --git a/xmake/core/base/private/instance_deps.lua b/xmake/core/base/private/instance_deps.lua index 080e040fa..2f5c52a7c 100644 --- a/xmake/core/base/private/instance_deps.lua +++ b/xmake/core/base/private/instance_deps.lua @@ -38,7 +38,7 @@ local table = require("base/table") -- -- if they're targets, their links order is reverse(orderdeps), e.g. foo: a -> b -> c -> d -- -function instance_deps.load_deps(instance, instances, deps, orderdeps, depspath) +function instance_deps.load_deps(instance, instances, deps, orderdeps, depspath, walkdep) local plaindeps = table.wrap(instance:get("deps")) local total = #plaindeps for idx, _ in ipairs(plaindeps) do @@ -47,21 +47,27 @@ function instance_deps.load_deps(instance, instances, deps, orderdeps, depspath) local depname = plaindeps[total + 1 - idx] local depinst = instances[depname] if depinst then - local depspath_sub - if depspath then - for idx, name in ipairs(depspath) do - if name == depname then - local circular_deps = table.slice(depspath, idx) - table.insert(circular_deps, depname) - os.raise("circular dependency(%s) detected!", table.concat(circular_deps, ", ")) + local continue_walk = true + if walkdep then + continue_walk = walkdep(instance, depinst) + end + if continue_walk then + local depspath_sub + if depspath then + for idx, name in ipairs(depspath) do + if name == depname then + local circular_deps = table.slice(depspath, idx) + table.insert(circular_deps, depname) + os.raise("circular dependency(%s) detected!", table.concat(circular_deps, ", ")) + end end + depspath_sub = table.join(depspath, depname) + end + instance_deps.load_deps(depinst, instances, deps, orderdeps, depspath_sub, walkdep) + if not deps[depname] then + deps[depname] = depinst + table.insert(orderdeps, depinst) end - depspath_sub = table.join(depspath, depname) - end - instance_deps.load_deps(depinst, instances, deps, orderdeps, depspath_sub) - if not deps[depname] then - deps[depname] = depinst - table.insert(orderdeps, depinst) end end end diff --git a/xmake/core/project/target.lua b/xmake/core/project/target.lua index 8cc2ff824..b9266ca35 100644 --- a/xmake/core/project/target.lua +++ b/xmake/core/project/target.lua @@ -324,16 +324,23 @@ function _instance:_invalidate(name) elseif name == "deps" then self._DEPS = nil self._ORDERDEPS = nil + self._INHERITDEPS = nil end end -- build deps function _instance:_build_deps() if target._project() then - local instances = target._project().targets() - self._DEPS = self._DEPS or {} - self._ORDERDEPS = self._ORDERDEPS or {} + local instances = target._project().targets() + self._DEPS = self._DEPS or {} + self._ORDERDEPS = self._ORDERDEPS or {} + self._INHERITDEPS = self._INHERITDEPS or {} instance_deps.load_deps(self, instances, self._DEPS, self._ORDERDEPS, {self:name()}) + -- @see https://github.com/xmake-io/xmake/issues/4689 + instance_deps.load_deps(self, instances, {}, self._INHERITDEPS, {self:name()}, function (t, dep) + local depinherit = t:extraconf("deps", dep:name(), "inherit") + return depinherit == nil or depinherit + end) end end @@ -344,26 +351,23 @@ end -- get values from target deps with {interface|public = ...} function _instance:_get_from_deps(name, result_values, result_sources, opt) - local orderdeps = self:orderdeps() + local orderdeps = self:orderdeps({inherit = true}) local total = #orderdeps for idx, _ in ipairs(orderdeps) do local dep = orderdeps[total + 1 - idx] - local depinherit = self:extraconf("deps", dep:name(), "inherit") - if depinherit == nil or depinherit then - local values = dep:get(name, opt) - if values ~= nil then - table.insert(result_values, values) - table.insert(result_sources, "dep::" .. dep:name()) - end - local dep_values = {} - local dep_sources = {} - dep:_get_from_options(name, dep_values, dep_sources, opt) - dep:_get_from_packages(name, dep_values, dep_sources, opt) - for idx, values in ipairs(dep_values) do - local dep_source = dep_sources[idx] - table.insert(result_values, values) - table.insert(result_sources, "dep::" .. dep:name() .. "/" .. dep_source) - end + local values = dep:get(name, opt) + if values ~= nil then + table.insert(result_values, values) + table.insert(result_sources, "dep::" .. dep:name()) + end + local dep_values = {} + local dep_sources = {} + dep:_get_from_options(name, dep_values, dep_sources, opt) + dep:_get_from_packages(name, dep_values, dep_sources, opt) + for idx, values in ipairs(dep_values) do + local dep_source = dep_sources[idx] + table.insert(result_values, values) + table.insert(result_sources, "dep::" .. dep:name() .. "/" .. dep_source) end end end @@ -550,6 +554,9 @@ function _instance:clone() if self._ORDERDEPS then instance._ORDERDEPS = table.clone(self._ORDERDEPS) end + if self._INHERITDEPS then + instance._INHERITDEPS = table.clone(self._INHERITDEPS) + end if self._RULES then instance._RULES = table.clone(self._RULES) end @@ -1127,14 +1134,15 @@ function _instance:deps() end -- get target ordered deps -function _instance:orderdeps() +function _instance:orderdeps(opt) + opt = opt or {} if not self:_is_loaded() then os.raise("please call target:orderdeps() in after_load()!") end if self._DEPS == nil then self:_build_deps() end - return self._ORDERDEPS + return opt.inherit and self._INHERITDEPS or self._ORDERDEPS end -- get target rules diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua index 255b69ca1..4ef3e8576 100644 --- a/xmake/core/tool/builder.lua +++ b/xmake/core/tool/builder.lua @@ -145,15 +145,12 @@ end -- add_cflags("", {interface = true}) -- function builder:_inherit_flags_from_targetdeps(flags, target) - local orderdeps = target:orderdeps() + local orderdeps = target:orderdeps({inherit = true}) local total = #orderdeps for idx, _ in ipairs(orderdeps) do local dep = orderdeps[total + 1 - idx] - local depinherit = target:extraconf("deps", dep:name(), "inherit") - if depinherit == nil or depinherit then - for _, flagkind in ipairs(self:_flagkinds()) do - self:_add_flags_from_flagkind(flags, dep, flagkind, {interface = true}) - end + for _, flagkind in ipairs(self:_flagkinds()) do + self:_add_flags_from_flagkind(flags, dep, flagkind, {interface = true}) end end end @@ -213,18 +210,24 @@ end -- add flags from the target options function builder:_add_flags_from_targetopts(flags, target) - for _, opt in ipairs(target:orderopts()) do - for _, flagkind in ipairs(self:_flagkinds()) do - self:_add_flags_from_flagkind(flags, opt, flagkind) + for _, flagkind in ipairs(self:_flagkinds()) do + local result = target:get_from(flagkind, "option::*") + if result then + for _, values in ipairs(table.wrap(result)) do + table.join2(flags, self:_mapflags(values, flagkind, target)) + end end end end -- add flags from the target packages function builder:_add_flags_from_targetpkgs(flags, target) - for _, pkg in ipairs(target:orderpkgs()) do - for _, flagkind in ipairs(self:_flagkinds()) do - table.join2(flags, self:_mapflags(pkg:get(flagkind), flagkind, target)) + for _, flagkind in ipairs(self:_flagkinds()) do + local result = target:get_from(flagkind, "package::*") + if result then + for _, values in ipairs(table.wrap(result)) do + table.join2(flags, self:_mapflags(values, flagkind, target)) + end end end end diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua index 0c643fdd2..bc1472b8e 100644 --- a/xmake/rules/utils/inherit_links/inherit_links.lua +++ b/xmake/rules/utils/inherit_links/inherit_links.lua @@ -86,7 +86,7 @@ function main(target) -- if target:data("inherit.links.exportlinks") ~= false then if targetkind == "static" then - for _, name in ipairs({"rpathdirs", "frameworkdirs", "frameworks", "linkdirs", "links", "syslinks"}) do + for _, name in ipairs({"rpathdirs", "frameworkdirs", "frameworks", "linkdirs", "links", "syslinks", "ldflags", "shflags"}) do local values = _get_values_from_target(target, name) if values and #values > 0 then target:add(name, values, {public = true}) @@ -99,9 +99,8 @@ function main(target) -- export rpathdirs for all shared library if targetkind == "binary" then local targetdir = target:targetdir() - for _, dep in ipairs(target:orderdeps()) do - local depinherit = target:extraconf("deps", dep:name(), "inherit") - if dep:kind() == "shared" and (depinherit == nil or depinherit) then + for _, dep in ipairs(target:orderdeps({inherit = true})) do + if dep:kind() == "shared" then local rpathdir = "@loader_path" local subdir = path.relative(path.directory(dep:targetfile()), targetdir) if subdir and subdir ~= '.' then |
