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

Add support for WASM_BUILD_CLEAN_TARGET environment variable that cleans up unnecessary wasm build artifacts #12672

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions utils/wasm-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
32 changes: 30 additions & 2 deletions utils/wasm-builder/src/wasm_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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)
}

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down