forked from NeoGeographyToolkit/BinaryBuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
funcs.sh
executable file
·278 lines (236 loc) · 8.11 KB
/
funcs.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/bin/bash
DARWIN_WHITELIST=(
/System/Library/Frameworks/AGL.framework/Versions/A/AGL
/System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices
/System/Library/Frameworks/Carbon.framework/Versions/A/Carbon
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
/System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices
/System/Library/Frameworks/GLUT.framework/Versions/A/GLUT
/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL
/System/Library/Frameworks/vecLib.framework/Versions/A/vecLib
/System/Library/Frameworks/QuickTime.framework/Versions/A/QuickTime
/System/Library/Frameworks/Accelerate.framework/Versions/A/Accelerate
/System/Library/Frameworks/AppKit.framework/Versions/C/AppKit
/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa
/usr/lib/libobjc.A.dylib
/usr/lib/libSystem.B.dylib
/usr/lib/libgcc_s.1.dylib
/usr/lib/libstdc++.6.dylib
)
is_whitelist()
{
local elem="$1"
shift 1
local i
for i in "${DARWIN_WHITELIST[@]}"; do
if [[ $i = $elem ]]; then
return 0
fi
done
return 1
}
is_framework()
{
echo $1 | grep -q '\.framework/'
}
getOS() {
echo ${__OS:="$(uname -s | sed -e 's/Darwin/OSX/')"}
}
# Die from any subshell. Make sure you set $self before calling this.
die() {
echo "$1" >&2
kill -s SIGTERM $self
}
# Magic here:
# In linux, $ORIGIN means (to the dynamic loader)
# "Path to the thing currently being loaded" (so, either to the bin or to the lib)
# It is the dirname of that path.
# Therefore, construct an rpath made up of the origin, the path to the root of
# the dist, and then the given list of rpaths, which should be relative to the
# root of the dist.
set_rpath_linux() {
local file="$1"
local root="$2"
shift 2
local rpath elt
for elt in "$@"; do
rpath="${rpath}${rpath:+:}\$ORIGIN/${root}${elt}"
done
chrpath -r "$rpath" "$file" &>/dev/null || die "chrpath failed"
}
# Magic here:
# much like Linux's $ORIGIN, @executable_path is a runtime-determined value...
# except that it always points to $(dirname BINARY) that caused the load, not the
# library (if it is one)
set_rpath_darwin() {
local file="$1"
local root="$2"
shift 2
local dir="$(dirname $file)"
# This is the library's name for itself (equiv to soname in linux, except
# it has full path)
local myname=$(otool -D $file | tail -n1)
# Skip the first line of otool, which is the header
otool -L $file | awk 'NR > 1 {print $1}' | while read entry; do
# Don't warn me about things I know I'm skipping
if is_whitelist $entry; then
continue
fi
# /tmp/build/install/lib/libvwCore.5.dylib
# base = libvwCore.5.dylib
# looks for @executable_path/../lib/libvwCore.5.dylib
# /opt/local/libexec/qt4-mac/lib/QtXml.framework/Versions/4/QtXml
# base = QtXml.framework/Versions/4/QtXml
# looks for @executable_path/../lib/QtXml.framework/Versions/4/QtXml
local base=""
if is_framework $entry; then
local fprefix="$(dirname ${entry%%.framework/*})"
base="${entry#${fprefix}/}"
else
base="$(basename $entry)"
fi
# OSX rpath points to one specific file, not anything that matches the
# library SONAME. Therefore, do the library discovery right now, rather
# than delaying until runtime like in Linux
local new=""
for rpath in "$@"; do
if [[ -r "${dir}/${root}/$rpath/$base" ]]; then
# This code assumes that the binaries are installed at $DISTDIR/bin
new="@executable_path/../$rpath/$base"
fi
done
if [[ -n $new ]]; then
# If the entry is the "self" one, it has to be changed differently
if [[ "$entry" = "$myname" ]]; then
install_name_tool -id $new $file || die "FAILED: install_name_tool -id $new $file"
else
install_name_tool -change $entry $new $file || die "FAILED: install_name_tool -change $entry $new $file"
fi
else
echo "ERROR: Skipped $file: $entry"
fi
done
}
# This takes 2 arguments:
# The first is the file (binary or library) to set rpath on
# The second is the path from $(dirname $file) to the root of the dist
set_rpath() {
case $(getOS) in
Linux) set_rpath_linux $* ;;
OSX) set_rpath_darwin $* ;;
*) die "Unknown OS: $(getOS)"
esac
}
do_strip_darwin() {
local file="$1"
strip -S $file
}
STRIP_FLAGS_SAFE="--strip-unneeded"
STRIP_FLAGS="${STRIP_FLAGS_SAFE} -R .comment"
inode_var_name() {
stat -c 'INODE_%d_%i' "$1"
}
#save_elf_sources() {
# type -P debugedit >/dev/null || return 0
#
# local file="$1"
# local inode=$(inode_var_name "$x")
# [[ -n ${!inode} ]] && return 0
# debugedit -b "${WORKDIR}" -d "${prepstrip_sources_dir}" \
# -l "${T}"/debug.sources "${x}"
#}
unset ${!INODE_*}
save_elf_debug() {
local file="$1"
local debug="$(dirname $file)/$(basename $file).debug"
# dont save debug info twice
[[ $file == *".debug" ]] && return 0
local inode=$(inode_var_name "$file")
if [[ -n ${!inode} ]] ; then
ln "$(dirname $debug)/${!inode}.debug" "$debug"
else
eval $inode=\$file
objcopy --only-keep-debug "$file" "$debug"
objcopy --add-gnu-debuglink="$debug" "$file"
fi
}
do_strip_linux() {
local file="$1"
# Okay, first, try to save debug information!
local type=$(file "$file")
if [[ -z $type ]]; then
strip -g "$file"
elif [[ $type == *"current ar archive"* ]] ; then
strip -g "$file"
elif [[ $type == *"SB executable"* || $type == *"SB shared object"* ]] ; then
#save_elf_sources "$file"
save_elf_debug "$file"
strip $STRIP_FLAGS "$file"
elif [[ $type == *"SB relocatable"* ]] ; then
#save_elf_sources "$file"
strip $STRIP_FLAGS_SAFE "$file"
fi
}
# This takes 1 argument: the binary or library to strip
do_strip() {
case $(getOS) in
Linux) do_strip_linux $* ;;
OSX) do_strip_darwin $* ;;
*) die "Unknown OS: $(getOS)"
esac
}
get_relative_path() {
local root="$1"
local path="$2"
root="$(cd $root && pwd)"
path="$(cd $(dirname $path) && pwd)"
if [[ -z $root ]] || [[ -z $path ]]; then
echo "root and path must exist" >&2
return 1
fi
# if $root isprefixof $path
if [[ ${path##$root} == $path ]]; then
echo "Path is not inside root" >&2
return 1
fi
local ret
while [[ $path != / ]]; do
if [[ $path == $root ]]; then
break
fi
path="$(dirname $path)"
ret="../$ret"
done
echo $ret
}
# Keep this in sync with the function in libexec-funcs.sh
isis_version() {
local ROOT="${1:-$ISISROOT}"
local ISIS_HEADER="${ROOT}/version"
if test -s ${ISIS_HEADER}; then
local version="$(head -1 < $ISIS_HEADER | sed 's/\([0-9]*\.[0-9]*\.[0-9]*\).*/\1/')"
else
local ISIS_HEADER="${ROOT}/src/base/objs/Constants/Constants.h"
local version="$(grep version $ISIS_HEADER 2>/dev/null | cut -d\" -f2)"
fi
if test -z "${version}"; then
msg "Unable to locate ISIS version header."
msg "Expected it at $ISIS_HEADER"
die "Perhaps your ISISROOT ($ROOT) is incorrect?"
fi
echo "$version"
}
# Keep this in sync with the function in libexec-funcs.sh
libc_version() {
locations=('/lib/x86_64-linux-gnu/libc.so.6' '/lib/i386-linux-gnu/libc.so.6' '/lib/i686-linux-gnu/libc.so.6' '/lib/libc.so.6' '/lib64/libc.so.6' '/lib32/libc.so.6');
# /lib/x86_64-linux-gnu/libc.so.6 |head -1 | sed 's/[^0-9.]*\([0-9.]*\).*/\1/'
for library in "${locations[@]}"; do
if [ -e $library ]; then
local version="$($library | head -1 | sed 's/[^0-9.]*\([0-9.]*\).*/\1/' )"
echo $version
return
fi
done
msg "Unable to determine libc version. This is likely our fault since we don't know where your OS put it."
die "Could you email the ASP developers with your OS information?"
}