Skip to content

Commit f6b8806

Browse files
committed
Added support for wasm32-wasi target
1 parent d523d40 commit f6b8806

File tree

6 files changed

+83
-10
lines changed

6 files changed

+83
-10
lines changed

.github/workflows/main.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ 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
6565
- target: i686-pc-windows-msvc
6666
rust: stable-i686-msvc
6767
os: windows-2016
@@ -88,6 +88,9 @@ jobs:
8888
- name: Install Rust (rustup)
8989
run: rustup update ${{ matrix.rust }} --no-self-update && rustup default ${{ matrix.rust }}
9090
- run: rustup target add ${{ matrix.target }}
91+
- name: Install Cargo-Wasi
92+
if: matrix.target == 'wasm32-wasi'
93+
run: cargo install cargo-wasi
9194
- name: Set crt-static
9295
if: matrix.crt_static == 'yes'
9396
run: echo RUSTFLAGS=-Ctarget-feature=+crt-static >> $GITHUB_ENV

ci/docker/asmjs-unknown-emscripten/Dockerfile

Lines changed: 0 additions & 3 deletions
This file was deleted.

ci/docker/wasm32-wasi/Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
FROM ubuntu:18.04
2+
3+
RUN apt-get update -y && apt-get install -y --no-install-recommends \
4+
ca-certificates \
5+
curl wget \
6+
make \
7+
perl \
8+
gcc \
9+
xz-utils \
10+
libc6-dev
11+
12+
# cargo-wasi setup (cargo-wasi depends on wasmtime to be present)
13+
RUN curl https://wasmtime.dev/install.sh -sSf | bash
14+
ENV PATH="$PATH:/root/.wasmtime/bin"
15+
16+
# Install WASI-SDK
17+
ENV WASI_VERSION=14
18+
ENV WASI_VERSION_FULL=${WASI_VERSION}.0
19+
RUN wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz
20+
RUN tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz -C /root
21+
22+
# WASI env setup
23+
ENV WASI_SDK_PATH=/root/wasi-sdk-${WASI_VERSION_FULL}
24+
ENV CC_wasm32-wasi="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"
25+
ENV CARGO_TARGET_WASM32_WASI_LINKER="${WASI_SDK_PATH}/bin/clang"
26+
27+
ENV RUSTFLAGS=-Ctarget-feature=-crt-static
28+

ci/run-docker.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ docker run \
2222
--env CARGO_HOME=/cargo \
2323
--workdir /usr/code \
2424
openssl-src-ci \
25-
bash -c "PATH=\$PATH:/rust/bin ci/run.sh $target"
25+
bash -c "PATH=\"\$PATH:/rust/bin:/cargo/bin\" ci/run.sh $target"
2626

ci/run.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22

33
target=$1
44
testcrate_dir="$(pwd)/testcrate"
5+
6+
cargo=$([ $target = "wasm32-wasi" ] && echo "cargo wasi" || echo "cargo")
7+
target_arg=$([ $target = "wasm32-wasi" ] && echo "" || echo "--target $target")
8+
59
set -ex
610

711
if [ "$1" = "aarch64-apple-darwin" ] ; then
812
export CARGO_TARGET_AARCH64_APPLE_DARWIN_RUNNER=echo
913
fi
1014

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
15+
$cargo test --manifest-path "$testcrate_dir/Cargo.toml" $target_arg -vvv
16+
$cargo test --manifest-path "$testcrate_dir/Cargo.toml" $target_arg -vvv --release
1317

1418
if [ "$1" = "x86_64-unknown-linux-gnu" ] ; then
15-
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv --all-features
19+
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vvv --all-features
1620

1721
# Run a few tests here:
1822
#

src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,42 @@ impl Build {
428428
configure.arg("-D__STDC_NO_ATOMICS__");
429429
}
430430

431+
if target.contains("wasi") {
432+
configure.args([
433+
// Termios isn't available whatsoever on WASM/WASI so we disable that
434+
// fatal error: 'termios.h' file not found
435+
"no-ui-console",
436+
// WASI doesn't support UNIX sockets so we preemptively disable it
437+
"no-sock",
438+
// WASI doesn't have a concept of syslog, so we disable it
439+
// fatal error: 'syslog.h' file not found
440+
"-DNO_SYSLOG",
441+
// WASI doesn't support (p)threads. Disabling preemptively.
442+
"no-threads",
443+
// WASI/WASM aren't really friends with ASM, so we disable it as well.
444+
"no-asm",
445+
// Disables the AFALG engine (AFALG-ENGine)
446+
// Since AFALG depends on `AF_ALG` support on the linux kernel side
447+
// it makes sense that we can't use it.
448+
"no-afalgeng",
449+
"-DOPENSSL_NO_AFALGENG=1",
450+
// wasm lacks signal support; to enable minimal signal emulation, compile with
451+
// -D_WASI_EMULATED_SIGNAL and link with -lwasi-emulated-signal
452+
"-D_WASI_EMULATED_SIGNAL",
453+
// WASI lacks process-associated clocks; to enable emulation of the `times` function using the wall
454+
// clock, which isn't sensitive to whether the program is running or suspended, compile with
455+
// -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks
456+
"-D_WASI_EMULATED_PROCESS_CLOCKS",
457+
// WASI lacks a true mmap; to enable minimal mmap emulation, compile
458+
// with -D_WASI_EMULATED_MMAN and link with -lwasi-emulated-mman
459+
"-D_WASI_EMULATED_MMAN",
460+
// WASI lacks process identifiers; to enable emulation of the `getpid` function using a
461+
// placeholder value, which doesn't reflect the host PID of the program, compile with
462+
// -D_WASI_EMULATED_GETPID and link with -lwasi-emulated-getpid
463+
"-D_WASI_EMULATED_GETPID",
464+
]);
465+
}
466+
431467
if target.contains("musl") {
432468
// Hack around openssl/openssl#7207 for now
433469
configure.arg("-DOPENSSL_NO_SECURE_MEMORY");
@@ -578,6 +614,11 @@ impl Artifacts {
578614
println!("cargo:lib={}", self.lib_dir.display());
579615
if self.target.contains("msvc") {
580616
println!("cargo:rustc-link-lib=user32");
617+
} else if self.target == "wasm32-wasi" {
618+
println!("cargo:rustc-link-lib=wasi-emulated-signal");
619+
println!("cargo:rustc-link-lib=wasi-emulated-process-clocks");
620+
println!("cargo:rustc-link-lib=wasi-emulated-mman");
621+
println!("cargo:rustc-link-lib=wasi-emulated-getpid");
581622
}
582623
}
583624
}

0 commit comments

Comments
 (0)