-
Notifications
You must be signed in to change notification settings - Fork 33
/
scope_env.sh
executable file
·328 lines (291 loc) · 8.17 KB
/
scope_env.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/bin/bash
print_help() {
echo "Usage Examples:"
echo " ./scope_env.sh run <cmd and args> (executes cmd with scope library)"
echo " ./scope_env.sh version (identifies the scope library version)"
echo " ./scope_env.sh platform (returns Linux or macOS)"
echo " ./scope_env.sh pkg_mgr (returns yum, apt-get, or brew)"
echo " ./scope_env.sh build (clones, installs tools, and builds scope)"
echo " ./scope_env.sh help (prints this message)"
}
determine_lib_path() {
if [ -z "$SCOPE_HOME" ]; then
export SCOPE_HOME=`pwd`
fi
PLATFORM=$(determine_platform)
ARCH=$(determine_arch)
if [ $PLATFORM == "macOS" ]; then
echo "$SCOPE_HOME/lib/macOS/libscope.so"
elif [ $PLATFORM == "Linux" ]; then
echo "$SCOPE_HOME/lib/linux/$ARCH/libscope.so"
else
echo "ERROR"
fi
}
run_scoped_cmd() {
if [ -z "$SCOPE_HOME" ]; then
export SCOPE_HOME=`pwd`
fi
PLATFORM=$(determine_platform)
LIBRARY=$(determine_lib_path)
if [ $PLATFORM == "macOS" ]; then
DYLD_INSERT_LIBRARIES=$LIBRARY DYLD_FORCE_FLAT_NAMESPACE=1 "$@"
elif [ $PLATFORM == "Linux" ]; then
LD_PRELOAD=$LIBRARY "$@"
else
echo "ERROR: bad platform: $PLATFORM"
fi
exit $?
}
print_version() {
local ok=1
if ! strings --version &>/dev/null; then
echo "Error: can't determine version without 'strings' command."
ok=0
fi
if ! grep --version &>/dev/null; then
echo "Error: can't determine version without 'grep' command."
ok=0
fi
if ! sed --version &>/dev/null; then
echo "Error: can't determine version without 'sed' command."
ok=0
fi
if (( ! ok )); then
exit 1
fi
LIBRARY=$(determine_lib_path)
# sed deletes 'Constructor (' from the beginning, and ')' from the end
strings $LIBRARY | grep "Constructor (Scope Version" | sed 's/.*(//' | sed 's/.$//'
return $?
}
determine_platform() {
if uname -s 2> /dev/null | grep -i "linux" > /dev/null
then
echo "Linux"
elif uname -s 2> /dev/null | grep -i darwin > /dev/null
then
echo "macOS"
else
echo "Error"
fi
}
determine_arch() {
echo $(uname -m)
}
determine_pkg_mgr() {
if yum --version &>/dev/null; then
echo "yum"
elif apt-get --version &>/dev/null; then
echo "apt-get"
elif brew --version &>/dev/null; then
echo "brew"
else
echo "Error"
fi
}
install_brew() {
echo "Installing homebrew."
# The initial "echo" is to pass the "hit enter to continue" prompt.
echo | /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
if [ $? = 0 ]; then
echo "Installation of homebrew successful."
else
echo "Installation of homebrew failed."
return 1
fi
}
platform_and_pkgmgr_defined() {
if [ "${PLATFORM}" = "Error" ]; then
echo "scope_env.sh does not support this platform type. Exiting."
exit 1
elif [ "${PKG_MGR}" = "Error" ]; then
echo "scope_env.sh does not support this package menager. Exiting."
exit 1
else
echo "Platform type is ${PLATFORM} with package manager ${PKG_MGR}."
fi
return 0
}
install_sudo() {
case $PKG_MGR in
"yum")
if ! sudo --version &>/dev/null; then
echo "Install of sudo is required."
if su -c "yum install sudo"; then
echo "Install of sudo was successful."
else
echo "Install of sudo was unsuccessful. Exiting."
exit 1
fi
fi
;;
"apt-get")
if ! sudo --version &>/dev/null; then
echo "Install of sudo is required."
if su -c "apt-get update && apt-get install -y sudo"; then
echo "Install of sudo was successful."
else
echo "Install of sudo was unsuccessful. Exiting."
exit 1
fi
fi
;;
"brew")
;;
esac
return 0
}
install_git() {
case $PKG_MGR in
"yum")
if ! git --version &>/dev/null; then
echo "Install of git is required."
if sudo yum install git; then
echo "Install of git was successful."
else
echo "Install of git was unsuccessful. Exiting."
exit 1
fi
fi
;;
"apt-get")
if ! git --version &>/dev/null; then
echo "Install of git is required."
if sudo apt-get install -y git; then
echo "Install of git was successful."
else
echo "Install of git was unsuccessful. Exiting."
exit 1
fi
fi
;;
"brew")
if ! git --version &>/dev/null; then
echo "Install of git is required."
if brew install git; then
echo "Install of git was successful."
else
echo "Install of git was unsuccessful. Exiting."
exit 1
fi
fi
;;
esac
return 0
}
clone_scope() {
if [ -d .git ]; then
echo "We're in an existing repo now. No need to clone."
return 0
fi
if [ -d "./scope" ]; then
echo "switching to existing ./scope directory."
cd ./scope;
clone_scope
return 0
fi
echo "Clone of scope.git is required."
if git clone git@bitbucket.org:cribl/scope.git; then
echo "Clone of scope.git was successful."
cd scope
#rm ../scope_env.sh
else
echo "Clone of scope.git was unsuccessful. Exiting."
exit 1
fi
return 0
}
install_build_tools() {
if ./install_build_tools.sh; then
echo "install_build_tools.sh was successful."
else
echo "install_build_tools.sh was unsuccessful. Exiting."
exit 1
fi
return 0
}
run_make() {
echo "Running make all."
if make all; then
echo "make all was successful."
else
echo "make all failed. Exiting."
exit 1
fi
echo "Running make test."
if make test; then
echo "make test was successful."
else
echo "make test was unsuccessful. Exiting."
exit 1
fi
return 0
}
prep() {
echo "Running scope_env.sh prep"
PLATFORM=$(determine_platform)
PKG_MGR=$(determine_pkg_mgr)
platform_and_pkgmgr_defined
install_sudo
install_git
clone_scope
install_build_tools
}
build_scope() {
echo "Running scope_env.sh."
PLATFORM=$(determine_platform)
PKG_MGR=$(determine_pkg_mgr)
# Mac's brew package manager may not be there unless we put it there...
if [ "${PLATFORM}" = "macOS" ] && [ "${PKG_MGR}" = "Error" ]; then
if install_brew; then
PKG_MGR=$(determine_pkg_mgr)
fi
fi
prep
run_make
print_version
}
build_scope_arm64() {
echo "Building Scope in ARM64 via docker"
localdir=$(pwd)
docker run --privileged --rm tonistiigi/binfmt --install all
docker run -i --rm -v ${localdir}:${localdir} --platform linux/arm64 ubuntu:20.04 sh -cx "cd ${localdir} && bash -ex ./scope_env.sh prep && make all"
}
if [ "$#" = 0 ]; then
echo "Expected at least one argument. Try './scope_env.sh help; for more info."
exit 1
fi
for arg in "$@"; do
case $arg in
"run")
shift;
run_scoped_cmd "$@"
;;
"version")
print_version
;;
"platform")
determine_platform
;;
"pkg_mgr")
determine_pkg_mgr
;;
"build")
build_scope
;;
"build_arm64")
build_scope_arm64
;;
"prep")
prep
;;
"help")
print_help
;;
*)
echo "Argument $arg is not expected. Try './scope_env.sh help' for expected arguments."
;;
esac
done
exit 0