Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Simplify fn compact_wasm_file
Browse files Browse the repository at this point in the history
  • Loading branch information
athei committed Sep 21, 2022
1 parent 40b4809 commit f33171b
Showing 1 changed file with 17 additions and 32 deletions.
49 changes: 17 additions & 32 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,52 +656,37 @@ fn compact_wasm_file(
project: &Path,
profile: Profile,
cargo_manifest: &Path,
wasm_binary_name: Option<String>,
out_name: Option<String>,
) -> (Option<WasmBinary>, Option<WasmBinary>, WasmBinaryBloaty) {
let default_wasm_binary_name = get_wasm_binary_name(cargo_manifest);
let wasm_file = project
let default_out_name = get_wasm_binary_name(cargo_manifest);
let out_name = out_name.unwrap_or_else(|| default_out_name.clone());
let in_path = project
.join("target/wasm32-unknown-unknown")
.join(profile.directory())
.join(format!("{}.wasm", default_wasm_binary_name));
.join(format!("{}.wasm", default_out_name));

let wasm_compact_file = if profile.wants_compact() {
let wasm_compact_file = project.join(format!(
"{}.compact.wasm",
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone()),
));
let (wasm_compact_path, wasm_compact_compressed_path) = if profile.wants_compact() {
let wasm_compact_path = project.join(format!("{}.compact.wasm", out_name,));
wasm_opt::OptimizationOptions::new_optimize_for_size_aggressively()
.zero_filled_memory(true)
.run(&wasm_file, Option::<&str>::None, &wasm_compact_file, Option::<&str>::None)
.run(&in_path, Option::<&str>::None, &wasm_compact_path, Option::<&str>::None)
.expect("Failed to compact generated WASM binary.");
Some(WasmBinary(wasm_compact_file))
} else {
None
};

let wasm_compact_compressed_file = wasm_compact_file.as_ref().and_then(|compact_binary| {
let file_name =
wasm_binary_name.clone().unwrap_or_else(|| default_wasm_binary_name.clone());

let wasm_compact_compressed_file =
project.join(format!("{}.compact.compressed.wasm", file_name));

if compress_wasm(&compact_binary.0, &wasm_compact_compressed_file) {
Some(WasmBinary(wasm_compact_compressed_file))
let wasm_compact_compressed_path =
project.join(format!("{}.compact.compressed.wasm", out_name));
if compress_wasm(&wasm_compact_path, &wasm_compact_compressed_path) {
(Some(WasmBinary(wasm_compact_path)), Some(WasmBinary(wasm_compact_compressed_path)))
} else {
None
(Some(WasmBinary(wasm_compact_path)), None)
}
});

let bloaty_file_name = if let Some(name) = wasm_binary_name {
format!("{}.wasm", name)
} else {
format!("{}.wasm", default_wasm_binary_name)
(None, None)
};

let bloaty_file = project.join(bloaty_file_name);
fs::copy(wasm_file, &bloaty_file).expect("Copying the bloaty file to the project dir.");
let bloaty_path = project.join(format!("{}.wasm", out_name));
fs::copy(in_path, &bloaty_path).expect("Copying the bloaty file to the project dir.");

(wasm_compact_file, wasm_compact_compressed_file, WasmBinaryBloaty(bloaty_file))
(wasm_compact_path, wasm_compact_compressed_path, WasmBinaryBloaty(bloaty_path))
}

fn compress_wasm(wasm_binary_path: &Path, compressed_binary_out_path: &Path) -> bool {
Expand Down

0 comments on commit f33171b

Please sign in to comment.