Skip to content

Normalize Clang forwarded system header paths - #4321

Merged
UebelAndre merged 2 commits into
bazelbuild:mainfrom
cerisier:cerisier/clang-header-paths
Oct 8, 2026
Merged

UebelAndre merged 2 commits into
bazelbuild:mainfrom
cerisier:cerisier/clang-header-paths

Conversation

@cerisier

@cerisier cerisier commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Cargo build scripts change working directory, so execroot-relative C/C++ header operands must be made absolute. The existing path mapper does not recognize -internal-externc-isystem, and clears its pending operand when it encounters -Xpreprocessor. Clang then loses the libc headers, causing failures such as stdlib.h not found when building lz4-sys.

Recognize the extern-C system-header category and skip either Clang forwarding token while awaiting the path. Keep absolute paths and unrelated flags unchanged. This supports both late -Xclang forwarding and early -Xpreprocessor forwarding without changing header order.

Eight generated-action regressions cover the two forwarding forms, two header categories, and relative/absolute operands. On unmodified main, the three newly affected relative-path cases report missing rewritten operands during analysis. With the fix, all 38 tests pass:

USE_BAZEL_VERSION=8.6.0 bazel test --lockfile_mode=off //cargo/tests/cargo_build_script/cc_args_and_env:all

@UebelAndre UebelAndre left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you!

@UebelAndre
UebelAndre added this pull request to the merge queue Oct 8, 2026
Merged via the queue into bazelbuild:main with commit f69123b Oct 8, 2026
3 checks passed
cerisier added a commit to hermeticbuild/hermetic-llvm that referenced this pull request Nov 17, 2026
Fixes #804 

## Why

Toolchain `-isystem` paths can outrank dependency headers, Clang
builtins, and libc++ wrappers. This can select the wrong header or break
`#include_next` chains. Explicit component directories need the search
order chosen by Clang's target toolchain, while builtins remain owned by
`-resource-dir`.

## How

Pass component paths through the driver categories that own their final
order:

- C++ roots use joined `-stdlib++-isystem<dir>` options, forwarded
through `/clang:` for clang-cl. On macOS, the selected split libc++
roots replace duplicate SDK discovery. Opt-in MSVC STL keeps `/imsvc`.
- GNU kernel/libc roots use `-Xclang -internal-externc-isystem -Xclang
<dir>`.
- MinGW and MSVC C/SDK roots use forwarded `-internal-isystem` pairs.
Clang builtins remain under `-resource-dir`.

Musl retains its existing `-isystem` flags and C-before-builtin order.
Its separate user-header precedence issue is tracked in #820, so its
desired-order goldens remain manual. This PR remains the alternative to
#818.

The required consumer fixes are merged and adopted through released
modules, without temporary overrides for these fixes:

- [rules_go #4739](bazel-contrib/rules_go#4739)
makes explicit Clang header paths absolute when cgo changes directories
and preserves forwarded path operands. Adopted through rules_go 0.65.0.
- [rules_rust #4321](bazelbuild/rules_rust#4321)
makes forwarded extern-C header paths absolute when Cargo build scripts
change directories. Adopted through rules_rust 0.75.0.
- [hermeticbuild/rules_rust
#85](hermeticbuild/rules_rust#85) ports that
path fix to the fork consumed by rules_rs. Adopted through rules_rs
0.2.4.
- [rules_foreign_cc
#1603](bazel-contrib/rules_foreign_cc#1603)
keeps complete Clang header-flag groups in `CC` because bootstrap
configure probes can omit `CFLAGS`. Adopted through rules_foreign_cc
0.17.0.

Longer term, the [LLVM `-cstdlib-isystem`
RFC](https://discourse.llvm.org/t/rfc-add-a-driver-option-to-specify-c-library-header-directories/92650)
proposes a public driver option for explicit C header roots, letting
Clang place them according to each target's search order without private
frontend flags.

## Tests

Bazel fixtures compile real `#include_next` wrappers and libc++ C
wrappers. Golden tests replay native Bazel compile actions with `-E -v`
and compare the effective quote and angle search lists. All 39 active
golden tests pass under [Bazel
8](https://app.buildbuddy.io/invocation/47dc38e5-9042-4b97-8340-5bb051ea3aa3),
[Bazel
9](https://app.buildbuddy.io/invocation/808e4295-808f-439d-bc49-cb75f169dee5),
and [Bazel 10
RC](https://app.buildbuddy.io/invocation/bf4ea14d-066e-4aea-a0f2-438648baad5c);
[native macOS arm64 C/C++ tests
pass](https://app.buildbuddy.io/invocation/62876405-755d-49c2-b56a-5ecc37563fd8).
Musl desired-order goldens remain manual until #820.
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.

2 participants