diff --git a/README.md b/README.md index dc0e71f..233be8a 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,17 @@ C:\repo\build> cmake -D OPENSSL_ROOT_DIR="C:/path/to/openssl-1.1/x86" .. C:\repo\build> cmake --build . --config Release ``` +### WebAssembly (Emscripten / Linux) + +It is possible to build WASM code using [emscripten](https://emscripten.org/). +Consult the manual for how to get it setup. No dependencies are needed as the build +script will download and compile `openssl` for wasm by itself. + +Once your emscripten environment is setup, call `build.sh` with the following flag. +```sh +$ ./build.sh em-wasm +``` + ## Install After the project has been compiled. run `sudo ./install.sh` or the following code if you dont want to use that: diff --git a/build.sh b/build.sh index 1e1deaf..f0e5f73 100755 --- a/build.sh +++ b/build.sh @@ -1,7 +1,17 @@ #!/bin/bash +BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + mkdir build 2> /dev/null pushd build > /dev/null -cmake .. $@ -make -B + +# Emscripten WASM build. +if [ $# -gt 0 ] && [ "$1" = "em-wasm" ]; then + shift 1 + . ${BASE_DIR}/scripts/emscripten.sh $@ +else + cmake $@ .. + make -B +fi + popd > /dev/null diff --git a/scripts/emscripten.sh b/scripts/emscripten.sh new file mode 100644 index 0000000..a108f3d --- /dev/null +++ b/scripts/emscripten.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" + +# Build openssl +. "${BASE_DIR}/emscripten_openssl.sh" "openssl" + +# Configure cmake +emconfigure cmake \ + -DCMAKE_BUILD_TYPE=Release \ + -DOPENSSL_ROOT_DIR="openssl" \ + -DOPENSSL_CRYPTO_LIBRARY="openssl/libcrypto.a" \ + -DOPENSSL_SSL_LIBRARY="openssl/libssl.a" \ + -G "Unix Makefiles" $@ .. + +emmake make diff --git a/scripts/emscripten_openssl.sh b/scripts/emscripten_openssl.sh new file mode 100644 index 0000000..fa8f65c --- /dev/null +++ b/scripts/emscripten_openssl.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Build OpenSSL + +BUILD_DIR=$1 +OPENSSL_VERSION=1.1.1d + +if [ ! -d "$BUILD_DIR" ]; then + mkdir -p "$BUILD_DIR" + + wget https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz + tar xf openssl-${OPENSSL_VERSION}.tar.gz -C "$BUILD_DIR" --strip 1 + rm -f openssl-${OPENSSL_VERSION}.tar.gz +fi + +pushd "$BUILD_DIR" + +emconfigure ./Configure gcc -no-tests -no-asm -static -no-sock \ + -no-afalgeng -DOPENSSL_SYS_NETWARE -DSIG_DFL=0 -DSIG_IGN=0 \ + -DHAVE_FORK=0 -DOPENSSL_NO_AFALGENG=1 --with-rand-seed=getrandom + +sed -i 's|^CROSS_COMPILE.*$|CROSS_COMPILE=|g' Makefile +#sed -i -E 's|^CFLAGS=(.*)$|CFLAGS=\1 -D__STDC_NO_ATOMICS__=1|g' Makefile + +emmake make -j 12 build_generated libssl.a libcrypto.a +popd