rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

`--@rules_rust//:extra_rustc_flags` are not propagated to cargo build scripts.

Open PiotrSikora opened this issue 4 years ago • 3 comments

--@rules_rust//:extra_rustc_flags (nor --@rules_rust//:extra_exec_rustc_flags for that matter) are not propagated to cargo build scripts, so they are missing from CARGO_ENCODED_RUSTFLAGS, which can result in failed feature detection.

I expect that extra_exec_rustc_flags should be used when building cargo build scripts, but extra_rustc_flags should be passed when running cargo build scripts, so that they match rustc_flags used for building real targets.

cc @djmarcin @krasimirgg

PiotrSikora avatar Apr 04 '22 07:04 PiotrSikora

Passing --extra_rustc_flags to CARGO_ENCODED_RUSTFLAGS certainly seems reasonable to me from the description.

I don't have context on why --extra_exec_rustc_flags wasn't passed to cargo_build_script, but the reason --extra_rustc_flags was not is because the flag was implemented to enable cross-cutting flags for final build outputs like rust_binary or rust_shared_library crates (the motivating example was enabling LTO) and these flags often either don't apply to the intermediate exec targets like build scripts and proc macros, or just plain end up breaking them (which was the case with LTO).

djmarcin avatar Apr 04 '22 19:04 djmarcin

Passing --extra_rustc_flags to CARGO_ENCODED_RUSTFLAGS certainly seems reasonable to me from the description.

+1

About --extra_exec_rustc_flags: I played a bit and I believe the --extra_exec_rustc_flags are propagated to the script [cfg = exec] rustc invocation that builds the build script: given a cargo_build_script(name = "build_script"), a rust_binary(name = "build_script_") is generated, whose label is then passed as the script [cfg = exec] attribute of the _build_script_run rule: https://github.com/bazelbuild/rules_rust/blob/4144ddeb9c5290a15e5ec1cf8df31393744f6005/cargo/cargo_build_script.bzl#L260 As part of the build of the script, I believe the --extra_exec_rustc_flags are passed to the rustc invocation building the build_script_ [and they are not baked into the free-standing build_script_ rust_binary since that's not exec cfg as a freestanding target].

krasimirgg avatar Apr 05 '22 08:04 krasimirgg

@krasimirgg yeah, I meant that neither are added to CARGO_ENCODED_RUSTFLAGS, not that they aren't used when compiling the build script. Sorry for the confusion!

PiotrSikora avatar Apr 05 '22 09:04 PiotrSikora