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 diff --git a/src/bootstrap/src/core/builder/cargo.rs b/src/bootstrap/src/core/builder/cargo.rs index c2029f97391d4..86f90c15525ea 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 @@ -601,6 +599,22 @@ impl Builder<'_> { } let mut rustflags = Rustflags::new(target); + + for arg in &self.config.rust_rustflags { + 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 { 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..f4122f04b9809 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 { @@ -854,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, @@ -937,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 { @@ -1431,6 +1435,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: 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, 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.", + }, ];