Skip to content

cargo: propagate target-scoped build-script linker arguments - #4304

Open
jrandolf wants to merge 3 commits into
bazelbuild:mainfrom
jrandolf:fix/scoped-build-script-link-args
Open

jrandolf wants to merge 3 commits into
bazelbuild:mainfrom
jrandolf:fix/scoped-build-script-link-args

Conversation

@jrandolf

Copy link
Copy Markdown
Contributor

Build scripts emitting binary or cdylib linker arguments currently lose those arguments. This recreates #4097 by @ashi009 on current main, retaining the original author’s commit and updating the implementation and regression coverage.

Both rustc-link-arg-cdylib and its legacy rustc-cdylib-link-arg alias reach cdylib consumers. Binary directives remain direct-only and exclude test harnesses. rustc-link-arg-bin=BIN=FLAG matches the exact Cargo binary name; generated targets can set cargo_bin_name when their Bazel name differs. Interleaved named and all-binary arguments retain their emission order, including flags containing equals signs. The new flag files use the current build-script path substitution mechanism.

André’s requested binary/library regression coverage now includes cdylibs, rlibs, two binary names, a test harness, transitive build scripts, and actual compiler/linker execution. An invalid flag for an unrelated binary must be filtered out for those builds to succeed. Buildifier formatting is corrected.

Related: #1062. The new binary-scoped argument format is internal to the runner/process-wrapper pair; existing general argument files retain their format.

The real-build regression also passes with --experimental_output_paths=strip on macOS.

Fixes #4299.

Validation: bazel test //cargo/tests/cargo_build_script/cdylib_bin_link_args:all //cargo/private/cargo_build_script_runner:all //util/process_wrapper:process_wrapper_test (10 tests passed); formatting and whitespace checks passed.

ashi009 and others added 3 commits September 28, 2026 13:05
A build script's cargo::rustc-cdylib-link-arg and cargo::rustc-link-arg-bins
directives were dropped with a warning, so flags like napi-rs's
`-undefined dynamic_lookup` never reached the linker.

Route them through BuildInfo to the targets cargo applies them to:

* rustc-cdylib-link-arg is collected into BuildInfo.cdylib_link_flags and applied
  to cdylib crates. Matching cargo, it propagates transitively: a cdylib picks up
  the cdylib link args of every build script in its transitive dependencies
  (rust-lang/cargo#9562).
* rustc-link-arg-bins is collected into BuildInfo.bin_link_flags and applied to
  binary crates.

Each set is written to its own flag file, added to the relevant rustc action as
an --arg-file, so a non-matching crate type never reads it.

The per-binary cargo::rustc-link-arg-bin=BIN=FLAG stays unsupported: a build
script's outputs are shared by every target that depends on it, so a flag cannot
be scoped to one named binary.

Fixes bazelbuild#1062.

This branch has not been deployed

No deployments
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.

Preserve binary-specific Cargo build-script linker arguments without leaking to other targets

2 participants