1 | #!/bin/bash |
---|
2 | |
---|
3 | UNAME=$(uname) |
---|
4 | |
---|
5 | logrun() { |
---|
6 | echo "$@" |
---|
7 | "$@" |
---|
8 | } |
---|
9 | |
---|
10 | get_rpaths_Darwin() { |
---|
11 | otool -l "$1" | grep LC_RPATH -A2 |grep path | awk '{print $2}' |
---|
12 | } |
---|
13 | get_rpaths_Linux() { |
---|
14 | patchelf --print-rpath "$1" | tr : '\n' | sed 's|$ORIGIN|'$ORIGIN'|' |
---|
15 | } |
---|
16 | get_rpaths() { |
---|
17 | get_rpaths_$UNAME "$@" |
---|
18 | } |
---|
19 | |
---|
20 | get_needed_Darwin() { |
---|
21 | otool -L "$1" |grep -v CoreFoundation | tail -n +2 | awk '{print $1}' |
---|
22 | } |
---|
23 | get_needed_Linux() { |
---|
24 | patchelf --print-needed "$1" |
---|
25 | } |
---|
26 | get_needed() { |
---|
27 | get_needed_$UNAME "$@" |
---|
28 | } |
---|
29 | |
---|
30 | remove_rpaths_Darwin() { |
---|
31 | for rpath in $(get_rpaths_Darwin $1); do |
---|
32 | logrun install_name_tool -delete_rpath $rpath "$1" |
---|
33 | done |
---|
34 | } |
---|
35 | remove_rpaths_Linux() { |
---|
36 | logrun patchelf --remove-rpath "$1" |
---|
37 | } |
---|
38 | remove_rpaths() { |
---|
39 | remove_rpaths_$UNAME "$@" |
---|
40 | } |
---|
41 | |
---|
42 | set_rpath_Darwin() { |
---|
43 | remove_rpaths_Darwin "$1" |
---|
44 | logrun install_name_tool -add_rpath "$2" "$1" |
---|
45 | } |
---|
46 | set_rpath_Linux() { |
---|
47 | logrun patchelf --set-rpath "$2" "$1" |
---|
48 | } |
---|
49 | set_rpath() { |
---|
50 | set_rpath_$UNAME "$@" |
---|
51 | } |
---|
52 | |
---|
53 | copy_libs_needed() { |
---|
54 | echo "--- Checking required libs for $1 ---" |
---|
55 | ORIGIN=$(dirname $1) |
---|
56 | rpaths=$(get_rpaths $1) |
---|
57 | self=$(echo ${1##*/} | sed 's/\+/\\\\+/g') |
---|
58 | dtneed=$(get_needed $1 | grep -vE "^(/usr/lib|/System)|$self" || true) |
---|
59 | dest=$(realpath $(dirname $1)/../lib) |
---|
60 | recurse= |
---|
61 | for lib in $dtneed; do |
---|
62 | #echo " checking for $lib" |
---|
63 | name="${lib#@rpath/}" |
---|
64 | if test -e "$dest/$name"; then |
---|
65 | continue; |
---|
66 | fi |
---|
67 | source= |
---|
68 | for path in "" $rpaths $LDPATHS; do |
---|
69 | if test -e "$path/$name"; then |
---|
70 | source="$path/$name" |
---|
71 | break; |
---|
72 | fi |
---|
73 | done |
---|
74 | if test -z "$source"; then |
---|
75 | echo Missing $name |
---|
76 | else |
---|
77 | mkdir -pv "$(dirname $dest/$name)" |
---|
78 | cp -v $source $dest/$name |
---|
79 | recurse="$recurse $dest/$name" |
---|
80 | fi |
---|
81 | done |
---|
82 | if [ -n "$recurse" ]; then |
---|
83 | echo "--- Recursing libs added for $1 --" |
---|
84 | for lib in $recurse; do |
---|
85 | copy_libs_needed $lib |
---|
86 | done |
---|
87 | echo "--- Recursion finished" |
---|
88 | fi |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | make_rpath_relative_Darwin() { |
---|
93 | set_rpath_Darwin "$1" @loader_path/../lib |
---|
94 | } |
---|
95 | make_rpath_relative_Linux() { |
---|
96 | set_rpath_Linux "$1" '$ORIGIN/../lib/arb/lib:$ORIGIN/../lib' |
---|
97 | } |
---|
98 | make_rpath_relative() { |
---|
99 | echo "--- Making rpath relative to binary $1 ---" |
---|
100 | make_rpath_relative_$UNAME "$@" |
---|
101 | } |
---|
102 | |
---|
103 | make_absolute_path_relative_Darwin() { |
---|
104 | echo "--- Checking for non-relative DLL references in $1 ---" |
---|
105 | for lib in $(get_needed_Darwin $1); do |
---|
106 | echo -ne "- checking lib $lib\t" |
---|
107 | if test ${lib:0:1} = "/"; then |
---|
108 | name=$(basename $lib) |
---|
109 | dir="$(dirname $lib)" |
---|
110 | if test "$dir" != "/usr/lib"; then |
---|
111 | echo "[FIXING]" |
---|
112 | logrun install_name_tool -change $lib @rpath/$name -add_rpath $dir "$1" |
---|
113 | else |
---|
114 | echo "(system) [OK]" |
---|
115 | fi |
---|
116 | else |
---|
117 | echo "[OK]" |
---|
118 | fi |
---|
119 | done |
---|
120 | } |
---|
121 | make_absolute_path_relative_Linux() { |
---|
122 | : |
---|
123 | } |
---|
124 | make_absolute_path_relative() { |
---|
125 | make_absolute_path_relative_$UNAME "$@" |
---|
126 | } |
---|
127 | |
---|
128 | fix_libs() { |
---|
129 | make_absolute_path_relative "$1" |
---|
130 | copy_libs_needed "$1" |
---|
131 | make_rpath_relative "$1" |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | if test "$0" != "-bash"; then |
---|
136 | set -e |
---|
137 | if test -n "$1"; then |
---|
138 | fix_libs "$1" |
---|
139 | fi |
---|
140 | set +e |
---|
141 | fi |
---|