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