Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't build for wasm target #827

Open
ivnsch opened this issue Oct 27, 2020 · 37 comments · May be fixed by #1010
Open

Can't build for wasm target #827

ivnsch opened this issue Oct 27, 2020 · 37 comments · May be fixed by #1010

Comments

@ivnsch
Copy link

ivnsch commented Oct 27, 2020

cargo build --lib --target "wasm32-unknown-unknown"

With rusqlite = {version = "0.24.1", features = ["bundled"]}:

cargo:warning=sqlite3/sqlite3.c:14012:10: fatal error: 'stdio.h' file not found

with rusqlite = {version = "0.24.1"}:

rust-lld: error: unable to find library -lsqlite3

It also happens with 0.23.1.

@thomcc
Copy link
Member

thomcc commented Oct 27, 2020

I think the only wasm target we currently support is wasm32-wasi, although I could be mistaken. @gwenn or @TjeuKayim (who added support for wasm32-wasi) might know.

@TjeuKayim
Copy link
Contributor

Compiling Rust that depends on C stdlib to WebAssembly is not possible with wasm32-unknown-unknown. So you need to use WASI or Emscripten to compile SQLite and link it to rusqlite. rustwasm/team#291 (comment)

@JimiHend
Copy link

I have the same pb with cargo build --lib --target "wasm32-wasi"

@TjeuKayim
Copy link
Contributor

Try to install wasi-sdk and set linker environment variables as explained at https://doc.rust-lang.org/nightly/nightly-rustc/rustc_target/spec/wasm32_wasi/index.html.

Enable these feature in Cargo.toml to use a simple SQLite VFS that is compatible with WASI:

[dependencies.rusqlite]
features = ["bundled", "wasm32-wasi-vfs"]

This is an example for Ubuntu to compile a project depending on rusqlite to WebAssembly:

sudo apt install libtinfo5
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-11/wasi-sdk_11.0_amd64_ubuntu20.04.deb
sudo dpkg -i wasi-sdk_11.0_amd64_ubuntu20.04.deb
rm wasi-sdk_11.0_amd64_ubuntu20.04.deb
export CC_wasm32_wasi=/opt/wasi-sdk/bin/clang
export CARGO_TARGET_WASM32_WASI_LINKER=/opt/wasi-sdk/bin/clang
RUSTFLAGS="-C target-feature=-crt-static" cargo build --lib --target "wasm32-wasi"

@ivnsch
Copy link
Author

ivnsch commented Oct 27, 2020

Thanks. I downloaded the sdk and set the env variables but it still complains about not finding clang... ok, this and Emscripten seem a bit complicated for what I had in mind, so I'm out, until/if it becomes "easy".

@JimiHend
Copy link

JimiHend commented Oct 27, 2020

Thanks too ! now I can finish the compilation.
but now I have a problem during the linking :-), an idea ?

error: could not compile `rusqlite-hello`.

Caused by:
  process didn't exit successfully: `rustc --crate-name rusqlite_hello --edition=2018 src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type cdylib --emit=dep-info,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=80559ab98fa64d60 --out-dir /wasm/target/wasm32-wasi/debug/deps --target wasm32-wasi -C linker=/opt/wasi-sdk/bin/clang -C incremental=/target/wasm32-wasi/debug/incremental -L dependency=/wasm/target/wasm32-wasi/debug/deps -L dependency=/wasm/target/debug/deps --extern rusqlite=/wasm/target/wasm32-wasi/debug/deps/librusqlite-dcf7d8d6c5a882ac.rlib --extern wasm_bindgen=/wasm/target/wasm32-wasi/debug/deps/libwasm_bindgen-8bbf2082a67fc026.rlib -Ctarget-feature=-crt-static -L native=/wasm/target/wasm32-wasi/debug/build/libsqlite3-sys-88be38f87ce66517/out` (exit code: 1)
root@dfdd2f516c46:/wasm# cargo build --lib --target "wasm32-wasi"
   Compiling rusqlite-hello v0.1.0 (/wasm)
