Skip to content

cargo_build_script: propagate cdylib and bin link args#4097

Open
ashi009 wants to merge 1 commit into
bazelbuild:mainfrom
ashi009:cdylib-bin-link-args
Open

cargo_build_script: propagate cdylib and bin link args#4097
ashi009 wants to merge 1 commit into
bazelbuild:mainfrom
ashi009:cdylib-bin-link-args

Conversation

@ashi009

@ashi009 ashi009 commented Jun 23, 2026

Copy link
Copy Markdown

Fixes #1062.

A build script's cargo::rustc-cdylib-link-arg, cargo::rustc-link-arg-bin and cargo::rustc-link-arg-bins 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:

The build-script runner writes each set to its own flag file, added to the rustc action as an --arg-file, so a non-matching crate type never reads it.

A build script's cargo::rustc-cdylib-link-arg, cargo::rustc-link-arg-bin 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/dylib 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-bin[s] is collected into BuildInfo.bin_link_flags and applied
  only to the owning crate's bin targets.

The build-script runner writes each set to its own flag file, added to the
relevant rustc action as an --arg-file, so a non-matching crate type never reads
it.

Fixes bazelbuild#1062.

@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.

Thanks! Can you add a test for somewhere under //cargo/tests/cargo_build_script that confirms the new flags are behaving as expected for binary and library consumers?

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.

Support cargo:rustc-link-arg-* instructions in build script runner

2 participants