#!/bin/bash

#Build qb3-wasm decoder as an JS6 module using emscripten
# Assume libQB3.a is already built and available in the current folder
# To build it on a system with emcc installed, run something like this
# CC=emcc CXX=emcc cmake -DBUILD_SHARED_LIBS=Off -B build .
#
# Assume json.hpp is in the include path for emcc

OPTS="-O3 -flto"

# Place json.hpp in the current folder
emcc $OPTS -fno-rtti -fno-exceptions -I . -c qb3decapi.cpp

#Build qb3-wasm module, see post.js for API
emcc $OPTS -o qb3-wasm.mjs qb3decapi.o libQB3.a \
    -s EXPORTED_FUNCTIONS=_malloc,_free \
    -s NO_EXIT_RUNTIME=1 \
    -s ALLOW_MEMORY_GROWTH=1 \
    --no-entry \
    --post-js post.js \
    -sENVIRONMENT=web,worker \
    -sEXPORTED_RUNTIME_METHODS=[writeArrayToMemory,UTF8ToString]
