Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active June 5, 2020 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/dedd68a05245db0376a803a19fd635e1 to your computer and use it in GitHub Desktop.
Save myobie/dedd68a05245db0376a803a19fd635e1 to your computer and use it in GitHub Desktop.
Download and compile libsodium to wasm using wasi-sdk
# $ ./test.sh > log.log 2>&1
~/src/github.com/wasi-sdk-test/tmp ~/src/github.com/wasi-sdk-test
--2020-06-05 09:11:56-- https://github.com/jedisct1/libsodium/releases/download/1.0.18-RELEASE/libsodium-1.0.18.tar.gz
Resolving github.com (github.com)... 140.82.118.3
Connecting to github.com (github.com)|140.82.118.3|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-production-release-asset-2e65be.s3.amazonaws.com/7710647/e52f2f80-b05e-11e9-82d6-073fa3532c76?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200605%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200605T071031Z&X-Amz-Expires=300&X-Amz-Signature=bf00a70658528de36119000082a48d773357b564170b37977e85ecf746908add&X-Amz-SignedHeaders=host&actor_id=0&repo_id=7710647&response-content-disposition=attachment%3B%20filename%3Dlibsodium-1.0.18.tar.gz&response-content-type=application%2Foctet-stream [following]
--2020-06-05 09:11:57-- https://github-production-release-asset-2e65be.s3.amazonaws.com/7710647/e52f2f80-b05e-11e9-82d6-073fa3532c76?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20200605%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20200605T071031Z&X-Amz-Expires=300&X-Amz-Signature=bf00a70658528de36119000082a48d773357b564170b37977e85ecf746908add&X-Amz-SignedHeaders=host&actor_id=0&repo_id=7710647&response-content-disposition=attachment%3B%20filename%3Dlibsodium-1.0.18.tar.gz&response-content-type=application%2Foctet-stream
Resolving github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.216.98.251
Connecting to github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.216.98.251|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1919817 (1.8M) [application/octet-stream]
Saving to: ‘libsodium-1.0.18.tar.gz’
0K .......... .......... .......... .......... .......... 2% 123K 15s
41500K .......... .......... . 100% 31.4M=6.1s
2020-06-05 09:12:07 (6.69 MB/s) - ‘wasi-sdk-10.0-macos.tar.gz’ saved [42517892/42517892]
~/src/github.com//wasi-sdk-test
CC: clang
clang: /Users/myobie/src/github.com/wasi-sdk-test/tmp/wasi-sdk-10.0/bin/clang
~/src/github.com/wasi-sdk-test/tmp/libsodium-1.0.18 ~/src/github.com/wasi-sdk-test
checking build system type... i386-apple-darwin19.5.0
checking host system type... wasm32-unknown-wasi
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for wasm32-wasi-strip... llvm-strip
checking for a thread-safe mkdir -p... build-aux/install-sh -c -d
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking whether UID '501' is supported by ustar format... yes
checking whether GID '20' is supported by ustar format... yes
checking how to create a ustar tar archive... gnutar
checking whether make supports nested variables... (cached) yes
checking whether to enable maintainer-specific portions of Makefiles... no
checking whether make supports the include directive... yes (GNU style)
checking for wasm32-wasi-gcc... clang
checking whether the C compiler works... no
configure: error: in `/Users/myobie/src/github.com/wasi-sdk-test/tmp/libsodium-1.0.18':
configure: error: C compiler cannot create executables
See `config.log' for more details
#!/bin/bash
set -e
set -o pipefail
rm -rf {tmp,lib}
mkdir -p {tmp,lib}
pushd tmp
wget https://github.com/jedisct1/libsodium/releases/download/1.0.18-RELEASE/libsodium-1.0.18.tar.gz
tar -xf libsodium-1.0.18.tar.gz
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-10/wasi-sdk-10.0-macos.tar.gz
tar -xf wasi-sdk-10.0-macos.tar.gz
popd
export PATH="$(cd tmp/wasi-sdk-10.0/bin && pwd):$PATH"
export WASI_SYSROOT="$(cd tmp/wasi-sdk-10.0/share/wasi-sysroot && pwd)"
export CC="clang" # NOTE: uncomment and this script will fail
export NM="llvm-nm"
export AR="llvm-ar"
export RANLIB="llvm-ranlib"
export STRIP="llvm-strip"
pushd tmp/libsodium-1.0.18
cat <<-'EOF' >src/main.c
#include <sodium.h>
int main(void)
{
return sodium_init();
}
EOF
mkdir -p wasm-build
PREFIX=$(cd wasm-build && pwd)
./configure --prefix="${PREFIX}" \
--with-sysroot="$WASI_SYSROOT" \
--host=wasm32-wasi \
--disable-ssp --disable-shared
FILES=$(find src -name "*.c")
clang \
--target=wasm32-wasi \
--sysroot="${WASI_SYSROOT}" \
-isysroot="${WASI_SYSROOT}" \
-Oz \
-flto \
-I src/libsodium/include \
-I src/libsodium/include/sodium \
-D _WASI_EMULATED_SIGNAL=1 \
-D ED25519_NONDETERMINISTIC=1 \
-D CONFIGURED=1 \
-Wl,--export-all \
-Wl,--lto-O2 \
-o ../../lib/sodium.wasm \
$FILES
popd
pushd lib
wasm2wat sodium.wasm -o sodium.wat
popd
rm -rf ./tmp/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment