#!/usr/bin/env bash
set -e

root="$(dirname $(realpath "$0"))"
mkdir -p build
cd build

# Run emconfigure with the normal configure command as an argument.
emcmake cmake ../HiGHS -DZLIB=OFF -DFAST_BUILD=OFF -DBUILD_SHARED_LIBS=OFF

# Run emmake with the normal make to generate wasm object files.
emmake make -j8 libhighs

# Compile the linked code generated by make to JavaScript + WebAssembly.
# 'project.o' should be replaced with the make output for your project, and
# you may need to rename it if it isn't something emcc recognizes
# (for example, it might have a different suffix like 'project.so' or
# 'project.so.1', or no suffix like just 'project' for an executable).
# If the project output is a library, you may need to add your 'main.c' file
# here as well.
# [-Ox] represents build optimisations (discussed in the next section).
export EMCC_CLOSURE_ARGS="--jscomp_off=checkTypes"
emcc -O3 \
	-s EXPORTED_FUNCTIONS="@$root/exported_functions.json" \
	-s EXPORTED_RUNTIME_METHODS="['cwrap']" \
	-s MODULARIZE=1 \
	-s ALLOW_MEMORY_GROWTH=1 \
	-s STACK_SIZE=4194304 \
	-flto \
	--closure 1 \
	--pre-js "$root/src/pre.js" \
	--post-js "$root/src/post.js" \
	lib/*.a -o highs.js
