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

Improve wasm32-wasi support #1332

Merged
merged 1 commit into from Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions examples/persons/README.md
@@ -0,0 +1,28 @@
# Persons example

## Run

```
$ cargo run --example persons
```

## Run (wasm32-wasi)

### Requisites

- [wasi-sdk](https://github.com/WebAssembly/wasi-sdk)
- [wasmtime](https://wasmtime.dev/)

```
# Set to wasi-sdk directory
$ export WASI_SDK_PATH=`<wasi-sdk-path>`
$ export CC_wasm32_wasi="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"
# Build
$ cargo build --example persons --target wasm32-wasi --release --features bundled
# Run
$ wasmtime target/wasm32-wasi/release/examples/persons.wasm
Found persons:
ID: 1, Name: Steven
ID: 2, Name: John
ID: 3, Name: Alex
```
42 changes: 42 additions & 0 deletions examples/persons/main.rs
@@ -0,0 +1,42 @@
extern crate rusqlite;
use rusqlite::{Connection, Result};

struct Person {
id: i32,
name: String,
}
fn main() -> Result<()> {
let conn = Connection::open_in_memory()?;

Check warning on line 9 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L8-L9

Added lines #L8 - L9 were not covered by tests

conn.execute(
"CREATE TABLE IF NOT EXISTS persons (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
)",
(), // empty list of parameters.
)?;

Check warning on line 17 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L11-L17

Added lines #L11 - L17 were not covered by tests

conn.execute(
"INSERT INTO persons (name) VALUES (?1), (?2), (?3)",
["Steven", "John", "Alex"].map(|n| n.to_string()),
)?;

Check warning on line 22 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L19-L22

Added lines #L19 - L22 were not covered by tests

let mut stmt = conn.prepare("SELECT id, name FROM persons")?;
let rows = stmt.query_map([], |row| {
Ok(Person {
id: row.get(0)?,
name: row.get(1)?,

Check warning on line 28 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L24-L28

Added lines #L24 - L28 were not covered by tests
})
})?;

Check warning on line 30 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L30

Added line #L30 was not covered by tests

println!("Found persons:");

Check warning on line 32 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L32

Added line #L32 was not covered by tests

for person in rows {
match person {
Ok(p) => println!("ID: {}, Name: {}", p.id, p.name),
Err(e) => eprintln!("Error: {:?}", e),

Check warning on line 37 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L34-L37

Added lines #L34 - L37 were not covered by tests
}
}

Ok(())
}

Check warning on line 42 in examples/persons/main.rs

View check run for this annotation

Codecov / codecov/patch

examples/persons/main.rs#L41-L42

Added lines #L41 - L42 were not covered by tests
11 changes: 8 additions & 3 deletions libsqlite3-sys/build.rs
Expand Up @@ -239,11 +239,16 @@
if !win_target() {
cfg.flag("-DHAVE_LOCALTIME_R");
}
// Target wasm32-wasi can't compile the default VFS
if env::var("TARGET").map_or(false, |v| v == "wasm32-wasi") {
cfg.flag("-DSQLITE_OS_OTHER")
cfg.flag("-USQLITE_THREADSAFE")
.flag("-DSQLITE_THREADSAFE=0")

Check warning on line 244 in libsqlite3-sys/build.rs

View check run for this annotation

Codecov / codecov/patch

libsqlite3-sys/build.rs#L243-L244

Added lines #L243 - L244 were not covered by tests
// https://github.com/rust-lang/rust/issues/74393
.flag("-DLONGDOUBLE_TYPE=double");
.flag("-DLONGDOUBLE_TYPE=double")
.flag("-D_WASI_EMULATED_MMAN")
.flag("-D_WASI_EMULATED_GETPID")
.flag("-D_WASI_EMULATED_SIGNAL")
.flag("-D_WASI_EMULATED_PROCESS_CLOCKS");

Check warning on line 251 in libsqlite3-sys/build.rs

View check run for this annotation

Codecov / codecov/patch

libsqlite3-sys/build.rs#L246-L251

Added lines #L246 - L251 were not covered by tests
if cfg!(feature = "wasm32-wasi-vfs") {
cfg.file("sqlite3/wasm32-wasi-vfs.c");
}
Expand Down