Skip to content

Commit

Permalink
Added support for wasm32-wasi target (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
OtaK authored Feb 1, 2022
1 parent 2f6e6e6 commit 85ef012
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 14 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.

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

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"

# 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"

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

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

set -ex

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

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 $target -vvv
cargo test --manifest-path "$testcrate_dir/Cargo.toml" --target $target -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 $target -vvv --all-features

# Run a few tests here:
#
Expand All @@ -35,5 +35,5 @@ if [ "$1" = "x86_64-unknown-linux-gnu" ] ; then
rm "$crate"
cd target/ci/package/openssl-src-*
cp -r "$testcrate_dir" .
cargo test --manifest-path "testcrate/Cargo.toml" --target $1 -vv
cargo test --manifest-path "testcrate/Cargo.toml" --target $target -vv
fi
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,44 @@ 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
"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
"-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
// The link argument is output in the `Artifacts::print_cargo_metadata` method
"-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
// The link argument is output in the `Artifacts::print_cargo_metadata` method
"-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
// The link argument is output in the `Artifacts::print_cargo_metadata` method
"-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
// The link argument is output in the `Artifacts::print_cargo_metadata` method
"-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 @@ -579,6 +617,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 85ef012

Please sign in to comment.