Skip to content

Commit c1c5eaf

Browse files
committed
Added support for wasm32-wasi target
1 parent 466ffd2 commit c1c5eaf

File tree

5 files changed

+73
-13
lines changed

5 files changed

+73
-13
lines changed

.github/workflows/main.yml

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ jobs:
5959
- target: x86_64-pc-windows-gnu
6060
rust: stable
6161
os: ubuntu-latest
62-
# - target: asmjs-unknown-emscripten
63-
# rust: stable
64-
# os: ubuntu-latest
62+
- target: wasm32-wasi
63+
rust: stable
64+
os: ubuntu-latest
65+
crt_static: yes
6566
- target: i686-pc-windows-msvc
6667
rust: stable-i686-msvc
6768
os: windows-2016

ci/docker/asmjs-unknown-emscripten/Dockerfile

-3
This file was deleted.

ci/run.sh

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ if [ "$1" = "aarch64-apple-darwin" ] ; then
88
export CARGO_TARGET_AARCH64_APPLE_DARWIN_RUNNER=echo
99
fi
1010

11-
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv
12-
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv --release
11+
if [ "$1" = "wasm32-wasi" ] ; then
12+
cargo install cargo-wasi
13+
curl https://wasmtime.dev/install.sh -sSf | bash
14+
cargo wasi test --manifest-path "$testcrate_dir/Cargo.toml" -vv
15+
cargo wasi test --manifest-path "$testcrate_dir/Cargo.toml" -vv --release
16+
else
17+
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv
18+
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv --release
19+
fi
1320

1421
if [ "$1" = "x86_64-unknown-linux-gnu" ] ; then
1522
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv --all-features

src/lib.rs

+50
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,56 @@ impl Build {
423423
configure.arg("-D__STDC_NO_ATOMICS__");
424424
}
425425

426+
if target.contains("wasi") {
427+
configure.args([
428+
// Termios isn't available whatsoever on WASM/WASI so we disable that
429+
// fatal error: 'termios.h' file not found
430+
"no-ui-console",
431+
// WASI doesn't support UNIX sockets so we preemptively disable it
432+
"no-sock",
433+
// WASI doesn't have a concept of syslog, so we disable it
434+
// fatal error: 'syslog.h' file not found
435+
"-DNO_SYSLOG",
436+
// wasm lacks signal support; to enable minimal signal emulation, compile with
437+
// -D_WASI_EMULATED_SIGNAL and link with -lwasi-emulated-signal
438+
"-D_WASI_EMULATED_SIGNAL",
439+
// WASI lacks process-associated clocks; to enable emulation of the `times` function using the wall
440+
// clock, which isn't sensitive to whether the program is running or suspended, compile with
441+
// -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks
442+
"-D_WASI_EMULATED_PROCESS_CLOCKS",
443+
// WASI lacks a true mmap; to enable minimal mmap emulation, compile
444+
// with -D_WASI_EMULATED_MMAN and link with -lwasi-emulated-mman
445+
"-D_WASI_EMULATED_MMAN",
446+
// WASI lacks process identifiers; to enable emulation of the `getpid` function using a
447+
// placeholder value, which doesn't reflect the host PID of the program, compile with
448+
// -D_WASI_EMULATED_GETPID and link with -lwasi-emulated-getpid
449+
"-D_WASI_EMULATED_GETPID",
450+
// ----------
451+
// The following arguments are taken from:
452+
// https://github.com/wapm-packages/OpenSSL/blob/master/build.sh#L21
453+
// Since this is a successful and reliable source for configuring WASM/I builds.
454+
// I will try to explain those to the best of my ability though.
455+
// ----------
456+
// WASI doesn't support (p)threads. Disabling preemptively.
457+
"no-threads",
458+
// WASI/WASM aren't really friends with ASM, so we disable it as well.
459+
"no-asm",
460+
// Disables the AFALG engine (AFALG-ENGine)
461+
// Since AFALG depends on `AF_ALG` support on the linux kernel side
462+
// it makes sense that we can't use it.
463+
"no-afalgeng",
464+
"-DOPENSSL_NO_AFALGENG=1",
465+
// ? Do we really want to always build statically for wasm? I guess yes?
466+
"static",
467+
// No fork(1) support in WASI, so we disable it
468+
"-DHAVE_FORK=0",
469+
"-DOPENSSL_NO_SPEED=1",
470+
"-DOPENSSL_SYS_NETWARE",
471+
"-DSIG_DFL=0",
472+
"-DSIG_IGN=0",
473+
]);
474+
}
475+
426476
if target.contains("musl") {
427477
// Hack around openssl/openssl#7207 for now
428478
configure.arg("-DOPENSSL_NO_SECURE_MEMORY");

testcrate/src/lib.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ extern "C" {
66
pub fn OpenSSL_version_num() -> c_ulong;
77
}
88

9-
#[test]
10-
fn version_works() {
11-
unsafe {
12-
println!("{:#x}", OpenSSL_version_num());
13-
assert!(OpenSSL_version_num() > 0);
9+
#[cfg(test)]
10+
mod tests {
11+
use super::OpenSSL_version_num;
12+
13+
#[test]
14+
fn version_works() {
15+
unsafe {
16+
println!("{:#x}", OpenSSL_version_num());
17+
assert!(OpenSSL_version_num() > 0);
18+
}
1419
}
1520
}

0 commit comments

Comments
 (0)