diff --git a/scripts/make.sh b/scripts/make.sh index 01b93ea32..9de74192b 100755 --- a/scripts/make.sh +++ b/scripts/make.sh @@ -6,6 +6,32 @@ source scripts/portability.sh # Shell functions called by the build +DASHN=-n +true & wait -n 2>/dev/null || { wait; unset DASHN; } +ratelimit() +{ + if [ "$#" -eq 0 ] + then + [ -z "$DASHN" ] && PIDS="$PIDS$! " + [ $((++COUNT)) -lt $CPUS ] && return 0 + fi + ((--COUNT)) + if [ -n "$DASHN" ] + then + wait -n + DONE=$(($DONE+$?)) + else + # MacOS uses an ancient version of bash which hasn't got "wait -n", and + # wait without arguments always returns 0 instead of process exit code. + # This keeps $CPUS less busy when jobs finish out of order. + wait ${PIDS%% *} + DONE=$(($DONE+$?)) + PIDS=${PIDS#* } + fi + + return $DONE +} + # Respond to V= by echoing command lines as well as running them do_loudly() { @@ -275,16 +301,13 @@ do do_loudly $BUILD -c $i -o $OUT & - # ratelimit to $CPUS many parallel jobs, detecting errors - [ $((++COUNT)) -ge $CPUS ] && { wait $DASHN; DONE=$?; : $((--COUNT)); } - [ $DONE -ne 0 ] && break + ratelimit || break done # wait for all background jobs, detecting errors -while [ $((COUNT--)) -gt 0 ] +while [ "$COUNT" -gt 0 ] do - wait $DASHN; - DONE=$((DONE+$?)) + ratelimit done done [ $DONE -ne 0 ] && exit 1 diff --git a/scripts/portability.sh b/scripts/portability.sh index 3beaf5216..9c5153e1c 100644 --- a/scripts/portability.sh +++ b/scripts/portability.sh @@ -41,9 +41,6 @@ fi # Probe number of available processors, and add one. : ${CPUS:=$(($(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null)+1))} -# Centos 7 bug workaround, EOL June 30 2024. TODO -DASHN=-n; wait -n 2>/dev/null; [ $? -eq 2 ] && unset DASHN - # If the build is using gnu tools, make them behave less randomly. export LANG=c export LC_ALL=C