-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·285 lines (259 loc) · 8.41 KB
/
setup.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
#!/bin/bash
#
# Set up the development environment
#
set -euo pipefail
SCRIPT_PATH="$(realpath "${BASH_SOURCE[0]}")"
SCRIPT_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"
cd "$SCRIPT_DIR"
msg() {
echo -e "[+] ${1-}" >&2
}
die() {
echo -e "[!] ${1-}" >&2
exit 1
}
if [[ $UID -eq 0 ]]; then
die 'Please run this script without root privilege'
fi
#
# Output a short name (v:DISTRO) of the Linux distribution
#
get_distro() {
export DISTRO
if test -f /etc/os-release; then # freedesktop.org and systemd
source /etc/os-release
DISTRO="$(echo "$NAME" | cut -f 1 -d ' ' | tr '[:upper:]' '[:lower:]')"
elif type lsb_release >/dev/null 2>&1; then # linuxbase.org
DISTRO="$(lsb_release -si | tr '[:upper:]' '[:lower:]')"
elif test -f /etc/lsb-release; then
# shellcheck source=/dev/null
source /etc/lsb-release
DISTRO="$(echo "$DISTRIB_ID" | tr '[:upper:]' '[:lower:]')"
elif test -f /etc/arch-release; then
DISTRO="arch"
elif test -f /etc/debian_version; then
# Older Debian, Ubuntu
DISTRO="debian"
elif test -f /etc/SuSe-release; then
# Older SuSE
DISTRO="opensuse"
elif test -f /etc/fedora-release; then
# Older Fedora
DISTRO="fedora"
elif test -f /etc/redhat-release; then
# Older Red Hat, CentOS
DISTRO="centos"
elif type uname >/dev/null 2>&1; then
# Fall back to uname
DISTRO="$(uname -s)"
else
die 'Unable to determine the distribution'
fi
}
#
# Build and install the package with PKGBUILD
#
makepkg_arch() {
TARGET="$1"
shift
msg "Building $TARGET..."
pushd "$TARGET"
makepkg -sri "$@"
popd # "$TARGET"
}
element_in() {
local e match="$1"
shift
for e in "$@"; do [[ "$e" == "$match" ]] && return 0; done
return 1
}
#
# Build and install the package with PKGBUILD
#
makepkg_manual() {
MAKEFLAGS="-j$(nproc)"
export MAKEFLAGS
[[ -z "${CFLAGS+x}" ]] && export CFLAGS=""
[[ -z "${CXXFLAGS+x}" ]] && export CXXFLAGS=""
TARGET="$1"
shift
msg "Building $TARGET..."
pushd "$TARGET"
# shellcheck disable=SC2016
sed -i PKGBUILD \
-e 's|\<python\> |python3 |g' \
-e '/[[:space:]]*rm -rf .*\$pkgdir\>.*$/d'
# shellcheck source=/dev/null
source PKGBUILD
srcdir="$(realpath src)"
pkgdir=/
mkdir -p "$srcdir"
# prepare the sources
i=0
# shellcheck disable=SC2154
for s in "${source[@]}"; do
target=${s%%::*}
url=${s#*::}
if [[ "$target" == "$url" ]]; then
target=$(basename "${url%%#*}" | sed 's/\.git$//')
fi
# fetch the source files if they do not exist already
if [[ ! -e "$target" ]]; then
# only support common tarballs and git sources
if [[ "$url" == git+http* ]]; then
# shellcheck disable=SC2001
git clone "$(echo "${url%%#*}" | sed -e 's/^git+//')" "$target"
# check out the corresponding revision if there is a fragment
fragment=${url#*#}
if [[ "$fragment" != "$url" ]]; then
pushd "$target"
git checkout "${fragment#*=}"
popd
fi
elif [[ "$url" == *.tar.* ]]; then
curl -L "$url" -o "$target" >/dev/null 2>&1
else
die "Unsupported source URL $url"
fi
fi
# create links in the src directory
ln -sf "../$target" "$srcdir/$target"
# extract tarballs if the target is not in noextract
# shellcheck disable=SC2154
if [[ "$target" == *.tar.* ]] &&
! element_in "$target" "${noextract[@]}"; then
tar -C "$srcdir" -xf "$srcdir/$target"
fi
i=$((i + 1))
done
# execute the PKGBUILD functions
pushd "$srcdir"
[ "$(type -t prepare)" = "function" ] && prepare
[ "$(type -t build)" = "function" ] && build
[ "$(type -t check)" = "function" ] && check
sudo bash -c "pkgdir=\"$pkgdir\"; srcdir=\"$srcdir\";
source \"$srcdir/../PKGBUILD\"; package"
popd # "$srcdir"
popd # "$TARGET"
}
#
# Build and install package from AUR
#
aur_install() {
TARGET="$1"
shift
if [[ -d "$TARGET" ]]; then
cd "$TARGET"
git pull
cd ..
else
git clone "https://aur.archlinux.org/$TARGET.git"
fi
if [[ "$DISTRO" == "arch" ]]; then
(makepkg_arch "$TARGET" "$@")
else
(makepkg_manual "$TARGET" "$@")
fi
rm -rf "$TARGET"
}
# Set up docker engine quick and dirty
get_docker() {
if command -v docker >/dev/null 2>&1; then
return
fi
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh ./get-docker.sh
rm -f ./get-docker.sh
}
# https://developer.hashicorp.com/vagrant/downloads#linux
get_vagrant() {
if command -v vagrant >/dev/null 2>&1; then
return
fi
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt-get update -y -qq
sudo apt-get install -y -qq vagrant
}
set_up_docker() {
# Enable and start the docker service.
if ! sudo systemctl enable --now docker; then
sudo journalctl -eu docker.service
die "Failed to start docker.service"
fi
# Add the current user to group `docker` if they aren't in it already.
if ! getent group docker | grep -qw "$USER"; then
sudo gpasswd -a "$USER" docker
fi
# Make sure docker is working by this point.
if ! docker ps &>/dev/null; then
exec sg docker "exec sg $(id -gn) $SCRIPT_PATH"
fi
}
build_s2e_docker_image() {
pushd "$SCRIPT_DIR/docker" >/dev/null
make s2e
make clean
popd >/dev/null
}
build_docker_images() {
pushd "$SCRIPT_DIR/docker" >/dev/null
make
make clean
popd >/dev/null
}
main() {
#
# See the following places for required dependencies.
# https://github.com/S2E/s2e-env/blob/master/s2e_env/dat/config.yaml
# https://github.com/S2E/s2e/blob/master/Dockerfile
# https://github.com/S2E/scripts/blob/master/Dockerfile.dist
#
get_distro
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
git -C "$PROJECT_DIR" submodule update --init --recursive
git -C "$PROJECT_DIR" submodule foreach --recursive git clean -xdf
if [[ "$DISTRO" == "arch" ]]; then
script_deps=(base-devel curl git)
build_deps=(gcc clang cmake ninja python-jinja docker python boost
graphviz)
style_deps=(clang yapf)
vm_deps=(vagrant virtualbox virtualbox-host-modules-arch)
# experiment_deps=(time python-matplotlib python-numpy python-pandas python-networkx)
depends=("${script_deps[@]}" "${build_deps[@]}" "${style_deps[@]}" "${vm_deps[@]}")
sudo pacman -Sy --needed --noconfirm "${script_deps[@]}"
if ! pacman -Q paru >/dev/null 2>&1; then
aur_install paru --asdeps --needed --noconfirm
fi
paru -Sy --asdeps --needed --noconfirm --removemake "${depends[@]}"
makepkg_arch mimesis-dev -srcfi --asdeps --noconfirm
elif [[ "$DISTRO" == "ubuntu" ]]; then
script_deps=(build-essential curl git)
build_deps=(g++ clang cmake ninja-build python3-jinja2 pkgconf
python3-venv libboost-all-dev graphviz)
style_deps=(clang-format yapf3)
vm_deps=(virtualbox virtualbox-ext-pack)
# experiment_deps=(time python3-matplotlib python3-numpy python3-pandas python3-networkx)
depends=("${script_deps[@]}" "${build_deps[@]}" "${style_deps[@]}" "${vm_deps[@]}")
sudo apt-get update -y -qq
sudo apt-get install -y -qq "${depends[@]}"
get_docker
get_vagrant
else
die "Unsupported distribution: $DISTRO"
fi
set_up_docker
build_s2e_docker_image
"$PROJECT_DIR/scripts/build.sh" --s2e-env
"$PROJECT_DIR/scripts/build.sh" --s2e-init
"$PROJECT_DIR/scripts/build.sh" --s2e-libps-deps
"$PROJECT_DIR/scripts/build.sh" --mimesis
"$PROJECT_DIR/scripts/build.sh" --s2e
"$PROJECT_DIR/scripts/build.sh" --s2e-image
build_docker_images
"$PROJECT_DIR/scripts/build.sh" --mimesis --stap --s2e
msg "Finished"
}
main "$@"
# vim: set ts=4 sw=4 et: