
COAL=../bin/coal
CC=gcc
CF=-pedantic

GEN_TESTS=literals_test math_test relop_test assign_test \
map_test range_test reduce_test array_test if_test \
lambda_test builtins_test recursion_test

all : ${GEN_TESTS} cbindings_test_assert modules_test_assert dft_ex filter_ex

# explicitly declare generated test executables
literals_test : literals.o literals_test.o
	${CC} -o $@ $?
    
math_test : math.o math_test.o
	${CC} -o $@ $?
    
relop_test : relop.o relop_test.o
	${CC} -o $@ $?
    
assign_test : assign.o assign_test.o
	${CC} -o $@ $?
    
map_test : map.o map_test.o
	${CC} -o $@ $?
    
range_test : range.o range_test.o
	${CC} -o $@ $?
    
reduce_test : reduce.o reduce_test.o
	${CC} -o $@ $?

array_test : array.o array_test.o
	${CC} -o $@ $?
    
if_test : if.o if_test.o
	${CC} -o $@ $?
    
lambda_test : lambda.o lambda_test.o
	${CC} -o $@ $?
    
builtins_test : builtins.o builtins_test.o
	${CC} -o $@ $?
    
recursion_test : recursion.o recursion_test.o
	${CC} -o $@ $?
    
# special cases
dft_ex : dft.o dft_ex.o
	${CC} -o $@ $?

dft.c : dft.coal
	${COAL} -storesize 1024 -o ${@:.c=} $<
    
filter_ex : filter.o filter_ex.o
	${CC} -o $@ $?

filter.c : filter.coal
	${COAL} -storesize 128 -o ${@:.c=} $<
    
cbindings.c : cbindings.coal
	${COAL} -storesize 9 -o ${@:.c=} $<
    
cbindings_test_assert : cbindings.o cbindings_test_assert.o
	${CC} -o $@ $?
    
modules.c : module_a.coal module_b.coal module_c.coal
	${COAL} -o ${@:.c=} $?

modules_test_assert : modules.o modules_test_assert.o
	${CC} -o $@ $?

# common rules
%_test.c :
	sed 's/HEADER/${@:_test.c=}.h/' harness.txt > $@

%.c : %.coal
	${COAL} -trace -o ${@:.c=} $<
    
%.o : %.c
	${CC} ${CF} -c $<
    
.PHONY : test
test : all
	./run_test.sh type_inference_test.sh
	./run_test.sh literals_test
	./run_test.sh math_test
	./run_test.sh relop_test
	./run_test.sh assign_test
	./run_test.sh map_test
	./run_test.sh range_test
	./run_test.sh reduce_test
	./run_test.sh array_test
	./run_test.sh if_test
	./run_test.sh lambda_test
	./run_test.sh builtins_test
	./run_test.sh recursion_test
	./run_test.sh dft_ex
	./cbindings_test_assert
	./modules_test_assert

.PHONY : clean
clean :
	rm -f *.out.txt *.diff.txt *.h *.o *.exe
	rm -f ${GEN_TESTS:=.c} ${GEN_TESTS:_test=.c}
	rm -f cbindings.c modules.c dft.c filter.c

