Skip to content

Commit

Permalink
feat: utilize more WASM target features
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan770 committed Jul 30, 2024
1 parent 1285c7d commit 21fd0c7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 32 deletions.
9 changes: 8 additions & 1 deletion crates/teddybear-js/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ categories.workspace = true
crate-type = ["cdylib", "rlib"]

[package.metadata.wasm-pack.profile.release]
wasm-opt = ["--enable-simd", "-Oz"]
wasm-opt = [
"--enable-bulk-memory",
"--enable-mutable-globals",
"--enable-nontrapping-float-to-int",
"--enable-sign-ext",
"--enable-simd",
"-Oz"
]

[dependencies]
teddybear-c2pa = { workspace = true }
Expand Down
25 changes: 13 additions & 12 deletions flake.lock

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

40 changes: 29 additions & 11 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
type = "github";
owner = "ipetkov";
repo = "crane";
ref = "v0.18.0";
inputs.nixpkgs.follows = "nixpkgs";
};

Expand Down Expand Up @@ -97,10 +98,23 @@

cargoArtifacts = craneLib.buildDepsOnly nativeArgs;

# https://webassembly.org/features
# https://github.com/rust-lang/rust/blob/master/compiler/rustc_target/src/target_features.rs#L323-L337
enabledWasmFeatures = [
"bulk-memory"
"mutable-globals"
"nontrapping-fptoint"
"sign-ext"
"simd128"
];

wasmArgs =
commonArgs
// {
RUSTFLAGS = "-Ctarget-feature=+simd128";
RUSTFLAGS = let
fmt = pkgs.lib.concatMapStringsSep "," (f: "+${f}") enabledWasmFeatures;
in "-C target-feature=${fmt}";

CARGO_BUILD_TARGET = "wasm32-unknown-unknown";
};

Expand All @@ -111,15 +125,19 @@
cargoExtraArgs = "-p teddybear-js --locked";
});

esm = pkgs.callPackage ./nix/package.nix {
inherit craneLib wasmArgs wasmCargoArtifacts wasm-pack;
};

cjs = pkgs.callPackage ./nix/package.nix {
inherit craneLib wasmArgs wasmCargoArtifacts wasm-pack;
mkPackage = buildForNode:
pkgs.callPackage ./nix/package.nix {
inherit
buildForNode
craneLib
wasmArgs
wasmCargoArtifacts
wasm-pack
;
};

buildForNode = true;
};
esm = mkPackage false;
cjs = mkPackage true;

uni = pkgs.callPackage ./nix/uni.nix {
inherit cjs esm;
Expand Down Expand Up @@ -149,7 +167,7 @@
checks = {
inherit cjs esm uni;

node = pkgs.callPackage ./nix/node-testing.nix {
e2e-test = pkgs.callPackage ./nix/node-testing.nix {
inherit uni;

src = nix-filter.lib.filter {
Expand All @@ -166,7 +184,7 @@
yarnLockHash = "sha256-LTKJdshmknuSy2hC3SfYRBR5SfDamoMVH+aeC/aKwyA=";
};

test = craneLib.cargoTest (nativeArgs
unit-test = craneLib.cargoTest (nativeArgs
// {
inherit cargoArtifacts;
});
Expand Down
6 changes: 3 additions & 3 deletions nix/package.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
binaryen,
buildForNode,
craneLib,
lib,
binaryen,
wabt,
wasmArgs,
wasmCargoArtifacts,
wabt,
wasm-bindgen-cli,
wasm-pack,
buildForNode ? false,
}: let
target =
if buildForNode
Expand Down
10 changes: 5 additions & 5 deletions nix/uni.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ stdenvNoCC.mkDerivation {
runHook preBuild
jq '.files = ["index_bg.js", "index_bg.wasm", "index.cjs", "index.mjs", "index.d.ts"]' \
${esm}/package.json \
> package.json
${esm}/package.json > package.json
jq '.main = "index.cjs"' package.json | sponge package.json
jq '.module = "index.mjs"' package.json | sponge package.json
jq '.sideEffects = ["./index.js", "./snippets/*"]' package.json | sponge package.json
jq '.main = "index.cjs"' package.json \
| jq '.module = "index.mjs"' \
| jq '.sideEffects = ["./index.cjs", "./index.mjs"]' \
| sponge package.json
# CJS
cp ${cjs}/index.js index.cjs
Expand Down

0 comments on commit 21fd0c7

Please sign in to comment.