Skip to content

Commit

Permalink
feat!: use wasm-opt -O (via wasm-opt-rs) as post-step of build (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
dj8yfo authored Oct 16, 2024
1 parent 1a795ee commit 740d1f6
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 14 deletions.
148 changes: 134 additions & 14 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions cargo-near-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pathdiff = { version = "0.2.1", features = ["camino"], optional = true }
unix_path = { version = "1.0.1", optional = true }
tempfile = { version = "3.10.1", optional = true }
shell-words = { version = "1.0.0", optional = true}
wasm-opt = "=0.116.1"
humantime = "2.1.0"

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.29.0", features = ["user", "process"], optional = true }
Expand Down
15 changes: 15 additions & 0 deletions cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub mod export;

/// builds a contract whose crate root is current workdir, or identified by [`Cargo.toml`/BuildOpts::manifest_path](crate::BuildOpts::manifest_path) location
pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
let start = std::time::Instant::now();
VersionMismatch::export_builder_and_near_abi_versions();
export::nep_330_build_command(&args)?;
env_keys::nep330::print_env();
Expand Down Expand Up @@ -145,6 +146,19 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
)?;

wasm_artifact.path = crate::fs::copy(&wasm_artifact.path, &out_dir)?;

if !args.no_wasmopt {
println!();
pretty_print::handle_step(
"Running an optimize for size post-step with wasm-opt...",
|| {
wasm_opt::OptimizationOptions::new_optimize_for_size()
.run(&wasm_artifact.path, &wasm_artifact.path)?;
Ok(())
},
)?;
}

wasm_artifact.builder_version_mismatch = builder_version_mismatch;

// todo! if we embedded, check that the binary exports the __contract_abi symbol
Expand Down Expand Up @@ -172,5 +186,6 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
}

messages.pretty_print();
pretty_print::duration(start, "cargo near build");
Ok(wasm_artifact)
}
12 changes: 12 additions & 0 deletions cargo-near-build/src/pretty_print.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Instant;

use colored::Colorize;

pub fn handle_step<F, T>(msg: &str, f: F) -> eyre::Result<T>
Expand All @@ -21,3 +23,13 @@ pub fn step(msg: &str) {
pub fn success(msg: &str) {
eprintln!("{} {}", "✓".bold().green(), msg);
}

pub fn duration(start: Instant, activity: &str) {
let duration = std::time::Duration::from_secs(start.elapsed().as_secs());
println!(
" {} {} in {}",
"Finished".bold().cyan(),
activity,
humantime::format_duration(duration)
);
}
8 changes: 8 additions & 0 deletions cargo-near-build/src/types/near/build/input/docker_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ impl super::Opts {
cli_args.extend(self.no_abi.then_some("--no-abi".into()));
cli_args.extend(self.no_embed_abi.then_some("--no-embed-abi".into()));
cli_args.extend(self.no_doc.then_some("--no-doc".into()));
cli_args.extend(self.no_wasmopt.then_some("--no-wasmopt".into()));

if let Some(ref features) = self.features {
cli_args.extend(["--features".into(), features.clone()]);
Expand Down Expand Up @@ -138,6 +139,13 @@ impl super::Opts {
Self::BUILD_COMMAND_CLI_CONFIG_ERR
)));
}
if self.no_wasmopt {
return Err(eyre::eyre!(format!(
"`{}` {}",
"--no-wasmopt ",
Self::BUILD_COMMAND_CLI_CONFIG_ERR
)));
}
if self.features.is_some() {
return Err(eyre::eyre!(format!(
"`{}` {}",
Expand Down
5 changes: 5 additions & 0 deletions cargo-near-build/src/types/near/build/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub struct Opts {
pub no_embed_abi: bool,
/// Do not include rustdocs in the embedded ABI
pub no_doc: bool,
/// do not run `wasm-opt -O` on the generated output as a post-step
pub no_wasmopt: bool,
/// Copy final artifacts to this directory
pub out_dir: Option<camino::Utf8PathBuf>,
/// Path to the `Cargo.toml` of the contract to build
Expand Down Expand Up @@ -94,6 +96,9 @@ impl Opts {
if self.no_doc {
cargo_args.push("--no-doc");
}
if self.no_wasmopt {
cargo_args.push("--no-wasmopt");
}
if let Some(ref out_dir) = self.out_dir {
cargo_args.extend_from_slice(&["--out-dir", out_dir.as_str()]);
}
Expand Down
6 changes: 6 additions & 0 deletions cargo-near/src/commands/build_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub struct BuildCommand {
/// Do not include rustdocs in the embedded ABI
#[interactive_clap(long)]
pub no_doc: bool,
/// Do not run `wasm-opt -O` on the generated output as a post-step
#[interactive_clap(long)]
pub no_wasmopt: bool,
/// Copy final artifacts to this directory
#[interactive_clap(long)]
#[interactive_clap(skip_interactive_input)]
Expand Down Expand Up @@ -93,6 +96,7 @@ impl From<CliBuildCommand> for BuildCommand {
no_abi: value.no_abi,
no_embed_abi: value.no_embed_abi,
no_doc: value.no_doc,
no_wasmopt: value.no_wasmopt,
features: value.features,
no_default_features: value.no_default_features,
out_dir: value.out_dir,
Expand Down Expand Up @@ -125,6 +129,7 @@ impl From<BuildCommand> for BuildOpts {
no_abi: value.no_abi,
no_embed_abi: value.no_embed_abi,
no_doc: value.no_doc,
no_wasmopt: value.no_wasmopt,
features: value.features,
no_default_features: value.no_default_features,
out_dir: value.out_dir.map(Into::into),
Expand All @@ -150,6 +155,7 @@ impl BuildCommandlContext {
no_abi: scope.no_abi,
no_embed_abi: scope.no_embed_abi,
no_doc: scope.no_doc,
no_wasmopt: scope.no_wasmopt,
out_dir: scope.out_dir.clone(),
manifest_path: scope.manifest_path.clone(),
features: scope.features.clone(),
Expand Down

0 comments on commit 740d1f6

Please sign in to comment.