Skip to content

Prioritize C toolchain library search paths - #39

Open
dzbarsky wants to merge 86 commits into
mainfrom
codex/toolchain-native-link-search-order
Open

Prioritize C toolchain library search paths#39
dzbarsky wants to merge 86 commits into
mainfrom
codex/toolchain-native-link-search-order

Conversation

@dzbarsky

Copy link
Copy Markdown
Member

Pass C toolchain -L options to rustc as -Lnative arguments so hermetic library directories precede cargo:rustc-link-search directories in the linker command. Preserve split -L arguments, Windows -LIBPATH options, and existing dependency linker flags.

Add analysis coverage for both linker preferences, transitive Cargo build-script arguments, and the actual hermetic LLVM glibc search directory.

dzbarsky and others added 30 commits July 21, 2026 21:58
Add support for tier 3 targets bpfeb-unknown-none and bpfel-unknown-none
(see

https://github.com/rust-lang/rust/blob/f5e2df7/src/doc/rustc/src/platform-support.md?plain=1#L311-L312).

This is modeled after bazelbuild#3507
and
should probably be updated if/when
bazelbuild/platforms#131 is merged.

(please use rebase merge when landing this as the proper commit message
is in the commit, rather than the PR description)

/cc @avrabe
…hollow rlibs: the RustcMetadata action runs rustc to completion with -Zno-codegen, emitting a .rlib archive. This approach mirrors the one used by buck2 and avoids needing to kill rustc mid-output in order to produce metadata.

While not fixing problems with SVH mismatches when non-determinism, this does simplify the codepath and uses a production tested technique that doesn't have any of the dangers associated with killing the rustc process while it's still active.
Port the sharding wrapper feature from bazelbuild#3774 into the hermeticbuild fork. The implementation wraps rust_test executables when experimental_enable_sharding is set while keeping rustc_compile_action's existing provider-list API for internal and extension callers.

rust_test now scans the returned providers to replace DefaultInfo for the wrapper, so extensions such as prost and wasm-bindgen continue to consume rustc_compile_action without API churn.

Co-authored-by: Brian Duff <bduff@linkedin.com>

Co-authored-by: Codex <noreply@openai.com>
Rustc emits GNU-like Windows staticlibs as lib<crate>.a, but rules_rust was stripping the lib prefix for all Windows non-rlib library outputs. Keep the prefix for staticlib outputs when the target ABI is gnu or gnullvm so declared outputs match rustc.
dzbarsky and others added 28 commits July 21, 2026 22:50
#25)

rust_shared_library links the cc toolchain's C++ runtime
(libstdc++/libc++ and the unwinder) dynamically, so it becomes an
external DT_NEEDED on the produced shared library. When shipping a
self-contained .so to a consumer that uses a different C++ stdlib,
that dynamic dependency is undesirable.

Add a `cc_runtime_linkage` attribute on rust_shared_library
(dynamic|static, default dynamic so existing behavior is unchanged).
When static, the C++ runtime is linked via
cc_toolchain.static_runtime_lib so it is embedded in the shared
library and no libstdc++/libc++/libunwind DT_NEEDED is emitted.

get_cc_toolchain_runtime_libs gains a `linkage` parameter resolved
from the attribute. Non-shared crate types are unchanged.
Add documentation extraction targets for the public cargo and rust Starlark packages. Declare the bazel_features, selects, and cc_debug_helper_bzl dependencies required by those targets.
Add ctx.coverage_instrumented() check so that only targets matching
--instrumentation_filter get -Cinstrument-coverage, consistent with
Bazel's recommended approach for rules.

For rust_test targets with a crate attribute, also check if the
underlying crate should be instrumented. Rust compiles the crate
sources directly into the test binary, so the test must be built
with -Cinstrument-coverage for the crate's code to produce coverage.

Add --instrumentation_filter=^// and --instrument_test_targets to CI
coverage tasks so all workspace targets (including tests) are
instrumented while excluding external dependencies.
Remove --instrument_test_targets from recommended settings since it is
not needed for the common case of rust_test with a crate attribute.
Document the inconsistency where #[cfg(test)] code gets instrumented
even without the flag.
This fixes a problem pointed out by
bazelbuild#4092 (comment).

`-Zsplit-dwarf-out-dir` is only available on nightly, so we have to gate
split debug info on it. Also added some tests that check that we don't
add the flag when using other channels.
Pass C toolchain -L options to rustc as -Lnative arguments so hermetic library directories precede cargo:rustc-link-search directories in the linker command. Preserve split -L arguments, Windows -LIBPATH options, and existing dependency linker flags.

Add analysis coverage for both linker preferences, transitive Cargo build-script arguments, and the actual hermetic LLVM glibc search directory.
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.

9 participants