Skip to content

cargo_build_script: propagate cdylib and binary link args#4097

Open
ashi009 wants to merge 2 commits into
bazelbuild:mainfrom
ashi009:cdylib-bin-link-args
Open

cargo_build_script: propagate cdylib and binary link args#4097
ashi009 wants to merge 2 commits 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 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:

Each set is written to its own flag file, added to the rustc action as an --arg-file, so a non-matching crate type never reads it. The per-binary rustc-link-arg-bin=BIN=FLAG stays unsupported — a build script's outputs are shared by every consumer, so a flag can't be scoped to one named binary.

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

@ashi009 ashi009 force-pushed the cdylib-bin-link-args branch 2 times, most recently from 789335d to b85b7a9 Compare June 24, 2026 04:15
@ashi009 ashi009 marked this pull request as draft June 24, 2026 04:17
@ashi009 ashi009 force-pushed the cdylib-bin-link-args branch 4 times, most recently from b98b5a3 to 516df28 Compare June 24, 2026 04:47
@ashi009 ashi009 marked this pull request as ready for review June 24, 2026 04:53
@ashi009 ashi009 force-pushed the cdylib-bin-link-args branch from 516df28 to 6a51b3e Compare June 24, 2026 04:54
@ashi009 ashi009 changed the title cargo_build_script: propagate cdylib and bin link args cargo_build_script: propagate cdylib and binary link args Jun 24, 2026
@ashi009 ashi009 force-pushed the cdylib-bin-link-args branch 3 times, most recently from 6e59649 to b0105ac Compare June 24, 2026 05:19
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.
@ashi009 ashi009 force-pushed the cdylib-bin-link-args branch from b0105ac to 83887c4 Compare June 24, 2026 05:35
@ashi009

ashi009 commented Jun 24, 2026

Copy link
Copy Markdown
Author

@UebelAndre added

@ashi009 ashi009 force-pushed the cdylib-bin-link-args branch from 0d941d4 to 1a4497f Compare June 24, 2026 06:15

@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! Looking good! Just need to fix buildifier

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