Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge staticlibs for package #5927

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions xmake/core/project/policy.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions xmake/modules/private/action/require/impl/actions/install.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -225,6 +226,38 @@ 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 linkname = table.wrap(package:get("links"))[1] or package:name()
local libfile_new = path.join(installdir, linkdir,
target.filename(linkname, "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")
Expand Down Expand Up @@ -421,6 +454,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)

Expand Down
4 changes: 2 additions & 2 deletions xmake/modules/utils/archive/merge_staticlib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down
Loading