summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2025-07-26 00:44:04 +0800
committerruki <[email protected]>2025-07-26 00:44:04 +0800
commit53d106bc017dc2e7db4bdff583ebe863c957ab9f (patch)
tree36aa368c8d2c0faec1b8556b45b632fe7280c7e7
parent8f3b582019d38f1aa9d774b1c752f9a5f18507fc (diff)
improve deplibs
-rw-r--r--xmake/modules/target/action/install/main.lua34
-rw-r--r--xmake/modules/target/action/uninstall/main.lua33
-rw-r--r--xmake/modules/utils/binary/deplibs.lua55
-rw-r--r--xmake/plugins/pack/batchcmds.lua33
4 files changed, 60 insertions, 95 deletions
diff --git a/xmake/modules/target/action/install/main.lua b/xmake/modules/target/action/install/main.lua
index 753503eaa..0d102ffd7 100644
--- a/xmake/modules/target/action/install/main.lua
+++ b/xmake/modules/target/action/install/main.lua
@@ -49,26 +49,6 @@ function _get_target_includedir(target, opt)
return path.join(opt.installdir, opt.includedir)
end
--- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
--- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
-function _get_target_package_deplibs(binaryfile, depends, libfiles, opt)
- local deplibs = get_depend_libraries(binaryfile, {plat = opt.plat, arch = opt.arch})
- local depends_new = hashset.new()
- for _, deplib in ipairs(deplibs) do
- local libname = path.filename(deplib)
- if not depends:has(libname) then
- depends:insert(libname)
- depends_new:insert(libname)
- end
- end
- for _, libfile in ipairs(libfiles) do
- local libname = path.filename(libfile)
- if depends_new:has(libname) then
- _get_target_package_deplibs(libfile, depends, libfiles, opt)
- end
- end
-end
-
function _get_target_package_libfiles(target, opt)
if option.get("nopkgs") then
return {}
@@ -88,9 +68,17 @@ function _get_target_package_libfiles(target, opt)
-- we can only reserve used libraries
if project.policy("install.strip_packagelibs") then
if target:is_binary() or target:is_shared() or opt.binaryfile then
- local depends = hashset.new()
- _get_target_package_deplibs(opt.binaryfile or target:targetfile(), depends, libfiles, {plat = target:plat(), arch = target:arch()})
- table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
+ -- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
+ -- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
+ local deplibs = get_depend_libraries(opt.binaryfile or target:targetfile(), {
+ recursive = true, plat = target:plat(), arch = target:arch()})
+ if deplibs then
+ local depends = hashset.new()
+ for _, deplib in ipairs(deplibs) do
+ depends:insert(path.filename(deplib))
+ end
+ table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
+ end
end
end
return libfiles
diff --git a/xmake/modules/target/action/uninstall/main.lua b/xmake/modules/target/action/uninstall/main.lua
index e94a29897..6eff73529 100644
--- a/xmake/modules/target/action/uninstall/main.lua
+++ b/xmake/modules/target/action/uninstall/main.lua
@@ -49,26 +49,6 @@ function _get_target_includedir(target, opt)
return path.join(opt.installdir, opt.includedir)
end
--- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
--- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
-function _get_target_package_deplibs(binaryfile, depends, libfiles, opt)
- local deplibs = get_depend_libraries(binaryfile, {plat = opt.plat, arch = opt.arch})
- local depends_new = hashset.new()
- for _, deplib in ipairs(deplibs) do
- local libname = path.filename(deplib)
- if not depends:has(libname) then
- depends:insert(libname)
- depends_new:insert(libname)
- end
- end
- for _, libfile in ipairs(libfiles) do
- local libname = path.filename(libfile)
- if depends_new:has(libname) then
- _get_target_package_deplibs(libfile, depends, libfiles, opt)
- end
- end
-end
-
function _get_target_package_libfiles(target, opt)
if option.get("nopkgs") then
return {}
@@ -88,9 +68,16 @@ function _get_target_package_libfiles(target, opt)
-- we can only reserve used libraries
if project.policy("install.strip_packagelibs") then
if target:is_binary() or target:is_shared() or opt.binaryfile then
- local depends = hashset.new()
- _get_target_package_deplibs(opt.binaryfile or target:targetfile(), depends, libfiles, {plat = target:plat(), arch = target:arch()})
- table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
+ -- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
+ -- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
+ local deplibs = get_depend_libraries(opt.binaryfile or target:targetfile(), {recursive = true, plat = target:plat(), arch = target:arch()})
+ if deplibs then
+ local depends = hashset.new()
+ for _, deplib in ipairs(deplibs) do
+ depends:insert(path.filename(deplib))
+ end
+ table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
+ end
end
end
return libfiles
diff --git a/xmake/modules/utils/binary/deplibs.lua b/xmake/modules/utils/binary/deplibs.lua
index fdc873b31..b81d644d8 100644
--- a/xmake/modules/utils/binary/deplibs.lua
+++ b/xmake/modules/utils/binary/deplibs.lua
@@ -67,27 +67,29 @@ function _get_depends_by_objdump(binaryfile, opt)
if result then
for _, line in ipairs(result:split("\n")) do
line = line:trim()
- if plat == "windows" or plat == "mingw" then
- if line:startswith("DLL Name:") then
- local filename = line:split(":")[2]:trim()
- if filename:endswith(".dll") then
- depends = depends or {}
- table.insert(depends, filename)
+ if not line:endswith(":") then
+ if plat == "windows" or plat == "mingw" then
+ if line:startswith("DLL Name:") then
+ local filename = line:split(":")[2]:trim()
+ if filename:endswith(".dll") then
+ depends = depends or {}
+ table.insert(depends, filename)
+ end
end
- end
- elseif plat == "macosx" or plat == "iphoneos" or plat == "appletvos" or plat == "watchos" then
- local filename = line:match(".-%.dylib") or line:match(".-%.framework")
- if filename then
- depends = depends or {}
- table.insert(depends, filename)
- end
- else
- if line:startswith("NEEDED") then
- local filename = line:split("%s+")[2]
- if filename and filename:endswith(".so") or filename:find("%.so[%.%d+]+$") then
+ elseif plat == "macosx" or plat == "iphoneos" or plat == "appletvos" or plat == "watchos" then
+ local filename = line:match(".-%.dylib") or line:match(".-%.framework")
+ if filename then
depends = depends or {}
table.insert(depends, filename)
end
+ else
+ if line:startswith("NEEDED") then
+ local filename = line:split("%s+")[2]
+ if filename and filename:endswith(".so") or filename:find("%.so[%.%d+]+$") then
+ depends = depends or {}
+ table.insert(depends, filename)
+ end
+ end
end
end
end
@@ -195,10 +197,13 @@ function _get_depends_by_otool(binaryfile, opt)
local result = try { function () return os.iorunv(otool.program, {"-L", binaryfile}) end }
if result then
for _, line in ipairs(result:split("\n")) do
- local filename = line:match(".-%.dylib") or line:match(".-%.framework")
- if filename then
- depends = depends or {}
- table.insert(depends, filename:trim())
+ line = line:trim()
+ if not line:endswith(":") then
+ local filename = line:match(".-%.dylib") or line:match(".-%.framework")
+ if filename then
+ depends = depends or {}
+ table.insert(depends, filename:trim())
+ end
end
end
end
@@ -240,18 +245,18 @@ function _resolve_filepath(binaryfile, dependfile, opt)
for _, rpath in ipairs(rpathlist) do
local filepath = dependfile:replace("@rpath/", rpath .. "/", {plain = true})
if os.isfile(filepath) then
- dependfile = filepath
+ dependfile = path.absolute(filepath)
break
elseif filepath:startswith("@loader_path/") then
filepath = filepath:replace("@loader_path/", path.directory(loaderfile) .. "/", {plain = true})
if os.isfile(filepath) then
- dependfile = filepath
+ dependfile = path.absolute(filepath)
break
end
elseif filepath:startswith("$ORIGIN/") then
filepath = filepath:replace("$ORIGIN/", path.directory(loaderfile) .. "/", {plain = true})
if os.isfile(filepath) then
- dependfile = filepath
+ dependfile = path.absolute(filepath)
break
end
end
@@ -304,8 +309,6 @@ end
--
function main(binaryfile, opt)
opt = opt or {}
- --opt.recursive = true
- --opt.resolve_path = true
if opt.resolve_path then
opt._loaderfile = binaryfile
end
diff --git a/xmake/plugins/pack/batchcmds.lua b/xmake/plugins/pack/batchcmds.lua
index c768453d0..df0c506e0 100644
--- a/xmake/plugins/pack/batchcmds.lua
+++ b/xmake/plugins/pack/batchcmds.lua
@@ -62,26 +62,6 @@ function _get_target_installdir(package, target)
return path.normalize(installdir)
end
--- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
--- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
-function _get_target_package_deplibs(binaryfile, depends, libfiles, opt)
- local deplibs = get_depend_libraries(binaryfile, {plat = opt.plat, arch = opt.arch})
- local depends_new = hashset.new()
- for _, deplib in ipairs(deplibs) do
- local libname = path.filename(deplib)
- if not depends:has(libname) then
- depends:insert(libname)
- depends_new:insert(libname)
- end
- end
- for _, libfile in ipairs(libfiles) do
- local libname = path.filename(libfile)
- if depends_new:has(libname) then
- _get_target_package_deplibs(libfile, depends, libfiles, opt)
- end
- end
-end
-
function _get_target_package_libfiles(target, opt)
if option.get("nopkgs") then
return {}
@@ -101,9 +81,16 @@ function _get_target_package_libfiles(target, opt)
-- we can only reserve used libraries
if project.policy("install.strip_packagelibs") then
if target:is_binary() or target:is_shared() or opt.binaryfile then
- local depends = hashset.new()
- _get_target_package_deplibs(opt.binaryfile or target:targetfile(), depends, libfiles, {plat = target:plat(), arch = target:arch()})
- table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
+ -- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
+ -- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
+ local deplibs = get_depend_libraries(opt.binaryfile or target:targetfile(), {recursive = true, plat = target:plat(), arch = target:arch()})
+ if deplibs then
+ local depends = hashset.new()
+ for _, deplib in ipairs(deplibs) do
+ depends:insert(path.filename(deplib))
+ end
+ table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
+ end
end
end
return libfiles