diff options
| author | ruki <[email protected]> | 2020-04-04 19:19:59 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-04-04 19:19:59 +0800 |
| commit | 08acbfa0837ce1e9cd45d8d626ffbda996073007 (patch) | |
| tree | fe5f4c9e5b3637b4c92b53faeb3bc85ea4f80f0d /xmake/rules | |
| parent | 292ac7314365149af63264f6dc6a3e16cc25b8b0 (diff) | |
improve to inherit links
Diffstat (limited to 'xmake/rules')
| -rw-r--r-- | xmake/rules/asm/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/rules/c++/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/rules/dlang/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/rules/objc++/xmake.lua | 3 | ||||
| -rw-r--r-- | xmake/rules/utils/inherit_links/inherit_links.lua | 88 | ||||
| -rw-r--r-- | xmake/rules/utils/inherit_links/xmake.lua | 24 |
6 files changed, 124 insertions, 0 deletions
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") + |
