-
Notifications
You must be signed in to change notification settings - Fork 6
/
macos-relative-dylibs.sh
executable file
·44 lines (34 loc) · 1.24 KB
/
macos-relative-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
#!/bin/bash -eu
process_binary() {
local binary="$1"
echo "[*] Processing $binary..."
# Find all linked libraries from the Homebrew prefix
local libraries=(
$(otool -L "$binary" | grep '/usr/local/opt/' | awk '{print $1}')
$(otool -L "$binary" | grep '/opt/homebrew/' | awk '{print $1}')
)
# If this is a dylib, the first entry is the ID of the library itself, which
# we don't really care about.
if [ "${binary##*.}" = "dylib" ]; then
libraries=("${libraries[@]:1}")
fi
# When empty, skip the loop to avoid a Bash unbound variable error
if [ ${#libraries[@]} -le 0 ]; then
return
fi
for lib in "${libraries[@]}"; do
local lib_basename=$(basename "$lib")
# Check that we have a copy of each linked library
if [ ! -f "$lib_basename" ]; then
echo " [-] Failed to find linked library $lib_basename"
continue
fi
# Change the path to be relative
install_name_tool -change "$lib" "@executable_path/$lib_basename" \
"$binary"
echo " [+] Made relative link to $lib_basename"
done
}
for binary in *.dylib $(find . -type f -perm +111); do
process_binary "$binary"
done