Skip to content

Commit

Permalink
Add the wasm profiles in just-in-time
Browse files Browse the repository at this point in the history
This helps to avoid repeating ourselves and fixes the warning
  • Loading branch information
bobbobbio committed Jan 22, 2024
1 parent a4596a6 commit f17eaa9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 29 deletions.
6 changes: 0 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
resolver = "2"
members = [ "crates/*" ]

[profile.wasm_dev]
inherits = "dev"

[profile.wasm_release]
inherits = "release"

[profile.dev]
panic = "abort"

Expand Down
8 changes: 0 additions & 8 deletions crates/maelstrom-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,3 @@ cargo_metadata.workspace = true
# These dependencies ensure the build script re-runs
maelstrom-plot.workspace = true
maelstrom-base.workspace = true

# These profiles are copied from the workspace root so they can be used when
# publishing. See this issue <https://github.com/rust-lang/cargo/issues/8264>
[profile.wasm_dev]
inherits = "dev"

[profile.wasm_release]
inherits = "release"
63 changes: 48 additions & 15 deletions crates/maelstrom-web/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::io::Write as _;
use std::path::{Path, PathBuf};
use std::process::Command;
use wasm_bindgen_cli_support::Bindgen;
Expand Down Expand Up @@ -90,29 +91,60 @@ fn create_tar(web_dir: &Path, pkg_dir: &Path, output_file: &Path) {
builder.finish().unwrap();
}

fn build_wasm(target: &str, profile: &str) {
sh(
[
"cargo",
"build",
"--lib",
"--target",
target,
"--profile",
profile,
],
".",
);
fn with_profiles(workspace_root: &Path, body: impl FnOnce() + std::panic::UnwindSafe) {
let cargo_toml = workspace_root.join("Cargo.toml");
let cargo_toml_old = workspace_root.join("Cargo.toml.old");
fs::copy(&cargo_toml, &cargo_toml_old).unwrap();

let mut f = fs::OpenOptions::new()
.read(true)
.append(true)
.open(&cargo_toml)
.unwrap();
f.write_all(
b"\
[profile.wasm_dev]\n\
inherits = \"dev\"\n\
[profile.wasm_release]\n\
inherits = \"release\"\n\
",
)
.unwrap();

let result = std::panic::catch_unwind(|| {
body();
});

fs::rename(cargo_toml_old, cargo_toml).unwrap();

result.unwrap();
}

fn build_wasm(target: &str, profile: &str, workspace_root: &Path) {
with_profiles(workspace_root, || {
sh(
[
"cargo",
"build",
"--lib",
"--target",
target,
"--profile",
profile,
],
".",
);
});
}

fn create_web_tar(profile: &str, build_dir: &Path) {
fn create_web_tar(profile: &str, build_dir: &Path, workspace_root: &Path) {
let target = "wasm32-unknown-unknown";
let pkg_dir = build_dir.join(profile).join("wasm_pkg");

fs::remove_dir_all(&pkg_dir).ok();
fs::create_dir_all(&pkg_dir).unwrap();

build_wasm(target, profile);
build_wasm(target, profile, workspace_root);
let wasm_file = build_dir
.join(target)
.join(profile)
Expand Down Expand Up @@ -142,6 +174,7 @@ fn main() {
create_web_tar(
&format!("wasm_{profile}"),
metadata.target_directory.as_std_path(),
metadata.workspace_root.as_std_path(),
);

// this should only be the case when publishing, and at that point we can't be the ones
Expand Down

0 comments on commit f17eaa9

Please sign in to comment.