aboutsummaryrefslogtreecommitdiffstats
path: root/dist/macos/bundle/reref_dylibs.sh
blob: 15191bd278267a8bea8f052df195b7b1b4db82a8 (plain) (blame)
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
#!/bin/sh

# $1 = binary (program or dylib)
B_TARGET=$1
if [ "x$B_TARGET" = "x" ]; then
    echo Which binary needs to be re-referenced?
    exit 1
fi

cd $(dirname $B_TARGET) || exit 1

# where to find non-system libraries relative to target's directory.
# the vast majority of the time this should be empty
B_REL_PATH=$2

# we're in target's directory now. trim off the path
B_TARGET=$(basename $B_TARGET)

# get a list of non-system libraries and make local copies
B_LIBS=$(otool -XL $B_TARGET | awk '{ print $1 }' | grep -Ev '^(/usr/lib|/System)')
[ $? -ne 0 ] && exit 1
for B_LIB in $B_LIBS; do
    B_LIB_NAME=$(basename $B_LIB)

    # ignore self-references
    [ "$B_TARGET" = "$B_LIB_NAME" ] && continue

    B_DST=${B_REL_PATH}${B_LIB_NAME}
    if [ ! -e $B_DST ]; then
        cp $B_LIB $B_DST || exit 1
        chmod u+rw $B_DST || exit 1
        # recursively call this script on libraries purposefully not passing
        # $B_REL_PATH so that it is only used explicitly
        $0 $B_DST
    fi

    # adjust the target's metadata to point to the local copy
    # rather than the system-wide copy which would only exist on
    # a development machine
    install_name_tool -change $B_LIB @loader_path/$B_DST  $B_TARGET || exit 1
done