From 4aad0e88a725b5fd6d873aac8b09061b4afb1a11 Mon Sep 17 00:00:00 2001 From: cheme Date: Wed, 26 Aug 2020 13:47:39 +0200 Subject: [PATCH 1/4] Ignore gc for debug build. --- utils/wasm-builder/src/wasm_project.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index 6f8f47881b03f..acc80f143f725 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -470,15 +470,21 @@ fn compact_wasm_file( cargo_manifest: &Path, wasm_workspace: &Path, ) -> (WasmBinary, WasmBinaryBloaty) { - let target = if is_release_build() { "release" } else { "debug" }; + let is_release_build = is_release_build(); + let target = if is_release_build { "release" } else { "debug" }; let wasm_binary = get_wasm_binary_name(cargo_manifest); let wasm_file = wasm_workspace.join("target/wasm32-unknown-unknown") .join(target) .join(format!("{}.wasm", wasm_binary)); let wasm_compact_file = project.join(format!("{}.compact.wasm", wasm_binary)); - wasm_gc::garbage_collect_file(&wasm_file, &wasm_compact_file) - .expect("Failed to compact generated WASM binary."); + if is_release_build { + wasm_gc::garbage_collect_file(&wasm_file, &wasm_compact_file) + .expect("Failed to compact generated WASM binary."); + } else { + fs::copy(&wasm_file, &wasm_compact_file) + .expect("Failed to compact generated WASM binary."); + } (WasmBinary(wasm_compact_file), WasmBinaryBloaty(wasm_file)) } From d0336a3575468392356f6ac0430f5c7a876eafd5 Mon Sep 17 00:00:00 2001 From: cheme Date: Fri, 28 Aug 2020 12:09:28 +0200 Subject: [PATCH 2/4] alternate implementation --- docs/README.adoc | 2 +- utils/wasm-builder/src/lib.rs | 35 ++++++++++++++++++-------- utils/wasm-builder/src/wasm_project.rs | 21 ++++++++-------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/docs/README.adoc b/docs/README.adoc index d1daeed07b5dc..e1ed86c2d5269 100644 --- a/docs/README.adoc +++ b/docs/README.adoc @@ -321,7 +321,7 @@ we support multiple environment variables: * `TRIGGER_WASM_BUILD` - Can be set to trigger a WASM build. On subsequent calls the value of the variable needs to change. As WASM builder instructs `cargo` to watch for file changes this environment variable should only be required in certain circumstances. -* `WASM_TARGET_DIRECTORY` - Will copy any build WASM binary to the given directory. The path needs +* `WASM_TARGET_DIRECTORY` - Will copy release build WASM binary to the given directory. The path needs to be absolute. * `WASM_BUILD_RUSTFLAGS` - Extend `RUSTFLAGS` given to `cargo build` while building the wasm binary. * `WASM_BUILD_NO_COLOR` - Disable color output of the wasm build. diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index ab64db56fec34..945ab53836a57 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -168,17 +168,30 @@ pub fn build_project_with_default_rustflags( default_rustflags, ); - write_file_if_changed( - file_name.into(), - format!( - r#" - pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}")); - pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}")); - "#, - wasm_binary = wasm_binary.wasm_binary_path_escaped(), - wasm_binary_bloaty = bloaty.wasm_binary_bloaty_path_escaped(), - ), - ); + if let Some(wasm_binary) = wasm_binary { + write_file_if_changed( + file_name.into(), + format!( + r#" + pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}")); + pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}")); + "#, + wasm_binary = wasm_binary.wasm_binary_path_escaped(), + wasm_binary_bloaty = bloaty.wasm_binary_bloaty_path_escaped(), + ), + ); + } else { + write_file_if_changed( + file_name.into(), + format!( + r#" + pub const WASM_BINARY: Option<&[u8]> = WASM_BINARY_BLOATY; + pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}")); + "#, + wasm_binary_bloaty = bloaty.wasm_binary_bloaty_path_escaped(), + ), + ); + } } /// Checks if the build of the WASM binary should be skipped. diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index acc80f143f725..4c927f7bdeabd 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -91,7 +91,7 @@ impl Drop for WorkspaceLock { pub fn create_and_compile( cargo_manifest: &Path, default_rustflags: &str, -) -> (WasmBinary, WasmBinaryBloaty) { +) -> (Option, WasmBinaryBloaty) { let wasm_workspace_root = get_wasm_workspace_root(); let wasm_workspace = wasm_workspace_root.join("wbuild"); @@ -113,7 +113,9 @@ pub fn create_and_compile( &wasm_workspace, ); - copy_wasm_to_target_directory(cargo_manifest, &wasm_binary); + wasm_binary.as_ref().map(|wasm_binary| + copy_wasm_to_target_directory(cargo_manifest, wasm_binary) + ); generate_rerun_if_changed_instructions(cargo_manifest, &project, &wasm_workspace); @@ -469,24 +471,23 @@ fn compact_wasm_file( project: &Path, cargo_manifest: &Path, wasm_workspace: &Path, -) -> (WasmBinary, WasmBinaryBloaty) { +) -> (Option, WasmBinaryBloaty) { let is_release_build = is_release_build(); let target = if is_release_build { "release" } else { "debug" }; let wasm_binary = get_wasm_binary_name(cargo_manifest); let wasm_file = wasm_workspace.join("target/wasm32-unknown-unknown") .join(target) .join(format!("{}.wasm", wasm_binary)); - let wasm_compact_file = project.join(format!("{}.compact.wasm", wasm_binary)); - - if is_release_build { + let wasm_compact_file = if is_release_build { + let wasm_compact_file = project.join(format!("{}.compact.wasm", wasm_binary)); wasm_gc::garbage_collect_file(&wasm_file, &wasm_compact_file) .expect("Failed to compact generated WASM binary."); + Some(WasmBinary(wasm_compact_file)) } else { - fs::copy(&wasm_file, &wasm_compact_file) - .expect("Failed to compact generated WASM binary."); - } + None + }; - (WasmBinary(wasm_compact_file), WasmBinaryBloaty(wasm_file)) + (wasm_compact_file, WasmBinaryBloaty(wasm_file)) } /// Custom wrapper for a [`cargo_metadata::Package`] to store it in From dd0afef29ff350a51d8eaf6a1f25bd812bf034b0 Mon Sep 17 00:00:00 2001 From: cheme Date: Mon, 7 Sep 2020 19:22:47 +0200 Subject: [PATCH 3/4] Update utils/wasm-builder/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- utils/wasm-builder/src/lib.rs | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index 945ab53836a57..b342ec6253fc8 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -168,30 +168,29 @@ pub fn build_project_with_default_rustflags( default_rustflags, ); - if let Some(wasm_binary) = wasm_binary { - write_file_if_changed( - file_name.into(), - format!( - r#" - pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}")); - pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}")); - "#, - wasm_binary = wasm_binary.wasm_binary_path_escaped(), - wasm_binary_bloaty = bloaty.wasm_binary_bloaty_path_escaped(), - ), - ); + let (wasm_binary, wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary { + ( + wasm_binary.wasm_binary_path_escaped(), + bloaty.wasm_binary_bloaty_path_escaped() + ) } else { - write_file_if_changed( + ( + wasm_binary.wasm_binary_path_escaped(), + bloaty.wasm_binary_bloaty_path_escaped() + ) + }; + + write_file_if_changed( file_name.into(), format!( r#" - pub const WASM_BINARY: Option<&[u8]> = WASM_BINARY_BLOATY; + pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}")); pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}")); "#, - wasm_binary_bloaty = bloaty.wasm_binary_bloaty_path_escaped(), + wasm_binary, + wasm_binary_bloaty, ), ); - } } /// Checks if the build of the WASM binary should be skipped. From 8f6fcff22301470ddc1733d7732c69ad005dfd92 Mon Sep 17 00:00:00 2001 From: cheme Date: Mon, 7 Sep 2020 19:30:56 +0200 Subject: [PATCH 4/4] fix --- utils/wasm-builder/src/lib.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index b342ec6253fc8..f1a1c7729a070 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -171,12 +171,12 @@ pub fn build_project_with_default_rustflags( let (wasm_binary, wasm_binary_bloaty) = if let Some(wasm_binary) = wasm_binary { ( wasm_binary.wasm_binary_path_escaped(), - bloaty.wasm_binary_bloaty_path_escaped() + bloaty.wasm_binary_bloaty_path_escaped(), ) } else { ( - wasm_binary.wasm_binary_path_escaped(), - bloaty.wasm_binary_bloaty_path_escaped() + bloaty.wasm_binary_bloaty_path_escaped(), + bloaty.wasm_binary_bloaty_path_escaped(), ) }; @@ -187,8 +187,8 @@ pub fn build_project_with_default_rustflags( pub const WASM_BINARY: Option<&[u8]> = Some(include_bytes!("{wasm_binary}")); pub const WASM_BINARY_BLOATY: Option<&[u8]> = Some(include_bytes!("{wasm_binary_bloaty}")); "#, - wasm_binary, - wasm_binary_bloaty, + wasm_binary = wasm_binary, + wasm_binary_bloaty = wasm_binary_bloaty, ), ); }