Skip to content

Commit be6e38c

Browse files
committed
Auto merge of #112756 - hi-rustin:rustin-patch-bootstrap, r=clubby789
Use RustOptimize to set optimize close #112678 Use RustOptimize to set optimize.
2 parents 8e2d5e3 + 7cab8f7 commit be6e38c

File tree

5 files changed

+65
-9
lines changed

5 files changed

+65
-9
lines changed

src/bootstrap/builder.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ impl<'a> Builder<'a> {
12071207
assert_eq!(target, compiler.host);
12081208
}
12091209

1210-
if self.config.rust_optimize {
1210+
if self.config.rust_optimize.is_release() {
12111211
// FIXME: cargo bench/install do not accept `--release`
12121212
if cmd != "bench" && cmd != "install" {
12131213
cargo.arg("--release");
@@ -1263,7 +1263,7 @@ impl<'a> Builder<'a> {
12631263
}
12641264

12651265
let profile_var = |name: &str| {
1266-
let profile = if self.config.rust_optimize { "RELEASE" } else { "DEV" };
1266+
let profile = if self.config.rust_optimize.is_release() { "RELEASE" } else { "DEV" };
12671267
format!("CARGO_PROFILE_{}_{}", profile, name)
12681268
};
12691269

@@ -1652,6 +1652,9 @@ impl<'a> Builder<'a> {
16521652
}
16531653
};
16541654
cargo.env(profile_var("DEBUG"), debuginfo_level.to_string());
1655+
if let Some(opt_level) = &self.config.rust_optimize.get_opt_level() {
1656+
cargo.env(profile_var("OPT_LEVEL"), opt_level);
1657+
}
16551658
if !self.config.dry_run() && self.cc.borrow()[&target].args().iter().any(|arg| arg == "-gz")
16561659
{
16571660
rustflags.arg("-Clink-arg=-gz");

src/bootstrap/config.rs

+43-5
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ pub struct Config {
202202
pub llvm_use_libcxx: bool,
203203

204204
// rust codegen options
205-
pub rust_optimize: bool,
205+
pub rust_optimize: RustOptimize,
206206
pub rust_codegen_units: Option<u32>,
207207
pub rust_codegen_units_std: Option<u32>,
208208
pub rust_debug_assertions: bool,
@@ -875,17 +875,55 @@ impl Default for StringOrBool {
875875
}
876876
}
877877

878+
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
879+
#[serde(untagged)]
880+
pub enum RustOptimize {
881+
#[serde(deserialize_with = "deserialize_and_validate_opt_level")]
882+
String(String),
883+
Bool(bool),
884+
}
885+
886+
impl Default for RustOptimize {
887+
fn default() -> RustOptimize {
888+
RustOptimize::Bool(false)
889+
}
890+
}
891+
892+
fn deserialize_and_validate_opt_level<'de, D>(d: D) -> Result<String, D::Error>
893+
where
894+
D: serde::de::Deserializer<'de>,
895+
{
896+
let v = String::deserialize(d)?;
897+
if ["0", "1", "2", "3", "s", "z"].iter().find(|x| **x == v).is_some() {
898+
Ok(v)
899+
} else {
900+
Err(format!(r#"unrecognized option for rust optimize: "{}", expected one of "0", "1", "2", "3", "s", "z""#, v)).map_err(serde::de::Error::custom)
901+
}
902+
}
903+
904+
impl RustOptimize {
905+
pub(crate) fn is_release(&self) -> bool {
906+
if let RustOptimize::Bool(true) | RustOptimize::String(_) = &self { true } else { false }
907+
}
908+
909+
pub(crate) fn get_opt_level(&self) -> Option<String> {
910+
match &self {
911+
RustOptimize::String(s) => Some(s.clone()),
912+
RustOptimize::Bool(_) => None,
913+
}
914+
}
915+
}
916+
878917
#[derive(Deserialize)]
879918
#[serde(untagged)]
880919
enum StringOrInt<'a> {
881920
String(&'a str),
882921
Int(i64),
883922
}
884-
885923
define_config! {
886924
/// TOML representation of how the Rust build is configured.
887925
struct Rust {
888-
optimize: Option<bool> = "optimize",
926+
optimize: Option<RustOptimize> = "optimize",
889927
debug: Option<bool> = "debug",
890928
codegen_units: Option<u32> = "codegen-units",
891929
codegen_units_std: Option<u32> = "codegen-units-std",
@@ -971,7 +1009,7 @@ impl Config {
9711009
config.ninja_in_file = true;
9721010
config.llvm_static_stdcpp = false;
9731011
config.backtrace = true;
974-
config.rust_optimize = true;
1012+
config.rust_optimize = RustOptimize::Bool(true);
9751013
config.rust_optimize_tests = true;
9761014
config.submodules = None;
9771015
config.docs = true;
@@ -1546,7 +1584,7 @@ impl Config {
15461584
config.llvm_assertions = llvm_assertions.unwrap_or(false);
15471585
config.llvm_tests = llvm_tests.unwrap_or(false);
15481586
config.llvm_plugins = llvm_plugins.unwrap_or(false);
1549-
config.rust_optimize = optimize.unwrap_or(true);
1587+
config.rust_optimize = optimize.unwrap_or(RustOptimize::Bool(true));
15501588

15511589
let default = debug == Some(true);
15521590
config.rust_debug_assertions = debug_assertions.unwrap_or(default);

src/bootstrap/config/tests.rs

+15
Original file line numberDiff line numberDiff line change
@@ -178,3 +178,18 @@ fn profile_user_dist() {
178178
}
179179
Config::parse_inner(&["check".to_owned()], get_toml);
180180
}
181+
182+
#[test]
183+
fn rust_optimize() {
184+
assert_eq!(parse("").rust_optimize.is_release(), true);
185+
assert_eq!(parse("rust.optimize = false").rust_optimize.is_release(), false);
186+
assert_eq!(parse("rust.optimize = true").rust_optimize.is_release(), true);
187+
assert_eq!(parse("rust.optimize = \"1\"").rust_optimize.get_opt_level(), Some("1".to_string()));
188+
assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.get_opt_level(), Some("s".to_string()));
189+
}
190+
191+
#[test]
192+
#[should_panic]
193+
fn invalid_rust_optimize() {
194+
parse("rust.optimize = \"a\"");
195+
}

src/bootstrap/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ impl Build {
785785
/// Component directory that Cargo will produce output into (e.g.
786786
/// release/debug)
787787
fn cargo_dir(&self) -> &'static str {
788-
if self.config.rust_optimize { "release" } else { "debug" }
788+
if self.config.rust_optimize.is_release() { "release" } else { "debug" }
789789
}
790790

791791
fn tools_dir(&self, compiler: Compiler) -> PathBuf {

src/bootstrap/test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -801,7 +801,7 @@ impl Step for Clippy {
801801
cargo.arg("-p").arg("clippy_dev");
802802
// clippy_dev gets confused if it can't find `clippy/Cargo.toml`
803803
cargo.current_dir(&builder.src.join("src").join("tools").join("clippy"));
804-
if builder.config.rust_optimize {
804+
if builder.config.rust_optimize.is_release() {
805805
cargo.env("PROFILE", "release");
806806
} else {
807807
cargo.env("PROFILE", "debug");

0 commit comments

Comments
 (0)