summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-09-04 23:13:28 +0800
committerruki <[email protected]>2021-09-04 23:13:28 +0800
commitb5b490676355766b03ca366fe3900495cbf4a9fa (patch)
tree401685018930d48534805a4d0cb7a5db550d3551
parentb89f852330498a8968c54140d25345841c59136a (diff)
add new merge archive rules
-rw-r--r--tests/projects/other/merge_archive2/src/add.c4
-rw-r--r--tests/projects/other/merge_archive2/src/mul.c4
-rw-r--r--tests/projects/other/merge_archive2/src/sub.c4
-rw-r--r--tests/projects/other/merge_archive2/src/subdir/add.c4
-rw-r--r--tests/projects/other/merge_archive2/src/subdir/sub.c4
-rw-r--r--tests/projects/other/merge_archive2/test.lua6
-rw-r--r--tests/projects/other/merge_archive2/xmake.lua18
-rw-r--r--xmake/core/project/policy.lua2
-rw-r--r--xmake/modules/utils/archive/merge_staticlib.lua13
-rw-r--r--xmake/rules/utils/merge_archive/xmake.lua29
10 files changed, 79 insertions, 9 deletions
diff --git a/tests/projects/other/merge_archive2/src/add.c b/tests/projects/other/merge_archive2/src/add.c
new file mode 100644
index 000000000..be1e084fc
--- /dev/null
+++ b/tests/projects/other/merge_archive2/src/add.c
@@ -0,0 +1,4 @@
+int add(int a, int b)
+{
+ return a + b;
+}
diff --git a/tests/projects/other/merge_archive2/src/mul.c b/tests/projects/other/merge_archive2/src/mul.c
new file mode 100644
index 000000000..c4292f9aa
--- /dev/null
+++ b/tests/projects/other/merge_archive2/src/mul.c
@@ -0,0 +1,4 @@
+int mul(int a, int b)
+{
+ return a * b;
+}
diff --git a/tests/projects/other/merge_archive2/src/sub.c b/tests/projects/other/merge_archive2/src/sub.c
new file mode 100644
index 000000000..b151ec4bc
--- /dev/null
+++ b/tests/projects/other/merge_archive2/src/sub.c
@@ -0,0 +1,4 @@
+int sub(int a, int b)
+{
+ return a - b;
+}
diff --git a/tests/projects/other/merge_archive2/src/subdir/add.c b/tests/projects/other/merge_archive2/src/subdir/add.c
new file mode 100644
index 000000000..318ed98f8
--- /dev/null
+++ b/tests/projects/other/merge_archive2/src/subdir/add.c
@@ -0,0 +1,4 @@
+int subdir_add(int a, int b)
+{
+ return a + b;
+}
diff --git a/tests/projects/other/merge_archive2/src/subdir/sub.c b/tests/projects/other/merge_archive2/src/subdir/sub.c
new file mode 100644
index 000000000..68c4d13ae
--- /dev/null
+++ b/tests/projects/other/merge_archive2/src/subdir/sub.c
@@ -0,0 +1,4 @@
+int subdir_sub(int a, int b)
+{
+ return a - b;
+}
diff --git a/tests/projects/other/merge_archive2/test.lua b/tests/projects/other/merge_archive2/test.lua
new file mode 100644
index 000000000..b76241be2
--- /dev/null
+++ b/tests/projects/other/merge_archive2/test.lua
@@ -0,0 +1,6 @@
+-- main entry
+function main(t)
+
+ -- build project
+ t:build()
+end
diff --git a/tests/projects/other/merge_archive2/xmake.lua b/tests/projects/other/merge_archive2/xmake.lua
new file mode 100644
index 000000000..eb23dd859
--- /dev/null
+++ b/tests/projects/other/merge_archive2/xmake.lua
@@ -0,0 +1,18 @@
+add_rules("mode.debug", "mode.release")
+
+target("add")
+ set_kind("static")
+ add_files("src/add.c")
+ add_files("src/subdir/add.c")
+
+target("sub")
+ set_kind("static")
+ add_files("src/sub.c")
+ add_files("src/subdir/sub.c")
+
+target("mul")
+ set_kind("static")
+ add_deps("add", "sub")
+ add_files("src/mul.c")
+ set_policy("build.merge_archive", true)
+
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua
index c624aed96..418d4d566 100644
--- a/xmake/core/project/policy.lua
+++ b/xmake/core/project/policy.lua
@@ -43,6 +43,8 @@ function policy.policies()
["check.target_package_licenses"] = {description = "Enable check the compatibility of target and package licenses.", default = true, type = "boolean"},
-- we can compile the source files for each target in parallel
["build.across_targets_in_parallel"] = {description = "Enable compile the source files for each target in parallel.", default = true, type = "boolean"},
+ -- merge archive intead of linking for all dependent targets
+ ["build.merge_archive"] = {description = "Enable merge archive intead of linking for all dependent targets.", default = false, type = "boolean"},
-- we need enable longpaths when building target or installing package
["platform.longpaths"] = {description = "Enable long paths when building target or installing package on windows.", default = false, type = "boolean"},
-- lock required packages
diff --git a/xmake/modules/utils/archive/merge_staticlib.lua b/xmake/modules/utils/archive/merge_staticlib.lua
index db72a8d97..8675f6c52 100644
--- a/xmake/modules/utils/archive/merge_staticlib.lua
+++ b/xmake/modules/utils/archive/merge_staticlib.lua
@@ -22,12 +22,17 @@
import("core.base.option")
-- merge *.a archive libraries for ar
-function _merge_for_ar(program, outputfile, libraryfiles, opt)
+function _merge_for_ar(target, program, outputfile, libraryfiles, opt)
opt = opt or {}
+ if target:is_plat("macosx") then
+ os.vrunv("libtool", table.join("-static", "-o", outputfile, libraryfiles))
+ else
+ os.vrunv(program, table.join("crsT", outputfile, libraryfiles))
+ end
end
-- merge *.a archive libraries for msvc/lib.exe
-function _merge_for_msvclib(program, outputfile, libraryfiles, opt)
+function _merge_for_msvclib(target, program, outputfile, libraryfiles, opt)
opt = opt or {}
end
@@ -36,7 +41,7 @@ function main(target, outputfile, libraryfiles)
local program, toolname = target:tool("ar")
if program and toolname then
if toolname:find("ar") then
- _merge_for_ar(program, outputfile, libraryfiles)
+ _merge_for_ar(target, program, outputfile, libraryfiles)
elseif toolname == "link" and target:is_plat("windows") then
local msvc
for _, toolchain_inst in ipairs(target:toolchains()) do
@@ -45,7 +50,7 @@ function main(target, outputfile, libraryfiles)
break
end
end
- _merge_for_msvclib((program:gsub("link%.exe", "lib.exe")), outputfile, libraryfiles, {envs = msvc and msvc:runenvs()})
+ _merge_for_msvclib(target, (program:gsub("link%.exe", "lib.exe")), outputfile, libraryfiles, {envs = msvc and msvc:runenvs()})
else
raise("cannot merge (%s): unknown ar tool %s!", table.concat(libraryfiles, ", "), toolname)
end
diff --git a/xmake/rules/utils/merge_archive/xmake.lua b/xmake/rules/utils/merge_archive/xmake.lua
index 6d57f55bb..63c947d08 100644
--- a/xmake/rules/utils/merge_archive/xmake.lua
+++ b/xmake/rules/utils/merge_archive/xmake.lua
@@ -18,12 +18,31 @@
-- @file xmake.lua
--
--- define rule: utils.merge.archive
rule("utils.merge.archive")
-
- -- set extensions
set_extensions(".a", ".lib")
-
- -- on build file
on_build_files("merge_archive")
+ after_link(function (target, opt)
+ if target:policy("build.merge_archive") and target:is_static() then
+ import("utils.archive.merge_staticlib")
+ import("core.project.depend")
+ import("private.utils.progress")
+ local libraryfiles = {}
+ for _, dep in ipairs(target:orderdeps()) do
+ if dep:is_static() then
+ table.insert(libraryfiles, dep:targetfile())
+ end
+ end
+ if #libraryfiles > 0 then
+ table.insert(libraryfiles, target:targetfile())
+ end
+ depend.on_changed(function ()
+ progress.show(opt.progress, "${color.build.target}merging.$(mode) %s", path.filename(target:targetfile()))
+ if #libraryfiles > 0 then
+ local tmpfile = os.tmpfile() .. path.extension(target:targetfile())
+ merge_staticlib(target, tmpfile, libraryfiles)
+ os.mv(tmpfile, target:targetfile())
+ end
+ end, {dependfile = target:dependfile(target:targetfile() .. ".merge_archive"), files = libraryfiles})
+ end
+ end)