error: linking with `/opt/wasi-sdk/bin/clang` failed: exit code: 1
  |
  = note: "/opt/wasi-sdk/bin/clang" "-Wl,--no-threads" "-Wl,-z" "-Wl,stack-size=1048576" "-Wl,--stack-first" "-Wl,--allow-undefined" "-Wl,--fatal-warnings" "-Wl,--no-demangle" "-Wl,--export-dynamic" "--target=wasm32-wasi" "-Wl,--eh-frame-hdr" "-nostartfiles" "-L" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib" "-L" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/self-contained" "wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.1jm45td0erya2n46.rcgu.o" "wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.1sotwl2es1gek4b7.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.2buxvyigkxynvkhd.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.2emho1x0c0oh5z5v.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.31ba78kkolcs15ub.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.35p2387yk45rfa7w.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.37tf4f0nrzaw1549.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.3iacsfc38h6q2ems.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.3j6t0m6whe4qwirm.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.3q75gwmvjy9j79n2.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.3r5a1mzhzatayz7m.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.45mrhw9ou7e99sa4.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.4jqsdzze7e60o28e.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.4py088s5yr9vs7hr.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.54yusrmxyoeu62xv.rcgu.o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.5gjhwydeld1beyzj.rcgu.o" "-o" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.wasm" "/wasm/target/wasm32-wasi/debug/deps/rusqlite_hello.4jvrmg0u302z7422.rcgu.o" "-Wl,--gc-sections" "-shared" "-nodefaultlibs" "-L" "/wasm/target/wasm32-wasi/debug/deps" "-L" "/wasm/target/debug/deps" "-L" "/wasm/target/wasm32-wasi/debug/build/libsqlite3-sys-88be38f87ce66517/out" "-L" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib" "/wasm/target/wasm32-wasi/debug/deps/librusqlite-dcf7d8d6c5a882ac.rlib" "/wasm/target/wasm32-wasi/debug/deps/libbitflags-ddf2e990de4b4142.rlib" "/wasm/target/wasm32-wasi/debug/deps/libsmallvec-009c22b5ffc86845.rlib" "/wasm/target/wasm32-wasi/debug/deps/libfallible_streaming_iterator-8c088a8885856989.rlib" "/wasm/target/wasm32-wasi/debug/deps/libfallible_iterator-8629b69a3bef07cf.rlib" "/wasm/target/wasm32-wasi/debug/deps/libhashlink-677a03c3eef521e0.rlib" "/wasm/target/wasm32-wasi/debug/deps/libhashbrown-263294d0a2f6b07b.rlib" "/wasm/target/wasm32-wasi/debug/deps/libahash-ed7718ad947f81cc.rlib" "/wasm/target/wasm32-wasi/debug/deps/liblibsqlite3_sys-17fc0924ab78932c.rlib" "/wasm/target/wasm32-wasi/debug/deps/libwasm_bindgen-8bbf2082a67fc026.rlib" "/wasm/target/wasm32-wasi/debug/deps/libcfg_if-afe5aba9a6d79b58.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libstd-2c2bb0b6d3b3945f.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libpanic_abort-77f11264e1293e2c.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libwasi-1536bfd7af2ca4cc.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libhashbrown-9bab1c7398d5e438.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/librustc_std_workspace_alloc-cacaec4359953c94.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libbacktrace-2bcf0ca774f6741a.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/librustc_demangle-7b3e5293b3167694.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libunwind-4b2d093212a4f307.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libcfg_if-db1cd1fa51facef1.rlib" "/tmp/rustcYBvAzO/liblibc-f32eeceec94652e4.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/liballoc-22ee6ab87476a160.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/librustc_std_workspace_core-0a05962f4a3dcb30.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libcore-32636c916ab1689a.rlib" "/root/.rustup/toolchains/1.46.0-x86_64-unknown-linux-gnu/lib/rustlib/wasm32-wasi/lib/libcompiler_builtins-b9c2bc8e8b2297fd.rlib" "-lc"
  = note: clang-10: warning: argument unused during compilation: '-shared' [-Wunused-command-line-argument]
          wasm-ld: error: unknown argument: --eh-frame-hdr
          clang-10: error: linker command failed with exit code 1 (use -v to see invocation)


error: aborting due to previous error

error: could not compile `rusqlite-hello`.

@TjeuKayim
Copy link
Contributor

The error unknown argument: --eh-frame-hdr is probably caused by this regression in Rust v1.46.0 rust-lang/rust#76679. Please try the current nightly version and try to downgrade to Rust v1.45.2. Indicate whether you get an error like wasm-ld: error: std-...-cgu.0.rcgu.o: Bad relocation type.

@JimiHend
Copy link

with v1.49.0-nightly :

error: linking with `/opt/wasi-sdk/bin/clang` failed: exit code: 1
= note: clang-10: warning: argument unused during compilation: '-shared' [-Wunused-command-line-argument]
        wasm-ld: error: std-396bcc707196f8c0.std.4bx4mcr6-cgu.0.rcgu.o: Bad relocation type:
        clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

with v1.45.2

error: linking with `/opt/wasi-sdk/bin/clang` failed: exit code: 1
  = note: clang-10: warning: argument unused during compilation: '-shared' [-Wunused-command-line-argument]
          wasm-ld: error: entry symbol not defined (pass --no-entry to supress): _start
          clang-10: error: linker command failed with exit code 1 (use -v to see invocation)

