From df1979f6ca8c30bb76e0ea40064ab0f14d14562e Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Sep 2023 10:26:00 +0300 Subject: [PATCH 1/3] meta - replace wasm2wat with internal impl --- Cargo.lock | 15 ++++++++++-- framework/meta/Cargo.toml | 1 + .../contract/output_contract/wasm_build.rs | 3 ++- framework/meta/src/tools.rs | 2 ++ framework/meta/src/tools/post_build.rs | 23 ------------------- framework/meta/src/tools/wasm_to_wat.rs | 9 ++++++++ 6 files changed, 27 insertions(+), 26 deletions(-) create mode 100644 framework/meta/src/tools/wasm_to_wat.rs diff --git a/Cargo.lock b/Cargo.lock index b208119b5d..a2e1cd41fe 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -2138,6 +2138,7 @@ dependencies = [ "tokio", "toml 0.5.11", "wasmparser", + "wasmprinter", "zip", ] @@ -3808,14 +3809,24 @@ checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" [[package]] name = "wasmparser" -version = "0.113.1" +version = "0.113.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a128cea7b8516703ab41b10a0b1aa9ba18d0454cd3792341489947ddeee268db" +checksum = "1fd0d44fab0bd78404e352f3399324eef76516a4580b52bc9031c60f064e98f3" dependencies = [ "indexmap 2.0.0", "semver", ] +[[package]] +name = "wasmprinter" +version = "0.2.67" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6615a5587149e753bf4b93f90fa3c3f41c88597a7a2da72879afcabeda9648f" +dependencies = [ + "anyhow", + "wasmparser", +] + [[package]] name = "web-sys" version = "0.3.64" diff --git a/framework/meta/Cargo.toml b/framework/meta/Cargo.toml index 54c066526a..7ad4357872 100644 --- a/framework/meta/Cargo.toml +++ b/framework/meta/Cargo.toml @@ -44,6 +44,7 @@ lazy_static = "1.4.0" convert_case = "0.6.0" hex = "0.4" wasmparser = "0.113.1" +wasmprinter = "0.2.67" [dependencies.multiversx-sc] version = "=0.43.4" diff --git a/framework/meta/src/cmd/contract/output_contract/wasm_build.rs b/framework/meta/src/cmd/contract/output_contract/wasm_build.rs index 901b6ce626..8e5504aebb 100644 --- a/framework/meta/src/cmd/contract/output_contract/wasm_build.rs +++ b/framework/meta/src/cmd/contract/output_contract/wasm_build.rs @@ -119,7 +119,8 @@ impl OutputContract { let output_wasm_path = format!("{output_path}/{}", self.wasm_output_name(build_args)); let output_wat_path = format!("{output_path}/{}", self.wat_output_name(build_args)); print_call_wasm2wat(&output_wasm_path, &output_wat_path); - post_build::run_wasm2wat(output_wasm_path.as_str(), output_wat_path.as_str()); + tools::wasm_to_wat(output_wasm_path.as_str(), output_wat_path.as_str()) + .expect("could not convert wasm to wat"); } fn extract_imports(&self, build_args: &BuildArgs, output_path: &str) { diff --git a/framework/meta/src/tools.rs b/framework/meta/src/tools.rs index fce1dc5ad3..7b5a04891a 100644 --- a/framework/meta/src/tools.rs +++ b/framework/meta/src/tools.rs @@ -1,6 +1,8 @@ mod git_describe; pub mod post_build; mod wasm_imports; +mod wasm_to_wat; pub use git_describe::git_describe; pub use wasm_imports::extract_wasm_imports; +pub use wasm_to_wat::wasm_to_wat; diff --git a/framework/meta/src/tools/post_build.rs b/framework/meta/src/tools/post_build.rs index b07fdf79b7..8768cfef6f 100644 --- a/framework/meta/src/tools/post_build.rs +++ b/framework/meta/src/tools/post_build.rs @@ -3,7 +3,6 @@ use std::process::{Command, Stdio}; use crate::cli_args::BuildArgs; const WASM_OPT_NAME: &str = "wasm-opt"; -const WASM2WAT_NAME: &str = "wasm2wat"; const TWIGGY_NAME: &str = "twiggy"; pub(crate) fn check_tools_installed(build_args: &mut BuildArgs) { @@ -11,10 +10,6 @@ pub(crate) fn check_tools_installed(build_args: &mut BuildArgs) { println!("Warning: {WASM_OPT_NAME} not installed"); build_args.wasm_opt = false; } - if build_args.wat && !is_wasm2wat_installed() { - println!("Warning: {WASM2WAT_NAME} not installed"); - build_args.wat = false; - } if build_args.has_twiggy_call() && !is_twiggy_installed() { println!("Warning: {TWIGGY_NAME} not installed"); build_args.twiggy_top = false; @@ -31,13 +26,6 @@ fn is_wasm_opt_installed() -> bool { .is_ok() } -fn is_wasm2wat_installed() -> bool { - Command::new(WASM2WAT_NAME) - .args(["--version"]) - .output() - .is_ok() -} - fn is_twiggy_installed() -> bool { Command::new(TWIGGY_NAME) .args(["--version"]) @@ -56,17 +44,6 @@ pub(crate) fn run_wasm_opt(output_wasm_path: &str) { assert!(exit_status.success(), "wasm-opt process failed"); } -pub(crate) fn run_wasm2wat(output_wasm_path: &str, output_wat_path: &str) { - let exit_status = Command::new(WASM2WAT_NAME) - .args([output_wasm_path, "--output", output_wat_path]) - .spawn() - .expect("failed to spawn wasm2wat process") - .wait() - .expect("wasm2wat was not running"); - - assert!(exit_status.success(), "wasm2wat process failed"); -} - fn run_with_stdout_file(stdout_file_name: &str, args: I) where I: IntoIterator, diff --git a/framework/meta/src/tools/wasm_to_wat.rs b/framework/meta/src/tools/wasm_to_wat.rs new file mode 100644 index 0000000000..c83383eee4 --- /dev/null +++ b/framework/meta/src/tools/wasm_to_wat.rs @@ -0,0 +1,9 @@ +use anyhow::Result; +use std::fs; + +/// Converts a .wasm file on the disk to .wat. +pub fn wasm_to_wat(output_wasm_path: &str, output_wat_path: &str) -> Result<()> { + let wat_string = wasmprinter::print_file(output_wasm_path)?; + fs::write(output_wat_path, wat_string)?; + Ok(()) +} From 7aa341ee4f41a8330098bbbb17c99659da00f395 Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Sep 2023 10:32:51 +0300 Subject: [PATCH 2/3] cleanup --- framework/meta/src/cmd/contract/output_contract/wasm_build.rs | 1 - framework/meta/src/print_util.rs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/framework/meta/src/cmd/contract/output_contract/wasm_build.rs b/framework/meta/src/cmd/contract/output_contract/wasm_build.rs index 8e5504aebb..0e22c469c8 100644 --- a/framework/meta/src/cmd/contract/output_contract/wasm_build.rs +++ b/framework/meta/src/cmd/contract/output_contract/wasm_build.rs @@ -137,7 +137,6 @@ impl OutputContract { print_extract_imports(&output_imports_json_path); let import_names = tools::extract_wasm_imports(&output_wasm_path) .expect("error occured while extracting imports from .wasm "); - println!("{import_names:?}"); write_imports_output(output_imports_json_path.as_str(), import_names.as_slice()); validate_ei(&import_names, &self.settings.check_ei); } diff --git a/framework/meta/src/print_util.rs b/framework/meta/src/print_util.rs index a6e3701946..e6fd7053d1 100644 --- a/framework/meta/src/print_util.rs +++ b/framework/meta/src/print_util.rs @@ -71,7 +71,7 @@ pub fn print_call_wasm_opt(wasm_path: &str) { pub fn print_call_wasm2wat(wasm_path: &str, wat_path: &str) { println!( "{}", - format!("Calling wasm2wat on {wasm_path} -> {wat_path} ...").green(), + format!("Extracting wat from {wasm_path} to {wat_path} ...").green(), ); } From d9cae84928caa81ff4ea570d19f3b768f3f6551d Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Wed, 27 Sep 2023 11:02:38 +0300 Subject: [PATCH 3/3] tools refactor --- .gitignore | 1 - .../meta/src/cmd/contract/meta_config.rs | 2 +- .../contract/output_contract/wasm_build.rs | 12 +++--- framework/meta/src/tools.rs | 20 +++++++++- .../src/tools/{post_build.rs => twiggy.rs} | 39 +------------------ framework/meta/src/tools/wasm_opt.rs | 21 ++++++++++ 6 files changed, 49 insertions(+), 46 deletions(-) rename framework/meta/src/tools/{post_build.rs => twiggy.rs} (53%) create mode 100644 framework/meta/src/tools/wasm_opt.rs diff --git a/.gitignore b/.gitignore index e50fdfaa61..630f52abb6 100644 --- a/.gitignore +++ b/.gitignore @@ -24,7 +24,6 @@ vm/**/Cargo.lock # Others my-vm-tests.sh quick.sh -twiggy* template-test # Zip output with example wasm binaries diff --git a/framework/meta/src/cmd/contract/meta_config.rs b/framework/meta/src/cmd/contract/meta_config.rs index 12b983dd5a..992a6a05e8 100644 --- a/framework/meta/src/cmd/contract/meta_config.rs +++ b/framework/meta/src/cmd/contract/meta_config.rs @@ -2,7 +2,7 @@ use std::fs; use multiversx_sc::abi::ContractAbi; -use crate::{cli_args::BuildArgs, tools::post_build::check_tools_installed, CargoTomlContents}; +use crate::{cli_args::BuildArgs, tools::check_tools_installed, CargoTomlContents}; use super::output_contract::{OutputContract, OutputContractGlobalConfig}; diff --git a/framework/meta/src/cmd/contract/output_contract/wasm_build.rs b/framework/meta/src/cmd/contract/output_contract/wasm_build.rs index 0e22c469c8..7b61617565 100644 --- a/framework/meta/src/cmd/contract/output_contract/wasm_build.rs +++ b/framework/meta/src/cmd/contract/output_contract/wasm_build.rs @@ -7,7 +7,7 @@ use crate::{ ei::EIVersion, mxsc_file_json::{save_mxsc_file_json, MxscFileJson}, print_util::*, - tools::{self, post_build}, + tools, }; impl OutputContract { @@ -108,7 +108,7 @@ impl OutputContract { let output_wasm_path = format!("{output_path}/{}", self.wasm_output_name(build_args)); print_call_wasm_opt(&output_wasm_path); - post_build::run_wasm_opt(output_wasm_path.as_str()); + tools::run_wasm_opt(output_wasm_path.as_str()); } fn run_wasm2wat(&self, build_args: &BuildArgs, output_path: &str) { @@ -173,7 +173,7 @@ impl OutputContract { if build_args.twiggy_top { let output_twiggy_top_path = format!("{output_path}/{}", self.twiggy_top_name(build_args)); - post_build::run_twiggy_top( + tools::twiggy::run_twiggy_top( output_wasm_path.as_str(), output_twiggy_top_path.as_str(), ); @@ -181,7 +181,7 @@ impl OutputContract { if build_args.twiggy_paths { let output_twiggy_paths_path = format!("{output_path}/{}", self.twiggy_paths_name(build_args)); - post_build::run_twiggy_paths( + tools::twiggy::run_twiggy_paths( output_wasm_path.as_str(), output_twiggy_paths_path.as_str(), ); @@ -189,7 +189,7 @@ impl OutputContract { if build_args.twiggy_monos { let output_twiggy_monos_path = format!("{output_path}/{}", self.twiggy_monos_name(build_args)); - post_build::run_twiggy_monos( + tools::twiggy::run_twiggy_monos( output_wasm_path.as_str(), output_twiggy_monos_path.as_str(), ); @@ -197,7 +197,7 @@ impl OutputContract { if build_args.twiggy_dominators { let output_twiggy_dominators_path = format!("{output_path}/{}", self.twiggy_dominators_name(build_args)); - post_build::run_twiggy_dominators( + tools::twiggy::run_twiggy_dominators( output_wasm_path.as_str(), output_twiggy_dominators_path.as_str(), ); diff --git a/framework/meta/src/tools.rs b/framework/meta/src/tools.rs index 7b5a04891a..c3bd3e6c84 100644 --- a/framework/meta/src/tools.rs +++ b/framework/meta/src/tools.rs @@ -1,8 +1,26 @@ mod git_describe; -pub mod post_build; +pub mod twiggy; mod wasm_imports; +mod wasm_opt; mod wasm_to_wat; pub use git_describe::git_describe; pub use wasm_imports::extract_wasm_imports; +pub use wasm_opt::run_wasm_opt; pub use wasm_to_wat::wasm_to_wat; + +use crate::cli_args::BuildArgs; + +pub fn check_tools_installed(build_args: &mut BuildArgs) { + if build_args.wasm_opt && !wasm_opt::is_wasm_opt_installed() { + println!("Warning: {} not installed", wasm_opt::WASM_OPT_NAME); + build_args.wasm_opt = false; + } + if build_args.has_twiggy_call() && !twiggy::is_twiggy_installed() { + println!("Warning: {} not installed", twiggy::TWIGGY_NAME); + build_args.twiggy_top = false; + build_args.twiggy_paths = false; + build_args.twiggy_monos = false; + build_args.twiggy_dominators = false; + } +} diff --git a/framework/meta/src/tools/post_build.rs b/framework/meta/src/tools/twiggy.rs similarity index 53% rename from framework/meta/src/tools/post_build.rs rename to framework/meta/src/tools/twiggy.rs index 8768cfef6f..3f93fcd459 100644 --- a/framework/meta/src/tools/post_build.rs +++ b/framework/meta/src/tools/twiggy.rs @@ -1,49 +1,14 @@ use std::process::{Command, Stdio}; -use crate::cli_args::BuildArgs; +pub const TWIGGY_NAME: &str = "twiggy"; -const WASM_OPT_NAME: &str = "wasm-opt"; -const TWIGGY_NAME: &str = "twiggy"; - -pub(crate) fn check_tools_installed(build_args: &mut BuildArgs) { - if build_args.wasm_opt && !is_wasm_opt_installed() { - println!("Warning: {WASM_OPT_NAME} not installed"); - build_args.wasm_opt = false; - } - if build_args.has_twiggy_call() && !is_twiggy_installed() { - println!("Warning: {TWIGGY_NAME} not installed"); - build_args.twiggy_top = false; - build_args.twiggy_paths = false; - build_args.twiggy_monos = false; - build_args.twiggy_dominators = false; - } -} - -fn is_wasm_opt_installed() -> bool { - Command::new(WASM_OPT_NAME) - .args(["--version"]) - .output() - .is_ok() -} - -fn is_twiggy_installed() -> bool { +pub fn is_twiggy_installed() -> bool { Command::new(TWIGGY_NAME) .args(["--version"]) .output() .is_ok() } -pub(crate) fn run_wasm_opt(output_wasm_path: &str) { - let exit_status = Command::new(WASM_OPT_NAME) - .args([output_wasm_path, "-Oz", "--output", output_wasm_path]) - .spawn() - .expect("failed to spawn wasm-opt process") - .wait() - .expect("wasm-opt was not running"); - - assert!(exit_status.success(), "wasm-opt process failed"); -} - fn run_with_stdout_file(stdout_file_name: &str, args: I) where I: IntoIterator, diff --git a/framework/meta/src/tools/wasm_opt.rs b/framework/meta/src/tools/wasm_opt.rs new file mode 100644 index 0000000000..9b818e5ef7 --- /dev/null +++ b/framework/meta/src/tools/wasm_opt.rs @@ -0,0 +1,21 @@ +use std::process::Command; + +pub const WASM_OPT_NAME: &str = "wasm-opt"; + +pub fn is_wasm_opt_installed() -> bool { + Command::new(WASM_OPT_NAME) + .args(["--version"]) + .output() + .is_ok() +} + +pub fn run_wasm_opt(output_wasm_path: &str) { + let exit_status = Command::new(WASM_OPT_NAME) + .args([output_wasm_path, "-Oz", "--output", output_wasm_path]) + .spawn() + .expect("failed to spawn wasm-opt process") + .wait() + .expect("wasm-opt was not running"); + + assert!(exit_status.success(), "wasm-opt process failed"); +}