zstd-rs icon indicating copy to clipboard operation
zstd-rs copied to clipboard

WASM build instructions for Nix

Open bouk opened this issue 8 months ago • 2 comments

Hi there, I was trying to build this package for a mixed wasm/non-wasm build and landed on adding the following for ensuring clang is available but only used for the wasm build:

export CC_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#clang)/bin/clang"
export AR_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#llvmPackages.bintools-unwrapped)/bin/llvm-ar"

This works for me on macOS when using nix

Perhaps this can be added to the wiki, I couldn't find a way to submit an edit

bouk avatar May 09 '25 13:05 bouk

To add to this, if the resulting wasm package has errors that look like "Can't resolve 'env' in ../pkg' or similar, the wasm.js tries to "import 'env'", you must disable zerousdcallregs and stackprotector, i.e

hardeningDisable = [ "zerocallusedregs" "stackprotector" ];

My full arguments:

CC_wasm32_unknown_unknown = "${llvmPackages.libcxxClang}/bin/clang";
AR_wasm32_unknown_unknown = "${llvmPackages.bintools-unwrapped}/bin/llvm-ar";
hardeningDisable = [ "zerocallusedregs" "stackprotector" ];

ensure libcxxClang is used, as that provides some standard C headers that zstd needs to compile. otherwise you will run into compile errors like "limits.h" not found. Of course, zstd must be in the buildInputs section.

insipx avatar Jun 13 '25 15:06 insipx

Right, I'm actually using this now:

export CC_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#llvmPackages_19.clang-unwrapped)/bin/clang"
export AR_wasm32_unknown_unknown="$(nix build --no-link --print-out-paths nixpkgs#llvmPackages_19.bintools-unwrapped)/bin/llvm-ar"
export CFLAGS_wasm32_unknown_unknown="-I $(nix build --no-link --print-out-paths nixpkgs#llvmPackages_19.clang-unwrapped.lib)/lib/clang/19/include"

This ensures the stdlib is available and the hardening flags that are normally set on the stdenv are not used.

bouk avatar Jun 19 '25 07:06 bouk