summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2020-04-04 19:19:59 +0800
committerruki <[email protected]>2020-04-04 19:19:59 +0800
commit08acbfa0837ce1e9cd45d8d626ffbda996073007 (patch)
treefe5f4c9e5b3637b4c92b53faeb3bc85ea4f80f0d
parent292ac7314365149af63264f6dc6a3e16cc25b8b0 (diff)
improve to inherit links
-rw-r--r--xmake/core/tool/builder.lua88
-rw-r--r--xmake/rules/asm/xmake.lua3
-rw-r--r--xmake/rules/c++/xmake.lua3
-rw-r--r--xmake/rules/dlang/xmake.lua3
-rw-r--r--xmake/rules/objc++/xmake.lua3
-rw-r--r--xmake/rules/utils/inherit_links/inherit_links.lua88
-rw-r--r--xmake/rules/utils/inherit_links/xmake.lua24
7 files changed, 124 insertions, 88 deletions
diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua
index d0fdc5a43..c3d67470e 100644
--- a/xmake/core/tool/builder.lua
+++ b/xmake/core/tool/builder.lua
@@ -111,80 +111,6 @@ function builder:_flagkinds()
return self._FLAGKINDS
end
--- inherit links from target deps
-function builder:_inherit_links_from_targetdeps(results, target, flagname)
-
- -- for all target deps
- local orderdeps = target:orderdeps()
- local total = #orderdeps
- local deplinks = {}
- for idx, _ in ipairs(orderdeps) do
-
- -- reverse deps order for links
- local dep = orderdeps[total + 1 - idx]
-
- -- the dependent target is static or shared library? inherit it's links
- local depkind = dep:targetkind()
- local targetkind = target:targetkind()
- local depinherit = target:extraconf("deps", dep:name(), "inherit")
- if (depkind == "static" or depkind == "shared" or depkind == "object") and (depinherit == nil or depinherit) then
- if targetkind == "binary" or targetkind == "shared" then
- if flagname == "links" or flagname == "syslinks" then
-
- -- add dependent link
- if depkind ~= "object" and flagname == "links" then
- table.insert(results, dep:basename())
- end
-
- -- inherit links from the depdent target
- self:_add_values_from_target(deplinks, dep, flagname)
-
- elseif flagname == "linkdirs" then
-
- -- add dependent linkdirs
- if depkind ~= "object" then
- table.insert(results, path.directory(dep:targetfile()))
- end
-
- -- inherit linkdirs from the depdent target
- self:_add_values_from_target(results, dep, flagname)
-
- elseif flagname == "rpathdirs" then
-
- -- add dependent rpathdirs
- if depkind ~= "object" then
- local rpathdir = "@loader_path"
- local subdir = path.relative(path.directory(dep:targetfile()), path.directory(target:targetfile()))
- if subdir and subdir ~= '.' then
- rpathdir = path.join(rpathdir, subdir)
- end
- table.insert(results, rpathdir)
- end
- end
-
- -- TODO deprecated
- elseif flagname == "includedirs" then
-
- -- add dependent headerdir
- if dep:get("headers") and os.isdir(dep:headerdir()) then
- table.insert(results, dep:headerdir())
- end
-
- -- add dependent configheader directory
- local configheader = dep:configheader()
- if configheader and os.isfile(configheader) then
- table.insert(results, path.directory(configheader))
- end
- end
- end
- end
-
- -- @note we need ensure add option and package links after all dependent targets
- if #deplinks > 0 then
- table.join2(results, deplinks)
- end
-end
-
-- inherit flags (only for public/interface) from target deps
--
-- e.g.
@@ -223,15 +149,6 @@ function builder:_inherit_values_from_targetdeps(values, target, name)
end
end
--- add values from target
-function builder:_add_values_from_target(values, target, name)
- table.join2(values, target:get(name))
- if target:type() == "target" then
- self:_add_values_from_targetopts(values, target, name)
- self:_add_values_from_targetpkgs(values, target, name)
- end
-end
-
-- add values from target options
function builder:_add_values_from_targetopts(values, target, name)
for _, opt in ipairs(target:orderopts()) do
@@ -395,11 +312,6 @@ function builder:_add_flags_from_language(flags, target, getters)
local results = {}
if target:type() == "target" then
- -- link? add includes and links of all dependent targets first
- if name == "links" or name == "syslinks" or name == "linkdirs" or name == "rpathdirs" or name == "includedirs" then
- self:_inherit_links_from_targetdeps(results, target, name)
- end
-
-- inherit flagvalues (public or interface) of all dependent targets
self:_inherit_values_from_targetdeps(results, target, name)
diff --git a/xmake/rules/asm/xmake.lua b/xmake/rules/asm/xmake.lua
index 33823b412..3a75fca39 100644
--- a/xmake/rules/asm/xmake.lua
+++ b/xmake/rules/asm/xmake.lua
@@ -29,6 +29,9 @@ rule("asm")
-- add build rules
add_deps("asm.build")
+ -- inherit links and linkdirs of all dependent targets by default
+ add_deps("utils.inherit.links")
+
-- support `add_files("src/*.o")` and `add_files("src/*.a")` to merge object and archive files to target
add_deps("utils.merge.object", "utils.merge.archive")
diff --git a/xmake/rules/c++/xmake.lua b/xmake/rules/c++/xmake.lua
index 1e9af0266..869d50b60 100644
--- a/xmake/rules/c++/xmake.lua
+++ b/xmake/rules/c++/xmake.lua
@@ -48,6 +48,9 @@ rule("c++")
-- add build rules
add_deps("c++.build", "c.build")
+ -- inherit links and linkdirs of all dependent targets by default
+ add_deps("utils.inherit.links")
+
-- support `add_files("src/*.o")` and `add_files("src/*.a")` to merge object and archive files to target
add_deps("utils.merge.object", "utils.merge.archive")
diff --git a/xmake/rules/dlang/xmake.lua b/xmake/rules/dlang/xmake.lua
index e28fbb2d5..bda381c0a 100644
--- a/xmake/rules/dlang/xmake.lua
+++ b/xmake/rules/dlang/xmake.lua
@@ -29,6 +29,9 @@ rule("dlang")
-- add build rules
add_deps("dlang.build")
+ -- inherit links and linkdirs of all dependent targets by default
+ add_deps("utils.inherit.links")
+
-- support `add_files("src/*.o")` and `add_files("src/*.a")` to merge object and archive files to target
add_deps("utils.merge.object", "utils.merge.archive")
diff --git a/xmake/rules/objc++/xmake.lua b/xmake/rules/objc++/xmake.lua
index 61cb590bc..e54aa869c 100644
--- a/xmake/rules/objc++/xmake.lua
+++ b/xmake/rules/objc++/xmake.lua
@@ -56,6 +56,9 @@ rule("objc++")
-- add build rules
add_deps("objc++.build", "objc.build")
+ -- inherit links and linkdirs of all dependent targets by default
+ add_deps("utils.inherit.links")
+
-- support `add_files("src/*.o")` and `add_files("src/*.a")` to merge object and archive files to target
add_deps("utils.merge.object", "utils.merge.archive")
diff --git a/xmake/rules/utils/inherit_links/inherit_links.lua b/xmake/rules/utils/inherit_links/inherit_links.lua
new file mode 100644
index 000000000..30106e397
--- /dev/null
+++ b/xmake/rules/utils/inherit_links/inherit_links.lua
@@ -0,0 +1,88 @@
+--!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-2020, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file inherit_links.lua
+--
+
+-- add values from target options
+function _add_values_from_targetopts(values, target, name)
+ for _, opt in ipairs(target:orderopts()) do
+ table.join2(values, table.wrap(opt:get(name)))
+ end
+end
+
+-- add values from target packages
+function _add_values_from_targetpkgs(values, target, name)
+ for _, pkg in ipairs(target:orderpkgs()) do
+ -- uses them instead of the builtin configs if exists extra package config
+ -- e.g. `add_packages("xxx", {links = "xxx"})`
+ local configinfo = target:pkgconfig(pkg:name())
+ if configinfo and configinfo[name] then
+ table.join2(values, configinfo[name])
+ else
+ -- uses the builtin package configs
+ table.join2(values, pkg:get(name))
+ end
+ end
+end
+
+-- get values from target
+function _get_values_from_target(target, name)
+ local values = table.wrap(target:get(name))
+ _add_values_from_targetopts(values, target, name)
+ _add_values_from_targetpkgs(values, target, name)
+ return values
+end
+
+-- main entry
+function main(target)
+
+ -- disable inherit.links for `add_deps()`?
+ if target:data("inherit.links") == false then
+ return
+ end
+
+ -- export links and linkdirs
+ local targetkind = target:targetkind()
+ local targetfile = target:targetfile()
+ if targetkind == "shared" or targetkind == "static" then
+ target:add("links", target:basename(), {interface = true})
+ target:add("linkdirs", path.directory(targetfile), {interface = true})
+ end
+
+ -- export all dependent links and linkdirs for static library
+ if targetkind == "static" then
+ for _, name in ipairs({"frameworkdirs", "frameworks", "linkdirs", "links", "syslinks"}) do
+ local values = _get_values_from_target(target, name)
+ if values and #values > 0 then
+ target:add(name, unpack(values), {interface = true})
+ end
+ end
+ end
+
+ -- export rpathdirs for all shared library
+ if targetkind == "binary" then
+ for _, dep in ipairs(target:orderdeps()) do
+ local rpathdir = "@loader_path"
+ local subdir = path.relative(path.directory(dep:targetfile()), path.directory(targetfile))
+ if subdir and subdir ~= '.' then
+ rpathdir = path.join(rpathdir, subdir)
+ end
+ target:add("rpathdirs", rpathdir)
+ end
+ end
+end
diff --git a/xmake/rules/utils/inherit_links/xmake.lua b/xmake/rules/utils/inherit_links/xmake.lua
new file mode 100644
index 000000000..3eb29f692
--- /dev/null
+++ b/xmake/rules/utils/inherit_links/xmake.lua
@@ -0,0 +1,24 @@
+--!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-2020, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file xmake.lua
+--
+
+-- define rule: utils.inherit.links
+rule("utils.inherit.links")
+ after_load("inherit_links")
+