#!/usr/bin/env zsh
# build.zsh -- compile MaxSAT core, sanity, and examples.

set -e
CC=${CC:-gcc}
CFLAGS=( ${=CFLAGS:--std=c99 -O2 -Wall -Wextra -Isrc} )

mkdir -p build

echo "=== compiling core ==="
$CC $CFLAGS -c src/sat.c    -o build/sat.o
$CC $CFLAGS -c src/maxsat.c -o build/maxsat.o
CORE_OBJS=(build/sat.o build/maxsat.o)

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

echo
echo "=== examples ==="
for ex in ${(f)"$(ls examples/ex*.c 2>/dev/null \
              | sed -E 's|examples/ex([0-9]+)_|\1 examples/ex\1_|' \
              | sort -n \
              | cut -d' ' -f2-)"}; do
    name=$(basename "$ex" .c)
    $CC $CFLAGS "$ex" $CORE_OBJS -o "build/$name"
    echo
    echo "----- $name -----"
    "./build/$name"
done

echo
echo "build complete."
