From 53d106bc017dc2e7db4bdff583ebe863c957ab9f Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 26 Jul 2025 00:44:04 +0800 Subject: improve deplibs --- xmake/plugins/pack/batchcmds.lua | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) (limited to 'xmake/plugins/pack/batchcmds.lua') 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 -- cgit v1.3.1 From e3759b8bd56d614b49d95256c04c7d093100d64a Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 26 Jul 2025 00:47:48 +0800 Subject: add resolve_hint_paths --- xmake/modules/target/action/install/main.lua | 10 ++++------ xmake/modules/target/action/uninstall/main.lua | 11 +++++------ xmake/modules/utils/binary/deplibs.lua | 17 ++++++++++++++++- xmake/plugins/pack/batchcmds.lua | 11 +++++------ 4 files changed, 30 insertions(+), 19 deletions(-) (limited to 'xmake/plugins/pack/batchcmds.lua') diff --git a/xmake/modules/target/action/install/main.lua b/xmake/modules/target/action/install/main.lua index 0d102ffd7..d9faa80ad 100644 --- a/xmake/modules/target/action/install/main.lua +++ b/xmake/modules/target/action/install/main.lua @@ -71,13 +71,11 @@ function _get_target_package_libfiles(target, opt) -- 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()}) + plat = target:plat(), arch = target:arch(), + recursive = true, resolve_path = true, resolve_hint_paths = libfiles}) 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) + local depends = hashset.from(deplibs) + table.remove_if(libfiles, function (_, libfile) return not depends:has(libfile) end) end end end diff --git a/xmake/modules/target/action/uninstall/main.lua b/xmake/modules/target/action/uninstall/main.lua index 6eff73529..2273ff76d 100644 --- a/xmake/modules/target/action/uninstall/main.lua +++ b/xmake/modules/target/action/uninstall/main.lua @@ -70,13 +70,12 @@ function _get_target_package_libfiles(target, opt) if target:is_binary() or target:is_shared() or opt.binaryfile then -- 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()}) + local deplibs = get_depend_libraries(opt.binaryfile or target:targetfile(), { + plat = target:plat(), arch = target:arch(), + recursive = true, resolve_path = true, resolve_hint_paths = libfiles}) 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) + local depends = hashset.from(deplibs) + table.remove_if(libfiles, function (_, libfile) return not depends:has(libfile) end) end end end diff --git a/xmake/modules/utils/binary/deplibs.lua b/xmake/modules/utils/binary/deplibs.lua index b81d644d8..15ca0b12b 100644 --- a/xmake/modules/utils/binary/deplibs.lua +++ b/xmake/modules/utils/binary/deplibs.lua @@ -235,6 +235,7 @@ end -- resolve file path with @rpath, @loader_path, and $ORIGIN function _resolve_filepath(binaryfile, dependfile, opt) local loaderfile = opt._loaderfile + local resolve_hint_paths = opt.resolve_hint_paths if dependfile:startswith("@rpath/") then local rpathlist = opt._rpathlist if rpathlist == nil then @@ -263,6 +264,19 @@ function _resolve_filepath(binaryfile, dependfile, opt) end end end + if not path.is_absolute(dependfile) then + if os.isfile(dependfile) then + dependfile = path.absolute(dependfile) + elseif resolve_hint_paths then + local filename = path.filename(dependfile) + for _, filepath in ipairs(resolve_hint_paths) do + if filename == path.filename(filepath) then + dependfile = path.absolute(filepath) + break + end + end + end + end dependfile = path.normalize(dependfile) if binaryfile ~= dependfile then return dependfile @@ -303,9 +317,10 @@ end -- get the library dependencies of the give binary files -- -- @param binaryfile the binary file --- @param opt the option, e.g. {recursive = false, resolve_path = true} +-- @param opt the option, e.g. {recursive = false, resolve_path = true, resolve_hint_paths = {}} -- - recursive: recursively get all sub-dependencies, sorted by topology -- - resolve_path: try to resolve the file full path, e.g. @rpath, @loader_path, $ORIGIN, relative path .. +-- - resolve_hint_paths: we can resolve and match path from them -- function main(binaryfile, opt) opt = opt or {} diff --git a/xmake/plugins/pack/batchcmds.lua b/xmake/plugins/pack/batchcmds.lua index df0c506e0..2418f39ac 100644 --- a/xmake/plugins/pack/batchcmds.lua +++ b/xmake/plugins/pack/batchcmds.lua @@ -83,13 +83,12 @@ function _get_target_package_libfiles(target, opt) if target:is_binary() or target:is_shared() or opt.binaryfile then -- 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()}) + local deplibs = get_depend_libraries(opt.binaryfile or target:targetfile(), { + plat = target:plat(), arch = target:arch(), + recursive = true, resolve_path = true, resolve_hint_paths = libfiles}) 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) + local depends = hashset.from(deplibs) + table.remove_if(libfiles, function (_, libfile) return not depends:has(libfile) end) end end end -- cgit v1.3.1