Rollup merge of #113273 - hi-rustin:rustin-patch-opt-level, r=Kobzol

Use String or Int to set the opt level

Address https://github.com/rust-lang/rust/pull/112756/files#r1249345725

Use String or Int to set the opt level.

r? ``@jyn514``
This commit is contained in:
Matthias Krüger 2023-07-09 16:33:36 +02:00 committed by GitHub
commit a46589f76b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 16 deletions

View File

@ -400,10 +400,20 @@ changelog-seen = 2
# ============================================================================= # =============================================================================
[rust] [rust]
# Whether or not to optimize the compiler and standard library. # Whether or not to optimize when compiling the compiler and standard library,
# and what level of optimization to use.
# WARNING: Building with optimize = false is NOT SUPPORTED. Due to bootstrapping, # WARNING: Building with optimize = false is NOT SUPPORTED. Due to bootstrapping,
# building without optimizations takes much longer than optimizing. Further, some platforms # building without optimizations takes much longer than optimizing. Further, some platforms
# fail to build without this optimization (c.f. #65352). # fail to build without this optimization (c.f. #65352).
# The valid options are:
# true - Enable optimizations.
# false - Disable optimizations.
# 0 - Disable optimizations.
# 1 - Basic optimizations.
# 2 - Some optimizations.
# 3 - All optimizations.
# "s" - Optimize for binary size.
# "z" - Optimize for binary size, but also turn off loop vectorization.
#optimize = true #optimize = true
# Indicates that the build should be configured for debugging Rust. A # Indicates that the build should be configured for debugging Rust. A
@ -757,7 +767,7 @@ changelog-seen = 2
# This option will override the same option under [build] section. # This option will override the same option under [build] section.
#profiler = build.profiler (bool) #profiler = build.profiler (bool)
# This option supports enable `rpath` in each target independently, # This option supports enable `rpath` in each target independently,
# and will override the same option under [rust] section. It only works on Unix platforms # and will override the same option under [rust] section. It only works on Unix platforms
#rpath = rust.rpath (bool) #rpath = rust.rpath (bool)

View File

@ -875,11 +875,10 @@ impl Default for StringOrBool {
} }
} }
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
#[serde(untagged)]
pub enum RustOptimize { pub enum RustOptimize {
#[serde(deserialize_with = "deserialize_and_validate_opt_level")]
String(String), String(String),
Int(u8),
Bool(bool), Bool(bool),
} }
@ -889,26 +888,74 @@ impl Default for RustOptimize {
} }
} }
fn deserialize_and_validate_opt_level<'de, D>(d: D) -> Result<String, D::Error> impl<'de> Deserialize<'de> for RustOptimize {
where fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
D: serde::de::Deserializer<'de>, where
{ D: Deserializer<'de>,
let v = String::deserialize(d)?; {
if ["0", "1", "2", "3", "s", "z"].iter().find(|x| **x == v).is_some() { deserializer.deserialize_any(OptimizeVisitor)
Ok(v)
} else {
Err(format!(r#"unrecognized option for rust optimize: "{}", expected one of "0", "1", "2", "3", "s", "z""#, v)).map_err(serde::de::Error::custom)
} }
} }
struct OptimizeVisitor;
impl<'de> serde::de::Visitor<'de> for OptimizeVisitor {
type Value = RustOptimize;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str(r#"one of: 0, 1, 2, 3, "s", "z", true, false"#)
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if ["s", "z"].iter().find(|x| **x == value).is_some() {
Ok(RustOptimize::String(value.to_string()))
} else {
Err(format_optimize_error_msg(value)).map_err(serde::de::Error::custom)
}
}
fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
if matches!(value, 0..=3) {
Ok(RustOptimize::Int(value as u8))
} else {
Err(format_optimize_error_msg(value)).map_err(serde::de::Error::custom)
}
}
fn visit_bool<E>(self, value: bool) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(RustOptimize::Bool(value))
}
}
fn format_optimize_error_msg(v: impl std::fmt::Display) -> String {
format!(
r#"unrecognized option for rust optimize: "{}", expected one of 0, 1, 2, 3, "s", "z", true, false"#,
v
)
}
impl RustOptimize { impl RustOptimize {
pub(crate) fn is_release(&self) -> bool { pub(crate) fn is_release(&self) -> bool {
if let RustOptimize::Bool(true) | RustOptimize::String(_) = &self { true } else { false } match &self {
RustOptimize::Bool(true) | RustOptimize::String(_) => true,
RustOptimize::Int(i) => *i > 0,
RustOptimize::Bool(false) => false,
}
} }
pub(crate) fn get_opt_level(&self) -> Option<String> { pub(crate) fn get_opt_level(&self) -> Option<String> {
match &self { match &self {
RustOptimize::String(s) => Some(s.clone()), RustOptimize::String(s) => Some(s.clone()),
RustOptimize::Int(i) => Some(i.to_string()),
RustOptimize::Bool(_) => None, RustOptimize::Bool(_) => None,
} }
} }

View File

@ -184,7 +184,10 @@ fn rust_optimize() {
assert_eq!(parse("").rust_optimize.is_release(), true); assert_eq!(parse("").rust_optimize.is_release(), true);
assert_eq!(parse("rust.optimize = false").rust_optimize.is_release(), false); assert_eq!(parse("rust.optimize = false").rust_optimize.is_release(), false);
assert_eq!(parse("rust.optimize = true").rust_optimize.is_release(), true); assert_eq!(parse("rust.optimize = true").rust_optimize.is_release(), true);
assert_eq!(parse("rust.optimize = \"1\"").rust_optimize.get_opt_level(), Some("1".to_string())); assert_eq!(parse("rust.optimize = 0").rust_optimize.is_release(), false);
assert_eq!(parse("rust.optimize = 1").rust_optimize.is_release(), true);
assert_eq!(parse("rust.optimize = 1").rust_optimize.get_opt_level(), Some("1".to_string()));
assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.is_release(), true);
assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.get_opt_level(), Some("s".to_string())); assert_eq!(parse("rust.optimize = \"s\"").rust_optimize.get_opt_level(), Some("s".to_string()));
} }