Skip to content

Commit

Permalink
Merge pull request #1229 from multiversx/replace-wasm2wat
Browse files Browse the repository at this point in the history
Replace wasm2wat with internal implementation
  • Loading branch information
andrei-marinica authored Sep 27, 2023
2 parents 49109b5 + d9cae84 commit 1e3b9ec
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 119 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ vm/**/Cargo.lock
# Others
my-vm-tests.sh
quick.sh
twiggy*
template-test

# Zip output with example wasm binaries
Expand Down
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions framework/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion framework/meta/src/cmd/contract/meta_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down
16 changes: 8 additions & 8 deletions framework/meta/src/cmd/contract/output_contract/wasm_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -136,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);
}
Expand Down Expand Up @@ -173,31 +173,31 @@ 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(),
);
}
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(),
);
}
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(),
);
}
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(),
);
Expand Down
2 changes: 1 addition & 1 deletion framework/meta/src/print_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
}

Expand Down
22 changes: 21 additions & 1 deletion framework/meta/src/tools.rs
Original file line number Diff line number Diff line change
@@ -1,6 +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;
}
}
105 changes: 0 additions & 105 deletions framework/meta/src/tools/post_build.rs

This file was deleted.

47 changes: 47 additions & 0 deletions framework/meta/src/tools/twiggy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::process::{Command, Stdio};

pub const TWIGGY_NAME: &str = "twiggy";

pub fn is_twiggy_installed() -> bool {
Command::new(TWIGGY_NAME)
.args(["--version"])
.output()
.is_ok()
}

fn run_with_stdout_file<I, S>(stdout_file_name: &str, args: I)
where
I: IntoIterator<Item = S>,
S: AsRef<std::ffi::OsStr>,
{
let stdout_file = std::fs::File::create(stdout_file_name).unwrap();
let _ = Command::new(TWIGGY_NAME)
.args(args)
.stdout(Stdio::from(stdout_file))
.spawn()
.expect("failed to spawn twiggy process")
.wait()
.expect("twiggy was not running");
}

pub(crate) fn run_twiggy_top(output_wasm_path: &str, output_twiggy_top_path: &str) {
run_with_stdout_file(
output_twiggy_top_path,
["top", "-n", "1000", output_wasm_path],
);
}

pub(crate) fn run_twiggy_paths(output_wasm_path: &str, output_twiggy_paths_path: &str) {
run_with_stdout_file(output_twiggy_paths_path, ["paths", output_wasm_path]);
}

pub(crate) fn run_twiggy_monos(output_wasm_path: &str, output_twiggy_monos_path: &str) {
run_with_stdout_file(output_twiggy_monos_path, ["monos", output_wasm_path]);
}

pub(crate) fn run_twiggy_dominators(output_wasm_path: &str, output_twiggy_dominators_path: &str) {
run_with_stdout_file(
output_twiggy_dominators_path,
["dominators", output_wasm_path],
);
}
21 changes: 21 additions & 0 deletions framework/meta/src/tools/wasm_opt.rs
Original file line number Diff line number Diff line change
@@ -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");
}
9 changes: 9 additions & 0 deletions framework/meta/src/tools/wasm_to_wat.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 1e3b9ec

Please sign in to comment.