Skip to content

Commit

Permalink
Separate the WASM repl in wrapper crate
Browse files Browse the repository at this point in the history
  • Loading branch information
yannham committed Jun 15, 2022
1 parent 24bdbde commit 841cfb7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
7 changes: 7 additions & 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 @@ -72,6 +72,7 @@ members = [
".",
"lsp/nls",
"utilities",
"nickel-wasm-repl",
]

# Enable this to use flamegraphs
Expand Down
46 changes: 8 additions & 38 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
src = if isDevShell then null else self;

buildPhase = ''
cargo build --workspace --release --frozen --offline
cargo build --workspace --exclude nickel-repl --release --frozen --offline
'';

doCheck = true;
Expand Down Expand Up @@ -213,7 +213,7 @@
RUST_SRC_PATH = "${rust}/lib/rustlib/src/rust/library";
};

buildNickelWASM =
buildNickelWasm =
{ channel ? "stable"
, optimize ? true
}:
Expand All @@ -228,8 +228,6 @@

src = self;

nativeBuildInputs = [ pkgs.jq ];

buildInputs = [
rust
pkgs.wasm-pack
Expand All @@ -238,42 +236,14 @@
cargoHome
] ++ missingSysPkgs;

preBuild = ''
# Wasm-pack requires to change the crate type. Cargo doesn't yet
# support having different crate types depending on the target, so
# we switch there
sed -i 's/\[lib\]/[lib]\ncrate-type = ["cdylib", "rlib"]/' Cargo.toml
# This is a hack to prevent the fs2 crate from being compiled on wasm.
# This may be able to be removed once one or more of these issues are resolved:
# https://github.com/bheisler/criterion.rs/issues/461
# https://github.com/rust-lang/cargo/issues/1596
# https://github.com/rust-lang/cargo/issues/1197
# https://github.com/rust-lang/cargo/issues/5777
sed -i '/nickel-lang-utilities/d' Cargo.toml
'';

buildPhase = ''
runHook preBuild
wasm-pack build --mode no-install -- --no-default-features --features repl-wasm --frozen --offline
cd nickel-wasm-repl
wasm-pack build --mode no-install -- --no-default-features --frozen --offline
# Because of wasm-pack not using existing wasm-opt
# (https://github.com/rustwasm/wasm-pack/issues/869), we have to
# run wasm-opt manually
echo "[Nix build script]Manually running wasm-opt..."
wasm-opt ${if optimize then "-O4 " else "-O0"} pkg/nickel_lang_bg.wasm -o pkg/nickel_lang_bg.wasm
runHook postBuild
'';

postBuild = ''
# Wasm-pack forces the name of both the normal crate and the
# generated NPM package to be the same. Unfortunately, there already
# exists a nickel package in the NPM registry, so we use nickel-repl
# instead
jq '.name = "nickel-repl"' pkg/package.json > package.json.patched \
&& rm -f pkg/package.json \
&& mv package.json.patched pkg/package.json
echo "[Nix build script] Manually running wasm-opt..."
wasm-opt ${if optimize then "-O4 " else "-O0"} pkg/nickel_repl_bg.wasm -o pkg/nickel_repl.wasm
'';

installPhase = ''
Expand Down Expand Up @@ -345,7 +315,7 @@
defaultPackage = packages.build;
packages = {
build = buildNickel { };
buildWasm = buildNickelWASM { optimize = true; };
buildWasm = buildNickelWasm { optimize = true; };
dockerImage = buildDocker packages.build; # TODO: docker image should be a passthru
inherit vscodeExtension;
inherit userManual;
Expand All @@ -360,7 +330,7 @@

checks = {
# wasm-opt can take long: eschew optimizations in checks
wasm = buildNickelWASM { channel = "stable"; optimize = false; };
wasm = buildNickelWasm { channel = "stable"; optimize = false; };
# out of sync, disabling for now -> https://github.com/tweag/nickel/issue/552
#specs = makamSpecs;
pre-commit = defaultPackage.pre-commit;
Expand Down
17 changes: 17 additions & 0 deletions nickel-wasm-repl/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "nickel-repl"
version = "0.1.0"
authors = ["Nickel team"]
license = "MIT"
readme = "README.md"
description = "WebAssembly REPL for the Nickel programming language."
homepage = "https://nickel-lang.org"
repository = "https://github.com/tweag/nickel"
keywords = ["configuration", "language", "nix", "nickel"]
edition = "2018"

[dependencies]
nickel-lang = {default-features = false, path = "../", version = "0.1.0", features=["repl-wasm"]}

[lib]
crate-type = ["cdylib", "rlib"]
1 change: 1 addition & 0 deletions nickel-wasm-repl/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub use nickel_lang::*;

0 comments on commit 841cfb7

Please sign in to comment.