From bdd12fc9b53fbf93fdc223aad6fb051ab7d8d1df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fukan=20Veziro=C4=9Flu?= Date: Mon, 30 Mar 2026 00:28:50 +0300 Subject: [PATCH] fix: fail build when Metal compiler header resolution fails When xcrun metal fails during JIT source generation (e.g. due to misconfigured Xcode CLI tools or missing SDK), the error messages are silently parsed as header file names. This causes critical headers like bf16.h to be omitted from the embedded Metal kernel sources, leading to runtime bfloat16_t type errors that are difficult to diagnose. Add validation after the Metal compiler header resolution step to detect malformed output and fail the build early with a clear error message instead of producing a broken binary. --- mlx/backend/metal/make_compiled_preamble.sh | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/mlx/backend/metal/make_compiled_preamble.sh b/mlx/backend/metal/make_compiled_preamble.sh index bb55ed3a3c..68aefb0ff6 100644 --- a/mlx/backend/metal/make_compiled_preamble.sh +++ b/mlx/backend/metal/make_compiled_preamble.sh @@ -34,6 +34,22 @@ mkdir -p "$OUTPUT_DIR" CCC="xcrun -sdk macosx metal -x metal" HDRS=$( $CCC -I"$SRC_DIR" -I"$JIT_INCLUDES" -DMLX_METAL_JIT -E -P -CC -C -H "$INPUT_FILE" $CFLAGS -w 2>&1 1>/dev/null ) +# Fail early if the Metal compiler returned errors instead of header paths. +# Valid lines start with '.' chars (depth indicators); anything else means +# xcrun/metal failed (e.g. missing SDK, misconfigured toolchain). +if [ -n "$HDRS" ]; then + invalid_lines=$(echo "$HDRS" | grep -v '^\.*\.' || true) + if [ -n "$invalid_lines" ]; then + echo "Error: Metal compiler header resolution failed for ${INPUT_FILE}" >&2 + echo "Expected lines starting with '.' but got:" >&2 + echo "$invalid_lines" >&2 + echo "" >&2 + echo "This usually means xcrun or the Metal toolchain is not configured correctly." >&2 + echo "Try running: xcrun -sdk macosx metal --version" >&2 + exit 1 + fi +fi + # Remove any included system frameworks (for MetalPerformancePrimitive headers) HDRS=$(echo "$HDRS" | grep -v "Xcode")