From 85ef012165a5f415a14442d754851d471a29492b Mon Sep 17 00:00:00 2001 From: Mathieu Amiot <1262712+OtaK@users.noreply.github.com> Date: Tue, 1 Feb 2022 16:16:44 +0100 Subject: [PATCH] Added support for wasm32-wasi target (#119) --- .github/workflows/main.yml | 6 +-- ci/docker/asmjs-unknown-emscripten/Dockerfile | 3 -- ci/docker/wasm32-wasi/Dockerfile | 28 ++++++++++++ ci/run-docker.sh | 2 +- ci/run.sh | 14 +++--- src/lib.rs | 43 +++++++++++++++++++ 6 files changed, 82 insertions(+), 14 deletions(-) delete mode 100644 ci/docker/asmjs-unknown-emscripten/Dockerfile create mode 100644 ci/docker/wasm32-wasi/Dockerfile diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 10bcc63e..d91798b0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/ci/docker/asmjs-unknown-emscripten/Dockerfile b/ci/docker/asmjs-unknown-emscripten/Dockerfile deleted file mode 100644 index 1b627dcb..00000000 --- a/ci/docker/asmjs-unknown-emscripten/Dockerfile +++ /dev/null @@ -1,3 +0,0 @@ -FROM trzeci/emscripten -ENV CARGO_TARGET_ASMJS_UNKNOWN_EMSCRIPTEN_RUNNER=node \ - CARGO_TARGET_ARM_UNKNOWN_LINUX_GNUEABI_LINKER=arm-linux-gnueabi-gcc diff --git a/ci/docker/wasm32-wasi/Dockerfile b/ci/docker/wasm32-wasi/Dockerfile new file mode 100644 index 00000000..b57d4bab --- /dev/null +++ b/ci/docker/wasm32-wasi/Dockerfile @@ -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 + diff --git a/ci/run-docker.sh b/ci/run-docker.sh index 9647d764..3d26a263 100755 --- a/ci/run-docker.sh +++ b/ci/run-docker.sh @@ -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" diff --git a/ci/run.sh b/ci/run.sh index 6ea35942..596510b5 100755 --- a/ci/run.sh +++ b/ci/run.sh @@ -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: # @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 66aa8728..eef27eeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"); @@ -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"); } } }