diff --git a/Cargo.lock b/Cargo.lock index ba1b015..92154e7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -596,6 +596,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "codespan-reporting" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" +dependencies = [ + "termcolor", + "unicode-width", +] + [[package]] name = "console" version = "0.15.0" @@ -683,6 +693,50 @@ dependencies = [ "zeroize", ] +[[package]] +name = "cxx" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4a41a86530d0fe7f5d9ea779916b7cadd2d4f9add748b99c2c029cbbdfaf453" +dependencies = [ + "cc", + "cxxbridge-flags", + "cxxbridge-macro", + "link-cplusplus", +] + +[[package]] +name = "cxx-build" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06416d667ff3e3ad2df1cd8cd8afae5da26cf9cec4d0825040f88b5ca659a2f0" +dependencies = [ + "cc", + "codespan-reporting", + "once_cell", + "proc-macro2", + "quote", + "scratch", + "syn", +] + +[[package]] +name = "cxxbridge-flags" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "820a9a2af1669deeef27cb271f476ffd196a2c4b6731336011e0ba63e2c7cf71" + +[[package]] +name = "cxxbridge-macro" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08a6e2fcc370a089ad3b4aaf54db3b1b4cee38ddabce5896b33eb693275f470" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -1210,9 +1264,9 @@ checksum = "884e2677b40cc8c339eaefcb701c32ef1fd2493d71118dc0ca4b6a736c93bd67" [[package]] name = "libc" -version = "0.2.124" +version = "0.2.137" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21a41fed9d98f27ab1c6d161da622a4fa35e8a54a8adc24bbf3ddd0ef70b0e50" +checksum = "fc7fcc620a3bff7cdd7a365be3376c97191aeaccc2a603e600951e452615bf89" [[package]] name = "libgit2-sys" @@ -1247,6 +1301,15 @@ dependencies = [ "safemem", ] +[[package]] +name = "link-cplusplus" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9272ab7b96c9046fbc5bc56c06c117cb639fe2d509df0c421cad82d2915cf369" +dependencies = [ + "cc", +] + [[package]] name = "linked-hash-map" version = "0.5.4" @@ -1790,7 +1853,7 @@ checksum = "643f8f41a8ebc4c5dc4515c82bb8abd397b527fc20fd681b7c011c2aee5d44fb" [[package]] name = "raen" -version = "0.1.0" +version = "0.1.1" dependencies = [ "aha-wit-parser 0.0.2", "anyhow", @@ -1804,6 +1867,7 @@ dependencies = [ "clap-cargo-extra", "filetime", "predicates", + "wasm-opt", "witgen 0.12.0", "witme", ] @@ -1919,9 +1983,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.5.5" +version = "1.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a11647b6b25ff05a515cb92c365cec08801e83423a235b51e231e1808747286" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" dependencies = [ "aho-corasick", "memchr", @@ -1936,9 +2000,9 @@ checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" [[package]] name = "regex-syntax" -version = "0.6.25" +version = "0.6.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" [[package]] name = "remove_dir_all" @@ -1991,6 +2055,12 @@ dependencies = [ "semver", ] +[[package]] +name = "rustversion" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8" + [[package]] name = "ryu" version = "1.0.9" @@ -2012,6 +2082,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "scratch" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8132065adcfd6e02db789d9285a0deb2f3fcb04002865ab67d5fb103533898" + [[package]] name = "semver" version = "1.0.14" @@ -2174,6 +2250,25 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "strum" +version = "0.24.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" + +[[package]] +name = "strum_macros" +version = "0.24.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e385be0d24f186b4ce2f9982191e7101bb737312ad61c1f2f984f34bcf85d59" +dependencies = [ + "heck 0.4.0", + "proc-macro2", + "quote", + "rustversion", + "syn", +] + [[package]] name = "subtle" version = "2.4.1" @@ -2584,6 +2679,47 @@ version = "0.10.2+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" +[[package]] +name = "wasm-opt" +version = "0.110.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b68e8037b4daf711393f4be2056246d12d975651b14d581520ad5d1f19219cec" +dependencies = [ + "anyhow", + "libc", + "strum", + "strum_macros", + "tempfile", + "thiserror", + "wasm-opt-cxx-sys", + "wasm-opt-sys", +] + +[[package]] +name = "wasm-opt-cxx-sys" +version = "0.110.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91adbad477e97bba3fbd21dd7bfb594e7ad5ceb9169ab1c93ab9cb0ada636b6f" +dependencies = [ + "anyhow", + "cxx", + "cxx-build", + "wasm-opt-sys", +] + +[[package]] +name = "wasm-opt-sys" +version = "0.110.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec4fa5a322a4e6ac22fd141f498d56afbdbf9df5debeac32380d2dcaa3e06941" +dependencies = [ + "anyhow", + "cc", + "cxx", + "cxx-build", + "regex", +] + [[package]] name = "wasmparser" version = "0.77.0" diff --git a/Cargo.toml b/Cargo.toml index a2257ce..16b79f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,7 +6,7 @@ keywords = ["WebAssembly", "Wasm", "wit", "WasmInterfaceTypes", "NEAR"] license = "MIT" name = "raen" repository = "https://github.com/raendev/raen" -version = "0.1.0" +version = "0.1.1" default-run = "raen" [dependencies] @@ -14,6 +14,7 @@ default-run = "raen" witme = {version = "0.3.1"} witgen = "0.12.0" cargo-witgen = "0.13.0" +wasm-opt = "0.110.1" ## Errors anyhow = "1.0.51" diff --git a/src/raen.rs b/src/raen.rs index a6fe0f6..551c2c7 100644 --- a/src/raen.rs +++ b/src/raen.rs @@ -13,6 +13,7 @@ use cargo_metadata::{camino::Utf8PathBuf, DependencyKind, Package, Target}; use cargo_witgen::Witgen; use clap::{Args, Parser}; use clap_cargo_extra::{ClapCargo, TargetTools}; +use wasm_opt::OptimizationOptions; use witme::app::NearCommand; use crate::ext::{compress_file, get_time, PackageExt}; @@ -48,6 +49,9 @@ pub struct Build { /// Only print build file path #[clap(long, short = 'q')] pub quiet: bool, + /// Use wasm-opt to further optimize the size of generated the Wasm binary + #[clap(long, short = 'w')] + pub wasm_opt: bool, } impl Raen { @@ -191,7 +195,7 @@ struct Foo {} let input = self.cargo.built_bin(t)?; let output = self.output_bin(t)?; let cmd = NearCommand::Inject { - input, + input: input.clone(), output, data: None, file: Some(file), @@ -200,10 +204,20 @@ struct Foo {} cmd.run()?; let output = format!("{:?}", bin_dir.join(bin_name)); let output = output.trim_matches('"'); + let extra = if self.wasm_opt { + let diff = std::fs::metadata(&input)?.len() as i64 - self.optimize(output)? as i64; + if diff < 0 { + "The orginal file was smaller than the added types. Report to the `https://github.com/raendev/raen/issues` for help.".to_owned() + } else { + format!("Saved {diff} bytes") + } + } else { + "".to_owned() + }; if self.quiet { println!("{}", output); } else { - println!("Built to:\n{}", output); + println!("Built to:\n{}\n{}", output, extra); } Ok(()) } @@ -274,6 +288,17 @@ struct Foo {} Ok(res) }) .collect::>>() - .and_then(|set| Ok(set.into_iter().collect::>())) + .map(IntoIterator::into_iter) + .map(Iterator::collect::>) + } + + fn optimize(&self, file: &str) -> Result { + let input = PathBuf::from(file); + let output = &input; + let wasm_out = PathBuf::from(output); + let mut options = OptimizationOptions::new_optimize_for_size_aggressively(); + options.converge = true; + options.run(input, &wasm_out)?; + Ok(std::fs::metadata(wasm_out)?.len()) } } diff --git a/tests/it/main.rs b/tests/it/main.rs index df7bc73..2170959 100644 --- a/tests/it/main.rs +++ b/tests/it/main.rs @@ -7,7 +7,10 @@ fn compile() { use std::fs; fs::remove_dir_all("./target/res").unwrap_or_default(); fs::remove_dir_all("./target/wit").unwrap_or_default(); - let mut build = Build::default(); + let mut build = Build { + wasm_opt: true, + ..Build::default() + }; build.cargo.cargo_build.release = true; build.cargo.workspace.exclude.push("raen".to_string()); build.cargo.workspace.workspace = true;