-
Notifications
You must be signed in to change notification settings - Fork 3
/
c2wasm
executable file
·49 lines (39 loc) · 978 Bytes
/
c2wasm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/sh
SCRIPT=$(realpath "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
CLANG="$SCRIPTPATH"/llvm/build/bin/clang
LLC="$SCRIPTPATH"/llvm/build/bin/llc
S2WASM="$SCRIPTPATH"/binaryen/build/bin/s2wasm
WAT2WASM="$SCRIPTPATH"/wabt/build/wat2wasm
if [ ! -x $CLANG ] || [ ! -x $LLC ] || [ ! -x $S2WASM ] || [ ! -x $WAT2WASM ]; then
echo Run build-all.sh first!
exit 255
fi
FILENAME="$1"
BASE="${FILENAME%.*}"
EXT="${FILENAME##*.}"
if [ "$#" -ne 1 ] || [ ! "$EXT" = "c" ]; then
echo "Usage: $0 source.c" >&2
exit 1
fi
$CLANG -emit-llvm --target=wasm32 -S $BASE.c -o $BASE.ll
if [ "$?" -ne 0 ]; then
echo "clang failed!" >&2
exit 2
fi
$LLC -march=wasm32 -filetype=asm $BASE.ll -o $BASE.s
if [ "$?" -ne 0 ]; then
echo "llc failed!" >&2
exit 2
fi
$S2WASM $BASE.s -o $BASE.wat
if [ "$?" -ne 0 ]; then
echo "s2wasm failed!" >&2
exit 2
fi
$WAT2WASM $BASE.wat -o $BASE.wasm
if [ "$?" -ne 0 ]; then
echo "wat2wasm failed!" >&2
exit 2
fi
rm $BASE.ll $BASE.s $BASE.wat