Skip to content

Instantly share code, notes, and snippets.

@NWilson
Last active December 11, 2017 17:42
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 NWilson/b0d7a643b66bcd4c118b26d979572c5b to your computer and use it in GitHub Desktop.
Save NWilson/b0d7a643b66bcd4c118b26d979572c5b to your computer and use it in GitHub Desktop.
Nick's way of setting up Wasm toolchain
# Check everything out here, and build here
BUILD_DIR=~/workspace/wasm
# Install all the tools, headers, Wasm libraries here, so that
# we can use it via "-isysroot" when compiling.
INSTALL_DIR=~/workspace/wasm-root
#####
##### Get LLVM, build WebAssembly
#####
cd $BUILD_DIR
git clone https://github.com/llvm-mirror/llvm.git llvm
git clone https://github.com/llvm-mirror/clang.git llvm/tools/clang
git clone https://github.com/llvm-mirror/lld.git llvm/tools/lld
mdir bld
cd bld
cmake -G Ninja \
-DCMAKE_INSTALL_PREFIX=$INSTALL_DIR \
-DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD=WebAssembly \
-DLLVM_TARGETS_TO_BUILD=X86 \
-DLLVM_DEFAULT_TARGET_TRIPLE=wasm32-unknown-unknown-wasm \
-DLLVM_USE_LINKER=gold \
-DCMAKE_BUILD_TYPE=Debug \
-DLLVM_ENABLE_ASSERTIONS=On \
..
ninja all install
#####
##### Get Musl, produce a working libc
#####
cd $BUILD_DIR
git clone git@github.com:NWilson/musl.git musl
cd musl
(
PATH=$INSTALL_DIR/bin:$PATH # Use Wasm Clang
export CC=clang
export LDFLAGS=-Wl,--no-entry
export CFLAGS=-fvisibility=hidden # Just a nicety to hide symbols
export CROSS_COMPILE=llvm- # Essential to use llvm-ar!
./configure --prefix=$INSTALL_DIR/usr/
make -j8 all
)
make install
cp arch/wasm/wasm.syms $INSTALL_DIR/share/
#####
##### libcxx/libcxxabi compilation
#####
cd $BUILD_DIR
git clone https://github.com/llvm-mirror/libcxx.git libcxx
mkdir -p libcxx/bld
cd libcxx/bld
(
PATH=$INSTALL_DIR/bin:$PATH # Use Wasm Clang
cmake -G Ninja \
-DCMAKE_CXX_FLAGS="-nostdlib++ -nostdlib -isysroot/home/ncw/workspace/llvm/compile-root/ -Wl,--allow-undefined -Wl,--no-entry" \
-DCMAKE_C_FLAGS="-nostdlib -isysroot/home/ncw/workspace/llvm/compile-root/ -Wl,--allow-undefined -Wl,--no-entry" \
-DCMAKE_BUILD_TYPE=Release \
-DLIBCXX_ENABLE_SHARED=False \
-DLIBCXX_HAS_MUSL_LIBC=True \
-DLLVM_CONFIG_PATH=$INSTALL_DIR/bin/llvm-config \
-DLIBCXX_ENABLE_THREADS=False \
-DCMAKE_AR=$INSTALL_DIR/bin/llvm-ar \
..
ninja libc++.a
)
git clone https://github.com/llvm-mirror/libcxxabi.git libcxxabi
mkdir -p libcxxabi/bld
cd libcxxabi/bld
(
PATH=$INSTALL_DIR/bin:$PATH # Use Wasm Clang
LIBCXX_BINARY_DIR=$BUILD_DIR/libcxx/bld
cmake -G Ninja \
-DCMAKE_CXX_FLAGS="-nostdlib++ -nostdlib -isysroot/home/ncw/workspace/llvm/compile-root/ -Wl,--allow-undefined -Wl,--no-entry -include ${LIBCXX_BINARY_DIR}/__config_site" \
-DCMAKE_C_FLAGS="-nostdlib -isysroot/home/ncw/workspace/llvm/compile-root/ -Wl,--allow-undefined -Wl,--no-entry" \
-DCMAKE_BUILD_TYPE=Release \
-DLLVM_CONFIG_PATH=$INSTALL_DIR/bin/llvm-config \
-DCMAKE_AR=$INSTALL_DIR/bin/llvm-ar \
-DLLVM_ENABLE_LIBCXX=True \
-DLIBCXXABI_LIBCXX_PATH=$BUILD_DIR/libcxx \
-DLIBCXXABI_ENABLE_SHARED=False \
-DLIBCXXABI_ENABLE_THREADS=False \
..
ninja libc++abi.a
)
#####
##### Using it all
#####
(
CC=$INSTALL_DIR/bin/clang
CFLAGS=-isysroot$INSTALL_DIR
# Sadly, LLD doesn't support sysroot, so we're stuck with using -L for now.
LDFLAGS="-L$INSTALL_DIR/usr/lib"
# Secondly, my version of Musl isn't targeted by Clang, so we have to use
# -nostdlib to wipe the bits it adds for Musl and add our own.
LDFLAGS="$LDFLAGS -nostdlib -lc -Wl,--entry=_start_wasm -Wl,-allow-undefined-file -Wl,$INSTALL_DIR/share/wasm.syms"
echo "int exportedFn() { return 42; }" > /tmp/test.c
$CC $CFLAGS -c -o /tmp/test.o /tmp/test.c
$CC $LDFLAGS -o /tmp/test /tmp/test.o
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment