Skip to content

Fix: clang-tidy on macOS resolve Apple SDK headers#724

Merged
ChaoWao merged 1 commit intohw-native-sys:mainfrom
ChaoWao:fix/macos-clang-tidy-isysroot
May 9, 2026
Merged

Fix: clang-tidy on macOS resolve Apple SDK headers#724
ChaoWao merged 1 commit intohw-native-sys:mainfrom
ChaoWao:fix/macos-clang-tidy-isysroot

Conversation

@ChaoWao
Copy link
Copy Markdown
Collaborator

@ChaoWao ChaoWao commented May 8, 2026

Summary

  • Homebrew clang-tidy on macOS bakes in a default sysroot path that often
    doesn't match the installed SDK, causing 'algorithm' file not found
    errors on fresh checkouts (the underlying issue is the libc include
    cascade triggered by libc++ internals, not <algorithm> itself).
  • tests/lint/clang_tidy.py now injects -isysroot $SDK and
    -isystem $SDK/usr/include/c++/v1 on macOS via --extra-arg=.
    No-op on Linux. Works whether phase 1 was built with Apple Clang
    or Homebrew Clang.
  • New docs/troubleshooting/macos-build.md documents the three-phase
    build toolchain matrix, the Apple-vs-Homebrew Clang asymmetry
    (xcrun runtime resolution vs baked default), the <algorithm> cascade
    failure mode, and an optional fully-Homebrew phase 1 setup
    (LDFLAGS pointing at Homebrew libc++ runtime, source-installs of
    nanobind/pybind11).
  • Move docs/macos-libomp-collision.md
    docs/troubleshooting/macos-libomp-collision.md so macOS-specific
    troubleshooting docs live in one place; update all in-repo cross
    references (conftest.py, docs/ci.md, docs/python-packaging.md,
    examples/workers/l3/*/main.py).

Why two flags

Flag Purpose
-isysroot $SDK Lets the libc cascade (<algorithm><wchar.h> via libc++ #include_next) resolve. Without this clang-tidy cannot lint any C++ file.
-isystem $SDK/usr/include/c++/v1 Forces clang-tidy onto Apple SDK's libc++ headers (matching what the Apple-Clang build sees) instead of Homebrew's bundled libc++. Eliminates "lint sees one libc++, build sees another" version drift.

xcrun --show-sdk-path returns the version-stable symlink
(MacOSX.sdk), so the patch survives SDK upgrades without code change.

Testing

  • pre-commit run --all-files (clang-tidy and markdownlint pass)
  • Spot-check mixed_example and spmd_starvation sim ST tests
    still pass
  • Linux CI green (no behavior change on non-Darwin)

Notes

This PR is intentionally narrow: only the lint + docs changes. A
follow-up PR refactors the dep computation in submit_task
(compute_task_fanin / register_task_outputs); discovering the
clang-tidy issue was a side effect of trying to land that refactor
through pre-commit on macOS.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new macOS troubleshooting guide and updates documentation paths across the repository. It also implements a fix in the linting script to handle macOS-specific header resolution for clang-tidy by injecting sysroot flags. Feedback indicates that the documentation snippet for the new logic should be updated to match the actual, more robust implementation in the code.

Comment thread docs/troubleshooting/macos-build.md
Homebrew LLVM ships clang-tidy on macOS (Apple Command Line Tools does
not), but its driver bakes in a default sysroot path that often does
not match the installed SDK on a fresh checkout — typical symptom is
``'algorithm' file not found`` because the libc include cascade
(`<algorithm>` → `<wchar.h>` via libc++ `#include_next`) cannot
resolve. Apple Clang escapes this by resolving the SDK at runtime via
xcrun.

`tests/lint/clang_tidy.py` now injects two `--extra-arg` flags on
macOS so the hook works regardless of whether phase 1 was built with
Apple Clang or Homebrew Clang:

  -isysroot $SDK                       # libc cascade resolves
  -isystem $SDK/usr/include/c++/v1     # lint sees same libc++ as build

Apple SDK is located via `xcrun --show-sdk-path`, returning the
version-stable symlink (`MacOSX.sdk`) so the patch survives SDK
upgrades.

Also organize macOS troubleshooting docs:

- New ``docs/troubleshooting/macos-build.md`` — three-phase toolchain
  matrix (simpler install / test orch / sim kernel), Apple-vs-Homebrew
  Clang asymmetry, the `<algorithm>` cascade explanation, and the
  optional Homebrew-Clang phase 1 setup (with LDFLAGS pointing at
  Homebrew libc++ runtime, source-installs of nanobind/pybind11).
- Move ``docs/macos-libomp-collision.md`` into ``docs/troubleshooting/``
  and update all internal cross-references (conftest.py, ci.md,
  python-packaging.md, examples/workers/l3/*/main.py).
@ChaoWao ChaoWao force-pushed the fix/macos-clang-tidy-isysroot branch from 75f679f to e78514f Compare May 9, 2026 01:04
@ChaoWao ChaoWao merged commit 71bf8ac into hw-native-sys:main May 9, 2026
27 of 28 checks passed
@ChaoWao ChaoWao deleted the fix/macos-clang-tidy-isysroot branch May 9, 2026 01:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant