diff --git a/crates/build/src/lib.rs b/crates/build/src/lib.rs index c832d935a..c94148db9 100644 --- a/crates/build/src/lib.rs +++ b/crates/build/src/lib.rs @@ -61,8 +61,11 @@ pub use self::{ OptimizationResult, }, workspace::{ + Lto, Manifest, ManifestPath, + OptLevel, + PanicStrategy, Profile, Workspace, }, diff --git a/crates/build/src/metadata.rs b/crates/build/src/metadata.rs index cf829fe6d..5266ccf96 100644 --- a/crates/build/src/metadata.rs +++ b/crates/build/src/metadata.rs @@ -26,8 +26,10 @@ use crate::{ BuildMode, BuildSteps, Features, + Lto, Network, OptimizationPasses, + Profile, UnstableFlags, Verbosity, }; @@ -180,7 +182,10 @@ pub fn execute( .with_root_package_manifest(|manifest| { manifest .with_added_crate_type("rlib")? - .with_profile_release_lto(false)? + .with_profile_release_defaults(Profile { + lto: Some(Lto::Thin), + ..Profile::default() + })? .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..8bc28cc21 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::{ + Lto, + OptLevel, + PanicStrategy, + Profile, + }, }; use anyhow::Result; diff --git a/crates/build/src/workspace/profile.rs b/crates/build/src/workspace/profile.rs index 9b2455134..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 { - opt_level: OptLevel, - lto: Lto, + pub opt_level: Option, + pub lto: Option, // `None` means use rustc default. - codegen_units: Option, - overflow_checks: bool, - panic: PanicStrategy, + pub codegen_units: Option, + 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, + ); } }