summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2024-12-04 23:37:51 +0800
committerruki <[email protected]>2024-12-04 23:37:51 +0800
commit3237e43498d1650426be1df79c63a71a7f5ca266 (patch)
tree9ed4030b753c735bacfb45cfb5e72dfbf7bfdf3e
parentf2557f754fd0204c4549ac3b985938329c42cd12 (diff)
merge staticlibs for package
-rw-r--r--xmake/core/project/policy.lua3
-rw-r--r--xmake/modules/private/action/require/impl/actions/install.lua35
-rw-r--r--xmake/modules/utils/archive/merge_staticlib.lua4
3 files changed, 40 insertions, 2 deletions
diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua
index 46b16e256..03053cb3e 100644
--- a/xmake/core/project/policy.lua
+++ b/xmake/core/project/policy.lua
@@ -149,6 +149,9 @@ function policy.policies()
["package.cmake_generator.ninja"] = {description = "Set cmake package use ninja for build", type = "boolean"},
-- Enable msbuild MultiToolTask
["package.msbuild.multi_tool_task"] = {description = "Enable msbuild MultiToolTask.", default = false, type = "boolean"},
+ -- Merge all static libraries after installing package
+ -- @see https://github.com/xmake-io/xmake/issues/5894
+ ["package.merge_staticlibs"] = {description = "Merge all static libraries after installing package", type = "boolean"},
-- C/C++ build cache
["package.build.ccache"] = {description = "Enable C/C++ package build cache.", default = false, type = "boolean"},
-- Stop to test on the first failure
diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua
index 1e64be14e..f7df8991e 100644
--- a/xmake/modules/private/action/require/impl/actions/install.lua
+++ b/xmake/modules/private/action/require/impl/actions/install.lua
@@ -26,6 +26,7 @@ import("core.project.target")
import("core.project.project")
import("core.platform.platform")
import("lib.detect.find_file")
+import("utils.archive.merge_staticlib")
import("private.tools.ccache")
import("private.action.require.impl.actions.test")
import("private.action.require.impl.actions.patch_sources")
@@ -225,6 +226,37 @@ function _fix_paths_for_precompiled_package(package)
end
end
+-- merge static libraries
+-- @see https://github.com/xmake-io/xmake/issues/5894
+function _merge_staticlibs(package)
+ local merge_staticlibs = project.policy("package.merge_staticlibs")
+ if merge_staticlibs == nil then
+ merge_staticlibs = package:policy("package.merge_staticlibs")
+ end
+ if merge_staticlibs and package:is_library()
+ and not package:config("shared") and not package:is_headeronly() and not package:is_moduleonly() then
+ local installdir = package:installdir()
+ local linkdirs = table.wrap(package:get("linkdirs") or "lib")
+ local libfiles = {}
+ for _, linkdir in ipairs(linkdirs) do
+ for _, libfile in ipairs(os.files(path.join(installdir, linkdir, "*"))) do
+ if libfile:endswith(".lib") or libfile:endswith(".a") then
+ table.insert(libfiles, libfile)
+ end
+ end
+ end
+ if #libfiles > 0 then
+ local linkdir = linkdirs[1]
+ local libfile_new = path.join(installdir, linkdir,
+ target.filename(package:name(), "static", {plat = package:plat(), arch = package:arch()}))
+ merge_staticlib(package, libfile_new, libfiles)
+ for _, libfile in ipairs(libfiles) do
+ os.rm(libfile)
+ end
+ end
+ end
+end
+
-- get failed install directory
function _get_installdir_failed(package)
return path.join(package:cachedir(), "installdir.failed")
@@ -421,6 +453,9 @@ function main(package)
os.cp(rulesdir, package:installdir())
end
+ -- merge static libraries
+ _merge_staticlibs(package)
+
-- leave the environments of all package dependencies
os.setenvs(oldenvs)
diff --git a/xmake/modules/utils/archive/merge_staticlib.lua b/xmake/modules/utils/archive/merge_staticlib.lua
index fb9c65c06..f02fcd981 100644
--- a/xmake/modules/utils/archive/merge_staticlib.lua
+++ b/xmake/modules/utils/archive/merge_staticlib.lua
@@ -32,7 +32,7 @@ function _merge_for_ar(target, program, outputfile, libraryfiles, opt)
-- because on windows/ndk, llvm-ar.exe may fail to write with no permission, even though it's a writable temp file.
--
-- @see https://github.com/xmake-io/xmake/issues/1973
- local archivefile = target:autogenfile((hash.uuid(outputfile):gsub("%-", ""))) .. ".a"
+ local archivefile = target.autogenfile and target:autogenfile((hash.uuid(outputfile):gsub("%-", ""))) .. ".a" or (os.tmpfile() .. ".a")
os.mkdir(path.directory(archivefile))
local tmpfile = os.tmpfile()
local mrifile = io.open(tmpfile, "w")
@@ -56,7 +56,7 @@ function _merge_for_msvclib(target, program, outputfile, libraryfiles, opt)
vstool.runv(program, table.join("-nologo", "-out:" .. outputfile, libraryfiles), {envs = opt.runenvs})
end
--- merge *.a archive libraries
+-- merge *.a archive libraries, @note target may be package.
function main(target, outputfile, libraryfiles)
local program, toolname = target:tool("ar")
if program and toolname then