@TjeuKayim
Copy link
Contributor

I'd recommend to use Rust v1.45.2 until the regression is fixed.
WASI expects a main/start function. You can either convert your cargo project from a lib to a bin, or use RUSTFLAGS='-C target-feature=-crt-static -C link-arg=-nostartfiles -C link-arg=-Wl,--no-entry to leave out the start symbol.

Currently, it is a hassle to compile rusqlite to WebAssembly, but it is doable. I've created two web applications with it, and they ran faster than I expected. SQLite is not really optimized for binary size though, so the WASM file is around 2MB.

@JimiHend
Copy link

Thx, the compilation works for me with Rust v1.45.2.
is it possible to import in JS the functions from the .wasm (like wasm-pack for wasm32-unknown-unknown) ?

@TjeuKayim
Copy link
Contributor

Nice, you got it compiled. Importing/exporting functions between Rust and JS is possible with WASI, just like the wasm32-unknown-unknown target. I've written manual bindings without JS dependencies. You can also use https://github.com/wasmerio/wasmer-js to run WASI modules in a browser. There should be a way to use tools like wasm-pack to generate the glue code, but I'm unaware of one.
I don't know about your use case, but you probably want to pass a SQLite DB file in or out of the WebAssembly module?

@JimiHend
Copy link

I want to test rusqlite with nodejs by passing a filename to rust.
For the moment, I'm using wasmer-js to run the wasm file but now I have SqliteFailure(Error { code: CannotOpen, extended_code: 14 } perhaps a problem with my FS bindings ...

@sefeng211
Copy link

Hi! I've also managed to compile the wasm file by using running CARGO_TARGET_WASM32_WASI_LINKER=/home/saber/.local/workspace/repos/taskoo/taskoo-core/wasi-sdk-12.0/bin/lld CFLAGS="--sysroot=/opt/wasi-sdk/wasi-sysroot" cargo build --target=wasm32-wasi --lib --verbose , however the the file seems to be broken.

