From 7944fd3f7fa00c1c3067d4102e9bfb5b623fc9b1 Mon Sep 17 00:00:00 2001 From: Sebastian Moser Date: Thu, 23 May 2024 00:24:09 +0200 Subject: [PATCH] added hello-world wasm example and npmPackage nix derivation --- flake.nix | 47 ++++++++++++++++++++++++++++++++++---- src/lib.rs | 9 +++++++- src/platform/wasm/mod.rs | 19 +++++++++++++++ src/platform/wasm/utils.rs | 0 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/platform/wasm/mod.rs create mode 100644 src/platform/wasm/utils.rs diff --git a/flake.nix b/flake.nix index d0fb590..341e727 100644 --- a/flake.nix +++ b/flake.nix @@ -21,23 +21,56 @@ let pkgs = nixpkgs.legacyPackages.${system}; wasmToolchain = fenix.packages.${system}.combine [ - fenix.packages.${system}.targets.wasm32-unknown-unknown.stable.toolchain - fenix.packages.${system}.complete.toolchain + fenix.packages.${system}.targets.wasm32-unknown-unknown.latest.toolchain + fenix.packages.${system}.latest.toolchain ]; - osToolchain = fenix.packages.${system}.complete.toolchain; + osToolchain = fenix.packages.${system}.latest.toolchain; wasmCrane = crane.lib.${system}.overrideToolchain wasmToolchain; osCrane = crane.lib.${system}.overrideToolchain osToolchain; + wasmArtifacts = wasmCrane.buildDepsOnly ({ + src = self; + doCheck = false; # tests does not work in wasm + CARGO_BUILD_TARGET = "wasm32-unknown-unknown"; + }); + in { ############################## PACKAGES ############################## packages.default = osCrane.buildPackage { src = "${self}"; + cargoExtraArgs = "--bin mize --features os-binary"; }; - packages = { + packages = rec { wasm = wasmCrane.buildPackage { src = "${self}"; + CARGO_BUILD_TARGET = "wasm32-unknown-unknown"; + doCheck = false; }; + + # Thanks to: + # - https://github.com/dl-solarity/hardhat-diamond-tools/blob/e5b8bc9624fbf89eda83a8ce4f6b6672ea83f550/flake.nix#L103 + # - https://github.com/ipetkov/crane/issues/362#issuecomment-1683220603 + npmPackage = wasmCrane.mkCargoDerivation { + name = "mize-npm-package"; + + # i can't use CARGO_BUILD_TARGET to set the target + # because then the cargo install run by wasm-pack tries to build the wasm-bindgen-cli for a wasm target... + cargoExtraArgs = "--target wasm32-unknown-unknown --features wasm-target"; + src = self; + + cargoArtifacts = wasmArtifacts; + doCheck = false; + + buildPhaseCargoCommand = '' + mkdir -p $out/pkg + + HOME=$(mktemp -d fake-homeXXXX) wasm-pack build --out-dir $out/pkg --scope=c2vi -- --features wasm-target + ''; + + buildInputs = with pkgs; [ wasm-bindgen-cli binaryen wasm-pack wasmToolchain ]; + }; + }; ############################## DEV SHELLS ############################## @@ -58,6 +91,10 @@ in { ]; }; - }); + }) // { + +############################## SOME GLOBAL OUTPUTS ############################## + inherit inputs; + }; } diff --git a/src/lib.rs b/src/lib.rs index 9698367..8b98d18 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,8 @@ static PROTO_VERSION: u8 = 1; +// the core part houses the code that can run on any platform +/* pub mod core { pub mod memstore; pub mod instance; @@ -14,7 +16,12 @@ pub mod core { } pub use core::*; - +// */ + +pub mod platform { + #[cfg(feature = "wasm-target")] + pub mod wasm; +} diff --git a/src/platform/wasm/mod.rs b/src/platform/wasm/mod.rs new file mode 100644 index 0000000..0241800 --- /dev/null +++ b/src/platform/wasm/mod.rs @@ -0,0 +1,19 @@ +mod utils; + +use wasm_bindgen::prelude::*; + +// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global +// allocator. +#[cfg(feature = "wee_alloc")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +#[wasm_bindgen] +extern { + fn alert(s: &str); +} + +#[wasm_bindgen] +pub fn greet() { + alert("Hello, wasm-game-of-life!"); +} diff --git a/src/platform/wasm/utils.rs b/src/platform/wasm/utils.rs new file mode 100644 index 0000000..e69de29