Skip to content

Commit

Permalink
Replace wasm-bindgen-cli with library wasm-bindgen-cli-support (#2257)
Browse files Browse the repository at this point in the history
Makes the build a bit more hermetic

Closes #2225

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)

<!-- This line will get updated when the PR build summary job finishes.
-->
PR Build Summary: https://build.rerun.io/pr/2257
  • Loading branch information
emilk committed Jun 15, 2023
1 parent e5eb631 commit 7772b25
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/reusable_build_and_test_wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ jobs:
run: ./scripts/setup_web.sh

# The first steps of setup_web.sh, for Windows:
- name: Install wasm32 and wasm-bindgen-cli for building the web-viewer Wasm on windows
- name: Install wasm32 cargo target for building the web-viewer Wasm on windows
if: inputs.platform == 'windows'
shell: bash
run: rustup target add wasm32-unknown-unknown && cargo install wasm-bindgen-cli --version 0.2.86
run: rustup target add wasm32-unknown-unknown

# The last step of setup_web.sh, for Windows.
# Since 'winget' is not available within the GitHub runner, we download the package directly:
Expand Down
2 changes: 2 additions & 0 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 crates/re_build_web_viewer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ all-features = true


[dependencies]
anyhow.workspace = true
cargo_metadata = "0.15"
wasm-bindgen-cli-support = "0.2.86"
40 changes: 16 additions & 24 deletions crates/re_build_web_viewer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

//! Build the Rerun web-viewer .wasm and generate the .js bindings for it.
use anyhow::Context as _;
use cargo_metadata::camino::Utf8PathBuf;

fn target_directory() -> Utf8PathBuf {
Expand All @@ -14,7 +15,7 @@ fn target_directory() -> Utf8PathBuf {
}

/// Build `re_viewer` as Wasm, generate .js bindings for it, and place it all into the `./web_viewer` folder.
pub fn build(release: bool, webgpu: bool) {
pub fn build(release: bool, webgpu: bool) -> anyhow::Result<()> {
eprintln!("Building web viewer wasm…");
eprintln!("We assume you've already run ./scripts/setup_web.sh");

Expand Down Expand Up @@ -93,7 +94,7 @@ pub fn build(release: bool, webgpu: bool) {
let status = cmd
.current_dir(root_dir)
.status()
.expect("Failed to build Wasm");
.context("Failed to build Wasm")?;
assert!(status.success(), "Failed to build Wasm");

// --------------------------------------------------------------------------------
Expand All @@ -106,29 +107,16 @@ pub fn build(release: bool, webgpu: bool) {
.join(build)
.join(format!("{crate_name}.wasm"));

let mut cmd = std::process::Command::new("wasm-bindgen");
cmd.args([
target_wasm_path.as_str(),
"--out-dir",
build_dir.as_str(),
"--out-name",
target_name.as_str(),
"--no-modules",
"--no-typescript",
]);
if !release {
cmd.arg("--debug");
}

eprintln!("> {cmd:?}");
let status = cmd
.current_dir(root_dir)
.status()
.expect("Failed to run wasm-bindgen");
assert!(status.success(), "Failed to run wasm-bindgen");
// wasm-bindgen --target web target_wasm_path --no-typescript --out-name target_name --out-dir build_dir
wasm_bindgen_cli_support::Bindgen::new()
.no_modules(true)?
.input_path(target_wasm_path.as_str())
.typescript(false)
.out_name(target_name.as_str())
.generate(build_dir.as_str())
.context("Failed to run wasm-bindgen")?;

// --------------------------------------------------------------------------------
// Optimize the wasm

if release {
eprintln!("Optimizing wasm with wasm-opt…");
Expand All @@ -143,9 +131,13 @@ pub fn build(release: bool, webgpu: bool) {
let status = cmd
.current_dir(root_dir)
.status()
.expect("Failed to run wasm-opt");
.context("Failed to run wasm-opt")?;
assert!(status.success(), "Failed to run wasm-opt");
}

// --------------------------------------------------------------------------------

eprintln!("Finished {wasm_path}");

Ok(())
}
8 changes: 6 additions & 2 deletions crates/re_build_web_viewer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@ fn main() -> ExitCode {
return ExitCode::FAILURE;
};

re_build_web_viewer::build(release, webgpu);
ExitCode::SUCCESS
if let Err(err) = re_build_web_viewer::build(release, webgpu) {
eprintln!("Failed to build web viewer: {err}");
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}

fn print_help() {
Expand Down
2 changes: 1 addition & 1 deletion crates/re_web_viewer_server/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ fn main() {
eprintln!("__ci feature detected: Skipping building of web viewer wasm.");
} else {
let release = std::env::var("PROFILE").unwrap() == "release";
re_build_web_viewer::build(release, is_tracked_env_var_set("RERUN_BUILD_WEBGPU"));
re_build_web_viewer::build(release, is_tracked_env_var_set("RERUN_BUILD_WEBGPU")).unwrap();
}
}
3 changes: 0 additions & 3 deletions scripts/setup_web.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ set -x
# For compiling to Wasm:
rustup target add wasm32-unknown-unknown

# For generating JS bindings:
cargo install wasm-bindgen-cli --version 0.2.86

# For local tests with `start_server.sh`:
# cargo install basic-http-server

Expand Down

0 comments on commit 7772b25

Please sign in to comment.