➜  haha git:(master) ✗ file target/wasm32-wasi/debug/haha.wasm      
target/wasm32-wasi/debug/haha.wasm: ERROR: Bad magic format `version %#x (MVP)' (bad format char: #)

Any thoughts on this?

@TjeuKayim
Copy link
Contributor

What runtime did you use? The .wasm file is not a Linux executable, but can be run with tools like https://wasmtime.dev/

@sefeng211
Copy link

Actually I guess the Bad magic format isn't a big deal. I think I've compiled it correctly, although I still haven't figured out how to run the code. I'd like to use it in browser.

@TjeuKayim
Copy link
Contributor

Try this tutorial to run your app in a browser: https://docs.wasmer.io/integrations/js/wasi/browser/examples/hello-world

@sefeng211
Copy link

Thanks! Do you think I need to use wasm-bindgen to generate the glue code to be able to use that?

@TjeuKayim
Copy link
Contributor

No, you don't need wasm-bindgen, it doesn't have out-of-the-box support for WASI, see rustwasm/wasm-bindgen#2209
I've manually written the glue code for a web app to use rusqlite, so that's possible.

@sefeng211
Copy link

Just to make sure I understand it correctly. It sounds like I can't use the wasm-bindgen cli to generate the glue code for a wasm file. Can I still use it as the rust library to export methods?

Also manually writing the glue code seems a lot of work. It'd be great we have some examples.

@passchaos
Copy link

@TjeuKayim After setting the wasi-sdk configuration, it still prompts that stdio.h file cannot be found

@TjeuKayim
Copy link
Contributor

Hi @passchaos , Can you provide some more details? Like what features you use in Cargo.toml for the rusqlite dependency.

@passchaos
Copy link

Hi @passchaos , Can you provide some more details? Like what features you use in Cargo.toml for the rusqlite dependency.

rusqlite = { version = "0.24.2", features = ["bundled", "wasm32-wasi-vfs"] }

@TjeuKayim
Copy link
Contributor

That seems fine. Are you using a similar cargo build command as in #827 (comment), and targeting wasm32-wasi?

@sefeng211
Copy link

@passchaos Also make sure you used wasi-sdk 10. I tried 11 and 12 in the past, and none of the them worked for me.

@passchaos
Copy link

@TjeuKayim @sefeng211 I tried to use different versions of wasi-sdk, but it did't work in the way mentioned above.
Finally, i changed libsqlite3-sys/build.rs, change the include attibute to the header file path of wasi-sdk, so that after the change, you can use cargo build --target=wasm32-wasi directly without setting any other environment variables.

@TjeuKayim
Copy link
Contributor

Finally, i changed libsqlite3-sys/build.rs, change the include attibute to the header file path of wasi-sdk

So, did you succeed in compiling rusqlite with hard coding the wasi-sdk path in build.rs?

@passchaos
Copy link

Finally, i changed libsqlite3-sys/build.rs, change the include attibute to the header file path of wasi-sdk

So, did you succeed in compiling rusqlite with hard coding the wasi-sdk path in build.rs?

Yes, but just set the header file path

@joshbenaron
Copy link

Hey, what's the update on this? I'd like to push to get this done as it would help out on a project to have WASM bindings for sqlite. I'd be happy to help in any way

@vdods
Copy link

vdods commented Jun 27, 2021

I'm also struggling with this one. I either get the OP's error "rust-lld: error: unable to find library -lsqlite3" when building my crate, or if I enabled the "bundled" feature, or try to build rusqlite directly using cargo build --features bundled --target wasm32-wasi, I get an error while building libsqlite3-sys:

error: failed to run custom build command for `libsqlite3-sys v0.22.2 (/home/vdods/files/github/rusqlite/rusqlite/libsqlite3-sys)`

Caused by:
  process didn't exit successfully: `/home/vdods/files/git/code/misc/rustplayground/sqlite_delta_log_2/target/debug/build/libsqlite3-sys-4267f5c168eaedcc/build-script-build` (exit code: 1)
  --- stdout
  cargo:rerun-if-changed=sqlite3/sqlite3.c
  cargo:rerun-if-changed=sqlite3/wasm32-wasi-vfs.c
  cargo:rerun-if-env-changed=SQLITE_MAX_VARIABLE_NUMBER
  cargo:rerun-if-env-changed=SQLITE_MAX_EXPR_DEPTH
  cargo:rerun-if-env-changed=LIBSQLITE3_FLAGS
  TARGET = Some("wasm32-wasi")
  OPT_LEVEL = Some("0")
  HOST = Some("x86_64-unknown-linux-gnu")
  CC_wasm32-wasi = None
  CC_wasm32_wasi = None
  TARGET_CC = None
  CC = None
  CFLAGS_wasm32-wasi = None
  CFLAGS_wasm32_wasi = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-wasi" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-D_POSIX_THREAD_SAFE_FUNCTIONS" "-DHAVE_ISNAN" "-DHAVE_LOCALTIME_R" "-o" "/home/vdods/files/git/code/misc/rustplayground/sqlite_delta_log_2/target/wasm32-wasi/debug/build/libsqlite3-sys-813fca2add87e1a6/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c"
  cargo:warning=In file included from sqlite3/sqlite3.c:26539:
  cargo:warning=/usr/include/pthread.h:682:6: error: 'regparm' is not valid on this platform
  cargo:warning=     __cleanup_fct_attribute;
  cargo:warning=     ^~~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=/usr/include/bits/pthreadtypes-arch.h:103:50: note: expanded from macro '__cleanup_fct_attribute'
  cargo:warning=# define __cleanup_fct_attribute __attribute__ ((__regparm__ (1)))
  cargo:warning=                                                 ^            ~
  cargo:warning=In file included from sqlite3/sqlite3.c:26539:
  cargo:warning=/usr/include/pthread.h:694:3: error: 'regparm' is not valid on this platform
  cargo:warning=  __cleanup_fct_attribute;
  cargo:warning=  ^~~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=/usr/include/bits/pthreadtypes-arch.h:103:50: note: expanded from macro '__cleanup_fct_attribute'
  cargo:warning=# define __cleanup_fct_attribute __attribute__ ((__regparm__ (1)))
  cargo:warning=                                                 ^            ~
  cargo:warning=In file included from sqlite3/sqlite3.c:26539:
  cargo:warning=/usr/include/pthread.h:717:6: error: 'regparm' is not valid on this platform
  cargo:warning=     __cleanup_fct_attribute;
  cargo:warning=     ^~~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=/usr/include/bits/pthreadtypes-arch.h:103:50: note: expanded from macro '__cleanup_fct_attribute'
  cargo:warning=# define __cleanup_fct_attribute __attribute__ ((__regparm__ (1)))
  cargo:warning=                                                 ^            ~
  cargo:warning=In file included from sqlite3/sqlite3.c:26539:
  cargo:warning=/usr/include/pthread.h:730:3: error: 'regparm' is not valid on this platform
  cargo:warning=  __cleanup_fct_attribute;
  cargo:warning=  ^~~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=/usr/include/bits/pthreadtypes-arch.h:103:50: note: expanded from macro '__cleanup_fct_attribute'
  cargo:warning=# define __cleanup_fct_attribute __attribute__ ((__regparm__ (1)))
  cargo:warning=                                                 ^            ~
  cargo:warning=In file included from sqlite3/sqlite3.c:26539:
  cargo:warning=/usr/include/pthread.h:735:6: error: 'regparm' is not valid on this platform
  cargo:warning=     __cleanup_fct_attribute __attribute__ ((__noreturn__))
  cargo:warning=     ^~~~~~~~~~~~~~~~~~~~~~~
  cargo:warning=/usr/include/bits/pthreadtypes-arch.h:103:50: note: expanded from macro '__cleanup_fct_attribute'
  cargo:warning=# define __cleanup_fct_attribute __attribute__ ((__regparm__ (1)))
  cargo:warning=                                                 ^            ~
  cargo:warning=5 errors generated.
  exit code: 1

  --- stderr


  error occurred: Command "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-wasi" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-D_POSIX_THREAD_SAFE_FUNCTIONS" "-DHAVE_ISNAN" "-DHAVE_LOCALTIME_R" "-o" "/home/vdods/files/git/code/misc/rustplayground/sqlite_delta_log_2/target/wasm32-wasi/debug/build/libsqlite3-sys-813fca2add87e1a6/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c" with args "clang" did not execute successfully (status code exit code: 1).

This is a bit weird, since I only see warnings, and I don't see the -Werror flag passed into clang which is what would cause warnings to be interpreted as errors.

@vdods
Copy link

vdods commented Jun 27, 2021

For a more specific way to replicate this, I tried to build rusqlite/libsqlite3-sys (in the libsqlite3-sys crate within the rusqlite workspace) via cargo build --features bundled --target wasm32-wasi and it gave the same error.

mhuesch added a commit to neighbour-hoods/social_sensemaker that referenced this issue Oct 10, 2021
the `holo_hash` crate depends on `rusqlite` which cannot build on wasm:
rusqlite/rusqlite#827

therefore I cannot create an `AgentPubKey` and thus cannot initiate a
call.

I wonder if I can rip the types out and rely on Serde deriving things
identically for identical structs...

(while waiting for an actual, long-term-viable fix).
@GeeWee
Copy link

GeeWee commented Jan 7, 2022

I get a different error when trying to build on Fedora 35. If I just run cargo wasi build I get the stdio.h not found as well

(Cargo.toml)

rusqlite = {version = "0.26.3", features=["bundled-full", "wasm32-wasi-vfs"]}
$ cargo wasi build

Caused by:
  process didn't exit successfully: `/home/geewee/programming/edge-api/target/debug/build/libsqlite3-sys-b798e717760d70e2/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-changed=sqlite3/sqlite3.c
  cargo:rerun-if-changed=sqlite3/wasm32-wasi-vfs.c
  cargo:rerun-if-env-changed=SQLITE_MAX_VARIABLE_NUMBER
  cargo:rerun-if-env-changed=SQLITE_MAX_EXPR_DEPTH
  cargo:rerun-if-env-changed=LIBSQLITE3_FLAGS
  TARGET = Some("wasm32-wasi")
  OPT_LEVEL = Some("0")
  HOST = Some("x86_64-unknown-linux-gnu")
  CC_wasm32-wasi = None
  CC_wasm32_wasi = None
  TARGET_CC = None
  CC = None
  CFLAGS_wasm32-wasi = None
  CFLAGS_wasm32_wasi = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-wasi" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-D_POSIX_THREAD_SAFE_FUNCTIONS" "-DHAVE_ISNAN" "-DHAVE_LOCALTIME_R" "-DSQLITE_ENABLE_UNLOCK_NOTIFY" "-o" "/home/geewee/programming/edge-api/target/wasm32-wasi/debug/build/libsqlite3-sys-a108609a6c19d418/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c"
  cargo:warning=sqlite3/sqlite3.c:13699:10: fatal error: 'stdio.h' file not found
  cargo:warning=#include <stdio.h>
  cargo:warning=         ^~~~~~~~~
  cargo:warning=1 error generated.
  exit status: 1

  --- stderr


  error occurred: Command "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-wasi" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-D_POSIX_THREAD_SAFE_FUNCTIONS" "-DHAVE_ISNAN" "-DHAVE_LOCALTIME_R" "-DSQLITE_ENABLE_UNLOCK_NOTIFY" "-o" "/home/geewee/programming/edge-api/target/wasm32-wasi/debug/build/libsqlite3-sys-a108609a6c19d418/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c" with args "clang" did not execute successfully (status code exit status: 1).

However if I follow the instructions above for Fedora (slightly modified)

# Install wasi-sdk 12 to local directory
$ export WASI_VERSION=12
$ export WASI_VERSION_FULL=${WASI_VERSION}.0
$ wget "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz"
$ tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz

Trying to compile with the instructions above

export CC_wasm32_wasi=`pwd`/wasi-sdk-12.0/bin/clang
export CARGO_TARGET_WASM32_WASI_LINKER=`pwd`/wasi-sdk-12.0/bin/clang
RUSTFLAGS="-C target-feature=-crt-static" cargo build --lib --target "wasm32-wasi"

Gives me the exact same error stdio.h not found error.

However if I use the wasi-sysroot as they write in their readme it fails with cannot find pthread.h

export WASI_SDK_PATH=`pwd`/wasi-sdk-12.0
CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"
export CC_wasm32_wasi=${CC}
export CARGO_TARGET_WASM32_WASI_LINKER=${CC}
RUSTFLAGS="-C target-feature=-crt-static" cargo build --lib --target "wasm32-wasi"


warning: sqlite3/sqlite3.c:27213:10: fatal error: 'pthread.h' file not found
warning: #include <pthread.h>
warning:          ^~~~~~~~~~~
warning: 1 error generated.

error: failed to run custom build command for `libsqlite3-sys v0.23.2`

