1
1
#! /usr/bin/env bash
2
2
3
+ # Exit on error:
4
+ set -Eeuo pipefail
5
+
3
6
GCC_VERSION=$1
4
7
5
- if [ $GCC_VERSION = " 14.1.0" ]; then
8
+ if [[ $GCC_VERSION == " 14.1.0" ] ]; then
6
9
GCC_SHA=" 46d50ea6a380f2c977e6aad187da216c35b793b31b3ea6de9646339c2a22f13c"
7
- elif [ $GCC_VERSION = " 13.2.0" ]; then
10
+ elif [[ $GCC_VERSION == " 13.2.0" ] ]; then
8
11
GCC_SHA=" 9940242a1390f897f8c3fe6e7a8821e863580797f9a6e80a91f52e41dd8086a1"
9
- elif [ $GCC_VERSION = " 12.3.0" ]; then
12
+ elif [[ $GCC_VERSION == " 12.3.0" ] ]; then
10
13
GCC_SHA=" b0686eb1905594bde7b746fc58be97aceac8f802d8b5171adb6a4e84f3906d30"
11
- elif [ $GCC_VERSION = " 10.5.0" ]; then
14
+ elif [[ $GCC_VERSION == " 10.5.0" ] ]; then
12
15
GCC_SHA=" 1cd4eef592bcc7b9ec77e2c21b50dabcff9b614b4cd1ec82a9dac238c8789c95"
13
16
fi
14
17
@@ -21,39 +24,95 @@ MIRRORS=(\
21
24
" https://alpha.mirror.svc.schuermann.io/files/tock" \
22
25
)
23
26
24
- if test -x /usr/bin/shasum; then
25
- CHECK_SHA_CMD=" shasum -a 256 -c"
26
- else
27
- CHECK_SHA_CMD=" sha256sum -c"
28
- fi
27
+ function check_sha256() {
28
+ if test -x /usr/bin/shasum; then
29
+ echo " $1 $2 " | shasum -a 256 -c
30
+ return $?
31
+ else
32
+ sha256sum -c <( echo " $1 $2 " )
33
+ return $?
34
+ fi
35
+ }
36
+
37
+ FOUND=0
29
38
30
- let FOUND=0
31
-
32
- # Try from each mirror until we successfully download a .zip file.
33
- for MIRROR in ${MIRRORS[@]} ; do
34
- URL=$MIRROR /$ZIP_FILE
35
- echo " Fetching libc++ from ${MIRROR} ..."
36
- echo " Fetching ${URL} ..."
37
- # Note: There must be two space characters for `shasum` (sha256sum doesn't care)
38
- wget -O $ZIP_FILE " $URL " && (echo " $GCC_SHA $ZIP_FILE " | $CHECK_SHA_CMD )
39
- if [ $? -ne 0 ]; then
40
- if test -f $ZIP_FILE ; then
41
- file $ZIP_FILE
42
- ls -l $ZIP_FILE
43
- shasum -a 256 $ZIP_FILE
39
+ function fetch_and_unpack() {
40
+ # Try from each mirror until we successfully download a .zip file.
41
+ for MIRROR in ${MIRRORS[@]} ; do
42
+ URL=$MIRROR /$ZIP_FILE
43
+ echo " Fetching libc++ from ${MIRROR} ..."
44
+ echo " Fetching ${URL} ..."
45
+ # Note: There must be two space characters for `shasum` (sha256sum doesn't care)
46
+ wget -O $ZIP_FILE " $URL " && check_sha256 " $GCC_SHA " " $ZIP_FILE "
47
+ if [ $? -ne 0 ]; then
48
+ if test -f $ZIP_FILE ; then
49
+ file $ZIP_FILE
50
+ ls -l $ZIP_FILE
51
+ shasum -a 256 $ZIP_FILE
52
+ fi
53
+ echo " WARNING: Fetching libc++ from mirror $MIRROR failed!" >&2
54
+ else
55
+ FOUND=1
56
+ break
44
57
fi
45
- echo " WARNING: Fetching libc++ from mirror $MIRROR failed!" >&2
58
+ done
59
+
60
+ if [[ $FOUND -ne 0 ]]; then
61
+ echo " Unpacking $ZIP_FILE ..."
62
+ # -n: don't overwrite existing files, -q: quiet mode
63
+ unzip -n -q $ZIP_FILE
64
+ echo " Done upacking $ZIP_FILE ..."
65
+ exit 0
46
66
else
47
- let FOUND=1
48
- break
67
+ echo " ERROR: Unable to find tock-libc++ "
68
+ exit -1
49
69
fi
50
- done
70
+ }
51
71
52
- if [[ $FOUND -ne 0 ]]; then
53
- echo " Unpacking $ZIP_FILE ..."
54
- unzip -q $ZIP_FILE
55
- echo " Done upacking $ZIP_FILE ..."
72
+ # We must ensure that multiple parallel fetch and unzip operations
73
+ # don't trample over each other, which we do by obtaining a write-lock
74
+ # on the ZIP file that's being downloaded / extracted.
75
+ #
76
+ # This will also truncate any already downloaded ZIP file, which is
77
+ # fine because we'll overwrite it anyways.
78
+ #
79
+ # Under Linux platforms, we can expect `flock` to be installed, whereas for
80
+ # BSDs and macOS, we use `shlock` instead:
81
+ if which flock 2>&1 > /dev/null; then
82
+ NONBLOCK_LOCK_ACQ_FAIL=0
83
+ # We'd like for bash to choose the file descriptor, but that syntax
84
+ # ({fd}<"${ZIP_FILE}") isn't supported on the ancient version of
85
+ # Bash shipped on macOS. So we just pick one (200).
86
+ while true ; do
87
+ : >> " ${ZIP_FILE} "
88
+ {
89
+ if [ $NONBLOCK_LOCK_ACQ_FAIL -eq 0 ]; then
90
+ flock -n 200 || NONBLOCK_LOCK_ACQ_FAIL=1
91
+ if [ $NONBLOCK_LOCK_ACQ_FAIL -ne 0 ]; then
92
+ # Try again, blocking this time:
93
+ echo " Could not acquire non-blocking lock on ${ZIP_FILE} , waiting for lock to be released..." >&2
94
+ continue
95
+ fi
96
+ else
97
+ flock 200
98
+ fi
99
+ echo " Acquired lock on file ${ZIP_FILE} " >&2
100
+ fetch_and_unpack " ${ZIP_FILE} "
101
+ } 200< " ${ZIP_FILE} "
102
+ done
103
+ elif which shlock 2>&1 > /dev/null; then
104
+ while true ; do
105
+ trap " rm -f ${ZIP_FILE} .lock" EXIT
106
+ if shlock -f " ${ZIP_FILE} .lock" -p " $BASHPID " ; then
107
+ break
108
+ else
109
+ echo " Could not acquire lock on ${ZIP_FILE} .lock, retrying in 10..." >&2
110
+ sleep 10
111
+ fi
112
+ done
113
+ echo " Acquired lock on file ${ZIP_FILE} " >&2
114
+ fetch_and_unpack " ${ZIP_FILE} "
56
115
else
57
- echo " ERROR: Unable to find tock-libc++ "
58
- exit - 1
116
+ echo " Neither \" flock \" nor \" shlock \" seem to be installed, cannot proceed! "
117
+ exit 1
59
118
fi
0 commit comments