From 281802152b3c572c8a6a2ba30c3e6abd34595cd6 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Mon, 22 Mar 2021 08:55:46 +0100 Subject: [PATCH 01/11] Reduce code for `OptimizationPasses` --- src/cmd/build.rs | 32 +++++++++++++++++++------- src/main.rs | 58 +++++++++++++++++++++++++----------------------- 2 files changed, 54 insertions(+), 36 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 60efcd5b..986a274e 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -29,8 +29,8 @@ use crate::{ crate_metadata::CrateMetadata, maybe_println, util, validate_wasm, workspace::{ManifestPath, Profile, Workspace}, - BuildArtifacts, BuildResult, OptimizationFlags, OptimizationPasses, OptimizationResult, - UnstableFlags, UnstableOptions, Verbosity, VerbosityFlags, + BuildArtifacts, BuildResult, OptimizationPasses, OptimizationResult, UnstableFlags, + UnstableOptions, Verbosity, VerbosityFlags, }; use anyhow::{Context, Result}; use colored::Colorize; @@ -66,8 +66,26 @@ pub struct BuildCommand { verbosity: VerbosityFlags, #[structopt(flatten)] unstable_options: UnstableOptions, - #[structopt(flatten)] - optimization_passes: OptimizationFlags, + /// Number of optimization passes, passed as an argument to wasm-opt. + /// + /// - `0`: execute no optimization passes + /// + /// - `1`: execute 1 optimization pass (quick & useful opts, useful for iteration builds) + /// + /// - `2`, execute 2 optimization passes (most opts, generally gets most perf) + /// + /// - `3`, execute 3 optimization passes (spends potentially a lot of time optimizing) + /// + /// - `4`, execute 4 optimization passes (also flatten the IR, which can take a lot more time and memory + /// but is useful on more nested / complex / less-optimized input) + /// + /// - `s`, execute default optimization passes, focusing on code size + /// + /// - `z`, execute default optimization passes, super-focusing on code size + /// + /// - + #[structopt(long = "optimization-passes", default_value)] + optimization_passes: OptimizationPasses, } impl BuildCommand { @@ -76,14 +94,12 @@ impl BuildCommand { let unstable_flags: UnstableFlags = TryFrom::<&UnstableOptions>::try_from(&self.unstable_options)?; let verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?; - let optimization_passes = - TryFrom::<&OptimizationFlags>::try_from(&self.optimization_passes)?; execute( &manifest_path, verbosity, self.build_artifact, unstable_flags, - optimization_passes, + self.optimization_passes, ) } } @@ -354,7 +370,7 @@ fn do_optimization( let output = Command::new("wasm-opt") .arg(dest_wasm) - .arg(format!("-O{}", optimization_level.to_str())) + .arg(format!("-O{}", optimization_level)) .arg("-o") .arg(dest_optimized) // the memory in our module is imported, `wasm-opt` needs to be told that diff --git a/src/main.rs b/src/main.rs index b34c10b0..32b5ee39 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,11 @@ use crate::cmd::{metadata::MetadataResult, BuildCommand, CheckCommand}; #[cfg(feature = "extrinsics")] use sp_core::{crypto::Pair, sr25519, H256}; -use std::{convert::TryFrom, path::PathBuf}; +use std::{ + convert::TryFrom, + fmt::{Display, Formatter, Result as DisplayResult}, + path::PathBuf, +}; #[cfg(feature = "extrinsics")] use subxt::PairSigner; @@ -93,7 +97,7 @@ impl ExtrinsicOpts { } } -#[derive(Clone, Debug, StructOpt)] +#[derive(Clone, StructOpt)] pub struct OptimizationFlags { /// Number of optimization passes, passed as an argument to wasm-opt. /// @@ -106,18 +110,18 @@ pub struct OptimizationFlags { /// - `3`, execute 3 optimization passes (spends potentially a lot of time optimizing) /// /// - `4`, execute 4 optimization passes (also flatten the IR, which can take a lot more time and memory - /// but is useful on more nested / complex / less-optimized input) + /// but is useful on more nested / complex / less-optimized input) /// /// - `s`, execute default optimization passes, focusing on code size /// /// - `z`, execute default optimization passes, super-focusing on code size /// /// - - #[structopt(long = "optimization-passes", default_value = "3")] - optimization_passes: String, + #[structopt(long = "optimization-passes", default_value)] + optimization_passes: OptimizationPasses, } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Debug)] pub enum OptimizationPasses { Zero, One, @@ -128,17 +132,32 @@ pub enum OptimizationPasses { Z, } +impl Display for OptimizationPasses { + fn fmt(&self, f: &mut Formatter<'_>) -> DisplayResult { + let out = match self { + OptimizationPasses::Zero => "0", + OptimizationPasses::One => "1", + OptimizationPasses::Two => "2", + OptimizationPasses::Three => "3", + OptimizationPasses::Four => "4", + OptimizationPasses::S => "s", + OptimizationPasses::Z => "z", + }; + write!(f, "{}", out) + } +} + impl Default for OptimizationPasses { fn default() -> OptimizationPasses { OptimizationPasses::Three } } -impl TryFrom<&OptimizationFlags> for OptimizationPasses { - type Error = Error; +impl std::str::FromStr for OptimizationPasses { + type Err = Error; - fn try_from(value: &OptimizationFlags) -> Result { - match value.optimization_passes.to_lowercase().as_str() { + fn from_str(input: &str) -> std::result::Result { + match input.to_lowercase().as_str() { "0" => Ok(OptimizationPasses::Zero), "1" => Ok(OptimizationPasses::One), "2" => Ok(OptimizationPasses::Two), @@ -146,29 +165,12 @@ impl TryFrom<&OptimizationFlags> for OptimizationPasses { "4" => Ok(OptimizationPasses::Four), "s" => Ok(OptimizationPasses::S), "z" => Ok(OptimizationPasses::Z), - _ => anyhow::bail!( - "Unknown optimization passes option {}", - value.optimization_passes - ), + _ => anyhow::bail!("Unknown optimization passes for option {}", input), } } } impl OptimizationPasses { - /// Returns the string representation of `OptimizationPasses` - #[cfg(not(feature = "binaryen-as-dependency"))] - pub(crate) fn to_str(&self) -> &str { - match self { - OptimizationPasses::Zero => "0", - OptimizationPasses::One => "1", - OptimizationPasses::Two => "2", - OptimizationPasses::Three => "3", - OptimizationPasses::Four => "4", - OptimizationPasses::S => "s", - OptimizationPasses::Z => "z", - } - } - /// Returns the number of optimization passes to do #[cfg(feature = "binaryen-as-dependency")] pub(crate) fn to_passes(&self) -> u32 { -- GitLab From 350aeae31eb1087c8034758a48605de5e8f74d46 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 10:13:42 +0100 Subject: [PATCH 02/11] =?UTF-8?q?Fix=20typo:=20zeros=20=E2=9E=9C=20zeroes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cmd/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 986a274e..308ac1da 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -374,7 +374,7 @@ fn do_optimization( .arg("-o") .arg(dest_optimized) // the memory in our module is imported, `wasm-opt` needs to be told that - // the memory is initialized to zeros, otherwise it won't run the + // the memory is initialized to zeroes, otherwise it won't run the // memory-packing pre-pass. .arg("--zero-filled-memory") .output()?; -- GitLab From 1b5a1280e2aed53a03b2b5718a235eadd8ca0332 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 10:02:03 +0100 Subject: [PATCH 03/11] Reduce code for `OptimizationPasses` --- src/cmd/build.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 308ac1da..7b62ed80 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -122,13 +122,12 @@ impl CheckCommand { let unstable_flags: UnstableFlags = TryFrom::<&UnstableOptions>::try_from(&self.unstable_options)?; let verbosity: Verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?; - let optimization_passes = OptimizationPasses::Zero; execute( &manifest_path, verbosity, BuildArtifacts::CheckOnly, unstable_flags, - optimization_passes, + OptimizationPasses::Zero, ) } } -- GitLab From f77e98a32f65e36817dd6b010b74bfe05c620be8 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 10:13:28 +0100 Subject: [PATCH 04/11] Add log output for optimization flags --- src/cmd/build.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 7b62ed80..d51afc77 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -333,6 +333,14 @@ fn do_optimization( // The default debug_info: false, }; + log::info!( + "Optimization level passed to `binaryen` dependency: {}", + codegen_config.optimization_level + ); + log::info!( + "Shrink level passed to `binaryen` dependency: {}", + codegen_config.shrink_level + ); let mut module = binaryen::Module::read(&dest_wasm_file_content) .map_err(|_| anyhow::anyhow!("binaryen failed to read file content"))?; module.optimize(&codegen_config); @@ -367,6 +375,10 @@ fn do_optimization( ); } + log::info!( + "Optimization level passed to wasm-opt: {}", + optimization_level + ); let output = Command::new("wasm-opt") .arg(dest_wasm) .arg(format!("-O{}", optimization_level)) -- GitLab From 2abf29e07c61ff6165f88524ad3e7f36a443d732 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 13:09:48 +0100 Subject: [PATCH 05/11] Support `optimization-passes` in the release profile --- src/cmd/build.rs | 74 +++++++++++++++++++++++++++++++++++---- src/main.rs | 41 ++++++++-------------- src/workspace/manifest.rs | 22 ++++++++++++ 3 files changed, 103 insertions(+), 34 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index d51afc77..063e60f9 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -28,7 +28,7 @@ use std::{process::Command, str}; use crate::{ crate_metadata::CrateMetadata, maybe_println, util, validate_wasm, - workspace::{ManifestPath, Profile, Workspace}, + workspace::{Manifest, ManifestPath, Profile, Workspace}, BuildArtifacts, BuildResult, OptimizationPasses, OptimizationResult, UnstableFlags, UnstableOptions, Verbosity, VerbosityFlags, }; @@ -83,9 +83,13 @@ pub struct BuildCommand { /// /// - `z`, execute default optimization passes, super-focusing on code size /// - /// - - #[structopt(long = "optimization-passes", default_value)] - optimization_passes: OptimizationPasses, + /// - The default value is `3` + /// + /// - It is possible to define the number of optimization passes in the `[profile.release]` of + /// your `Cargo.toml` as e.g. `optimization-passes = "3"`. The CLI argument always takes + /// precedence over the profile value. + #[structopt(long = "optimization-passes")] + optimization_passes: Option, } impl BuildCommand { @@ -94,12 +98,28 @@ impl BuildCommand { let unstable_flags: UnstableFlags = TryFrom::<&UnstableOptions>::try_from(&self.unstable_options)?; let verbosity = TryFrom::<&VerbosityFlags>::try_from(&self.verbosity)?; + + // The CLI flag `optimization-passes` overwrites optimization passes which are + // potentially defined in the `Cargo.toml` profile. + let optimization_passes = match self.optimization_passes { + Some(opt_passes) => opt_passes, + None => { + let mut manifest = Manifest::new(manifest_path.clone())?; + match manifest.get_profile_release_optimization_passes() { + // if no setting is found, neither on the cli nor in the profile, + // then we use the default + None => OptimizationPasses::default(), + Some(opt_passes) => opt_passes, + } + } + }; + execute( &manifest_path, verbosity, self.build_artifact, unstable_flags, - self.optimization_passes, + optimization_passes, ) } } @@ -481,8 +501,11 @@ pub(crate) fn execute( #[cfg(test)] mod tests_ci_only { use crate::{ - cmd, util::tests::with_tmp_dir, BuildArtifacts, ManifestPath, OptimizationPasses, - UnstableFlags, Verbosity, + cmd::{self, BuildCommand}, + util::tests::with_tmp_dir, + workspace::Manifest, + BuildArtifacts, ManifestPath, OptimizationPasses, UnstableFlags, UnstableOptions, + Verbosity, VerbosityFlags, }; #[test] @@ -553,4 +576,41 @@ mod tests_ci_only { Ok(()) }) } + + #[test] + fn cli_optimization_passes_must_take_precedence_over_profile() { + with_tmp_dir(|path| { + // given + cmd::new::execute("new_project", Some(path)).expect("new project creation failed"); + let cargo_toml_path = path.join("new_project").join("Cargo.toml"); + let manifest_path = + ManifestPath::new(&cargo_toml_path).expect("manifest path creation failed"); + // we write "4" as the optimization passes into the release profile + assert!(Manifest::new(manifest_path.clone())? + .set_profile_release_optimization_passes(String::from("4").into()) + .is_ok()); + let cmd = BuildCommand { + manifest_path: Some(cargo_toml_path), + build_artifact: BuildArtifacts::All, + verbosity: VerbosityFlags::default(), + unstable_options: UnstableOptions::default(), + + // we choose zero optimization passes as the "cli" parameter + optimization_passes: Some(OptimizationPasses::Zero), + }; + + // when + let res = cmd.exec().expect("build failed"); + let optimization = res + .optimization_result + .expect("no optimization result available"); + + // then + // we have to truncate here to account for a possible small delta + // in the floating point numbers + assert!(optimization.optimized_size.trunc() == optimization.original_size.trunc()); + + Ok(()) + }) + } } diff --git a/src/main.rs b/src/main.rs index 32b5ee39..b979b158 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,7 @@ use std::{ convert::TryFrom, fmt::{Display, Formatter, Result as DisplayResult}, path::PathBuf, + str::FromStr, }; #[cfg(feature = "extrinsics")] use subxt::PairSigner; @@ -97,30 +98,6 @@ impl ExtrinsicOpts { } } -#[derive(Clone, StructOpt)] -pub struct OptimizationFlags { - /// Number of optimization passes, passed as an argument to wasm-opt. - /// - /// - `0`: execute no optimization passes - /// - /// - `1`: execute 1 optimization pass (quick & useful opts, useful for iteration builds) - /// - /// - `2`, execute 2 optimization passes (most opts, generally gets most perf) - /// - /// - `3`, execute 3 optimization passes (spends potentially a lot of time optimizing) - /// - /// - `4`, execute 4 optimization passes (also flatten the IR, which can take a lot more time and memory - /// but is useful on more nested / complex / less-optimized input) - /// - /// - `s`, execute default optimization passes, focusing on code size - /// - /// - `z`, execute default optimization passes, super-focusing on code size - /// - /// - - #[structopt(long = "optimization-passes", default_value)] - optimization_passes: OptimizationPasses, -} - #[derive(Clone, Copy, Debug)] pub enum OptimizationPasses { Zero, @@ -157,7 +134,11 @@ impl std::str::FromStr for OptimizationPasses { type Err = Error; fn from_str(input: &str) -> std::result::Result { - match input.to_lowercase().as_str() { + // We need to replace " here, since the input string could come + // from either the CLI or the `Cargo.toml` profile section. + // If it is from the profile it could e.g. be "3" or 3. + let normalized_input = input.replace("\"", "").to_lowercase(); + match normalized_input.as_str() { "0" => Ok(OptimizationPasses::Zero), "1" => Ok(OptimizationPasses::One), "2" => Ok(OptimizationPasses::Two), @@ -170,6 +151,12 @@ impl std::str::FromStr for OptimizationPasses { } } +impl From for OptimizationPasses { + fn from(str: String) -> Self { + OptimizationPasses::from_str(&str).expect("conversion failed") + } +} + impl OptimizationPasses { /// Returns the number of optimization passes to do #[cfg(feature = "binaryen-as-dependency")] @@ -196,7 +183,7 @@ impl OptimizationPasses { } } -#[derive(Clone, Debug, StructOpt)] +#[derive(Default, Clone, Debug, StructOpt)] pub struct VerbosityFlags { /// No output printed to stdout #[structopt(long)] @@ -240,7 +227,7 @@ impl TryFrom<&VerbosityFlags> for Verbosity { } } -#[derive(Clone, Debug, StructOpt)] +#[derive(Default, Clone, Debug, StructOpt)] struct UnstableOptions { /// Use the original manifest (Cargo.toml), do not modify for build optimizations #[structopt(long = "unstable-options", short = "Z", number_of_values = 1)] diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index dc1699bb..db0a246f 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -17,6 +17,8 @@ use anyhow::{Context, Result}; use super::{metadata, Profile}; +use crate::OptimizationPasses; + use std::convert::TryFrom; use std::{ collections::HashSet, @@ -160,6 +162,26 @@ impl Manifest { Ok(self) } + /// Extract optimization-passes from `[profile.release]` + pub fn get_profile_release_optimization_passes(&mut self) -> Option { + self.get_profile_release_table_mut() + .ok()? + .get("optimization-passes") + .map(|val| val.to_string()) + .map(Into::into) + } + + /// Extract optimization-passes from `[profile.release]` + #[cfg(test)] + pub fn set_profile_release_optimization_passes( + &mut self, + passes: OptimizationPasses, + ) -> Result> { + Ok(self + .get_profile_release_table_mut()? + .insert("optimization-passes".to_string(), passes.to_string().into())) + } + /// Set `[profile.release]` lto flag pub fn with_profile_release_lto(&mut self, enabled: bool) -> Result<&mut Self> { let lto = self -- GitLab From eda7d93aafcd3652b51ce4f07cd6cfe4eba34aa0 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 09:59:20 +0100 Subject: [PATCH 06/11] Add link to Windows binary releases to Readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fb4c0d94..41a86bab 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ We optimize the resulting contract Wasm using `binaryen`. You have two options f Install [`binaryen`](https://github.com/WebAssembly/binaryen#tools) with a version >= 99. Many package managers have it available nowadays ‒ e.g. it's a package for [Debian/Ubuntu](https://tracker.debian.org/pkg/binaryen), [Homebrew](https://formulae.brew.sh/formula/binaryen) and [Arch Linux](https://archlinux.org/packages/community/x86_64/binaryen/). + For Windows, [binary releases are available](https://github.com/WebAssembly/binaryen/releases). After you've installed the package execute `cargo install --force cargo-contract`. - _Build `binaryen` as a dependency when installing `cargo-contract`:_ -- GitLab From 998ed5f66e9cb49086775e6c4701f0ff3bd34860 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 13:43:37 +0100 Subject: [PATCH 07/11] Improve failed assert message --- src/cmd/build.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 063e60f9..c6610190 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -608,7 +608,14 @@ mod tests_ci_only { // then // we have to truncate here to account for a possible small delta // in the floating point numbers - assert!(optimization.optimized_size.trunc() == optimization.original_size.trunc()); + let optimized_size = optimization.optimized_size.trunc(); + let original_size = optimization.original_size.trunc(); + assert!( + optimized_size == original_size, + "The optimized size {:?} differs from the original size {:?}", + optimized_size, + original_size + ); Ok(()) }) -- GitLab From 3421bc44a00de1a5d8f3999ab3efb425dfca5ab6 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 14:11:58 +0100 Subject: [PATCH 08/11] Account for `binaryen-rs` behavior --- src/cmd/build.rs | 7 ++++++- src/main.rs | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index c6610190..93de595e 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -363,7 +363,12 @@ fn do_optimization( ); let mut module = binaryen::Module::read(&dest_wasm_file_content) .map_err(|_| anyhow::anyhow!("binaryen failed to read file content"))?; - module.optimize(&codegen_config); + + if optimization_level != OptimizationPasses::Zero { + // binaryen-rs does still use the default optimization passes for zero, + // due to executing `addDefaultOptimizationPasses`. + module.optimize(&codegen_config); + } let mut optimized_wasm_file = File::create(dest_optimized)?; optimized_wasm_file.write_all(&module.write())?; diff --git a/src/main.rs b/src/main.rs index b979b158..326c3d69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,7 @@ impl ExtrinsicOpts { } #[derive(Clone, Copy, Debug)] +#[cfg_attr(test, derive(PartialEq))] pub enum OptimizationPasses { Zero, One, -- GitLab From 2c75c2442d6775b9020e6c77f4af6feeb4235fcc Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 14:36:43 +0100 Subject: [PATCH 09/11] Link GitHub issue in comment --- src/cmd/build.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 93de595e..a2ef1bcc 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -365,8 +365,9 @@ fn do_optimization( .map_err(|_| anyhow::anyhow!("binaryen failed to read file content"))?; if optimization_level != OptimizationPasses::Zero { - // binaryen-rs does still use the default optimization passes for zero, - // due to executing `addDefaultOptimizationPasses`. + // binaryen-rs still uses the default optimization passes, even if zero + // is passed. this is the ticket for it: https://github.com/pepyakin/binaryen-rs/issues/56. + // we can remove the if condition here once the issue is fixed. module.optimize(&codegen_config); } -- GitLab From b66ddadb1b6a29307ab0649c188efd5dc6582b03 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Tue, 23 Mar 2021 19:08:33 +0100 Subject: [PATCH 10/11] Implement comments --- src/cmd/build.rs | 4 ++-- src/workspace/manifest.rs | 38 ++++++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index a2ef1bcc..8bb4c325 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -105,7 +105,7 @@ impl BuildCommand { Some(opt_passes) => opt_passes, None => { let mut manifest = Manifest::new(manifest_path.clone())?; - match manifest.get_profile_release_optimization_passes() { + match manifest.get_profile_optimization_passes() { // if no setting is found, neither on the cli nor in the profile, // then we use the default None => OptimizationPasses::default(), @@ -593,7 +593,7 @@ mod tests_ci_only { ManifestPath::new(&cargo_toml_path).expect("manifest path creation failed"); // we write "4" as the optimization passes into the release profile assert!(Manifest::new(manifest_path.clone())? - .set_profile_release_optimization_passes(String::from("4").into()) + .set_profile_optimization_passes(String::from("4").into()) .is_ok()); let cmd = BuildCommand { manifest_path: Some(cargo_toml_path), diff --git a/src/workspace/manifest.rs b/src/workspace/manifest.rs index db0a246f..ce44c030 100644 --- a/src/workspace/manifest.rs +++ b/src/workspace/manifest.rs @@ -162,24 +162,46 @@ impl Manifest { Ok(self) } - /// Extract optimization-passes from `[profile.release]` - pub fn get_profile_release_optimization_passes(&mut self) -> Option { - self.get_profile_release_table_mut() - .ok()? + /// Extract `optimization-passes` from `[package.metadata.contract]` + pub fn get_profile_optimization_passes(&mut self) -> Option { + self.toml + .get("package")? + .as_table()? + .get("metadata")? + .as_table()? + .get("contract")? + .as_table()? .get("optimization-passes") .map(|val| val.to_string()) .map(Into::into) } - /// Extract optimization-passes from `[profile.release]` + /// Set `optimization-passes` in `[package.metadata.contract]` #[cfg(test)] - pub fn set_profile_release_optimization_passes( + pub fn set_profile_optimization_passes( &mut self, passes: OptimizationPasses, ) -> Result> { Ok(self - .get_profile_release_table_mut()? - .insert("optimization-passes".to_string(), passes.to_string().into())) + .toml + .entry("package") + .or_insert(value::Value::Table(Default::default())) + .as_table_mut() + .ok_or(anyhow::anyhow!("package section should be a table"))? + .entry("metadata") + .or_insert(value::Value::Table(Default::default())) + .as_table_mut() + .ok_or(anyhow::anyhow!("metadata section should be a table"))? + .entry("contract") + .or_insert(value::Value::Table(Default::default())) + .as_table_mut() + .ok_or(anyhow::anyhow!( + "metadata.contract section should be a table" + ))? + .insert( + "optimization-passes".to_string(), + value::Value::String(passes.to_string()), + )) } /// Set `[profile.release]` lto flag -- GitLab From 54a4e24b7a392dca274866e055df0e3595c0ac02 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Wed, 24 Mar 2021 10:36:08 +0100 Subject: [PATCH 11/11] Update `--help` --- src/cmd/build.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cmd/build.rs b/src/cmd/build.rs index 8bb4c325..abef85ef 100644 --- a/src/cmd/build.rs +++ b/src/cmd/build.rs @@ -85,9 +85,9 @@ pub struct BuildCommand { /// /// - The default value is `3` /// - /// - It is possible to define the number of optimization passes in the `[profile.release]` of - /// your `Cargo.toml` as e.g. `optimization-passes = "3"`. The CLI argument always takes - /// precedence over the profile value. + /// - It is possible to define the number of optimization passes in the + /// `[package.metadata.contract]` of your `Cargo.toml` as e.g. `optimization-passes = "3"`. + /// The CLI argument always takes precedence over the profile value. #[structopt(long = "optimization-passes")] optimization_passes: Option, } -- GitLab