Skip to content

Commit fcc2809

Browse files
authored
Merge pull request #5706 from xmake-io/install
Improve to Install target libs
2 parents ad91dd8 + b9daa8d commit fcc2809

File tree

3 files changed

+107
-67
lines changed

3 files changed

+107
-67
lines changed

xmake/modules/target/action/install/main.lua

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ end
4343

4444
-- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
4545
-- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
46-
function _get_target_package_deplibs(target, depends, libfiles, binaryfile)
47-
local deplibs = get_depend_libraries(binaryfile, {plat = target:plat(), arch = target:arch()})
46+
function _get_target_package_deplibs(binaryfile, depends, libfiles, opt)
47+
local deplibs = get_depend_libraries(binaryfile, {plat = opt.plat, arch = opt.arch})
4848
local depends_new = hashset.new()
4949
for _, deplib in ipairs(deplibs) do
5050
local libname = path.filename(deplib)
@@ -56,7 +56,7 @@ function _get_target_package_deplibs(target, depends, libfiles, binaryfile)
5656
for _, libfile in ipairs(libfiles) do
5757
local libname = path.filename(libfile)
5858
if depends_new:has(libname) then
59-
_get_target_package_deplibs(target, depends, libfiles, libfile)
59+
_get_target_package_deplibs(libfile, depends, libfiles, opt)
6060
end
6161
end
6262
end
@@ -65,6 +65,7 @@ function _get_target_package_libfiles(target, opt)
6565
if option.get("nopkgs") then
6666
return {}
6767
end
68+
opt = opt or {}
6869
local libfiles = {}
6970
for _, pkg in ipairs(target:orderpkgs(opt)) do
7071
if pkg:enabled() and pkg:get("libfiles") then
@@ -78,15 +79,40 @@ function _get_target_package_libfiles(target, opt)
7879
end
7980
-- we can only reserve used libraries
8081
if project.policy("install.strip_packagelibs") then
81-
if target:is_binary() or target:is_shared() then
82+
if target:is_binary() or target:is_shared() or opt.binaryfile then
8283
local depends = hashset.new()
83-
_get_target_package_deplibs(target, depends, libfiles, target:targetfile())
84+
_get_target_package_deplibs(opt.binaryfile or target:targetfile(), depends, libfiles, {plat = target:plat(), arch = target:arch()})
8485
table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
8586
end
8687
end
8788
return libfiles
8889
end
8990

91+
-- get target libraries
92+
function _get_target_libfiles(target, libfiles, binaryfile, refs)
93+
if not refs[target] then
94+
local plaindeps = target:get("deps")
95+
if plaindeps then
96+
for _, depname in ipairs(plaindeps) do
97+
local dep = target:dep(depname)
98+
if dep then
99+
if dep:is_shared() then
100+
local depfile = dep:targetfile()
101+
if os.isfile(depfile) then
102+
table.insert(libfiles, depfile)
103+
end
104+
_get_target_libfiles(dep, libfiles, dep:targetfile(), refs)
105+
elseif dep:is_library() then
106+
_get_target_libfiles(dep, libfiles, binaryfile, refs)
107+
end
108+
end
109+
end
110+
end
111+
table.join2(libfiles, _get_target_package_libfiles(target, {binaryfile = binaryfile}))
112+
refs[target] = true
113+
end
114+
end
115+
90116
-- copy file with symlinks
91117
function _copy_file_with_symlinks(srcfile, outputdir)
92118
if os.islink(srcfile) then
@@ -139,22 +165,11 @@ end
139165

140166
-- install shared libraries
141167
function _install_shared_libraries(target, opt)
142-
local bindir = target:is_plat("windows", "mingw") and _get_target_bindir(target, opt) or _get_target_libdir(target, opt)
168+
local bindir = target:is_plat("windows", "mingw") and _get_target_bindir(target, opt) or _get_target_libdir(target, opt)
143169

144170
-- get all dependent shared libraries
145171
local libfiles = {}
146-
for _, dep in ipairs(target:orderdeps()) do
147-
if dep:kind() == "shared" then
148-
local depfile = dep:targetfile()
149-
if os.isfile(depfile) then
150-
table.insert(libfiles, depfile)
151-
end
152-
end
153-
table.join2(libfiles, _get_target_package_libfiles(dep, {interface = true}))
154-
end
155-
table.join2(libfiles, _get_target_package_libfiles(target))
156-
157-
-- deduplicate libfiles, prevent packages using the same libfiles from overwriting each other
172+
_get_target_libfiles(target, libfiles, target:targetfile(), {})
158173
libfiles = table.unique(libfiles)
159174

160175
-- do install
@@ -205,7 +220,7 @@ end
205220

