rusqlite: Can't build for wasm target

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.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 4
  • Comments: 37 (14 by maintainers)

Commits related to this issue

Most upvoted comments

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 }

@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.

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 😉