diff options
| author | ruki <[email protected]> | 2021-09-05 00:27:39 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-09-05 00:27:39 +0800 |
| commit | 58ab15fca9879617a5a3f31d7cdfa9b6dbbc87cd (patch) | |
| tree | 20b52ad45af51fdfc5324344078b3af9a56f8a51 /xmake/modules | |
| parent | f1494df1cf0a7cbeb6c5f63d13f8e4699804fe9f (diff) | |
| parent | 06b0a3c599b1cfb681dbbd89b4784190aadd2f8a (diff) | |
Merge pull request #1639 from xmake-io/mergelib
Improve to merge static library
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/utils/archive/merge_staticlib.lua | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/xmake/modules/utils/archive/merge_staticlib.lua b/xmake/modules/utils/archive/merge_staticlib.lua new file mode 100644 index 000000000..dc0130219 --- /dev/null +++ b/xmake/modules/utils/archive/merge_staticlib.lua @@ -0,0 +1,73 @@ +--!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") +import("private.tools.vstool") + +-- merge *.a archive libraries for ar +function _merge_for_ar(target, program, outputfile, libraryfiles, opt) + opt = opt or {} + if target:is_plat("macosx", "iphoneos", "watchos", "appletvos") then + os.vrunv("libtool", table.join("-static", "-o", outputfile, libraryfiles)) + else + 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 + +-- 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 +function main(target, outputfile, libraryfiles) + local program, toolname = target:tool("ar") + if program and toolname then + if toolname:find("ar") then + _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 + if toolchain_inst:name() == "msvc" then + msvc = toolchain_inst + break + end + end + _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 + else + raise("cannot merge (%s): ar not found!", table.concat(libraryfiles, ", ")) + end +end + |
