Skip to content

Instantly share code, notes, and snippets.

@Codezigineer
Last active February 21, 2023 01:52
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 Codezigineer/501c09f8a3114ce7bb32c7d27301650b to your computer and use it in GitHub Desktop.
Save Codezigineer/501c09f8a3114ce7bb32c7d27301650b to your computer and use it in GitHub Desktop.
webcomp: Very simple WebAssembly compiler based on LLVM 8+
#!/bin/bash
if [[ "$(curl https://gist.github.com/Codezigineer/501c09f8a3114ce7bb32c7d27301650b/raw/webcomp.sh)" != "$(cat $0)" ]]; then
cd "$(dirname $0)"
curl https://gist.github.com/Codezigineer/501c09f8a3114ce7bb32c7d27301650b/raw/webcomp.sh -o webcomp
fi
usage() {
echo "Usage: webcomp [ -f infile ] [ -o outfile ] [ -O0 ] [ -O1 ] [ -O2 ] [ -O3 ] [ -t target {default is wasm32} ] [ -O ]"
}
set -e
target=wasm32
optlevel=
while getopts ":f:o:O0:O1:O2:O3:t:O:" o; do
case "${o}" in
f)
in="$in ${OPTARG}"
;;
o)
out=${OPTARG}
;;
O0)
;;
O1)
optlevel=-01
;;
O2)
optlevel=-02
;;
O3)
optlevel=-03
;;
t)
target=${OPTARG}
;;
*)
if [[ -z "${in}" ]] || [[ -z "${out}" ]]; then
usage
exit 1
fi
;;
esac
done
if [[ -z "$in" ]]; then
usage
exit 1;
elif [[ -z "$out" ]]; then
usage
exit 1;
fi
if [[ "$target" != "wasm32" ]] && [[ "$target" != "wasm64" ]]; then
target=wasm32
fi
oldpwd=$PWD
folder=$(mktemp -d)
cd $folder
wget https://github.com/PicsumProjects/wclibc/releases/download/latest/wclibc-${target}.wasm -o wclibc.wasm
clang \
--target=$target \
-nostdlib \
-Wl,--no-entry \
-Wl,--export-all \
./wclibc.wasm
$in
-o $oldpwd/$out
cd $oldpwd
rm -rf $folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment