Repository navigation
Expand file tree
/
Copy pathpackage-linux-runtime.sh
More file actions
executable file
·362 lines (312 loc) · 7.88 KB
/
Copy pathpackage-linux-runtime.sh
File metadata and controls
executable file
·362 lines (312 loc) · 7.88 KB
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
#!/usr/bin/env bash
set -euo pipefail
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
BUILD_DIR="${PROJECT_ROOT}/vendor/mpv/buildout"
SOIA_UTILS_DIR="${PROJECT_ROOT}/vendor/soia_utils"
CONFIG_DATA_SRC="${PROJECT_ROOT}/vendor/config.data"
OUT_DIR="${PROJECT_ROOT}/release"
PKG_NAME=""
DEFAULT_PKG_NAME="libmpv-local-linux"
ORIGINAL_ARGC="$#"
BUILD_MACHINE="$(uname -m)"
case "$BUILD_MACHINE" in
arm64) FFMPEG_BUILD_ARCH="aarch64" ;;
*) FFMPEG_BUILD_ARCH="$BUILD_MACHINE" ;;
esac
FFMPEG_BUILD_NAME="${FFMPEG_BUILD_NAME:-linux-$FFMPEG_BUILD_ARCH}"
FFMPEG_PREFIX="${FFMPEG_PREFIX:-$PROJECT_ROOT/vendor/ffmpeg-build/$FFMPEG_BUILD_NAME}"
usage() {
cat <<'USAGE'
Usage:
bash ./package-linux-runtime.sh --pkg-name <name> [--build-dir <dir>] [--out-dir <dir>]
Build a self-contained Linux runtime package:
- libmpv shared libraries (.so*)
- all runtime shared library dependencies except core glibc/loader libs (recursive)
- runtime search path rewritten to $ORIGIN (when patchelf is available)
- SHA256 checksum
Defaults:
(no args) --pkg-name libmpv-local-linux
USAGE
}
while [ "$#" -gt 0 ]; do
case "$1" in
--build-dir)
BUILD_DIR="$2"
shift 2
;;
--out-dir)
OUT_DIR="$2"
shift 2
;;
--pkg-name)
PKG_NAME="$2"
shift 2
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage
exit 1
;;
esac
done
if [ -z "$PKG_NAME" ]; then
if [ "$ORIGINAL_ARGC" -eq 0 ]; then
PKG_NAME="$DEFAULT_PKG_NAME"
else
echo "--pkg-name is required" >&2
usage
exit 1
fi
fi
if [ ! -d "$BUILD_DIR" ]; then
echo "Build directory not found: $BUILD_DIR" >&2
exit 1
fi
if ! command -v readelf >/dev/null 2>&1; then
echo "readelf not found in PATH" >&2
exit 1
fi
LIB_DIR="$OUT_DIR/lib"
mkdir -p "$LIB_DIR"
rm -f "$LIB_DIR"/*
TMP_DIR="$(mktemp -d)"
VISITED_FILE="${TMP_DIR}/visited.txt"
touch "$VISITED_FILE"
trap 'rm -rf "$TMP_DIR"' EXIT
already_visited() {
grep -Fqx -- "$1" "$VISITED_FILE"
}
mark_visited() {
printf '%s\n' "$1" >> "$VISITED_FILE"
}
canonical_path() {
readlink -f "$1" 2>/dev/null || realpath "$1"
}
list_needed_deps() {
readelf -d "$1" 2>/dev/null | awk '
/\(NEEDED\)/ {
line=$0
sub(/^.*\[/, "", line)
sub(/\].*$/, "", line)
if (line != "") print line
}'
}
list_rpaths() {
readelf -d "$1" 2>/dev/null | awk -F'[][]' '
/\(RPATH\)|\(RUNPATH\)/ {
if ($2 != "") print $2
}'
}
is_system_dep_path() {
case "$(basename "$1")" in
linux-vdso.so.1)
return 0
;;
ld-linux-x86-64.so.2|ld-linux-aarch64.so.1|ld-linux.so.2)
return 0
;;
libc.so.6|libm.so.6|libdl.so.2|libpthread.so.0|librt.so.1|libresolv.so.2)
return 0
;;
libEGL.so.1)
return 0
;;
libnsl.so.1|libutil.so.1|libcrypt.so.1)
return 0
;;
*)
return 1
;;
esac
}
resolve_dep() {
local owner="$1"
local dep="$2"
local owner_dir candidate rpath expanded path_dir arch_dir
owner_dir="$(cd "$(dirname "$owner")" && pwd)"
arch_dir="$(uname -m)-linux-gnu"
case "$dep" in
/*)
[ -e "$dep" ] && { echo "$dep"; return 0; }
;;
esac
if [[ "$dep" == */* ]]; then
candidate="${owner_dir}/${dep}"
[ -e "$candidate" ] && { echo "$candidate"; return 0; }
fi
while IFS= read -r rpath; do
[ -n "$rpath" ] || continue
IFS=':' read -r -a _rpaths <<< "$rpath"
for expanded in "${_rpaths[@]}"; do
[ -n "$expanded" ] || continue
expanded="${expanded//\$\{ORIGIN\}/$owner_dir}"
expanded="${expanded//\$ORIGIN/$owner_dir}"
candidate="${expanded}/${dep}"
[ -e "$candidate" ] && { echo "$candidate"; return 0; }
done
done < <(list_rpaths "$owner")
for path_dir in \
"$owner_dir" \
"$LIB_DIR" \
"$BUILD_DIR" \
"$FFMPEG_PREFIX/lib" \
"/usr/local/lib" \
"/usr/lib/${arch_dir}" \
"/lib/${arch_dir}" \
"/usr/lib64" \
"/lib64" \
"/usr/lib" \
"/lib"; do
[ -n "$path_dir" ] || continue
candidate="${path_dir}/${dep}"
[ -e "$candidate" ] && { echo "$candidate"; return 0; }
done
OLD_IFS="$IFS"
IFS=':'
for path_dir in ${LD_LIBRARY_PATH:-}; do
[ -n "$path_dir" ] || continue
candidate="${path_dir}/${dep}"
if [ -e "$candidate" ]; then
IFS="$OLD_IFS"
echo "$candidate"
return 0
fi
done
IFS="$OLD_IFS"
return 1
}
scan_and_copy_deps() {
local owner="$1"
local owner_canonical dep resolved dep_name target
[ -e "$owner" ] || return 0
owner_canonical="$(canonical_path "$owner")"
if already_visited "$owner_canonical"; then
return 0
fi
mark_visited "$owner_canonical"
while IFS= read -r dep; do
[ -n "$dep" ] || continue
if is_system_dep_path "$dep"; then
continue
fi
if ! resolved="$(resolve_dep "$owner" "$dep")"; then
echo "Unresolved non-system dependency: $dep (owner: $owner)" >&2
return 1
fi
dep_name="$(basename "$dep")"
target="${LIB_DIR}/${dep_name}"
if [ ! -e "$target" ]; then
cp -vL "$resolved" "$target"
chmod u+w "$target" || true
fi
scan_and_copy_deps "$target"
done < <(list_needed_deps "$owner")
}
rewrite_rpath_if_possible() {
local file
if ! command -v patchelf >/dev/null 2>&1; then
echo "Warning: patchelf not found; skip RPATH rewrite to \$ORIGIN." >&2
return 0
fi
for file in "$LIB_DIR"/*.so*; do
[ -f "$file" ] || continue
[ -L "$file" ] && continue
patchelf --set-rpath '$ORIGIN' "$file"
done
}
strip_shared_libs_if_possible() {
local file
if ! command -v strip >/dev/null 2>&1; then
echo "Warning: strip not found; skip symbol stripping." >&2
return 0
fi
for file in "$LIB_DIR"/*.so*; do
[ -f "$file" ] || continue
[ -L "$file" ] && continue
strip --strip-unneeded "$file" 2>/dev/null || strip "$file" || true
done
}
verify_deps_resolved() {
local file dep dep_name
for file in "$LIB_DIR"/*.so*; do
[ -e "$file" ] || continue
while IFS= read -r dep; do
[ -n "$dep" ] || continue
if is_system_dep_path "$dep"; then
continue
fi
dep_name="$(basename "$dep")"
if [ -e "$LIB_DIR/$dep_name" ]; then
continue
fi
echo "Packaged dependency missing: $dep (owner: $file)" >&2
return 1
done < <(list_needed_deps "$file")
done
}
copy_root_mpv_libs() {
local found=0
local src
shopt -s nullglob
for src in "$BUILD_DIR"/libmpv*.so*; do
if [ -f "$src" ] || [ -L "$src" ]; then
cp -vP "$src" "$LIB_DIR/"
found=1
fi
done
if [ "$found" -eq 0 ]; then
echo "No libmpv*.so* found in $BUILD_DIR" >&2
exit 1
fi
}
copy_soia_utils_lib() {
local arch triple src
arch="$(uname -m)"
case "$arch" in
x86_64)
triple="x86_64-unknown-linux-gnu"
;;
aarch64|arm64)
triple="aarch64-unknown-linux-gnu"
;;
*)
echo "Unsupported Linux architecture for soia_utils: $arch" >&2
exit 1
;;
esac
src="${SOIA_UTILS_DIR}/${triple}/libsoia_utils.so"
if [ ! -f "$src" ]; then
echo "soia_utils shared library not found for ${triple}: $src" >&2
exit 1
fi
cp -vP "$src" "$LIB_DIR/"
}
copy_config_data() {
if [ ! -f "$CONFIG_DATA_SRC" ]; then
echo "Required config file not found: $CONFIG_DATA_SRC" >&2
exit 1
fi
mkdir -p "$LIB_DIR"
cp -vP "$CONFIG_DATA_SRC" "${LIB_DIR}/config.data"
}
echo "Preparing Linux runtime bundle from: $BUILD_DIR"
copy_root_mpv_libs
copy_soia_utils_lib
copy_config_data
for file in "$LIB_DIR"/libmpv*.so* "$LIB_DIR"/libsoia_utils*.so*; do
[ -e "$file" ] || continue
scan_and_copy_deps "$file"
done
if [ -n "${CI:-}" ]; then
rewrite_rpath_if_possible
strip_shared_libs_if_possible
verify_deps_resolved
tar -czf "${PKG_NAME}.tar.gz" -C "$OUT_DIR" .
sha256sum "${PKG_NAME}.tar.gz" > "${PKG_NAME}.tar.gz.sha256"
echo "Created package: ${PKG_NAME}.tar.gz"
echo "Created checksum: ${PKG_NAME}.tar.gz.sha256"
fi