206221
-- install shared library
207222
function _install_shared(target, opt)
208-
local bindir = target:is_plat("windows", "mingw") and _get_target_bindir(target, opt) or _get_target_libdir(target, opt)
223+
local bindir = target:is_plat("windows", "mingw") and _get_target_bindir(target, opt) or _get_target_libdir(target, opt)
209224
os.mkdir(bindir)
210225
local targetfile = target:targetfile()
211226

xmake/modules/target/action/uninstall/main.lua

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ end
4343

4444
-- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
4545
-- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
46-
function _get_target_package_deplibs(target, depends, libfiles, binaryfile)
47-
local deplibs = get_depend_libraries(binaryfile, {plat = target:plat(), arch = target:arch()})
46+
function _get_target_package_deplibs(binaryfile, depends, libfiles, opt)
47+
local deplibs = get_depend_libraries(binaryfile, {plat = opt.plat, arch = opt.arch})
4848
local depends_new = hashset.new()
4949
for _, deplib in ipairs(deplibs) do
5050
local libname = path.filename(deplib)
@@ -56,7 +56,7 @@ function _get_target_package_deplibs(target, depends, libfiles, binaryfile)
5656
for _, libfile in ipairs(libfiles) do
5757
local libname = path.filename(libfile)
5858
if depends_new:has(libname) then
59-
_get_target_package_deplibs(target, depends, libfiles, libfile)
59+
_get_target_package_deplibs(libfile, depends, libfiles, opt)
6060
end
6161
end
6262
end
@@ -65,6 +65,7 @@ function _get_target_package_libfiles(target, opt)
6565
if option.get("nopkgs") then
6666
return {}
6767
end
68+
opt = opt or {}
6869
local libfiles = {}
6970
for _, pkg in ipairs(target:orderpkgs(opt)) do
7071
if pkg:enabled() and pkg:get("libfiles") then
@@ -78,15 +79,40 @@ function _get_target_package_libfiles(target, opt)
7879
end
7980
-- we can only reserve used libraries
8081
if project.policy("install.strip_packagelibs") then
81-
if target:is_binary() or target:is_shared() then
82+
if target:is_binary() or target:is_shared() or opt.binaryfile then
8283
local depends = hashset.new()
83-
_get_target_package_deplibs(target, depends, libfiles, target:targetfile())
84+
_get_target_package_deplibs(opt.binaryfile or target:targetfile(), depends, libfiles, {plat = target:plat(), arch = target:arch()})
8485
table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
8586
end
8687
end
8788
return libfiles
8889
end
8990

91+
-- get target libraries
92+
function _get_target_libfiles(target, libfiles, binaryfile, refs)
93+
if not refs[target] then
94+
local plaindeps = target:get("deps")
95+
if plaindeps then
96+
for _, depname in ipairs(plaindeps) do
97+
local dep = target:dep(depname)
98+
if dep then
99+
if dep:is_shared() then
100+
local depfile = dep:targetfile()
101+
if os.isfile(depfile) then
102+
table.insert(libfiles, depfile)
103+
end
104+
_get_target_libfiles(dep, libfiles, dep:targetfile(), refs)
105+
elseif dep:is_library() then
106+
_get_target_libfiles(dep, libfiles, binaryfile, refs)
107+
end
108+
end
109+
end
110+
end
111+
table.join2(libfiles, _get_target_package_libfiles(target, {binaryfile = binaryfile}))
112+
refs[target] = true
113+
end
114+
end
115+
90116
-- remove file with symbols
91117
function _remove_file_with_symbols(filepath)
92118
if os.islink(filepath) then
@@ -130,20 +156,10 @@ end
130156
-- uninstall shared libraries
131157
function _uninstall_shared_libraries(target, opt)
132158
local bindir = target:is_plat("windows", "mingw") and _get_target_bindir(target, opt) or _get_target_libdir(target, opt)
159+
133160
-- get all dependent shared libraries
134161
local libfiles = {}
135-
for _, dep in ipairs(target:orderdeps()) do
136-
if dep:kind() == "shared" then
137-
local depfile = dep:targetfile()
138-
if os.isfile(depfile) then
139-
table.insert(libfiles, depfile)
140-
end
141-
end
142-
table.join2(libfiles, _get_target_package_libfiles(dep, {interface = true}))
143-
end
144-
table.join2(libfiles, _get_target_package_libfiles(target))
145-
146-
-- deduplicate libfiles, prevent packages using the same libfiles from overwriting each other
162+
_get_target_libfiles(target, libfiles, target:targetfile(), {})
147163
libfiles = table.unique(libfiles)
148164

149165
-- do uninstall

xmake/plugins/pack/batchcmds.lua

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ end
6363

