forked from xonotic/netradiant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
library-bundler
executable file
·528 lines (456 loc) · 10.2 KB
/
library-bundler
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#! /usr/bin/env bash
set -e
export LANG='C.UTF-8'
export LANGUAGE="${LANG}"
_sed () {
case "${system_name}" in
'macos'|'freebsd')
gsed "${@}"
;;
*)
sed "${@}"
;;
esac
}
_cp () {
case "${system_name}" in
'macos'|'freebsd')
gcp -R --preserve=timestamps -H -L "${1}" "${2}"
;;
*)
cp -R --preserve=timestamps -H -L "${1}" "${2}"
;;
esac
}
Common::noOp () {
true
}
Common::getPath () {
local file_path="${1}"
if command -v cygpath >/dev/null
then
if [ "${file_path}" = '-' ]
then
tr '\n' '\0' \
| xargs -0 -P1 -I{} \
cygpath --unix '{}'
else
cygpath --unix "${file_path}"
fi
else
if [ "${file_path}" = '-' ]
then
cat
else
printf '%s\n' "${file_path}"
fi
fi \
| _sed -e 's|/*$||'
}
Common::grepLdd () {
case "${system_name}" in
'macos')
egrep '^\t/'
;;
*)
egrep ' => '
;;
esac
}
Common::stripLdd () {
case "${system_name}" in
'macos')
_sed -e 's/^\t\(.*\) (compatibility version .*/\1/'
;;
*)
_sed -e 's/ (0x[0-9a-f]*)$//;s/^.* => //'
;;
esac
}
Multi::excludeLdd () {
case "${system_name}" in
'linux'|'freebsd')
# - always bundle built-in libraries
# - always rely on up-to-date x11 and gl libraries, bundling them will break on future distros
# - gtk is not easily bundlable on linux because it looks for harcoded system path to optional
# shared libraries like image codecs, theme engines, sound notification system, etc.
# so expect user to install gtk first
# - since we ask user to instal gtk, we can also ask them to install gtkglext,
# which is likely to pull gtk itself, x11 and gl dependencies
# - old fontconfig does not work correctly if newer fontconfig configuration is installed
# - if gtk and fontconfig is installed, pango and freetype are
local ldd_line
while read ldd_line
do
if echo "${ldd_line}" | egrep '/builtins/'
then
echo "${ldd_line}"
elif echo "${ldd_line}" \
| egrep -q '/libc\.|/libstdc\+\+\.|/libdl\.|/libm\.|/libX|/libxcb|/libGL|/libICE\.|/libSM\.|/libpthread\.'
then
Common::noOp
elif echo "${ldd_line}" \
| egrep -q '/libatk|/libgdk|/libgtk|/libgio|/libglib|/libgmodule|/libgobject|/libcairo|/libpango|/libfontconfig|/libfreetype'
then
Common::noOp
# FreeBSD specific
elif echo "${ldd_line}" \
| egrep -q '/libc\+\+|/libgxxrt'
then
Common::noOp
else
echo "${ldd_line}"
fi
done
;;
'windows')
egrep -i '\.dll => [A-Z]:\\msys64\\'
;;
'macos')
egrep -v '^\t/System/|^\t/usr/lib/'
;;
esac
}
Multi::filterLib () {
Common::grepLdd \
| Multi::excludeLdd \
| Common::stripLdd \
| Common::getPath -
}
Multi::printLdd () {
local exe_file="${1}"
case "${system_name}" in
'linux'|'freebsd')
ldd "${exe_file}"
;;
'windows')
ntldd --recursive "${exe_file}"
;;
'macos')
otool -L "${exe_file}"
esac
}
Multi::getGtkThemeName () {
case "${system_name}" in
'linux'|'freebsd')
echo 'Adwaita'
;;
'windows')
echo 'MS-Windows'
;;
*)
echo 'Raleigh'
;;
esac
}
Multi::getGtkLibName () {
case "${system_name}" in
'linux'|'freebsd')
echo 'libgtk-x11-2.0.so.0'
;;
'windows')
echo 'libgtk-win32-2.0-0.dll'
;;
'macos')
echo 'libgtk-quartz-2.0.0.dylib'
;;
esac
}
Multi::getRootPrefix () {
local lib_file="${1}"
case "${system_name}" in
'linux')
echo 'usr'
;;
'freebsd'|'macos')
echo 'usr/local'
;;
'windows')
basename "${lib_file}" \
| xargs -n1 -P1 which \
| cut -f2 -d'/'
;;
esac
}
Multi::getLibPrefix () {
local lib_file="${1}"
case "${system_name}" in
'linux'|'freebsd')
dirname "${lib_file}" \
| cut -f3- -d'/'
;;
'windows')
echo 'lib'
;;
'macos')
echo 'lib'
;;
esac
}
Multi::getGtkDeps () {
local lib_prefix="${1}"
local gtk_theme_name="${2}"
case "${system_name}" in
'linux'|'freebsd'|'windows')
cat <<-EOF
share/themes/${gtk_theme_name}/gtk-2.0
share/icons/hicolor
${lib_prefix}/gdk-pixbuf-2.0
${lib_prefix}/gtk-2.0
EOF
;;
'macos')
cat <<-EOF
etc/fonts
share/themes/${gtk_theme_name}/gtk-2.0
share/fontconfig
share/icons/hicolor
share/locale
${lib_prefix}/gdk-pixbuf-2.0
${lib_prefix}/gtk-2.0
EOF
;;
esac
case "${system_name}" in
'linux'|'freebsd')
cat <<-EOF
${lib_prefix}/libatk-bridge-2.0.so.0
${lib_prefix}/libcanberra-0.30
${lib_prefix}/libcanberra.so.0
${lib_prefix}/libcanberra-gtk.so.0
EOF
;;
esac
}
Multi::rewriteLoadersCache () {
local bundle_component_path="${1}"
local cache_file
find "${bundle_component_path}" \
-type f \
\( \
-name 'loaders.cache' \
-o -name 'immodules.cache' \
\) \
| while read cache_file
do
_sed \
-e 's|^"/[^"]*/lib/|"lib/|;s| "/[^"]*/share/| "share/|;/^# ModulesPath = /d;/^# Created by /d;/^#$/d' \
-i "${cache_file}"
done
}
Multi::bundleGtkDepsFromFile () {
local lib_file="${1}"
local component_dir
local real_component_dir
local bundle_component_dir
lib_basename="$(basename "${lib_file}")"
gtk_lib_name="$(Multi::getGtkLibName)"
if [ "${lib_basename}" = "${gtk_lib_name}" ]
then
root_prefix="$(Multi::getRootPrefix "${lib_file}")"
lib_prefix="$(Multi::getLibPrefix "${lib_file}")"
gtk_theme_name="$(Multi::getGtkThemeName)"
for component_dir in $(Multi::getGtkDeps "${lib_prefix}" "${gtk_theme_name}")
do
bundle_component_dir="$(echo "${component_dir}" | _sed -e 's|^'"${lib_prefix}"'|lib|')"
if ! [ -e "${bundle_dir}/${bundle_component_dir}" ]
then
real_component_dir="$(realpath "/${root_prefix}/${component_dir}")"
mkdir -p "${bundle_dir}/$(dirname "${bundle_component_dir}")"
_cp \
"${real_component_dir}" \
"${bundle_dir}/${bundle_component_dir}"
Multi::rewriteLoadersCache "${bundle_dir}/${bundle_component_dir}"
fi
done
fi
}
Multi::bundleLibFromFile () {
local exe_file="${1}"
local lib_file
Multi::printLdd "${exe_file}" \
| Multi::filterLib \
| while read lib_file
do
if [ "${lib_file}" = 'not found' ]
then
printf 'ERROR: library not found while bundling %s (but link worked)\n' "${exe_file}" >&2
Multi::printLdd "${exe_file}" | grep 'not found'
exit 1
fi
lib_basename="$(basename "${lib_file}")"
if [ -f "${lib_dir}/${lib_basename}" ]
then
continue
fi
_cp \
"${lib_file}" \
"${lib_dir}/${lib_basename}"
Multi::bundleGtkDepsFromFile "${lib_file}"
case "${system_name}" in
'macos')
Multi::bundleLibFromFile "${lib_file}"
;;
esac
done
}
Multi::cleanUp () {
# Remove from bundle things that useless to be distributed,
# like headers or static libraries, also remove
# empty directories.
find "${bundle_dir}/lib" \
-type f \
-name '*.a' \
-exec rm -f {} \;
find "${bundle_dir}/lib" \
-type f \
-name '*.h' \
-exec rm -f {} \;
find "${bundle_dir}/lib" \
-depth \
-type d \
-empty \
-exec rmdir {} \;
}
Linux::getRpath () {
local exe_file="${1}"
local exe_dir="$(dirname "${exe_file}")"
local path_start="$(printf '%s' "${bundle_dir}" | wc -c)"
path_start="$((${path_start} + 1))"
local exe_subdir="$(echo "${exe_dir}" | cut -c "${path_start}-" | _sed -e 's|//*|/|;s|^/||')"
local rpath_origin='$ORIGIN'
if [ "${exe_subdir}" = '' ]
then
printf '%s/lib\n' "${rpath_origin}"
else
if [ "${exe_subdir}" = 'lib' ]
then
printf '%s\n' "${rpath_origin}"
else
local num_parent_dir="$(echo "${exe_subdir}" | tr '/' '\n' | wc -l)"
local rpath_subdir
local i=0
while [ "${i}" -lt "${num_parent_dir}" ]
do
rpath_subdir="${rpath_subdir}/.."
i="$((${i} + 1))"
done
printf '%s%s/lib\n' "${rpath_origin}" "${rpath_subdir}"
fi
fi
}
Linux::patchExe () {
local exe_file="${1}"
local linux_rpath_string=$"$(Linux::getRpath "${exe_file}")"
chmod u+w,go-w "${exe_file}"
patchelf --set-rpath "${linux_rpath_string}" "${exe_file}"
}
Linux::patchLib () {
local lib_dir="${1}"
local exe_file
find "${lib_dir}" \
-type f \
-name '*.so*' \
| while read exe_file
do
Linux::patchExe "${exe_file}"
chmod ugo-x "${exe_file}"
done
}
Darwin::patchExe () {
local exe_file="${1}"
Multi::printLdd "${exe_file}" \
| Multi::filterLib \
| while read lib_file
do
new_path="$(echo "${lib_file}" | _sed -e 's|^/.*/lib/|@executable_path/lib/|')"
id_name="$(echo "${lib_file}" | _sed -e 's|.*/||g')"
chmod u+w,go-w "${exe_file}"
install_name_tool -change "${lib_file}" "${new_path}" "${exe_file}"
install_name_tool -id "${id_name}" "${exe_file}"
done
}
Darwin::patchLib () {
local lib_dir="${1}"
local exe_file
find "${lib_dir}" \
-type f \
\( \
-name '*.dylib' \
-o -name '*.so' \
\) \
| while read exe_file
do
Darwin::patchExe "${exe_file}"
chmod ugo-x "${exe_file}"
done
}
Windows::listLibForManifest () {
local lib_dir="${1}"
find "${lib_dir}" \
-maxdepth 1 \
-type f \
-name '*.dll' \
-exec basename {} \; \
| tr '\n' '\0' \
| xargs -0 -P1 -I{} \
printf ' <file name="{}"/>\n'
}
Windows::writeManifest () {
local lib_dir="${1}"
cat > "${manifest_file}" <<-EOF
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity type="win32" name="lib" version="1.0.0.0"/>
$(Windows::listLibForManifest "${lib_dir}")
</assembly>
EOF
}
system_name="${1}"; shift
bundle_dir="${1}"; shift
if ! [ -z "${1}" ]
then
exe_file="${1}"; shift
fi
bundle_dir="$(Common::getPath "${bundle_dir}")"
registry_dir="${bundle_dir}/registry"
lib_dir="${bundle_dir}/lib"
manifest_file="${lib_dir}/lib.manifest"
exe_action='Common::noOp'
lib_action='Common::noOp'
case "${system_name}" in
'register')
mkdir -p "${registry_dir}"
Common::getPath "${exe_file}" > "${registry_dir}/$(uuidgen)"
exit
;;
'linux'|'freebsd')
exe_action='Linux::patchExe'
lib_action='Linux::patchLib'
;;
'windows')
lib_action='Windows::writeManifest'
;;
'macos')
exe_action='Darwin::patchExe'
lib_action='Darwin::patchLib'
;;
*)
printf 'ERROR: unsupported system: %s\n' "${system_name}" >&2
exit 1
;;
esac
mkdir -p "${lib_dir}"
if [ -d "${registry_dir}" ]
then
for registry_entry in "${registry_dir}"/*
do
exe_file="$(cat "${registry_entry}")"
Multi::bundleLibFromFile "${exe_file}"
"${exe_action}" "${exe_file}"
rm "${registry_entry}"
"${exe_action}" "${exe_file}"
done
rmdir "${registry_dir}"
fi
"${lib_action}" "${lib_dir}"
Multi::cleanUp