Skip to content

Commit e19cfb9

Browse files
committed
Implement timeout function for macOS since coreutils is no longer needed to be installed
1 parent 6761cd7 commit e19cfb9

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

‎cmake/macos-brew-deps.sh‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/bin/bash
22
# Source this file on macOS, then call `install_macos_brew_deps`.
33
#
4-
# Installs cmake, pkg-config, wget, unzip, coreutils, llvm@19 and lld@19.
4+
# Installs cmake, pkg-config, wget, unzip, llvm@19 and lld@19.
55
# On Intel Macs Homebrew no longer publishes bottles for newer macOS, so some
6-
# formulae (openssl@4, llvm@19, lld@19) would be built from source, which is
6+
# formulae (llvm@19, lld@19) would be built from source, which is
77
# very slow or broken. The older Intel (macOS Sonoma) bottles are still on
88
# GHCR, so install those manually first.
99

‎peter-jr‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,39 @@ if ! realpath --relative-to=. . >/dev/null 2>&1; then
118118
}
119119
fi
120120

121+
# macOS has no `timeout` (GNU coreutils); use `gtimeout` if installed, otherwise
122+
# fall back to a small perl implementation. Supports `[-s SIG] [--foreground] DURATION CMD...`.
123+
if ! command -v timeout >/dev/null 2>&1; then
124+
if command -v gtimeout >/dev/null 2>&1; then
125+
timeout() { gtimeout "$@"; }
126+
else
127+
timeout()
128+
{
129+
local sig=15
130+
while [[ "${1:-}" == -* ]]; do
131+
case "$1" in
132+
-s) sig="$2"; shift 2;;
133+
-s*) sig="${1#-s}"; shift;;
134+
--foreground) shift;;
135+
*) shift;;
136+
esac
137+
done
138+
local secs="$1"
139+
shift
140+
perl -e '
141+
my ($secs, $sig) = (shift, shift);
142+
my $pid = fork();
143+
die "fork: $!" unless defined $pid;
144+
if (!$pid) { exec @ARGV or exit 127; }
145+
$SIG{ALRM} = sub { kill $sig, $pid; waitpid($pid, 0); exit 124; };
146+
alarm $secs;
147+
waitpid($pid, 0);
148+
exit(($? & 127) ? 128 + ($? & 127) : ($? >> 8));
149+
' "$secs" "$sig" "$@"
150+
}
151+
fi
152+
fi
153+
121154
findTests()
122155
{
123156
for loc in "${testLocations[@]}"

0 commit comments

Comments
 (0)