This repository has been archived by the owner on Apr 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
alien-fix-mac.sh
executable file
·116 lines (91 loc) · 2.22 KB
/
alien-fix-mac.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/bin/bash
#
# Global variables
#
export LOG="$HOME/alien-relocation.log"
export COUNT_ERR=0
export COUNT_TOT=0
#
# Functions
#
# Log facility
function Log() {
let COUNT_TOT++
if [ $2 == 0 ]; then
STAT=" OK "
else
STAT="FAIL"
let COUNT_ERR++
fi
echo "[$STAT] $1"
echo "[$STAT] $1" >> $LOG
}
# Symlink to .so and relocation
function ProcessLib() {
local LIBNAME LIBDIR LIBID POSTFIX OWD S
LIBNAME="$1"
LIBDIR=`dirname $LIBNAME`
LIBNAME=`basename $LIBNAME`
OWD=`pwd`
cd "$LIBDIR"
# Symbolic links
ln -nfs "$LIBNAME."dylib "$LIBNAME."so
Log "Symlink of $LIBDIR/$LIBNAME.dylib to $LIBNAME.so" $?
# Perms
chmod 0644 "$LIBDIR/$LIBNAME."dylib
# If the library is a symlink, skip it
#if [ -L "$LIBDIR/$LIBNAME."dylib ]; then
# return
#fi
# Change library id (first output of otool -L, or otool -D)
LIBID=`otool -D "$LIBNAME."dylib | grep -v ':$'`
if [[ "$LIBID" =~ ^/opt/alien/(.*) ]]; then
POSTFIX="${BASH_REMATCH[1]}"
install_name_tool -id "$ALIEN_DIR/$POSTFIX" "$LIBNAME."dylib 2> /dev/null
Log "Change library ID of $LIBDIR/$LIBNAME.dylib from $LIBID to $ALIEN_DIR/$POSTFIX" $?
fi
# Change hardcoded library paths (from otool -L)
otool -L "$LIBNAME."dylib | grep -v ':$' | cut -f2 | cut -f1 -d' ' |
while read S
do
if [ ! -e "$S" ]; then
if [[ "$S" =~ ^/opt/alien/(.*) ]]; then
POSTFIX="${BASH_REMATCH[1]}"
install_name_tool -change "$S" "$ALIEN_DIR/$POSTFIX" "$LIBNAME."dylib 2> /dev/null
Log "Change library dependency of $LIBDIR/$LIBNAME.dylib from $S to $ALIEN_DIR/$POSTFIX" $?
fi
fi
done
cd "$OWD"
}
# Main function
function Main() {
local R S LIBNAME LIBDIR OWD
if [ "$ALIEN_DIR" == "" ]; then
echo "\$ALIEN_DIR envvar not set"
exit 1
fi
ALIEN_TEMP=`mktemp /tmp/alien_dylib_temp_XXXXXX`
OWD=`pwd`
cd "$ALIEN_DIR"
find . -name '*.dylib' > $ALIEN_TEMP
cd "$OWD"
while read R
do
R=$ALIEN_DIR/${R:2}
if [[ "$R" =~ ^(.*).dylib$ ]]; then
LIBNAME=${BASH_REMATCH[1]}
ProcessLib "$LIBNAME"
fi
done < $ALIEN_TEMP
rm $ALIEN_TEMP
echo ""
echo "Actions done: $COUNT_TOT"
echo "Errors: $COUNT_ERR"
echo ""
echo "Output saved on $LOG"
}
#
# Entry point
#
Main "$@"