Skip to content

Commit 67d822f

Browse files
committed
lib/fetch-{libc++,newlib}: use shlock for macOS and FreeBSD support
1 parent a033684 commit 67d822f

File tree

2 files changed

+162
-102
lines changed

2 files changed

+162
-102
lines changed

lib/fetch-libc++.sh

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env bash
22

33
# Exit on error:
4-
set -e
4+
set -Eeuo pipefail
55

66
GCC_VERSION=$1
77

8-
if [ $GCC_VERSION = "14.1.0" ]; then
8+
if [[ $GCC_VERSION == "14.1.0" ]]; then
99
GCC_SHA="46d50ea6a380f2c977e6aad187da216c35b793b31b3ea6de9646339c2a22f13c"
10-
elif [ $GCC_VERSION = "13.2.0" ]; then
10+
elif [[ $GCC_VERSION == "13.2.0" ]]; then
1111
GCC_SHA="9940242a1390f897f8c3fe6e7a8821e863580797f9a6e80a91f52e41dd8086a1"
12-
elif [ $GCC_VERSION = "12.3.0" ]; then
12+
elif [[ $GCC_VERSION == "12.3.0" ]]; then
1313
GCC_SHA="b0686eb1905594bde7b746fc58be97aceac8f802d8b5171adb6a4e84f3906d30"
14-
elif [ $GCC_VERSION = "10.5.0" ]; then
14+
elif [[ $GCC_VERSION == "10.5.0" ]]; then
1515
GCC_SHA="1cd4eef592bcc7b9ec77e2c21b50dabcff9b614b4cd1ec82a9dac238c8789c95"
1616
fi
1717

@@ -24,60 +24,90 @@ MIRRORS=(\
2424
"https://alpha.mirror.svc.schuermann.io/files/tock"\
2525
)
2626

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+
}
3236

3337
FOUND=0
3438

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+
3567
# We must ensure that multiple parallel fetch and unzip operations
3668
# don't trample over each other, which we do by obtaining a write-lock
3769
# on the ZIP file that's being downloaded / extracted.
3870
#
3971
# This will also truncate any already downloaded ZIP file, which is
4072
# 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}"
4483
{
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
8190
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

lib/fetch-newlib.sh

Lines changed: 81 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/usr/bin/env bash
22

33
# Exit on error:
4-
set -e
4+
set -Eeuo pipefail
55

66
NEWLIB_VERSION=$1
77

8-
if [ $NEWLIB_VERSION = "4.4.0.20231231" ]; then
8+
if [[ $NEWLIB_VERSION == "4.4.0.20231231" ]]; then
99
NEWLIB_SHA="686af44e1bba625eb24b3cfb1fd2d48a61848c1edebbd49b5dbec554ebf2ea94"
10-
elif [ $NEWLIB_VERSION = "4.3.0.20230120" ]; then
10+
elif [[ $NEWLIB_VERSION == "4.3.0.20230120" ]]; then
1111
NEWLIB_SHA="2595f02f7cb2fd2e444f4ddc7955deca4c52deb3f91411c4d28326be8b0d9e0d"
12-
elif [ $NEWLIB_VERSION = "4.2.0.20211231" ]; then
12+
elif [[ $NEWLIB_VERSION == "4.2.0.20211231" ]]; then
1313
NEWLIB_SHA="5916d76f1cc3c0f5487275823c85a9a9954edfa15f5706342ecb254d634ed559"
1414
fi
1515

@@ -20,62 +20,92 @@ ZIP_FILE=libtock-newlib-$NEWLIB_VERSION.zip
2020
MIRRORS=(\
2121
"https://www.cs.virginia.edu/~bjc8c/archive/tock"\
2222
"https://alpha.mirror.svc.schuermann.io/files/tock"\
23-
)
23+
)
2424

25-
if test -x /usr/bin/shasum; then
26-
CHECK_SHA_CMD="shasum -a 256 -c"
27-
else
28-
CHECK_SHA_CMD="sha256sum -c"
29-
fi
25+
function check_sha256() {
26+
if test -x /usr/bin/shasum; then
27+
echo "$1 $2" | shasum -a 256 -c
28+
return $?
29+
else
30+
sha256sum -c <(echo "$1 $2")
31+
return $?
32+
fi
33+
}
3034

3135
FOUND=0
3236

