Skip to content

Commit

Permalink
Added support for wasm32-wasi target
Browse files Browse the repository at this point in the history
  • Loading branch information
OtaK committed Jan 31, 2022
1 parent d523d40 commit 23a235a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ jobs:
- target: x86_64-pc-windows-gnu
rust: stable
os: ubuntu-latest
# - target: asmjs-unknown-emscripten
# rust: stable
# os: ubuntu-latest
- target: wasm32-wasi
rust: stable
os: ubuntu-latest
- target: i686-pc-windows-msvc
rust: stable-i686-msvc
os: windows-2016
Expand Down
3 changes: 0 additions & 3 deletions ci/docker/asmjs-unknown-emscripten/Dockerfile

This file was deleted.

29 changes: 29 additions & 0 deletions ci/docker/wasm32-wasi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
FROM rust:1-slim

RUN apt-get update -y && apt-get install -y --no-install-recommends \
ca-certificates \
curl wget \
make \
perl \
gcc \
xz-utils \
libc6-dev

# cargo-wasi setup (cargo-wasi depends on wasmtime to be present)
RUN curl https://wasmtime.dev/install.sh -sSf | bash
ENV PATH="$PATH:/root/.wasmtime/bin"
RUN cargo install cargo-wasi

# Install WASI-SDK
ENV WASI_VERSION=14
ENV WASI_VERSION_FULL=${WASI_VERSION}.0
RUN wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-${WASI_VERSION}/wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz
RUN tar xvf wasi-sdk-${WASI_VERSION_FULL}-linux.tar.gz -C /root

# WASI env setup
ENV WASI_SDK_PATH=/root/wasi-sdk-${WASI_VERSION_FULL}
ENV CC_wasm32-wasi="${WASI_SDK_PATH}/bin/clang --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot"
ENV CARGO_TARGET_WASM32_WASI_LINKER="${WASI_SDK_PATH}/bin/clang"

ENV RUSTFLAGS=-Ctarget-feature=-crt-static

2 changes: 1 addition & 1 deletion ci/run-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ docker run \
--env CARGO_HOME=/cargo \
--workdir /usr/code \
openssl-src-ci \
bash -c "PATH=\$PATH:/rust/bin ci/run.sh $target"
bash -c "PATH=\$PATH:/rust/bin:/cargo/bin ci/run.sh $target"

11 changes: 8 additions & 3 deletions ci/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

target=$1
testcrate_dir="$(pwd)/testcrate"

cargo=$([ $target = "wasm32-wasi" ] && echo "cargo wasi" || echo "cargo")
target_arg=$([ $target = "wasm32-wasi" ] && echo "" || echo "--target $target")

set -ex

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

cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $1 -vv --release
$cargo test --manifest-path "$testcrate_dir/Cargo.toml" $target_arg -vvv
$cargo test --manifest-path "$testcrate_dir/Cargo.toml" $target_arg -vvv --release


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

# Run a few tests here:
#
Expand Down
41 changes: 41 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,42 @@ impl Build {
configure.arg("-D__STDC_NO_ATOMICS__");
}

if target.contains("wasi") {
configure.args([
// Termios isn't available whatsoever on WASM/WASI so we disable that
// fatal error: 'termios.h' file not found
"no-ui-console",
// WASI doesn't support UNIX sockets so we preemptively disable it
"no-sock",
// WASI doesn't have a concept of syslog, so we disable it
// fatal error: 'syslog.h' file not found
"-DNO_SYSLOG",
// WASI doesn't support (p)threads. Disabling preemptively.
"no-threads",
// WASI/WASM aren't really friends with ASM, so we disable it as well.
"no-asm",
// Disables the AFALG engine (AFALG-ENGine)
// Since AFALG depends on `AF_ALG` support on the linux kernel side
// it makes sense that we can't use it.
"no-afalgeng",
"-DOPENSSL_NO_AFALGENG=1",
// wasm lacks signal support; to enable minimal signal emulation, compile with
// -D_WASI_EMULATED_SIGNAL and link with -lwasi-emulated-signal
"-D_WASI_EMULATED_SIGNAL",
// WASI lacks process-associated clocks; to enable emulation of the `times` function using the wall
// clock, which isn't sensitive to whether the program is running or suspended, compile with
// -D_WASI_EMULATED_PROCESS_CLOCKS and link with -lwasi-emulated-process-clocks
"-D_WASI_EMULATED_PROCESS_CLOCKS",
// WASI lacks a true mmap; to enable minimal mmap emulation, compile
// with -D_WASI_EMULATED_MMAN and link with -lwasi-emulated-mman
"-D_WASI_EMULATED_MMAN",
// WASI lacks process identifiers; to enable emulation of the `getpid` function using a
// placeholder value, which doesn't reflect the host PID of the program, compile with
// -D_WASI_EMULATED_GETPID and link with -lwasi-emulated-getpid
"-D_WASI_EMULATED_GETPID",
]);
}

if target.contains("musl") {
// Hack around openssl/openssl#7207 for now
configure.arg("-DOPENSSL_NO_SECURE_MEMORY");
Expand Down Expand Up @@ -578,6 +614,11 @@ impl Artifacts {
println!("cargo:lib={}", self.lib_dir.display());
if self.target.contains("msvc") {
println!("cargo:rustc-link-lib=user32");
} else if self.target == "wasm32-wasi" {
println!("cargo:rustc-link-lib=wasi-emulated-signal");
println!("cargo:rustc-link-lib=wasi-emulated-process-clocks");
println!("cargo:rustc-link-lib=wasi-emulated-mman");
println!("cargo:rustc-link-lib=wasi-emulated-getpid");
}
}
}

0 comments on commit 23a235a

Please sign in to comment.