From b89f852330498a8968c54140d25345841c59136a Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 4 Sep 2021 11:11:34 +0800 Subject: merge_staticlib stub --- xmake/modules/utils/archive/merge_staticlib.lua | 56 +++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 xmake/modules/utils/archive/merge_staticlib.lua diff --git a/xmake/modules/utils/archive/merge_staticlib.lua b/xmake/modules/utils/archive/merge_staticlib.lua new file mode 100644 index 000000000..db72a8d97 --- /dev/null +++ b/xmake/modules/utils/archive/merge_staticlib.lua @@ -0,0 +1,56 @@ +--!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-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file merge_staticlib.lua +-- + +-- imports +import("core.base.option") + +-- merge *.a archive libraries for ar +function _merge_for_ar(program, outputfile, libraryfiles, opt) + opt = opt or {} +end + +-- merge *.a archive libraries for msvc/lib.exe +function _merge_for_msvclib(program, outputfile, libraryfiles, opt) + opt = opt or {} +end + +-- merge *.a archive libraries +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) + elseif toolname == "link" and target:is_plat("windows") then + local msvc + for _, toolchain_inst in ipairs(target:toolchains()) do + if toolchain_inst:name() == "msvc" then + msvc = toolchain_inst + break + end + end + _merge_for_msvclib((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 + else + raise("cannot merge (%s): ar not found!", table.concat(libraryfiles, ", ")) + end +end + -- cgit v1.3.1 From b5b490676355766b03ca366fe3900495cbf4a9fa Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 4 Sep 2021 23:13:28 +0800 Subject: add new merge archive rules --- tests/projects/other/merge_archive2/src/add.c | 4 +++ tests/projects/other/merge_archive2/src/mul.c | 4 +++ tests/projects/other/merge_archive2/src/sub.c | 4 +++ .../projects/other/merge_archive2/src/subdir/add.c | 4 +++ .../projects/other/merge_archive2/src/subdir/sub.c | 4 +++ tests/projects/other/merge_archive2/test.lua | 6 +++++ tests/projects/other/merge_archive2/xmake.lua | 18 ++++++++++++++ xmake/core/project/policy.lua | 2 ++ xmake/modules/utils/archive/merge_staticlib.lua | 13 +++++++--- xmake/rules/utils/merge_archive/xmake.lua | 29 ++++++++++++++++++---- 10 files changed, 79 insertions(+), 9 deletions(-) create mode 100644 tests/projects/other/merge_archive2/src/add.c create mode 100644 tests/projects/other/merge_archive2/src/mul.c create mode 100644 tests/projects/other/merge_archive2/src/sub.c create mode 100644 tests/projects/other/merge_archive2/src/subdir/add.c create mode 100644 tests/projects/other/merge_archive2/src/subdir/sub.c create mode 100644 tests/projects/other/merge_archive2/test.lua create mode 100644 tests/projects/other/merge_archive2/xmake.lua 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) -- cgit v1.3.1 From feefe1dce66ea47e4a968887fbcb81cb0f9caf33 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 4 Sep 2021 23:16:14 +0800 Subject: improve mergelib --- xmake/modules/utils/archive/merge_staticlib.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/modules/utils/archive/merge_staticlib.lua b/xmake/modules/utils/archive/merge_staticlib.lua index 8675f6c52..b5adbd98f 100644 --- a/xmake/modules/utils/archive/merge_staticlib.lua +++ b/xmake/modules/utils/archive/merge_staticlib.lua @@ -24,7 +24,7 @@ import("core.base.option") -- merge *.a archive libraries for ar function _merge_for_ar(target, program, outputfile, libraryfiles, opt) opt = opt or {} - if target:is_plat("macosx") then + if target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then os.vrunv("libtool", table.join("-static", "-o", outputfile, libraryfiles)) else os.vrunv(program, table.join("crsT", outputfile, libraryfiles)) -- cgit v1.3.1 From b54c309384c334c265072e15a23692e52f11b0b7 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 4 Sep 2021 23:43:32 +0800 Subject: improve merge lib for ar --- tests/projects/other/merge_archive2/src/main.c | 17 +++++++++++++++++ tests/projects/other/merge_archive2/xmake.lua | 3 +++ xmake/modules/utils/archive/merge_staticlib.lua | 12 +++++++++++- xmake/rules/utils/merge_archive/xmake.lua | 3 ++- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 tests/projects/other/merge_archive2/src/main.c diff --git a/tests/projects/other/merge_archive2/src/main.c b/tests/projects/other/merge_archive2/src/main.c new file mode 100644 index 000000000..927c9173e --- /dev/null +++ b/tests/projects/other/merge_archive2/src/main.c @@ -0,0 +1,17 @@ +#include + +int add(int a, int b); +int sub(int a, int b); +int mul(int a, int b); +int subdir_add(int a, int b); +int subdir_sub(int a, int b); + +int main(int argc, char** argv) +{ + printf("%d\n", add(1, 1)); + printf("%d\n", sub(1, 1)); + printf("%d\n", mul(1, 1)); + printf("%d\n", subdir_add(1, 1)); + printf("%d\n", subdir_sub(1, 1)); + return 0; +} diff --git a/tests/projects/other/merge_archive2/xmake.lua b/tests/projects/other/merge_archive2/xmake.lua index eb23dd859..be1f45de0 100644 --- a/tests/projects/other/merge_archive2/xmake.lua +++ b/tests/projects/other/merge_archive2/xmake.lua @@ -16,3 +16,6 @@ target("mul") add_files("src/mul.c") set_policy("build.merge_archive", true) +target("test") + add_deps("mul") + add_files("src/main.c") diff --git a/xmake/modules/utils/archive/merge_staticlib.lua b/xmake/modules/utils/archive/merge_staticlib.lua index b5adbd98f..cbafa4bdb 100644 --- a/xmake/modules/utils/archive/merge_staticlib.lua +++ b/xmake/modules/utils/archive/merge_staticlib.lua @@ -27,7 +27,17 @@ function _merge_for_ar(target, program, outputfile, libraryfiles, opt) if target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then os.vrunv("libtool", table.join("-static", "-o", outputfile, libraryfiles)) else - os.vrunv(program, table.join("crsT", outputfile, libraryfiles)) + local tmpfile = os.tmpfile() + local mrifile = io.open(tmpfile, "w") + mrifile:print("create %s", outputfile) + for _, libraryfile in ipairs(libraryfiles) do + mrifile:print("addlib %s", libraryfile) + end + mrifile:print("save") + mrifile:print("end") + mrifile:close() + os.vrunv(program, {"-M"}, {stdin = tmpfile}) + os.rm(tmpfile) end end diff --git a/xmake/rules/utils/merge_archive/xmake.lua b/xmake/rules/utils/merge_archive/xmake.lua index 63c947d08..62e3900ff 100644 --- a/xmake/rules/utils/merge_archive/xmake.lua +++ b/xmake/rules/utils/merge_archive/xmake.lua @@ -40,7 +40,8 @@ rule("utils.merge.archive") if #libraryfiles > 0 then local tmpfile = os.tmpfile() .. path.extension(target:targetfile()) merge_staticlib(target, tmpfile, libraryfiles) - os.mv(tmpfile, target:targetfile()) + os.cp(tmpfile, target:targetfile()) + os.rm(tmpfile) end end, {dependfile = target:dependfile(target:targetfile() .. ".merge_archive"), files = libraryfiles}) end -- cgit v1.3.1 From 1e4cd9f26fdae27e8253f5b214260830d719953b Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 4 Sep 2021 23:46:20 +0800 Subject: mergelib for lib.exe --- xmake/modules/utils/archive/merge_staticlib.lua | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xmake/modules/utils/archive/merge_staticlib.lua b/xmake/modules/utils/archive/merge_staticlib.lua index cbafa4bdb..dc0130219 100644 --- a/xmake/modules/utils/archive/merge_staticlib.lua +++ b/xmake/modules/utils/archive/merge_staticlib.lua @@ -20,6 +20,7 @@ -- imports import("core.base.option") +import("private.tools.vstool") -- merge *.a archive libraries for ar function _merge_for_ar(target, program, outputfile, libraryfiles, opt) @@ -44,6 +45,7 @@ end -- merge *.a archive libraries for msvc/lib.exe function _merge_for_msvclib(target, program, outputfile, libraryfiles, opt) opt = opt or {} + vstool.runv(program, table.join("-nologo", "-out:" .. outputfile, libraryfiles), {envs = opt.runenvs}) end -- merge *.a archive libraries @@ -60,7 +62,7 @@ function main(target, outputfile, libraryfiles) break end end - _merge_for_msvclib(target, (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, {runenvs = msvc and msvc:runenvs()}) else raise("cannot merge (%s): unknown ar tool %s!", table.concat(libraryfiles, ", "), toolname) end -- cgit v1.3.1 From 06b0a3c599b1cfb681dbbd89b4784190aadd2f8a Mon Sep 17 00:00:00 2001 From: ruki Date: Sun, 5 Sep 2021 00:21:14 +0800 Subject: update changelog --- CHANGELOG.md | 2 ++ xmake/core/project/policy.lua | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4524d2b69..d83e6378c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ * [#1618](https://github.com/xmake-io/xmake/issues/1618): Improve vala to support to generate libraries and bindings * Improve Qt rules to support Qt 4.x * Improve `set_symbols("debug")` to generate pdb file for clang on windows +* [#1638](https://github.com/xmake-io/xmake/issues/1638): Improve to merge static library ## v2.5.7 @@ -1079,6 +1080,7 @@ * [#1618](https://github.com/xmake-io/xmake/issues/1618): 改进 vala 支持构建动态库和静态库程序 * 改进 Qt 规则去支持 Qt 4.x * 改进 `set_symbols("debug")` 支持 clang/windows 生成 pdb 文件 +* [#1638](https://github.com/xmake-io/xmake/issues/1638): 改进合并静态库 ## v2.5.7 diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 418d4d566..8491e6883 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -44,7 +44,7 @@ function policy.policies() -- 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"}, + ["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 -- cgit v1.3.1