Caused by:
  process didn't exit successfully: `/home/geewee/programming/edge-api/target/debug/build/libsqlite3-sys-b798e717760d70e2/build-script-build` (exit status: 1)
  --- stdout
  cargo:rerun-if-changed=sqlite3/sqlite3.c
  cargo:rerun-if-changed=sqlite3/wasm32-wasi-vfs.c
  cargo:rerun-if-env-changed=SQLITE_MAX_VARIABLE_NUMBER
  cargo:rerun-if-env-changed=SQLITE_MAX_EXPR_DEPTH
  cargo:rerun-if-env-changed=LIBSQLITE3_FLAGS
  TARGET = Some("wasm32-wasi")
  OPT_LEVEL = Some("0")
  HOST = Some("x86_64-unknown-linux-gnu")
  CC_wasm32-wasi = None
  CC_wasm32_wasi = Some("/home/geewee/programming/edge-api/edge-api/wasi-sdk-12.0/bin/clang --sysroot=/home/geewee/programming/edge-api/edge-api/wasi-sdk-12.0/share/wasi-sysroot")
  CFLAGS_wasm32-wasi = None
  CFLAGS_wasm32_wasi = None
  TARGET_CFLAGS = None
  CFLAGS = None
  CRATE_CC_NO_DEFAULTS = None
  DEBUG = Some("true")
  running: "/home/geewee/programming/edge-api/edge-api/wasi-sdk-12.0/bin/clang" "--sysroot=/home/geewee/programming/edge-api/edge-api/wasi-sdk-12.0/share/wasi-sysroot" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-wasi" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-D_POSIX_THREAD_SAFE_FUNCTIONS" "-DHAVE_ISNAN" "-DHAVE_LOCALTIME_R" "-DSQLITE_ENABLE_UNLOCK_NOTIFY" "-o" "/home/geewee/programming/edge-api/target/wasm32-wasi/debug/build/libsqlite3-sys-a108609a6c19d418/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c"
  cargo:warning=sqlite3/sqlite3.c:27213:10: fatal error: 'pthread.h' file not found
  cargo:warning=#include <pthread.h>
  cargo:warning=         ^~~~~~~~~~~
  cargo:warning=1 error generated.
  exit status: 1

  --- stderr


  error occurred: Command "/home/geewee/programming/edge-api/edge-api/wasi-sdk-12.0/bin/clang" "--sysroot=/home/geewee/programming/edge-api/edge-api/wasi-sdk-12.0/share/wasi-sysroot" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-wasi" "-DSQLITE_CORE" "-DSQLITE_DEFAULT_FOREIGN_KEYS=1" "-DSQLITE_ENABLE_API_ARMOR" "-DSQLITE_ENABLE_COLUMN_METADATA" "-DSQLITE_ENABLE_DBSTAT_VTAB" "-DSQLITE_ENABLE_FTS3" "-DSQLITE_ENABLE_FTS3_PARENTHESIS" "-DSQLITE_ENABLE_FTS5" "-DSQLITE_ENABLE_JSON1" "-DSQLITE_ENABLE_LOAD_EXTENSION=1" "-DSQLITE_ENABLE_MEMORY_MANAGEMENT" "-DSQLITE_ENABLE_RTREE" "-DSQLITE_ENABLE_STAT2" "-DSQLITE_ENABLE_STAT4" "-DSQLITE_SOUNDEX" "-DSQLITE_THREADSAFE=1" "-DSQLITE_USE_URI" "-DHAVE_USLEEP=1" "-D_POSIX_THREAD_SAFE_FUNCTIONS" "-DHAVE_ISNAN" "-DHAVE_LOCALTIME_R" "-DSQLITE_ENABLE_UNLOCK_NOTIFY" "-o" "/home/geewee/programming/edge-api/target/wasm32-wasi/debug/build/libsqlite3-sys-a108609a6c19d418/out/sqlite3/sqlite3.o" "-c" "sqlite3/sqlite3.c" with args "clang" did not execute successfully (status code exit status: 1).

