#!/usr/bin/env zsh
# build.zsh -- compile the MILP solver, sanity tests, and the four examples.
#
# No Makefile, no Xcode project: a single TU library plus four demos.

set -e

CC=${CC:-clang}
CFLAGS=( ${=CFLAGS:--std=c99 -O2 -Wall -Wextra -Isrc} )
LDFLAGS=(-lm)

mkdir -p build

echo "=== compiling core ==="
$CC $CFLAGS -c src/milp.c -o build/milp.o

CORE_OBJS=(build/milp.o)

echo "=== sanity ==="
$CC $CFLAGS sanity.c $CORE_OBJS -o build/sanity $LDFLAGS
./build/sanity

echo
echo "=== examples ==="
for ex in examples/ex1_knapsack.c examples/ex2_assignment.c \
          examples/ex3_setcover.c examples/ex4_wta.c; do
    name=$(basename "$ex" .c)
    $CC $CFLAGS "$ex" $CORE_OBJS -o "build/$name" $LDFLAGS
    echo
    echo "----- $name -----"
    "./build/$name"
done

echo
echo "build complete."