6464
-- we need to get all deplibs, e.g. app -> libfoo.so -> libbar.so ...
6565
-- @see https://github.com/xmake-io/xmake/issues/5325#issuecomment-2242597732
66-
function _get_target_package_deplibs(target, depends, libfiles, binaryfile)
67-
local deplibs = get_depend_libraries(binaryfile, {plat = target:plat(), arch = target:arch()})
66+
function _get_target_package_deplibs(binaryfile, depends, libfiles, opt)
67+
local deplibs = get_depend_libraries(binaryfile, {plat = opt.plat, arch = opt.arch})
6868
local depends_new = hashset.new()
6969
for _, deplib in ipairs(deplibs) do
7070
local libname = path.filename(deplib)
@@ -76,12 +76,16 @@ function _get_target_package_deplibs(target, depends, libfiles, binaryfile)
7676
for _, libfile in ipairs(libfiles) do
7777
local libname = path.filename(libfile)
7878
if depends_new:has(libname) then
79-
_get_target_package_deplibs(target, depends, libfiles, libfile)
79+
_get_target_package_deplibs(libfile, depends, libfiles, opt)
8080
end
8181
end
8282
end
8383

8484
function _get_target_package_libfiles(target, opt)
85+
if option.get("nopkgs") then
86+
return {}
87+
end
88+
opt = opt or {}
8589
local libfiles = {}
8690
for _, pkg in ipairs(target:orderpkgs(opt)) do
8791
if pkg:enabled() and pkg:get("libfiles") then
@@ -94,14 +98,41 @@ function _get_target_package_libfiles(target, opt)
9498
end
9599
end
96100
-- we can only reserve used libraries
97-
if target:is_binary() or target:is_shared() then
98-
local depends = hashset.new()
99-
_get_target_package_deplibs(target, depends, libfiles, target:targetfile())
100-
table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
101+
if project.policy("install.strip_packagelibs") then
102+
if target:is_binary() or target:is_shared() or opt.binaryfile then
103+
local depends = hashset.new()
104+
_get_target_package_deplibs(opt.binaryfile or target:targetfile(), depends, libfiles, {plat = target:plat(), arch = target:arch()})
105+
table.remove_if(libfiles, function (_, libfile) return not depends:has(path.filename(libfile)) end)
106+
end
101107
end
102108
return libfiles
103109
end
104110

111+
-- get target libraries
112+
function _get_target_libfiles(target, libfiles, binaryfile, refs)
113+
if not refs[target] then
114+
local plaindeps = target:get("deps")
115+
if plaindeps then
116+
for _, depname in ipairs(plaindeps) do
117+
local dep = target:dep(depname)
118+
if dep then
119+
if dep:is_shared() then
120+
local depfile = dep:targetfile()
121+
if os.isfile(depfile) then
122+
table.insert(libfiles, depfile)
123+
end
124+
_get_target_libfiles(dep, libfiles, dep:targetfile(), refs)
125+
elseif dep:is_library() then
126+
_get_target_libfiles(dep, libfiles, binaryfile, refs)
127+
end
128+
end
129+
end
130+
end
131+
table.join2(libfiles, _get_target_package_libfiles(target, {binaryfile = binaryfile}))
132+
refs[target] = true
133+
end
134+
end
135+
105136
-- copy file with symlinks
106137
function _copy_file_with_symlinks(batchcmds_, srcfile, outputdir)
107138
if os.islink(srcfile) then
@@ -190,18 +221,7 @@ function _install_target_shared_libraries(target, batchcmds_, opt)
190221

191222
-- get all dependent shared libraries
192223
local libfiles = {}
193-
for _, dep in ipairs(target:orderdeps()) do
194-
if dep:kind() == "shared" then
195-
local depfile = dep:targetfile()
196-
if os.isfile(depfile) then
197-
table.insert(libfiles, depfile)
198-
end
199-
end
200-
table.join2(libfiles, _get_target_package_libfiles(dep, {interface = true}))
201-
end
202-
table.join2(libfiles, _get_target_package_libfiles(target))
203-
204-
-- deduplicate libfiles, prevent packages using the same libfiles from overwriting each other
224+
_get_target_libfiles(target, libfiles, target:targetfile(), {})
205225
libfiles = table.unique(libfiles)
206226

207227
-- do install
@@ -248,18 +268,7 @@ function _uninstall_target_shared_libraries(target, batchcmds_, opt)
248268

249269
-- get all dependent shared libraries
250270
local libfiles = {}
251-
for _, dep in ipairs(target:orderdeps()) do
252-
if dep:kind() == "shared" then
253-
local depfile = dep:targetfile()
254-
if os.isfile(depfile) then
255-
table.insert(libfiles, depfile)
256-
end
257-
end
258-
table.join2(libfiles, _get_target_package_libfiles(dep, {interface = true}))
259-
end
260-
table.join2(libfiles, _get_target_package_libfiles(target))
261-
262-
-- deduplicate libfiles, prevent packages using the same libfiles from overwriting each other
271+
_get_target_libfiles(target, libfiles, target:targetfile(), {})
263272
libfiles = table.unique(libfiles)
264273

265274
-- do uninstall

0 commit comments

Comments
 (0)