1

I am trying to compile a rust project into WebAssembly with wasm-pack. One of its dependencies is a rust wrapper around C++ code. This dependency can, on its own, be successfully compiled to WebAssembly with Emscripten. However if I try to compile my project or the dependency itself with wasm-pack I get the following error:

  running: "clang" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "--target=wasm32-unknown-unknown" "-I" "src" "-Wall" "
-Wextra" "-o" "/home/ahelwer/src/tlaplus/tree-sitter-tlaplus/target/wasm32-unknown-unknown/release/build/tree-sitter-tlaplus-90
d9bfea3134c6dd/out/src/parser.o" "-c" "src/parser.c"
  cargo:warning=In file included from src/parser.c:1:
  cargo:warning=src/tree_sitter/parser.h:10:10: fatal error: 'stdlib.h' file not found
  cargo:warning=#include <stdlib.h>
  cargo:warning=         ^~~~~~~~~~
  cargo:warning=1 error generated.
  exit status: 1

So it can't find libc. I know Emscripten includes the ability to link programs against the libc (based on musl) it comes with. Does wasm-pack also have this ability? Do you have to modify the build.rs file of the native dependency in some way?

ahelwer
  • 1,441
  • 13
  • 29
  • 2
    You could in theory [hand](https://stackoverflow.com/a/73615409/401059) your C++ compiler a full stdlib via `CFLAGS=--sysroot=/path/to/wasi-sysroot`. If you're lucky, it won't result in any wasi-related imports and you can use the module as is, but in general, it seems wasm-pack [doesn't](https://github.com/rustwasm/wasm-pack/issues/741#issuecomment-1094064702) support this. – Caesar Jan 06 '23 at 00:37
  • @Caesar thanks for the pointers! What do you mean by wasi-related imports? Like I should hope the dependency links against stdlib functions to compile but those functions are never called during runtime, and if they were it would be a dynamic library load failure? – ahelwer Jan 06 '23 at 13:56
  • 1
    Quite close. Two small nits: 1. Some of the stdlib functions (e.g. `sqrt`) don't actually interact with the "OS", so they require no imports 2. The fact that functions that require imports (e.g. `fwrite`) are never called has to be realized by the optimizer/linker, and it actually has to remove the functions/imports at link time. (Not sure when exactly the removing happens.) – Caesar Jan 09 '23 at 05:24

0 Answers0