From 8cf9c08af2ee7dc47fae19a519a6d96a78c590bf Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Jul 2023 08:55:25 +0100 Subject: [PATCH 1/3] Set lto of metadata release profile to Thin --- crates/build/src/lib.rs | 3 +++ crates/build/src/metadata.rs | 7 ++++++- crates/build/src/workspace/manifest.rs | 10 ---------- crates/build/src/workspace/mod.rs | 7 ++++++- crates/build/src/workspace/profile.rs | 10 +++++----- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index a1fd2aee3..ca95b2850 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -63,6 +63,9 @@ pub use self::{ Manifest, ManifestPath, Profile, + Lto, + OptLevel, + PanicStrategy, Workspace, }, }; diff --git a/crates/build/src/metadata.rs b/crates/build/src/metadata.rs index d6cad3fa2..e5d8962f3 100644 --- a/crates/build/src/metadata.rs +++ b/crates/build/src/metadata.rs @@ -23,6 +23,8 @@ use crate::{ ManifestPath, Workspace, }, + Profile, + Lto, BuildMode, BuildSteps, Features, @@ -188,7 +190,10 @@ pub(crate) fn execute( .with_root_package_manifest(|manifest| { manifest .with_added_crate_type("rlib")? - .with_profile_release_lto(false)? + .with_profile_release_defaults(Profile { + lto: Lto::Thin, + ..Profile::default_contract_release() + })? .with_empty_workspace(); Ok(()) })? diff --git a/crates/build/src/workspace/manifest.rs b/crates/build/src/workspace/manifest.rs index 4b42249f6..83dbbc39f 100644 --- a/crates/build/src/workspace/manifest.rs +++ b/crates/build/src/workspace/manifest.rs @@ -214,16 +214,6 @@ impl Manifest { .map(Into::into) } - /// Set `[profile.release]` lto flag - pub fn with_profile_release_lto(&mut self, enabled: bool) -> Result<&mut Self> { - let lto = self - .profile_release_table_mut()? - .entry("lto") - .or_insert(enabled.into()); - *lto = enabled.into(); - Ok(self) - } - /// Set preferred defaults for the `[profile.release]` section /// /// # Note diff --git a/crates/build/src/workspace/mod.rs b/crates/build/src/workspace/mod.rs index a9fa4728a..587088f92 100644 --- a/crates/build/src/workspace/mod.rs +++ b/crates/build/src/workspace/mod.rs @@ -24,7 +24,12 @@ pub use self::{ Manifest, ManifestPath, }, - profile::Profile, + profile::{ + Profile, + Lto, + OptLevel, + PanicStrategy, + }, }; use anyhow::Result; diff --git a/crates/build/src/workspace/profile.rs b/crates/build/src/workspace/profile.rs index 9b2455134..6f0489547 100644 --- a/crates/build/src/workspace/profile.rs +++ b/crates/build/src/workspace/profile.rs @@ -18,12 +18,12 @@ use toml::value; /// Subset of cargo profile settings to configure defaults for building contracts pub struct Profile { - opt_level: OptLevel, - lto: Lto, + pub opt_level: OptLevel, + pub lto: Lto, // `None` means use rustc default. - codegen_units: Option, - overflow_checks: bool, - panic: PanicStrategy, + pub codegen_units: Option, + pub overflow_checks: bool, + pub panic: PanicStrategy, } impl Profile { From 457890c2b8549e1495cdec75bac2bdc31a96c5d3 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Jul 2023 09:00:12 +0100 Subject: [PATCH 2/3] Fmt --- crates/build/src/lib.rs | 4 ++-- crates/build/src/metadata.rs | 4 ++-- crates/build/src/workspace/mod.rs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index ca95b2850..362bb2319 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -60,12 +60,12 @@ pub use self::{ OptimizationResult, }, workspace::{ + Lto, Manifest, ManifestPath, - Profile, - Lto, OptLevel, PanicStrategy, + Profile, Workspace, }, }; diff --git a/crates/build/src/metadata.rs b/crates/build/src/metadata.rs index e5d8962f3..1ee721f49 100644 --- a/crates/build/src/metadata.rs +++ b/crates/build/src/metadata.rs @@ -23,13 +23,13 @@ use crate::{ ManifestPath, Workspace, }, - Profile, - Lto, BuildMode, BuildSteps, Features, + Lto, Network, OptimizationPasses, + Profile, UnstableFlags, Verbosity, }; diff --git a/crates/build/src/workspace/mod.rs b/crates/build/src/workspace/mod.rs index 587088f92..8bc28cc21 100644 --- a/crates/build/src/workspace/mod.rs +++ b/crates/build/src/workspace/mod.rs @@ -25,10 +25,10 @@ pub use self::{ ManifestPath, }, profile::{ - Profile, Lto, OptLevel, PanicStrategy, + Profile, }, }; From 0afe2cb45f4ae07ea7381b1b0c892013a2808ec0 Mon Sep 17 00:00:00 2001 From: Andrew Jones Date: Thu, 6 Jul 2023 09:40:27 +0100 Subject: [PATCH 3/3] Use profile defaults if not specified --- crates/build/src/metadata.rs | 4 +- crates/build/src/workspace/profile.rs | 56 ++++++++++++++++++--------- 2 files changed, 39 insertions(+), 21 deletions(-) diff --git a/crates/build/src/metadata.rs b/crates/build/src/metadata.rs index 1ee721f49..7fc62391d 100644 --- a/crates/build/src/metadata.rs +++ b/crates/build/src/metadata.rs @@ -191,8 +191,8 @@ pub(crate) fn execute( manifest .with_added_crate_type("rlib")? .with_profile_release_defaults(Profile { - lto: Lto::Thin, - ..Profile::default_contract_release() + lto: Some(Lto::Thin), + ..Profile::default() })? .with_empty_workspace(); Ok(()) diff --git a/crates/build/src/workspace/profile.rs b/crates/build/src/workspace/profile.rs index 6f0489547..e9e22f324 100644 --- a/crates/build/src/workspace/profile.rs +++ b/crates/build/src/workspace/profile.rs @@ -16,25 +16,29 @@ use toml::value; -/// Subset of cargo profile settings to configure defaults for building contracts +/// Subset of cargo profile settings to configure defaults for building contracts. +/// +/// All fields are optional, and if not set, the default value from cargo will be used. +/// See https://doc.rust-lang.org/cargo/reference/profiles.html#default-profiles. +#[derive(Default)] pub struct Profile { - pub opt_level: OptLevel, - pub lto: Lto, + pub opt_level: Option, + pub lto: Option, // `None` means use rustc default. pub codegen_units: Option, - pub overflow_checks: bool, - pub panic: PanicStrategy, + pub overflow_checks: Option, + pub panic: Option, } impl Profile { /// The preferred set of defaults for compiling a release build of a contract pub fn default_contract_release() -> Profile { Profile { - opt_level: OptLevel::Z, - lto: Lto::Fat, + opt_level: Some(OptLevel::Z), + lto: Some(Lto::Fat), codegen_units: Some(1), - overflow_checks: true, - panic: PanicStrategy::Abort, + overflow_checks: Some(true), + panic: Some(PanicStrategy::Abort), } } @@ -46,18 +50,32 @@ impl Profile { /// - If a profile setting is not defined, the value from this profile instance will /// be added pub(super) fn merge(&self, profile: &mut value::Table) { - let mut set_value_if_vacant = |key: &'static str, value: value::Value| { - if !profile.contains_key(key) { - profile.insert(key.into(), value); + fn set_value_if_vacant( + key: &'static str, + value: Option, + profile: &mut value::Table, + ) where + T: Into, + { + if let Some(value) = value { + if !profile.contains_key(key) { + profile.insert(key.into(), value.into()); + } } - }; - set_value_if_vacant("opt-level", self.opt_level.to_toml_value()); - set_value_if_vacant("lto", self.lto.to_toml_value()); - if let Some(codegen_units) = self.codegen_units { - set_value_if_vacant("codegen-units", codegen_units.into()); } - set_value_if_vacant("overflow-checks", self.overflow_checks.into()); - set_value_if_vacant("panic", self.panic.to_toml_value()); + set_value_if_vacant( + "opt-level", + self.opt_level.map(OptLevel::to_toml_value), + profile, + ); + set_value_if_vacant("lto", self.lto.map(Lto::to_toml_value), profile); + set_value_if_vacant("codegen-units", self.codegen_units, profile); + set_value_if_vacant("overflow-checks", self.overflow_checks, profile); + set_value_if_vacant( + "panic", + self.panic.map(PanicStrategy::to_toml_value), + profile, + ); } }