Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(build): make scripts/build.sh work under Nix on macOS

Kris Williams1aaa8297f3d0
diff --git a/Makefile.cbm b/Makefile.cbm
index f52d4fce4..1aa8918bf 100644
--- a/Makefile.cbm
+++ b/Makefile.cbm
@@ -12,6 +12,15 @@
# Linux: gcc/g++ — system default with full sanitizer support
# CI scripts pass CC/CXX explicitly; don't rely on defaults here
+# Target architecture (macOS): build.sh/test.sh export ARCHFLAGS="-arch <arch>"
+# (see scripts/env.sh). Fold it into the compiler drivers with `override` so it
+# reaches EVERY compile and link recipe — including the vendored objects below
+# that use their own *_CFLAGS — and so it survives a command-line `CC=` override.
+# ARCHFLAGS is empty on Linux/Windows and for native direct `make` invocations,
+# leaving CC/CXX unchanged there.
+override CC := $(CC) $(ARCHFLAGS)
+override CXX := $(CXX) $(ARCHFLAGS)
+
# ── Common flags ─────────────────────────────────────────────────
# Include paths for:
diff --git a/scripts/build.sh b/scripts/build.sh
index a8c59e736..bbb9bda74 100755
--- a/scripts/build.sh
+++ b/scripts/build.sh
@@ -87,12 +87,12 @@ verify_compiler "$CC"
# Step 1: Clean C build artifacts only (not node_modules — npm ci handles that)
rm -rf "$ROOT/build/c"
-# Step 2: Build (with arch prefix on macOS)
+# Step 2: Build (Makefile applies $ARCHFLAGS for the target arch on macOS)
if $WITH_UI; then
- $ARCH_PREFIX make -j"$NPROC" -f Makefile.cbm cbm-with-ui \
+ make -j"$NPROC" -f Makefile.cbm cbm-with-ui \
CFLAGS_EXTRA="$CFLAGS_EXTRA" "${EXTRA_MAKE_ARGS[@]+"${EXTRA_MAKE_ARGS[@]}"}"
else
- $ARCH_PREFIX make -j"$NPROC" -f Makefile.cbm cbm \
+ make -j"$NPROC" -f Makefile.cbm cbm \
CFLAGS_EXTRA="$CFLAGS_EXTRA" "${EXTRA_MAKE_ARGS[@]+"${EXTRA_MAKE_ARGS[@]}"}"
fi
diff --git a/scripts/env.sh b/scripts/env.sh
index df932b766..8cb5f9999 100755
--- a/scripts/env.sh
+++ b/scripts/env.sh
@@ -5,7 +5,7 @@
#
# Exports:
# ARCH — target architecture (arm64 / x86_64)
-# ARCH_PREFIX — "arch -arm64" on macOS, empty on Linux/Windows
+# ARCHFLAGS — "-arch <arch>" on macOS (target slice for clang/ld), empty elsewhere
# NPROC — number of CPU cores
# OS — darwin / linux / windows
@@ -36,16 +36,29 @@ esac
# CBM_ARCH env var or --arch flag override (parsed by calling script)
ARCH="${CBM_ARCH:-$HW_ARCH}"
-# ── Build arch prefix (macOS only) ─────────────────────────────
-ARCH_PREFIX=""
+# ── Target-architecture flags (macOS only) ─────────────────────
+# Select the target slice explicitly with clang/ld's -arch instead of
+# relaunching make under `arch -<arch>`. Explicit -arch is the only approach
+# that works across toolchains: a Nix/Homebrew clang wrapper has a fixed
+# target triple, so `arch -x86_64 make` would still emit native arm64. It also
+# lets host tools (node, codegen) keep running natively during a cross-build.
+# Makefile.cbm folds $(ARCHFLAGS) into CC/CXX so it reaches every compile and
+# link, including the vendored objects. Empty on Linux/Windows.
+ARCHFLAGS=""
if [[ "$OS" == "darwin" ]]; then
- ARCH_PREFIX="arch -${ARCH}"
+ ARCHFLAGS="-arch ${ARCH}"
fi
+export ARCHFLAGS
# ── Detect parallelism ─────────────────────────────────────────
NPROC=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4)
-# ── Verify compiler is available for target arch ───────────────
+# ── Verify compiler can build for the target arch ──────────────
+# On macOS, PROBE actual capability instead of inspecting the compiler's own
+# Mach-O header. The driver is often a wrapper script (Nix, ccache, Homebrew)
+# or a clang that cross-compiles, so `file` on the binary says nothing about
+# what it can target — it just sees a shell script or a host-arch executable.
+# Compiling + linking a trivial program with -arch <target> is the truth.
verify_compiler() {
local compiler="$1"
local bin
@@ -57,20 +70,20 @@ verify_compiler() {
fi
if [[ "$OS" == "darwin" ]]; then
- local file_info
- file_info="$(file "$bin" 2>/dev/null)"
-
- if [[ "$ARCH" == "arm64" ]] && ! echo "$file_info" | grep -qE "arm64|universal"; then
- echo "WARNING: $compiler ($bin) is x86_64 only — cannot build native arm64" >&2
- echo " Install arm64 GCC: arch -arm64 /opt/homebrew/bin/brew install gcc" >&2
- echo " Or override: scripts/test.sh --arch x86_64" >&2
- exit 1
- fi
-
- if [[ "$ARCH" == "x86_64" ]] && ! echo "$file_info" | grep -qE "x86_64|universal"; then
- echo "WARNING: $compiler ($bin) is arm64 only — cannot build x86_64" >&2
+ local probe_out
+ probe_out="$(mktemp -t cbm-archprobe.XXXXXX)"
+ if ! printf 'int main(void){return 0;}\n' \
+ | "$compiler" -arch "$ARCH" -x c - -o "$probe_out" >/dev/null 2>&1; then
+ rm -f "$probe_out"
+ echo "ERROR: $compiler cannot build for -arch $ARCH ($bin)" >&2
+ echo " A trivial $ARCH compile + link failed with this toolchain." >&2
+ if [[ "$ARCH" != "$HW_ARCH" ]]; then
+ echo " Cross-building $HW_ARCH -> $ARCH needs the $ARCH SDK slice + runtime;" >&2
+ echo " to build for this machine instead, re-run with: --arch $HW_ARCH" >&2
+ fi
exit 1
fi
+ rm -f "$probe_out"
fi
}
diff --git a/scripts/lint.sh b/scripts/lint.sh
index 62c912b24..60e023b47 100755
--- a/scripts/lint.sh
+++ b/scripts/lint.sh
@@ -36,11 +36,11 @@ bash "$ROOT/scripts/check-no-test-skips.sh"
if $CI_ONLY; then
echo "=== CI mode: cppcheck + clang-format ==="
- $ARCH_PREFIX make -j2 -f Makefile.cbm lint-ci "${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}"
+ make -j2 -f Makefile.cbm lint-ci "${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}"
echo "=== CI linters passed ==="
else
echo "=== Full lint: clang-tidy + cppcheck + clang-format ==="
- $ARCH_PREFIX make -j3 -f Makefile.cbm lint "${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}"
+ make -j3 -f Makefile.cbm lint "${MAKE_ARGS[@]+"${MAKE_ARGS[@]}"}"
fi
echo "=== All linters passed ==="
diff --git a/scripts/repro.sh b/scripts/repro.sh
index 299831302..d585a9ec7 100755
--- a/scripts/repro.sh
+++ b/scripts/repro.sh
@@ -47,7 +47,7 @@ export ASAN_OPTIONS="detect_leaks=0${ASAN_OPTIONS:+:$ASAN_OPTIONS}"
# test-repro both builds and runs the runner; tolerate its non-zero (red) exit.
set +e
-$ARCH_PREFIX make -j"$NPROC" -f Makefile.cbm test-repro $MAKE_ARGS 2>&1 | tee "$OUT"
+make -j"$NPROC" -f Makefile.cbm test-repro $MAKE_ARGS 2>&1 | tee "$OUT"
set -e
# The runner prints a "<N> passed[, <M> failed]" summary line only if it actually
diff --git a/scripts/test.sh b/scripts/test.sh
index 62210b426..cd98491e2 100755
--- a/scripts/test.sh
+++ b/scripts/test.sh
@@ -57,8 +57,8 @@ verify_compiler "$CC"
# Step 1: Clean
scripts/clean.sh
-# Step 2 + 3: Build and run tests (with arch prefix on macOS)
-$ARCH_PREFIX make -j"$NPROC" -f Makefile.cbm test $MAKE_ARGS
+# Step 2 + 3: Build and run tests (Makefile applies $ARCHFLAGS on macOS)
+make -j"$NPROC" -f Makefile.cbm test $MAKE_ARGS
# Step 4: C++ large-TU index-hang regression guard (#410). Runs the PROD binary
# in a subprocess with a wall-clock timeout — a hang must fail, not block the run.
@@ -72,7 +72,7 @@ fi
# Step 5: Parent-death watchdog regression (#406/#407). Builds the prod stdio
# binary and verifies it self-exits when its launching parent is killed.
echo "=== Step 5: parent-death watchdog regression (#406/#407) ==="
-$ARCH_PREFIX make -j"$NPROC" -f Makefile.cbm cbm $MAKE_ARGS
+make -j"$NPROC" -f Makefile.cbm cbm $MAKE_ARGS
bash "$ROOT/tests/test_parent_watchdog.sh"
# Step 6: security-strings URL allow-list regression. The MSYS2 CLANG64 toolchain