-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup
executable file
·452 lines (396 loc) · 11.3 KB
/
setup
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#!/bin/sh
_GO_VER=1.20.3
VERBOSITY=0
TEMP_D=""
SQFSNG_GIT="${SQFSNG_GIT:-https://github.com/AgentD/squashfs-tools-ng.git}"
SQFSNG_REF="${SQFSNG_REF:-origin/fixes-1.0.0}"
[ -n "$HOME" ] || export HOME=$(echo ~)
error() { echo "$@" 1>&2; }
fail() { local r=$?; [ $r -eq 0 ] && r=1; failrc "$r" "$@"; }
failrc() { local r=$1; shift; [ $# -eq 0 ] || error "$@"; exit $r; }
Usage() {
cat <<EOF
Usage: ${0##*/} [ options ] install-prefix
Get everything needed to build squashfs go package.
non-system packages are installed to install-prefix.
Then they can be used with:
PREFIX="\$PWD/mydir"
LD_LIBRARY_PATH=\$PREFIX/lib
PKG_CONFIG_PATH=\$PREFIX/lib/pkgconfig
options:
--squashfs-tools-ng DIR get squashfs-tools-ng to DIR
EOF
}
bad_Usage() { Usage 1>&2; [ $# -eq 0 ] || error "$@"; return 1; }
cleanup() {
[ -z "${TEMP_D}" -o ! -d "${TEMP_D}" ] || rm -Rf "${TEMP_D}"
}
debug() {
local level=${1}; shift;
[ "${level}" -lt "${VERBOSITY}" ] && return
error "${@}"
}
has_cmd() {
command -v "$1" >/dev/null 2>&1
}
install_packages_apt() {
local packages="\
autoconf
autogen
automake
binutils
git
libc6-dev
liblz4-dev
liblzma-dev
libtool
libzstd-dev
make
pkg-config
tar
squashfs-tools
zlib1g-dev
"
apt-get update -q &&
apt-get install --no-install-recommends --assume-yes $packages || {
error "failed to install packages:" $packages
return 1
}
}
is_centos_7() {
for f in /etc/centos-release /etc/redhat-release; do
[ -f "$f" ] && grep -q "release 7" "$f" && return 0
done
return 1
}
install_packages_yum() {
local out="" statics=""
# we need epel for libzstd, but centos8 has it.
if is_centos_7; then
if ! rpm -q libzstd-static >/dev/null 2>&1; then
local epel7u="https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm"
if ! rpm -q epel-release >/dev/null 2>&1; then
yum install --assumeyes "$epel7u" || {
error "failed to install epel from $epel_url"
}
fi
fi
statics="glibc-static lz4-static zlib-static libzstd-static"
fi
local packages="\
autoconf
automake
binutils
git
glibc-devel
libtool
libzstd-devel
lz4-devel
make
squashfs-tools
tar
xz
xz-devel
zlib-devel
"
yum install --assumeyes $packages $statics
}
install_packages() {
[ "$(id -u)" = "0" ] || fail "sorry, have to be root for install packages."
if has_cmd apt-get; then
install_packages_apt
elif has_cmd yum; then
install_packages_yum
else
error "no idea how to install packages here"
return 1
fi
}
as_root() {
if [ "$(id -u)" = "0" ]; then
"$@"
else
has_cmd sudo || {
error "You don't have sudo, and you're not root. Sorry."
error "Try as root running: $*"
return 1
}
sudo "$@"
fi
}
is_goroot() {
[ -d "$1/bin" -a -d "$1/src" -a -d "$1/pkg" -a -x "$1/bin/go" ] || return
_RET=$("$1/bin/go" env GOROOT)
}
has_go() {
has_cmd go && {
_RET=$(go env GOROOT)
return
}
[ -z "$GOPATH" ] || set -- "$GOPATH/go" "$GOPATH/go/"go[0-9]*
set -- "$@" "$HOME/"go "$HOME/go/"go[0-9]*
for d in "$@"; do
is_goroot "$d" && {
debug 1 "GOROOT at $GOPATH/go"
return 0
}
done
return 1
}
download() {
debug 0 "downloading $1 -> $2"
curl --location "$1" > "$2.tmp.$$" && mv "$2.tmp.$$" "$2"
}
link_bins() {
local linkdir="${1%/}/" from_d="$2"
mkdir -p "$linkdir" || {
error "failed to create $linkdir"
return 1
}
set -- "${from_d%/}"/*
ln -sf -- "$@" "${linkdir}" || {
error "failed to $* to ${linkdir}"
return 1
}
debug 1 "Linked $* to $linkdir"
}
install_go() {
local ver_or_url="${1:-${_GO_VER}}" govdir="$2" tball="" url=""
if [ -z "${TEMP_D}" ]; then
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
fail "failed to make tempdir"
trap cleanup EXIT
fi
local linkdir="" goroot=""
# govdir: go version dir - the directory that contains go-X.Y.Z dirs.
if [ -z "$govdir" ]; then
if [ "$(id -u)" != "0" ]; then
[ -d "$HOME/go" ] || mkdir -p "$HOME/go" || {
error "failed to create $HOME/go"
return
}
govdir="$HOME/go"
linkdir="$HOME/bin"
else
govdir="/usr/local"
linkdir="/usr/local/bin"
fi
else
if [ -e "$govdir" ]; then
error "$govdir exists."
return 1
fi
goroot="$govdir"
govdir=$(dirname "${goroot%/}")
fi
mkdir -p "$govdir" || {
error "failed to create $govdir"
return 1
}
if echo "$ver_or_url" | grep -q "^[0-9]\+.[0-9]\+.[0-9]\+$"; then
local ver="${ver_or_url}"
local url="https://golang.org/dl/go$ver.linux-amd64.tar.gz"
tball="${TEMP_D}/go${ver}.tar.gz"
download "$url" "$tball" || {
error "failed to download $url for version $ver"
return 1
}
elif [ -f "$ver_or_url" ]; then
tball="$ver_or_url"
else
case "$ver_or_url" in
http://*|https://*)
url="$ver_or_url";
tball="${TEMP_D}/go.tar.gz";
download "$url" "$tball" || {
error "failed to download $url"
return
};;
*) error "Don't know what to do with $ver_or_url"; return 1;;
esac
fi
local exdir="${TEMP_D}/exdir"
debug 1 "extracting $tball"
mkdir "$exdir" || { error "failed to make $exdir"; return 1; }
tar -C "$exdir" -xf "$tball" || {
error "failed extraction of $tball"
return 1
}
is_goroot "$exdir/go" || {
error "$tball did not contain a 'go' directory that was a goroot."
return 1
}
out=$("$exdir/go/bin/go" version) || {
error "failed to get version from $exdir/go"
return 1
}
# out should look like
#go version go1.14.5 linux/amd64
local ver="${out#go version }"
ver=${ver%% *}
ver=${ver#go}
if [ -z "$goroot" ]; then
goroot="${govdir}/go-$ver"
fi
if [ -e "$goroot" ]; then
error "$goroot exists. remove it."
return 1
elif [ -L "$goroot" ]; then
error "$goroot is a dangling symlink - remove it."
return 1
fi
debug 0 "go version is $ver. Installing to ${goroot}"
[ -d "$goroot" ] || mkdir -p "$goroot" || {
error "Failed to create $goroot"
}
[ ! -d "$goroot" ] || rmdir "$goroot" || {
error "$goroot already exists - remove it. sorry."
return 1
}
mv "$exdir/go" "$goroot" || {
error "failed to move extracted go dir into $goroot"
return 1
}
if [ -n "$linkdir" ]; then
link_bins "$linkdir" "$goroot/bin" || {
error "Failed to link binaries to $linkdir."
return 1
}
debug 0 "GOROOT is $goroot bins linked into $linkdir"
local cur=""
if cur=$(command -v "go"); then
[ "$cur" = "$linkdir/go" -a "$cur" != "$goroot/bin/go" ] || {
debug 0 "go in current path is $cur != $linkdir/go"
}
else
debug 0 "$linkdir appears to not be in your path."
fi
else
debug 0 "GOROOT is $goroot add $goroot/bin to your PATH"
fi
return 0
}
print_success() {
local prefix="$1"
cat <<EOF
------
Successfully prepped system for building.
Now to build, do:
export LD_LIBRARY_PATH=$prefix/lib\${LD_LIBRARY_PATH:+:\$LD_LIBRARY_PATH}
export PKG_CONFIG_PATH=$prefix/lib/pkgconfig\${PKG_CONFIG_PATH:+:\$PKG_CONFIG_PATH}
export PATH=$prefix/bin:\$PATH
make
EOF
}
check_prefix() {
local prefix="$1"
shift
if [ "$prefix" = "/usr/local" ]; then
debug 1 "prefix is /usr/local, trying with defaults"
else
set -- PKG_CONFIG_PATH="$prefix/lib/pkgconfig"
fi
env "$@" pkg-config libsquashfs1
}
main() {
local short_opts="hIv"
local long_opts="help,install-packages,squashfs-tools-ng:,verbose"
local getopt_out=""
getopt_out=$(getopt --name "${0##*/}" \
--options "${short_opts}" --long "${long_opts}" -- "$@") &&
eval set -- "${getopt_out}" ||
{ bad_Usage; return; }
local cur="" next=""
local output="" squashfs_ng_d="$PWD/squashfs-tools-ng" install_packages=false
while [ $# -ne 0 ]; do
cur="$1"; next="$2";
case "$cur" in
-h|--help) Usage ; exit 0;;
--install-packages) install_packages=true;;
--squashfs-tools-ng) squashfs_ng_d=$next; shift;;
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));;
--) shift; break;;
esac
shift;
done
[ $# -ne 0 ] || { bad_Usage "must provide install prefix"; return; }
[ $# -eq 1 ] || { bad_Usage "got $# args ($*) expected just 1"; return; }
local prefix_in="$1" prefix="" start_d="$PWD"
[ -d "$prefix_in" ] || mkdir -p "$prefix_in" || {
error "failed to create $prefix_in"
return 1
}
prefix=$(cd "$prefix_in" && pwd) || {
error "failed tog et full path for $prefix_in"
return 1
}
TEMP_D=$(mktemp -d "${TMPDIR:-/tmp}/${0##*/}.XXXXXX") ||
fail "failed to make tempdir"
trap cleanup EXIT
if [ "$install_packages" = "true" ]; then
as_root "$0" install-packages || {
error "failed to install packages"
return 1
}
fi
if ! has_go; then
install_go || {
error "failed to install go."
return 1
}
fi
if [ -d "$squashfs_ng_d" ]; then
debug 1 "using existing squashfs-tools-ng dir in $squashfs_ng_d"
else
local pdir=$(dirname "$squashfs_ng_d")
[ -d "$pdir" ] || mkdir -p "$pdir" || {
error "failed to create $pdir";
return 1
}
git clone "${SQFSNG_GIT}" "$squashfs_ng_d" || {
error "failed to clone ${SQFSNG_GIT} to $squashfs_ng_d"
return 1
}
if [ "${SQFSNG_REF}" != "UNCHANGED" ]; then
debug 1 "checking out ${SQFSNG_REF}"
(
cd "$squashfs_ng_d" || {
error "failed cd to squashfs dir '$squashfs_ng_d'"
exit 1
}
git checkout "${SQFSNG_REF}" || {
error "Failed to checkout commitish ${SQFSNG_REF}"
exit 1
}
) || return
else
debug 1 "SQFSNG_REF set to UNCHANGED. leaving tree as is."
fi
fi
cd "$squashfs_ng_d" || {
error "failed to cd $squashfs_ng_d"
return 1
}
./autogen.sh &&
./configure "--prefix=$prefix" &&
make &&
make install || {
error "failed to build squashfs-tools-ng"
error "maybe try as root: $0 install-packages"
return 1
}
check_prefix "$prefix" || {
error "Failed to check for squashfs via pkg-config"
return 1
}
print_success $prefix
}
if [ "$1" = "install-packages" ]; then
install_packages "$@"
elif [ "$1" = "install-go" ]; then
shift
install_go "$@"
else
main "$@"
fi
# vi: ts=4 expandtab