diff --git a/Cargo.lock b/Cargo.lock index c200abe6..fe75f0dd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2535,6 +2535,7 @@ dependencies = [ "maelstrom-base", "maelstrom-test", "maelstrom-util", + "maelstrom-web", "maplit", "serde", "serde_with", diff --git a/Cargo.toml b/Cargo.toml index 2f18e9ae..d5b2eea0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/crates/maelstrom-broker/Cargo.toml b/crates/maelstrom-broker/Cargo.toml index bb940fa4..a922a11c 100644 --- a/crates/maelstrom-broker/Cargo.toml +++ b/crates/maelstrom-broker/Cargo.toml @@ -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 diff --git a/crates/maelstrom-broker/build.rs b/crates/maelstrom-broker/build.rs deleted file mode 100644 index fdcc21ee..00000000 --- a/crates/maelstrom-broker/build.rs +++ /dev/null @@ -1,31 +0,0 @@ -use std::{path::Path, process::Command}; - -fn sh<'a>(cmd: impl IntoIterator, dir: impl AsRef) { - 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=../maelstrom-web/build.sh"); - println!("cargo:rerun-if-changed=../maelstrom-web/Cargo.toml"); - println!("cargo:rerun-if-changed=../maelstrom-web/src/"); - println!("cargo:rerun-if-changed=../maelstrom-web/www/"); - println!("cargo:rerun-if-changed=../maelstrom-plot/src/"); - println!("cargo:rerun-if-changed=../maelstrom-base/src/"); - - #[cfg(not(debug_assertions))] - let profile = "release"; - - #[cfg(debug_assertions)] - let profile = "dev"; - - sh( - ["crates/maelstrom-web/build.sh", &format!("wasm_{profile}")], - "../..", - ); -} diff --git a/crates/maelstrom-broker/src/http.rs b/crates/maelstrom-broker/src/http.rs index f1836807..e35bcadc 100644 --- a/crates/maelstrom-broker/src/http.rs +++ b/crates/maelstrom-broker/src/http.rs @@ -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, @@ -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, } diff --git a/crates/maelstrom-web/Cargo.toml b/crates/maelstrom-web/Cargo.toml index 91f70f08..284f11d2 100644 --- a/crates/maelstrom-web/Cargo.toml +++ b/crates/maelstrom-web/Cargo.toml @@ -10,7 +10,7 @@ rust-version.workspace = true version.workspace = true [lib] -crate-type = ["cdylib"] +crate-type = ["cdylib", "lib"] [dependencies] anyhow.workspace = true diff --git a/crates/maelstrom-web/build.rs b/crates/maelstrom-web/build.rs new file mode 100644 index 00000000..d97fd4be --- /dev/null +++ b/crates/maelstrom-web/build.rs @@ -0,0 +1,41 @@ +use std::{ + path::{Path, PathBuf}, + process::Command, +}; + +fn sh<'a>(cmd: impl IntoIterator, dir: impl AsRef) { + 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-/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(), + ], + ".", + ); + } +} diff --git a/crates/maelstrom-web/build.sh b/crates/maelstrom-web/build.sh index 8220a8ce..46fb16c4 100755 --- a/crates/maelstrom-web/build.sh +++ b/crates/maelstrom-web/build.sh @@ -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 @@ -33,5 +31,3 @@ fi cp www/* $pkg_dir tar --create --directory $pkg_dir . --file $build_dir/$profile/web.tar - -popd diff --git a/crates/maelstrom-web/src/lib.rs b/crates/maelstrom-web/src/lib.rs index be8751cd..e868e1be 100644 --- a/crates/maelstrom-web/src/lib.rs +++ b/crates/maelstrom-web/src/lib.rs @@ -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");