#!/usr/bin/env zsh
# build.zsh -- compile lcg core, sanity tests, 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/lcg.c             -o build/lcg.o
$CC $CFLAGS -c src/prop_alldiff.c    -o build/prop_alldiff.o
$CC $CFLAGS -c src/prop_cumulative.c -o build/prop_cumulative.o

CORE_OBJS=(build/sat.o build/lcg.o build/prop_alldiff.o build/prop_cumulative.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."
