diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e8f637..ed6e6dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed +- Building the C library on macOS no longer calls `nproc`. `make -j` uses + `sysctl -n hw.ncpu`, then `getconf _NPROCESSORS_ONLN`, then 1 (#95). - The source-build installer copies the FFI library under the platform-specific name used by the FFI resolver, so successful fallback builds can be found (#71). - `BinaryDownloader::download()` no longer calls `curl_close()`, which is a no-op since PHP 8.0 diff --git a/src/Builder.php b/src/Builder.php index 7fa2e7b..814fabd 100644 --- a/src/Builder.php +++ b/src/Builder.php @@ -61,9 +61,9 @@ public function build(): string throw BuildException::cmakeFailed($cmakeExitCode); } - // Run make (single invocation; exit code only) + // macOS has no GNU nproc binary. sysctl is the Darwin count; getconf covers the rest. $makeCmd = sprintf( - 'cd %s && make -j$(nproc)', + 'cd %s && make -j"$(sysctl -n hw.ncpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)"', escapeshellarg($buildPath) ); diff --git a/tests/make-jobs-darwin.test.sh b/tests/make-jobs-darwin.test.sh new file mode 100644 index 0000000..b3c2c7e --- /dev/null +++ b/tests/make-jobs-darwin.test.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# Builder.php must not ask make for $(nproc). macOS does not ship nproc. +set -euo pipefail +root=$(cd "$(dirname "$0")/.." && pwd) +builder="$root/src/Builder.php" + +# The single quotes are the point: this is a source search, not a command. +# shellcheck disable=SC2016 +if grep -q '$(nproc)' "$builder"; then + echo "Builder.php still calls nproc" >&2 + exit 1 +fi + +work=$(mktemp -d) +set +e +nproc >"$work/red.out" 2>"$work/red.err" +red=$? +set -e +if [ "$red" -eq 0 ]; then + echo "nproc unexpectedly succeeded" >&2 + exit 1 +fi +if ! grep -q "command not found" "$work/red.err"; then + echo "expected nproc command not found" >&2 + cat "$work/red.err" >&2 + exit 1 +fi + +jobs=$(sysctl -n hw.ncpu 2>/dev/null || getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1) +printf '%s\n' "$jobs" | grep -Eq '^[1-9][0-9]*$' +echo "make jobs darwin ok (nproc exit $red, jobs $jobs)" +rm -rf "$work"