1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, Xmake Open Source Community.
--
-- @author ruki
-- @file main.lua
--
-- imports
import("core.base.hashset")
import("core.project.project")
import("utils.binary.deplibs", {alias = "get_depend_libraries"})
import("utils.binary.rpath", {alias = "rpath_utils"})
function _get_target_libdir(target, opt)
if not opt.installdir then
return target:libdir()
end
assert(opt.libdir, "opt.libdir is missing")
return path.join(opt.installdir, opt.libdir)
end
function _get_target_bindir(target, opt)
if not opt.installdir then
return target:bindir()
end
assert(opt.bindir, "opt.bindir is missing")
return path.join(opt.installdir, opt.bindir)
end
function _get_target_includedir(target, opt)
if not opt.installdir then
return target:includedir()
end
assert(opt.includedir, "opt.includedir is missing")
return path.join(opt.installdir, opt.includedir)
end
function _get_target_package_libfiles(target, opt)
opt = opt or {}
if not opt.packages then
return {}
end
local libfiles = {}
for _, pkg in ipairs(target:orderpkgs(opt)) do
if pkg:enabled() and pkg:get("libfiles") then
for _, libfile in ipairs(table.wrap(pkg:get("libfiles"))) do
local filename = path.filename(libfile)
if filename:endswith(".dll") or filename:endswith(".so") or filename:find("%.so[%.%d+]+$") or filename:endswith(".dylib") then
table.insert(libfiles, libfile)
end
end
end
end
-- 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
-- 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(), {
plat = target:plat(), arch = target:arch(),
recursive = true, resolve_path = true, resolve_hint_paths = libfiles})
if deplibs then
local depends = hashset.from(deplibs)
table.remove_if(libfiles, function (_, libfile) return not depends:has(libfile) end)
end
end
end
return libfiles
end
-- get target libraries
function _get_target_libfiles(target, libfiles, binaryfile, refs, opt)
if not refs[target] then
local plaindeps = target:get("deps")
if plaindeps then
for _, depname in ipairs(plaindeps) do
local dep = target:dep(depname)
if dep then
if dep:is_shared() then
local depfile = dep:targetfile()
if os.isfile(depfile) then
table.insert(libfiles, depfile)
end
_get_target_libfiles(dep, libfiles, dep:targetfile(), refs, opt)
elseif dep:is_library() then
_get_target_libfiles(dep, libfiles, binaryfile, refs, opt)
end
end
end
end
table.join2(libfiles, _get_target_package_libfiles(target, table.join({binaryfile = binaryfile}, opt)))
refs[target] = true
end
end
-- copy file with symlinks
function _copy_file_with_symlinks(srcfile, outputdir)
if os.islink(srcfile) then
local srcfile_symlink = os.readlink(srcfile)
if not path.is_absolute(srcfile_symlink) then
srcfile_symlink = path.join(path.directory(srcfile), srcfile_symlink)
end
_copy_file_with_symlinks(srcfile_symlink, outputdir)
os.vcp(srcfile, path.join(outputdir, path.filename(srcfile)), {symlink = true, force = true})
else
os.vcp(srcfile, path.join(outputdir, path.filename(srcfile)))
end
end
-- install files
function _install_files(target)
local srcfiles, dstfiles = target:installfiles()
if srcfiles and dstfiles then
for idx, srcfile in ipairs(srcfiles) do
os.vcp(srcfile, dstfiles[idx])
end
end
for _, dep in ipairs(target:orderdeps()) do
local srcfiles, dstfiles = dep:installfiles(dep:installdir(), {interface = true})
if srcfiles and dstfiles then
for idx, srcfile in ipairs(srcfiles) do
os.vcp(srcfile, dstfiles[idx])
end
end
end
end
-- install headers
function _install_headers(target, opt)
local srcheaders, dstheaders = target:headerfiles(_get_target_includedir(target, opt), {installonly = true})
if srcheaders and dstheaders then
for idx, srcheader in ipairs(srcheaders) do
os.vcp(srcheader, dstheaders[idx])
end
end
for _, dep in ipairs(target:orderdeps()) do
local srcfiles, dstfiles = dep:headerfiles(_get_target_includedir(dep, opt), {installonly = true, interface = true})
if srcfiles and dstfiles then
for idx, srcfile in ipairs(srcfiles) do
os.vcp(srcfile, dstfiles[idx])
end
end
end
end
-- install shared libraries
function _install_shared_libraries(target, opt)
local bindir = target:is_plat("windows", "mingw") and _get_target_bindir(target, opt) or _get_target_libdir(target, opt)
-- get all dependent shared libraries
local libfiles = {}
_get_target_libfiles(target, libfiles, target:targetfile(), {}, opt)
libfiles = table.unique(libfiles)
-- do install
for _, libfile in ipairs(libfiles) do
local filename = path.filename(libfile)
local filepath = path.join(bindir, filename)
if os.isfile(filepath) and hash.sha256(filepath) ~= hash.sha256(libfile) then
wprint("'%s' already exists in install dir, we are copying '%s' to overwrite it.", filepath, libfile)
end
_copy_file_with_symlinks(libfile, bindir)
end
end
-- update install rpath, we can only get and update rpathdirs with `{installonly = true}`
-- e.g. add_rpathdirs("@loader_path/../lib", {installonly = true})
function _update_install_rpath(target, opt)
if target:is_plat("windows", "mingw") then
return
end
local bindir = _get_target_bindir(target, opt)
local targetfile = path.join(bindir, target:filename())
if target:policy("install.rpath") then
local result, sources = target:get_from("rpathdirs", "*")
if result and sources then
for idx, rpathdirs in ipairs(result) do
local source = sources[idx]
local extraconf = target:extraconf_from("rpathdirs", source)
for _, rpathdir in ipairs(rpathdirs) do
local extra = extraconf and extraconf[rpathdir] or nil
if extra and extra.installonly then
rpath_utils.insert(targetfile, rpathdir, {plat = target:plat(), arch = target:arch()})
end
end
end
end
end
end
-- install binary
function _install_binary(target, opt)
if opt.libraries then
_install_shared_libraries(target, opt)
end
if opt.binaries then
local bindir = _get_target_bindir(target, opt)
os.mkdir(bindir)
os.vcp(target:targetfile(), bindir)
os.trycp(target:symbolfile(), path.join(bindir, path.filename(target:symbolfile())))
_update_install_rpath(target, opt)
end
end
-- install shared library
function _install_shared(target, opt)
if opt.libraries then
local bindir = target:is_plat("windows", "mingw") and _get_target_bindir(target, opt) or _get_target_libdir(target, opt)
os.mkdir(bindir)
local targetfile = target:targetfile()
if target:is_plat("windows", "mingw") then
-- install *.lib for shared/windows (*.dll) target
-- @see https://github.com/xmake-io/xmake/issues/714
os.vcp(target:targetfile(), bindir)
local libdir = _get_target_libdir(target, opt)
local implibfile = target:artifactfile("implib")
if os.isfile(implibfile) then
os.mkdir(libdir)
os.vcp(implibfile, libdir)
end
else
-- install target with soname and symlink
_copy_file_with_symlinks(targetfile, bindir)
end
os.trycp(target:symbolfile(), path.join(bindir, path.filename(target:symbolfile())))
_install_shared_libraries(target, opt)
end
if opt.headers then
_install_headers(target, opt)
end
end
-- install static library
function _install_static(target, opt)
if opt.libraries then
local libdir = _get_target_libdir(target, opt)
os.mkdir(libdir)
os.vcp(target:targetfile(), libdir)
os.trycp(target:symbolfile(), path.join(libdir, path.filename(target:symbolfile())))
end
if opt.headers then
_install_headers(target, opt)
end
end
-- install headeronly library
function _install_headeronly(target, opt)
if opt.headers then
_install_headers(target, opt)
end
end
-- install moduleonly library
function _install_moduleonly(target, opt)
if opt.headers then
_install_headers(target, opt)
end
end
function main(target, opt)
opt = opt or {}
if opt.headers == nil then
opt.headers = true
end
if opt.binaries == nil then
opt.binaries = true
end
if opt.libraries == nil then
opt.libraries = true
end
if opt.packages == nil then
opt.packages = true
end
local installdir = opt.installdir or target:installdir()
if not installdir then
wprint("please use `xmake install -o installdir` or `set_installdir` to set install directory.")
return
end
print("installing %s to %s ..", target:name(), installdir)
if target:is_binary() then
_install_binary(target, opt)
elseif target:is_shared() then
_install_shared(target, opt)
elseif target:is_static() then
_install_static(target, opt)
elseif target:is_headeronly() then
_install_headeronly(target, opt)
elseif target:is_moduleonly() then
_install_moduleonly(target, opt)
end
_install_files(target)
end
|