Skip to content

Commit 9c9b745

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

File tree

6 files changed

+84
-10
lines changed

6 files changed

+84
-10
lines changed

.github/workflows/main.yml

Lines changed: 3 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

ci/docker/asmjs-unknown-emscripten/Dockerfile

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

ci/docker/wasm32-wasi/Dockerfile

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
FROM rust:1-slim
2+
3+
RUN apt-get update -y && apt-get install -y --no-install-recommends \
4+
ca-certificates \
5+
curl wget \
6+
software-properties-common lsb-release gpg-agent gnupg gnupg1 gnupg2 \
7+
make \
8+
perl \
9+
gcc \
10+
xz-utils \
11+
libc6-dev
12+
13+
RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)" -- 14
14+
RUN update-alternatives --install /usr/bin/llvm-config llvm-config /usr/bin/llvm-config-14 14
15+
RUN update-alternatives --install /usr/bin/clang clang /usr/bin/clang-14 14
16+
17+
RUN curl https://wasmtime.dev/install.sh -sSf | bash
18+
RUN cargo install cargo-wasi
19+
20+
# Install WASI-SDK
21+
ENV WASI_VERSION=14
22+
ENV WASI_VERSION_FULL=${WASI_VERSION}.0
23+
RUN wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz
24+
RUN tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz -C /root
25+
26+
ENV WASI_SDK_PATH=/root/wasi-sdk-${WASI_VERSION_FULL}
27+
ENV CC_wasm32-wasi="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"
28+
ENV CARGO_TARGET_WASM32_WASI_LINKER="${WASI_SDK_PATH}/bin/clang"
29+
30+
ENV RUSTFLAGS=-Ctarget-feature=-crt-static
31+
env PATH="$PATH:/root/.wasmtime/bin"

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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
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
17+
1318

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

1722
# Run a few tests here:
1823
#

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-arg=-lwasi-emulated-signal");
619+
println!("cargo:rustc-link-arg=-lwasi-emulated-process-clocks");
620+
println!("cargo:rustc-link-arg=-lwasi-emulated-mman");
621+
println!("cargo:rustc-link-arg=-lwasi-emulated-getpid");
581622
}
582623
}
583624
}

0 commit comments

Comments
 (0)