#!/usr/bin/env zsh
# build.zsh -- compile sat / euf / smt, sanity tests, and the four examples.

set -e

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

mkdir -p build

echo "=== compiling core ==="
$CC $CFLAGS -c src/rat.c -o build/rat.o
$CC $CFLAGS -c src/sat.c -o build/sat.o
$CC $CFLAGS -c src/euf.c -o build/euf.o
$CC $CFLAGS -c src/lra.c -o build/lra.o
$CC $CFLAGS -c src/smt.c -o build/smt.o

CORE_OBJS=(build/rat.o build/sat.o build/euf.o build/lra.o build/smt.o)

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

echo
echo "=== examples ==="
# Sort by the number after "ex" so ex2 comes before ex10.
for ex in ${(f)"$(ls examples/ex*.c \
              | 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."
