-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle-dylibs.sh
executable file
·97 lines (82 loc) · 2.28 KB
/
bundle-dylibs.sh
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
#!/bin/zsh
# Copyright (c) 2021 Imre Horvath <imi [dot] horvath [at] gmail [dot] com>
# MIT License
# Should work with /bin/sh or /bin/bash too.
# Inspired by:
# https://github.com/renard/emacs-build-macosx/blob/master/build-emacs
# https://github.com/trojanfoe/xcodedevtools/blob/master/copy_dylibs.py
set -e
relpath="../Frameworks" # dylibs go to @executable_path/../Frameworks by default
usage_text="Usage: $(basename $0) [-h] [-l relative_path] path_to_app
-h Show this usage description and exit.
-l relative_path
Specifies, where to put the bundled dylibs relative to the executable.
When omitted, defaults to: \"$relpath\"."
usage() {
echo "$usage_text" 1>&2
exit ${1:-1}
}
while getopts ':hl:' opt; do
case "$opt" in
h)
usage 0
;;
l)
relpath="$OPTARG"
;;
\?)
echo "Invalid option -$OPTARG" 1>&2
usage
;;
esac
done
shift $((OPTIND-1))
[[ $# -eq 1 ]] || usage
bundlepath="$1"
libdir="$bundlepath/Contents/MacOS/$relpath"
list_dylibs() {
local file="$1"
echo $(otool -L "$file" |\
sed -n 's/^[[:space:]]*\(.*\) (.*$/\1/p' |\
sed -e '/^\/System/d' -e '/^\/usr\/lib/d' -e '/^@/d')
}
process_dylibs() {
local dylibs="$1"
local file="$2"
local dylib
local name
local dest
for dylib in $(echo $dylibs); do
name="$(basename $dylib)"
dest="$libdir/$name"
if [[ ! -e $dest ]]; then
cp "$dylib" "$dest"
chmod 644 "$dest"
install_name_tool -id "@rpath/$name" "$dest"
process_dylibs "$(list_dylibs $dest)" "$dest"
fi
install_name_tool -change "$dylib" "@rpath/$name" "$file"
done
}
process_executable() {
local executable="$1"
local dylibs="$(list_dylibs $executable)"
if [[ -n $dylibs ]]; then
mkdir -p "$libdir"
process_dylibs "$dylibs" "$executable"
install_name_tool -add_rpath "@executable_path/$relpath" "$executable"
fi
}
if [[ $(basename $bundlepath) =~ [.]app$ ]] &&
[[ -f $bundlepath/Contents/Info.plist ]] &&
[[ -d $bundlepath/Contents/MacOS ]]; then
for executable in $(find "$bundlepath/Contents/MacOS" -type f -perm +111); do
if [[ $(file $executable) =~ Mach-O.*executable ]]; then
process_executable "$executable"
fi
done
else
echo "\"$bundlepath\" does not apper to be an application bundle." 1>&2
exit 1
fi
exit 0