I have tried with wasi-sdk version 10 as well and the same issue happens.

These issues seem relevant
WebAssembly/wasi-sdk#174
WebAssembly/wasi-sdk#22
WebAssembly/wasi-libc#209

Did anyone stumble into something similar?

@polyrand
Copy link
Contributor

@GeeWee I got the same error. (M1 Mac, tried wasi-sdk 10, 12 and 14).

I started trying to compile the sqlite3.c amalgamation separately, running the clang command in the error message you posted (which I also got). By using the DSQLITE_THREADSAFE=0 flag, the pthread.h error went away, but I got several new errors I couldn't fix. In any case, disabling thread safety may not work, since rusqlite doesn't seem to allow it: https://github.com/rusqlite/rusqlite/blob/master/tests/deny_single_threaded_sqlite_config.rs (Found it from #746).

This is the command I was running:

${WASI_SDK_PATH}/bin/clang \
    --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot \
    -O0 \
    -ffunction-sections \
    -fdata-sections \
    -fPIC \
    -g \
    -fno-omit-frame-pointer \
    --target=wasm32-wasi \
    -DSQLITE_CORE \
    -D_WASI_EMULATED_MMAN \
    -DSQLITE_MAX_MMAP_SIZE=0 \
    -DSQLITE_DEFAULT_FOREIGN_KEYS=1 \
    -DSQLITE_ENABLE_API_ARMOR \
    -DSQLITE_ENABLE_COLUMN_METADATA \
    -DSQLITE_ENABLE_DBSTAT_VTAB \
    -DSQLITE_ENABLE_FTS3 \
    -DSQLITE_ENABLE_FTS3_PARENTHESIS \
    -DSQLITE_ENABLE_FTS5 \
    -DSQLITE_ENABLE_JSON1 \
    -DSQLITE_ENABLE_LOAD_EXTENSION=1 \
    -DSQLITE_ENABLE_MEMORY_MANAGEMENT \
    -DSQLITE_ENABLE_RTREE \
    -DSQLITE_ENABLE_STAT2 \
    -DSQLITE_ENABLE_STAT4 \
    -DSQLITE_SOUNDEX \
    -DSQLITE_THREADSAFE=0 \
    -DSQLITE_USE_URI \
    -DHAVE_USLEEP=1 \
    -D_POSIX_THREAD_SAFE_FUNCTIONS \
    -DHAVE_ISNAN \
    -DHAVE_LOCALTIME_R \
    -o \
    $(pwd)/sqlite3.o \
    -c sqlite-amalgamation-3370200/sqlite3.c

(I included the D_WASI_EMULATED_MMAN flag following an error message from clang).

I'll report back if I manage to fix something.

@s20208413
Copy link

Managed to work !

With wasi-sdk 14.0, idea from #1116

