From 416418a932a3c2ff61a1b9de72301275cdaa0795 Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Mon, 10 Nov 2025 13:16:24 +0100 Subject: [PATCH 1/5] feat: add `rust.rustflags` option to `bootstrap.toml` This makes easy to persistently pass any flag to the compiler when building rustc. For example you can use a different linker by putting the following in `bootstrap.toml`: ```toml [rust] rustflags = ["-Clinker=clang", "-Clink-arg=--ld-path=wild"] ``` --- src/bootstrap/src/core/builder/cargo.rs | 5 +++++ src/bootstrap/src/core/config/config.rs | 3 +++ src/bootstrap/src/core/config/toml/rust.rs | 2 ++ 3 files changed, 10 insertions(+) diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index c2029f97391d4..a48b41820fe7f 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -601,6 +601,11 @@ impl Builder<'_> { } let mut rustflags = Rustflags::new(target); + + for arg in &self.config.rust_rustflags { + rustflags.arg(arg); + } + if build_compiler_stage != 0 { if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") { cargo.args(s.split_whitespace()); diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 4b7ae6df360dc..05af7cfe4f7ed 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -224,6 +224,7 @@ pub struct Config { pub rust_std_features: BTreeSet, pub rust_break_on_ice: bool, pub rust_parallel_frontend_threads: Option, + pub rust_rustflags: Vec, pub llvm_profile_use: Option, pub llvm_profile_generate: bool, @@ -571,6 +572,7 @@ impl Config { bootstrap_override_lld_legacy: rust_bootstrap_override_lld_legacy, std_features: rust_std_features, break_on_ice: rust_break_on_ice, + rustflags: rust_rustflags, } = toml.rust.unwrap_or_default(); let Llvm { @@ -1431,6 +1433,7 @@ impl Config { rust_randomize_layout: rust_randomize_layout.unwrap_or(false), rust_remap_debuginfo: rust_remap_debuginfo.unwrap_or(false), rust_rpath: rust_rpath.unwrap_or(true), + rust_rustflags: rust_rustflags.unwrap_or_default(), rust_stack_protector, rust_std_features: rust_std_features .unwrap_or(BTreeSet::from([String::from("panic-unwind")])), diff --git a/src/bootstrap/src/core/config/toml/rust.rs b/src/bootstrap/src/core/config/toml/rust.rs index cb48c7d9aadad..f17ce984cd976 100644 --- a/src/bootstrap/src/core/config/toml/rust.rs +++ b/src/bootstrap/src/core/config/toml/rust.rs @@ -33,6 +33,7 @@ define_config! { channel: Option = "channel", musl_root: Option = "musl-root", rpath: Option = "rpath", + rustflags: Option> = "rustflags", strip: Option = "strip", frame_pointers: Option = "frame-pointers", stack_protector: Option = "stack-protector", @@ -373,6 +374,7 @@ pub fn check_incompatible_options_for_ci_rustc( parallel_frontend_threads: _, bootstrap_override_lld: _, bootstrap_override_lld_legacy: _, + rustflags: _, } = ci_rust_config; // There are two kinds of checks for CI rustc incompatible options: From 1e9e5d2df864e99df355b68e3edc747df8585151 Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Tue, 11 Nov 2025 13:04:53 +0100 Subject: [PATCH 2/5] make `RUSTFLAGS` take precedence over `rustflags` from `bootstrap.toml` --- src/bootstrap/src/core/builder/cargo.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index a48b41820fe7f..d38d1551512b2 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -25,9 +25,7 @@ struct Rustflags(String, TargetSelection); impl Rustflags { fn new(target: TargetSelection) -> Rustflags { - let mut ret = Rustflags(String::new(), target); - ret.propagate_cargo_env("RUSTFLAGS"); - ret + Rustflags(String::new(), target) } /// By default, cargo will pick up on various variables in the environment. However, bootstrap @@ -606,6 +604,8 @@ impl Builder<'_> { rustflags.arg(arg); } + rustflags.propagate_cargo_env("RUSTFLAGS"); + if build_compiler_stage != 0 { if let Ok(s) = env::var("CARGOFLAGS_NOT_BOOTSTRAP") { cargo.args(s.split_whitespace()); From 35892e8656fbdf55235a38ebf1f17ea796fb16fa Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Mon, 10 Nov 2025 16:19:51 +0100 Subject: [PATCH 3/5] feat: add target specific rustflags to 'bootstrap.toml` --- src/bootstrap/src/core/builder/cargo.rs | 9 +++++++++ src/bootstrap/src/core/config/config.rs | 2 ++ src/bootstrap/src/core/config/toml/target.rs | 2 ++ 3 files changed, 13 insertions(+) diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index d38d1551512b2..86f90c15525ea 100644 --- a/src/bootstrap/src/core/builder/cargo.rs +++ b/src/bootstrap/src/core/builder/cargo.rs @@ -604,6 +604,15 @@ impl Builder<'_> { rustflags.arg(arg); } + // target specific rustflags take precedence over general rustflags + if let Some(target_rustflags) = + self.config.target_config.get(&target).map(|t| &t.rustflags[..]) + { + for arg in target_rustflags { + rustflags.arg(arg); + } + } + rustflags.propagate_cargo_env("RUSTFLAGS"); if build_compiler_stage != 0 { diff --git a/src/bootstrap/src/core/config/config.rs b/src/bootstrap/src/core/config/config.rs index 05af7cfe4f7ed..f4122f04b9809 100644 --- a/src/bootstrap/src/core/config/config.rs +++ b/src/bootstrap/src/core/config/config.rs @@ -856,6 +856,7 @@ impl Config { sanitizers: target_sanitizers, profiler: target_profiler, rpath: target_rpath, + rustflags: target_rustflags, crt_static: target_crt_static, musl_root: target_musl_root, musl_libdir: target_musl_libdir, @@ -939,6 +940,7 @@ impl Config { target.sanitizers = target_sanitizers; target.profiler = target_profiler; target.rpath = target_rpath; + target.rustflags = target_rustflags.unwrap_or_default(); target.optimized_compiler_builtins = target_optimized_compiler_builtins; target.jemalloc = target_jemalloc; if let Some(backends) = target_codegen_backends { diff --git a/src/bootstrap/src/core/config/toml/target.rs b/src/bootstrap/src/core/config/toml/target.rs index 4c7afa50b9658..847b75e696b47 100644 --- a/src/bootstrap/src/core/config/toml/target.rs +++ b/src/bootstrap/src/core/config/toml/target.rs @@ -37,6 +37,7 @@ define_config! { sanitizers: Option = "sanitizers", profiler: Option = "profiler", rpath: Option = "rpath", + rustflags: Option> = "rustflags", crt_static: Option = "crt-static", musl_root: Option = "musl-root", musl_libdir: Option = "musl-libdir", @@ -70,6 +71,7 @@ pub struct Target { pub sanitizers: Option, pub profiler: Option, pub rpath: Option, + pub rustflags: Vec, pub crt_static: Option, pub musl_root: Option, pub musl_libdir: Option, From 493dd58e16e7e3a6f30cabfd5a3f33a469113ce0 Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Tue, 11 Nov 2025 13:30:32 +0100 Subject: [PATCH 4/5] document new `rustflags` options in `bootstrap.example.toml` --- bootstrap.example.toml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bootstrap.example.toml b/bootstrap.example.toml index 0d0f5e405d1f4..fe99542af06b0 100644 --- a/bootstrap.example.toml +++ b/bootstrap.example.toml @@ -705,6 +705,12 @@ # desired in distributions, for example. #rust.rpath = true +# Additional flags to pass to `rustc`. +# Passed before everything else (including per target `rustflags`). +# Applies to all stages and targets. +# +#rust.rustflags = [] + # Indicates whether symbols should be stripped using `-Cstrip=symbols`. #rust.strip = false @@ -1005,6 +1011,12 @@ # and will override the same option under [rust] section. It only works on Unix platforms #rpath = rust.rpath (bool) +# Additional flags to pass to `rustc`. +# Passed after global `rust.rustflags` but before everything else. +# Applies to all stages. +# +#rustflags = [] + # Force static or dynamic linkage of the standard library for this target. If # this target is a host for rustc, this will also affect the linkage of the # compiler itself. This is useful for building rustc on targets that normally From c700e86e2d4d61b29395b64f9e7d35c435089c60 Mon Sep 17 00:00:00 2001 From: Karol Zwolak Date: Mon, 10 Nov 2025 17:14:33 +0100 Subject: [PATCH 5/5] add entry to bootstrap change tracker for new `rustflags` options --- src/bootstrap/src/utils/change_tracker.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/bootstrap/src/utils/change_tracker.rs b/src/bootstrap/src/utils/change_tracker.rs index 59140b9ce84c8..72b76a5e6e435 100644 --- a/src/bootstrap/src/utils/change_tracker.rs +++ b/src/bootstrap/src/utils/change_tracker.rs @@ -581,4 +581,9 @@ pub const CONFIG_CHANGE_HISTORY: &[ChangeInfo] = &[ severity: ChangeSeverity::Info, summary: "The `build.python` option is now respected on macOS (previously ignored and forced to be /usr/bin/python3).", }, + ChangeInfo { + change_id: 148795, + severity: ChangeSeverity::Info, + summary: "New options `rust.rustflags` for all targets and `rustflags` par target that will pass specified flags to rustc for all stages. Target specific flags override (are passed after) global `rust.rustflags` ones.", + }, ];