Skip to content

Normalize Clang forwarded system header paths - #85

Merged
dzbarsky merged 2 commits into
mainfrom
cerisier/clang-header-paths
Oct 13, 2026
Merged

dzbarsky merged 2 commits into
mainfrom
cerisier/clang-header-paths

Conversation

@cerisier

@cerisier cerisier commented Oct 8, 2026

Copy link
Copy Markdown
Member

Port of bazelbuild#4321 onto this fork's main, preserving its existing path mapper and user compile-flag regressions. This is the copy needed by consumers such as rules_rs that pin hermeticbuild/rules_rust.

Cargo build scripts change working directory, so execroot-relative header operands must be made absolute. Recognize -internal-externc-isystem and preserve -Xpreprocessor while awaiting the header path, just as for -Xclang. Otherwise Clang loses libc headers, producing errors such as stdlib.h not found. Absolute paths and unrelated flags stay unchanged.

Eight generated-action regressions cover both forwarding forms, both header categories, and relative/absolute operands. The corresponding tests expose three missing rewrites on upstream's unmodified baseline. All 40 tests in this fork's flag-handling package pass:

USE_BAZEL_VERSION=8.6.0 bazel test --config=remote --lockfile_mode=off \
  --local_test_jobs=1 //cargo/tests/cargo_build_script/cc_args_and_env:all

The first concurrent macOS run hit a generate-xml.sh segmentation fault on the existing bindir_relative_test; the sequential retry passed all 40 tests. Buildifier formatting/lint and whitespace checks pass.

Merge blocker for hermeticbuild/hermetic-llvm#819 and hermeticbuild/hermetic-llvm#820, alongside the bazelbuild PR. Consumers must adopt a release or pin containing this fix before the hermetic-llvm stack merges.

@cerisier
cerisier marked this pull request as ready for review October 8, 2026 11:29
@dzbarsky
dzbarsky merged commit 669f49c into main Oct 13, 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