Skip to content

Commit

Permalink
nix: add wasm support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomjnixon committed Jan 29, 2024
1 parent 020813b commit 74d808b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
30 changes: 29 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
flake-utils.lib.eachDefaultSystem
(system:
let
pkgs = nixpkgs.legacyPackages.${system};
pkgs = import nixpkgs {
system = system;
config.allowUnsupportedSystem = true; # for eigen wasm
};
pkgs_wasm = pkgs.pkgsCross.wasi32;

devtools = [
pkgs.clang-tools
Expand All @@ -33,6 +37,30 @@
nativeBuildInputs = attrs.nativeBuildInputs ++ devtools;
});
devShells.default = devShells.libear;

# wasm versions of packages

packages.xsimd_wasm = pkgs_wasm.xsimd.overrideAttrs {
# this should be an overlay
version = pkgs.xsimd.version;
src = pkgs.xsimd.src;
};
packages.libear_wasm = (pkgs_wasm.callPackage ./nix/libear.nix {
src = ./.;
boost = pkgs.boost; # doesn't build for wasm, but we only use headers, so use system version
xsimd = packages.xsimd_wasm;
}).overrideAttrs (super: {
cmakeBuildType = "MinSizeRel";
});

devShells.libear_wasm = packages.libear_wasm.overrideAttrs (attrs: {
nativeBuildInputs = attrs.nativeBuildInputs ++ devtools ++ [
pkgs.wabt
pkgs.wasmtime
pkgs.nodejs
pkgs.nodePackages.prettier
];
});
}
);
}
Expand Down
20 changes: 19 additions & 1 deletion nix/libear.nix
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
{ lib, buildPackages, stdenv, cmake, src, ninja, boost, eigen, xsimd }:
{ lib, buildPackages, stdenv, cmake, src, ninja, boost, eigen, xsimd, binaryen, nodejs }:
let
isCross = stdenv.buildPlatform != stdenv.hostPlatform;
isWasm = stdenv.hostPlatform.isWasm;
in
(stdenv.mkDerivation {
name = "libear";
inherit src;
nativeBuildInputs = [
cmake
ninja
] ++ lib.optionals isWasm [
binaryen # gets used automatically by clang-ld if in path
nodejs
];

buildInputs = [ boost eigen xsimd ];
cmakeFlags = [
"-DEAR_USE_INTERNAL_EIGEN=OFF"
Expand All @@ -17,7 +22,20 @@ in
]
++ lib.optionals isCross [
"-DCMAKE_CROSSCOMPILING_EMULATOR=${stdenv.hostPlatform.emulator buildPackages}"
] ++ lib.optionals isWasm [
"-DEAR_NO_EXCEPTIONS=ON"
];


doCheck = true;

preConfigure = lib.optionalString isWasm ''
# for wasmtime cache
HOME=$(pwd)
# forced off in make-derivation.nix when build platform can't execute host
# platform, but we have an emulator
doCheck=1
'';

env.NIX_CFLAGS_COMPILE = lib.optionalString isWasm "-DEIGEN_HAS_CXX11_ATOMIC=0 -DCATCH_CONFIG_NO_POSIX_SIGNALS";
})

0 comments on commit 74d808b

Please sign in to comment.