diff --git a/libsqlite3-sys/build.rs b/libsqlite3-sys/build.rs
index 33c4772..219cda1 100644
--- a/libsqlite3-sys/build.rs
+++ b/libsqlite3-sys/build.rs
@@ -122,10 +122,11 @@ mod build_bundled {
             .flag("-DSQLITE_ENABLE_STAT2")
             .flag("-DSQLITE_ENABLE_STAT4")
             .flag("-DSQLITE_SOUNDEX")
-            .flag("-DSQLITE_THREADSAFE=1")
+            .flag("-DSQLITE_THREADSAFE=0")
             .flag("-DSQLITE_USE_URI")
             .flag("-DHAVE_USLEEP=1")
             .flag("-D_POSIX_THREAD_SAFE_FUNCTIONS") // cross compile with MinGW
+            .flag("-D_WASI_EMULATED_MMAN")
             .warnings(false);
 
         if cfg!(feature = "bundled-sqlcipher") {
@@ -242,7 +243,7 @@ mod build_bundled {
             cfg.flag("-DHAVE_LOCALTIME_R");
         }
         // Target wasm32-wasi can't compile the default VFS
-        if is_compiler("wasm32-wasi") {
+        if env::var("TARGET").as_deref() == Ok("wasm32-wasi") || is_compiler("wasm32-wasi") {
             cfg.flag("-DSQLITE_OS_OTHER")
                 // https://github.com/rust-lang/rust/issues/74393
                 .flag("-DLONGDOUBLE_TYPE=double");
> env CC_wasm32_wasi="/wasi-sdk-14.0/bin/clang" CARGO_TARGET_WASM32_WASI_LINKER=/wasi-sdk-14.0/bin/clang RUSTFLAGS="-C target-feature=-crt-static"  cargo build --lib --target wasm32-wasi --features bundled,wasm32-wasi-vfs --release
   Compiling vcpkg v0.2.15
   Compiling cc v1.0.72
   Compiling pkg-config v0.3.24
   Compiling libsqlite3-sys v0.23.2 (/rusqlite/libsqlite3-sys)
    Finished release [optimized] target(s) in 1m 13s

just compile, no test ;-)

@polyrand
Copy link
Contributor

polyrand commented Feb 17, 2022

Yeah, with the changes in #1116 it works (I'm the author of that PR). I tried building a wasm32-wasi executable with bundled SQLite, and it worked when running it with wasmtime. (just copied the example in the README, nothing fancy).

I also had to set up the AR env var. The script I used is something like:

#!/usr/bin/env bash

# set WASI_SDK_PATH to the correct location in your system

export WASI_SYSROOT="${WASI_SDK_PATH}/share/wasi-sysroot"
export CC="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SYSROOT}"
export AR="${WASI_SDK_PATH}/bin/llvm-ar"
export CC_wasm32_wasi="${CC}"
export CARGO_TARGET_WASM32_WASI_LINKER="${WASI_SDK_PATH}/bin/clang"

export LIBSQLITE3_FLAGS="\
    -DSQLITE_OS_OTHER \
    -USQLITE_TEMP_STORE \
    -DSQLITE_TEMP_STORE=3 \
    -USQLITE_THREADSAFE \
    -DSQLITE_THREADSAFE=0 \
    -DSQLITE_OMIT_LOCALTIME \
    -DSQLITE_OMIT_LOAD_EXTENSION \
    -DLONGDOUBLE_TYPE=double"

cargo build --release --target "wasm32-wasi"

Cargo.toml file:

Note: ../rusqlite/compile-wasm32-wasi is a path to the branch used for #1116.

[package]
name = "play"
version = "0.1.0"
edition = "2021"

[dependencies]
rusqlite = { path = "../rusqlite/compile-wasm32-wasi", version = "0.26.3", features = [
    "bundled",
    "wasm32-wasi-vfs",
] }

Finally, run with:

wasmtime target/wasm32-wasi/release/play.wasm

# Output:
# Found person Person { id: 1, name: "Steven", data: None }

@johncardiologs
Copy link

johncardiologs commented Aug 9, 2022

EDIT: ahhh I realized I needed to run rustup target add wasm32-wasi... After that, i was getting: error occurred: Failed to find tool. Is clang installed? -- however I realized this because I misspelled my path to the wasi-sdk (which was /opt/wasi-sdk on my machine). Thank you for the script!

This worked for me on the latest versions of nightly rust / cargo and the latest wasi-sdk.


(Outdated)

I realize it's been 6 months, but I was trying this method @polyrand and got this error when running your script:

// snip snip many more similar errors up here

error[E0531]: cannot find tuple struct or tuple variant `Some` in this scope
    --> /home/john/.cargo/registry/src/github.com-1ecc6299db9ec823/fallible-iterator-0.2.0/src/lib.rs:2598:20
     |
2598 |             (None, Some(b)) => Some(b),
     |                    ^^^^ not found in this scope

error[E0425]: cannot find function, tuple struct or tuple variant `Some` in this scope
    --> /home/john/.cargo/registry/src/github.com-1ecc6299db9ec823/fallible-iterator-0.2.0/src/lib.rs:2598:32
     |
2598 |             (None, Some(b)) => Some(b),
     |                                ^^^^ not found in this scope

Some errors have detailed explanations: E0405, E0412, E0416, E0425, E0432, E0433, E0463, E0531.
error: could not compile `fallible-iterator` due to 820 previous errors

Any clues by any chance? Using rustup -V: rustup 1.25.1 (bb60b1e89 2022-07-12) and cargo -V: cargo 1.65.0-nightly (4fd148c47 2022-08-03).
Using wasi-sdk v. 14.

@gwenn
Copy link
Collaborator

gwenn commented May 7, 2023

May be fixed by #1321

@adamnovak
Copy link

I don't think #1321 fixes this. I still can't build for wasm32-unknown-unknown, because sqlite3.c wants stdio.h, which is not available on a target without a real libc.

It looks like wasm32-unknown-unknown support is required if you want to use rusqlite in the same project as wasm-bindgen, which isn't planning to ship a version that can build for wasm32-wasi: rustwasm/wasm-bindgen#3421

And cross-linking between the two targets is apparently also broken: rustwasm/wasm-bindgen#3454

So it looks like the only way to get the two crates into the same project would be for rusqlite to be able to build on wasm32-unknown-unknown, with no libc and no VFS, and then have the user provide a VFS.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.