Skip to content

Commit

Permalink
Change the way we build maelstrom-web wasm code
Browse files Browse the repository at this point in the history
This new way should allow publishing hopefully
  • Loading branch information
bobbobbio committed Jan 21, 2024
1 parent a1b1936 commit bec2536
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 43 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ maelstrom-container = { version = "0.5.0-dev", path = "crates/maelstrom-containe
maelstrom-plot = { version = "0.5.0-dev", path = "crates/maelstrom-plot" }
maelstrom-test = { version = "0.5.0-dev", path = "crates/maelstrom-test" }
maelstrom-util = { version = "0.5.0-dev", path = "crates/maelstrom-util" }
maelstrom-web = { version = "0.5.0-dev", path = "crates/maelstrom-web" }
maelstrom-worker = { version = "0.5.0-dev", path = "crates/maelstrom-worker" }
maelstrom-worker-child = { version = "0.5.0-dev", path = "crates/maelstrom-worker-child" }
nc = { version = "0.8.18", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions crates/maelstrom-broker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ hyper-tungstenite.workspace = true
hyper.workspace = true
maelstrom-base.workspace = true
maelstrom-util.workspace = true
maelstrom-web.workspace = true
serde.workspace = true
serde_with.workspace = true
tar.workspace = true
Expand Down
31 changes: 0 additions & 31 deletions crates/maelstrom-broker/build.rs

This file was deleted.

7 changes: 1 addition & 6 deletions crates/maelstrom-broker/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use futures::{
use hyper::{server::conn::Http, service::Service, upgrade::Upgraded, Body, Request, Response};
use hyper_tungstenite::{tungstenite, HyperWebsocket, WebSocketStream};
use maelstrom_base::{proto::BrokerToClient, ClientId};
use maelstrom_web::WASM_TAR;
use slog::{debug, error, o, Logger};
use std::{
collections::HashMap,
Expand All @@ -32,12 +33,6 @@ use tar::Archive;
use tokio::{net::TcpListener, sync::mpsc::UnboundedReceiver};
use tungstenite::Message;

/// The embedded website. This is built from [`maelstrom-web`].
#[cfg(not(debug_assertions))]
const WASM_TAR: &[u8] = include_bytes!("../../../target/wasm_release/web.tar");
#[cfg(debug_assertions)]
const WASM_TAR: &[u8] = include_bytes!("../../../target/wasm_dev/web.tar");

pub struct TarHandler {
map: HashMap<String, &'static [u8]>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/maelstrom-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rust-version.workspace = true
version.workspace = true

[lib]
crate-type = ["cdylib"]
crate-type = ["cdylib", "lib"]

[dependencies]
anyhow.workspace = true
Expand Down
41 changes: 41 additions & 0 deletions crates/maelstrom-web/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use std::{
path::{Path, PathBuf},
process::Command,
};

fn sh<'a>(cmd: impl IntoIterator<Item = &'a str>, dir: impl AsRef<Path>) {
let cmd: Vec<&str> = cmd.into_iter().collect();
let status = Command::new(cmd[0])
.args(&cmd[1..])
.current_dir(dir)
.status()
.unwrap();
assert!(status.success(), "{cmd:?} failed with status: {status:?}");
}

fn main() {
println!("cargo:rerun-if-changed=build.sh");

#[cfg(not(debug_assertions))]
let profile = "release";

#[cfg(debug_assertions)]
let profile = "dev";

let mut build_dir = PathBuf::from(std::env::var("OUT_DIR").unwrap());
// should be some path like ../target/debug/build/maelstrom-web-<hash>/out
for _ in 0..4 {
build_dir.pop();
}

if std::env::var("TARGET").unwrap() != "wasm32-unknown-unknown" {
sh(
[
"./build.sh",
&format!("wasm_{profile}"),
build_dir.to_str().unwrap(),
],
".",
);
}
}
6 changes: 1 addition & 5 deletions crates/maelstrom-web/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
set -ex

profile=$1

pushd crates/maelstrom-web
build_dir=$2

target=wasm32-unknown-unknown
build_dir=../../target
pkg_dir=$build_dir/$profile/wasm_pkg/
mkdir -p $pkg_dir

Expand All @@ -33,5 +31,3 @@ fi

cp www/* $pkg_dir
tar --create --directory $pkg_dir . --file $build_dir/$profile/web.tar

popd
6 changes: 6 additions & 0 deletions crates/maelstrom-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ pub async fn start() -> Result<(), JsValue> {
Err(e) => panic!("error: {e:?}"),
}
}

#[cfg(all(not(target_arch = "wasm32"), not(debug_assertions)))]
pub const WASM_TAR: &[u8] = include_bytes!("../../../target/wasm_release/web.tar");

#[cfg(all(not(target_arch = "wasm32"), debug_assertions))]
pub const WASM_TAR: &[u8] = include_bytes!("../../../target/wasm_dev/web.tar");

0 comments on commit bec2536

Please sign in to comment.