Skip to content

Commit

Permalink
Merge pull request #5927 from xmake-io/mergelibs
Browse files Browse the repository at this point in the history
Merge staticlibs for package
  • Loading branch information
waruqi authored Dec 4, 2024
2 parents f2557f7 + d44164f commit cb46a68
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
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

0 comments on commit cb46a68

Please sign in to comment.