37+
function fetch_and_unpack() {
38+
# Try from each mirror until we successfully download a .zip file.
39+
for MIRROR in ${MIRRORS[@]}; do
40+
URL=$MIRROR/$ZIP_FILE
41+
echo "Fetching newlib from ${MIRROR}..."
42+
echo " Fetching ${URL}..."
43+
# Note: There must be two space characters for `shasum` (sha256sum doesn't care)
44+
wget -O $ZIP_FILE "$URL" && check_sha256 "$NEWLIB_SHA" "$ZIP_FILE"
45+
if [ $? -ne 0 ]; then
46+
echo " WARNING: Fetching newlib from mirror $MIRROR failed!" >&2
47+
else
48+
FOUND=1
49+
break
50+
fi
51+
done
52+
53+
if [[ $FOUND -ne 0 ]]; then
54+
echo "Unpacking $ZIP_FILE..."
55+
# -n: don't overwrite existing files, -q: quiet mode
56+
unzip -n -q $ZIP_FILE
57+
echo "Done upacking $ZIP_FILE..."
58+
exit 0
59+
else
60+
echo "ERROR: Unable to find tock-newlib"
61+
exit -1
62+
fi
63+
}
64+
3365
# We must ensure that multiple parallel fetch and unzip operations
3466
# don't trample over each other, which we do by obtaining a write-lock
3567
# on the ZIP file that's being downloaded / extracted.
3668
#
3769
# This will also truncate any already downloaded ZIP file, which is
3870
# fine because we'll overwrite it anyways.
39-
NONBLOCK_LOCK_ACQ_FAIL=0
40-
while true; do
41-
: >> ${ZIP_FILE}
71+
#
72+
# Under Linux platforms, we can expect `flock` to be installed, whereas for
73+
# BSDs and macOS, we use `shlock` instead:
74+
if which flock 2>&1 >/dev/null; then
75+
NONBLOCK_LOCK_ACQ_FAIL=0
76+
# We'd like for bash to choose the file descriptor, but that syntax
77+
# ({fd}<"${ZIP_FILE}") isn't supported on the ancient version of
78+
# Bash shipped on macOS. So we just pick one (200).
79+
while true; do
80+
: >> "${ZIP_FILE}"
4281
{
43-
if [ $NONBLOCK_LOCK_ACQ_FAIL -eq 0 ]; then
44-
flock -n $fd || NONBLOCK_LOCK_ACQ_FAIL=1
45-
if [ $NONBLOCK_LOCK_ACQ_FAIL -ne 0 ]; then
46-
# Try again, blocking this time:
47-
echo "Could not acquire non-blocking lock on ${ZIP_FILE}, waiting for lock to be released..." >&2
48-
continue
49-
fi
50-
else
51-
flock $fd
52-
fi
53-
echo "Acquired lock on file ${ZIP_FILE}" >&2
54-
55-
# Try from each mirror until we successfully download a .zip file.
56-
for MIRROR in ${MIRRORS[@]}; do
57-
URL=$MIRROR/$ZIP_FILE
58-
echo "Fetching newlib from ${MIRROR}..."
59-
echo " Fetching ${URL}..."
60-
# Note: There must be two space characters for `shasum` (sha256sum doesn't care)
61-
wget -O $ZIP_FILE "$URL" && (echo "$NEWLIB_SHA $ZIP_FILE" | $CHECK_SHA_CMD)
62-
if [ $? -ne 0 ]; then
63-
echo " WARNING: Fetching newlib from mirror $MIRROR failed!" >&2
64-
else
65-
FOUND=1
66-
break
67-
fi
68-
done
69-
70-
if [[ $FOUND -ne 0 ]]; then
71-
echo "Unpacking $ZIP_FILE..."
72-
# -n: don't overwrite existing files, -q: quiet mode
73-
unzip -n -q $ZIP_FILE
74-
echo "Done upacking $ZIP_FILE..."
75-
exit 0
76-
else
77-
echo "ERROR: Unable to find tock-newlib"
78-
exit -1
82+
if [ $NONBLOCK_LOCK_ACQ_FAIL -eq 0 ]; then
83+
flock -n 200 || NONBLOCK_LOCK_ACQ_FAIL=1
84+
if [ $NONBLOCK_LOCK_ACQ_FAIL -ne 0 ]; then
85+
# Try again, blocking this time:
86+
echo "Could not acquire non-blocking lock on ${ZIP_FILE}, waiting for lock to be released..." >&2
87+
continue
7988
fi
80-
} {fd}<${ZIP_FILE}
81-
done
89+
else
90+
flock 200
91+
fi
92+
echo "Acquired lock on file ${ZIP_FILE}" >&2
93+
fetch_and_unpack "${ZIP_FILE}"
94+
} 200<"${ZIP_FILE}"
95+
done
96+
elif which shlock 2>&1 >/dev/null; then
97+
while true; do
98+
trap "rm -f ${ZIP_FILE}.lock" EXIT
99+
if shlock -f "${ZIP_FILE}.lock" -p "$BASHPID"; then
100+
break
101+
else
102+
echo "Could not acquire lock on ${ZIP_FILE}.lock, retrying in 10..." >&2
103+
sleep 10
104+
fi
105+
done
106+
echo "Acquired lock on file ${ZIP_FILE}" >&2
107+
fetch_and_unpack "${ZIP_FILE}"
108+
else
109+
echo "Neither \"flock\" nor \"shlock\" seem to be installed, cannot proceed!"
110+
exit 1
111+
fi

0 commit comments

Comments
 (0)