Skip to content

bootstrap: CARGO_TARGET_$TARGET_RUSTFLAGS is appended to RUSTFLAGS #94874

Open
@jonhoo

Description

@jonhoo

In the bootstrap build, we respect CARGO_TARGET_$TARGET_RUSTFLAGS so that users can set target-specific rustflags if they wish. Unfortunately, the semantics of CARGO_TARGET_$TARGET_RUSTFLAGS in bootstrap do not match those of Cargo. In Cargo, CARGO_TARGET_$TARGET_RUSTFLAGS are equivalent to setting target.$target.rustflags, and it is overridden by the RUSTFLAGS environment variable. Whereas what bootstrap currently does is concatenate them:

rust/src/bootstrap/builder.rs

Lines 1802 to 1807 in 2c6a29a

// Inherit `RUSTFLAGS` by default ...
self.env(prefix);
// ... and also handle target-specific env RUSTFLAGS if they're configured.
let target_specific = format!("CARGO_TARGET_{}_{}", crate::envify(&self.1.triple), prefix);
self.env(&target_specific);

Now, bootstrap is in a slightly weird position where it always sets RUSTFLAGS, so the "usual" rules don't really make sense. Instead, the intention is probably to allow users to set a "general" set of flags for rustc (through RUSTFLAGS) more akin to build.rustflags and then override them for specific targets if desired. But unfortunately that doesn't match today's semantics either since the two environment variables are simply merged.

The big downside of having them always merge is that it makes it much more difficult to deal with targets that need fewer flags than the "general" set. For example, I have to set -Clink-arg=--gcc-toolchain=... in RUSTFLAGS. But when trying to build for Android targets that particular GCC toolchain won't work. So, I need to "reset" rustflags to not include the general set from RUSTFLAGS for those targets. But today, I have no way of doing that.

Now, in theory, I could just not set RUSTFLAGS and all would be well. But unfortunately that won't work either because RUSTFLAGS is the only environment variable that can be set to pass flags to rustc for building rustbuild itself (see, for example, #94234). And since in my environment --gcc-toolchain= is a required argument to the linker, I'm stuck between a rock and a hard place — either I set RUSTFLAGS and my Android builds fail, or I don't set it and the rustbuild build fails.

Meta

rustc --version --verbose:

rustc 1.59.0 (9d1b2106e 2022-02-23)
binary: rustc
commit-hash: 9d1b2106e23b1abd32fce1f17267604a5102f57a
commit-date: 2022-02-23
host: x86_64-unknown-linux-gnu
release: 1.59.0
LLVM version: 13.0.0

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.T-bootstrapRelevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions