From 62df9d0ecddd45c9abdba4a3e6e013f72bdfed0c Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Fri, 23 Sep 2022 12:36:33 +0300 Subject: [PATCH] Add support for `WASM_BUILD_CLEAN_TARGET` environment variable that cleans up unnecessary wasm build artifacts --- utils/wasm-builder/src/lib.rs | 3 +++ utils/wasm-builder/src/wasm_project.rs | 32 ++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/utils/wasm-builder/src/lib.rs b/utils/wasm-builder/src/lib.rs index fc86a06170a50..47e86b63cefd2 100644 --- a/utils/wasm-builder/src/lib.rs +++ b/utils/wasm-builder/src/lib.rs @@ -146,6 +146,9 @@ const WASM_BUILD_NO_COLOR: &str = "WASM_BUILD_NO_COLOR"; /// Environment variable to set the toolchain used to compile the wasm binary. const WASM_BUILD_TOOLCHAIN: &str = "WASM_BUILD_TOOLCHAIN"; +/// Environment variable to instruct cleanup of `target` after successful wasm build. +const WASM_BUILD_CLEAN_TARGET: &str = "WASM_BUILD_CLEAN_TARGET"; + /// Environment variable that makes sure the WASM build is triggered. const FORCE_WASM_BUILD_ENV: &str = "FORCE_WASM_BUILD"; diff --git a/utils/wasm-builder/src/wasm_project.rs b/utils/wasm-builder/src/wasm_project.rs index 07219676413fc..f6acdfffe7a54 100644 --- a/utils/wasm-builder/src/wasm_project.rs +++ b/utils/wasm-builder/src/wasm_project.rs @@ -130,7 +130,7 @@ pub(crate) fn create_and_compile( features_to_enable, ); - let profile = build_project(&project, default_rustflags, cargo_cmd); + let profile = build_project(&project, default_rustflags, &cargo_cmd); let (wasm_binary, wasm_binary_compressed, bloaty) = compact_wasm_file(&project, profile, project_cargo_toml, wasm_binary_name); @@ -156,6 +156,10 @@ pub(crate) fn create_and_compile( build_helper::warning!("Error while adjusting the mtime of the wasm binaries: {}", err) } + if env::var(crate::WASM_BUILD_CLEAN_TARGET).is_ok() { + clean_target(&project, &cargo_cmd); + } + (final_wasm_binary, bloaty) } @@ -602,7 +606,7 @@ fn offline_build() -> bool { fn build_project( project: &Path, default_rustflags: &str, - cargo_cmd: CargoCommandVersioned, + cargo_cmd: &CargoCommandVersioned, ) -> Profile { let manifest_path = project.join("Cargo.toml"); let mut build_cmd = cargo_cmd.command(); @@ -651,6 +655,30 @@ fn build_project( } } +/// Build the project to create the WASM binary. +fn clean_target(project: &Path, cargo_cmd: &CargoCommandVersioned) { + let manifest_path = project.join("Cargo.toml"); + let mut build_cmd = cargo_cmd.command(); + + build_cmd + .arg("clean") + .arg(format!("--manifest-path={}", manifest_path.display())) + // Unset the `CARGO_TARGET_DIR` to prevent a cargo deadlock (cargo locks a target dir + // exclusive). The runner project is created in `CARGO_TARGET_DIR` and executing it will + // create a sub target directory inside of `CARGO_TARGET_DIR`. + .env_remove("CARGO_TARGET_DIR"); + + if super::color_output_enabled() { + build_cmd.arg("--color=always"); + } + + match build_cmd.status().map(|s| s.success()) { + Ok(true) => (), + // Use `process.exit(1)` to have a clean error output. + _ => process::exit(1), + } +} + /// Compact the WASM binary using `wasm-gc` and compress it using zstd. fn compact_wasm_file( project: &Path,