CC = gcc
CFLAGS = -ansi -Wall -pedantic
DEBUG =
DEMO = demo/1fulladder.out demo/2minus.out demo/3cm2.out demo/4clk2.out \
       demo/5cm24.out

BIN = bin/scifi

OBJS = tmp/block.o tmp/function.o tmp/function_table.o tmp/main.o \
       tmp/parser.o tmp/registers.o tmp/scifi.o tmp/checker.o \
       tmp/hipopt.o tmp/memory.o

REPORTEX = doc/rapport.tex
REPORT = doc/rapport.pdf

.PHONY: all clean clear debug test doc tar depend

all: .depend $(BIN)

depend: .depend

.depend:
	gcc -MM src/*.c src/hipopt/*.c > tmp/.depend || rm -f tmp/.depend
	sed 's/\(.\+\.o\)/tmp\/\1/g' tmp/.depend > .depend || rm -f .depend

debug: clean
	# -pg
	DEBUG="-pg -g" make -e $(BIN)

optimize: clean
	#TODO -DNDEBUG
	DEBUG="-O3 -DNDEBUG" make -e $(BIN)

tar: scifi.tgz

scifi.tgz: clean
	mkdir -p tmp
	tar -c --exclude=.git --exclude=scifi.tgz ../scifi | gzip > $@

include .depend

$(OBJS):
	$(CC) $(DEBUG) $(CFLAGS) -c $< -o $@

$(BIN): $(OBJS)
	$(CC) $(DEBUG) $^ -o $@

doc: $(REPORTEX)
	pdflatex -output-directory tmp $<
	pdflatex -output-directory tmp $<
	pdflatex -output-directory tmp $<
	cp tmp/rapport.pdf $(REPORT)

%.out: %.nls %.in all
	bin/demo.sh `basename $*`

demo: $(DEMO)
	@echo "Démonstration terminée !"

demo-cycle: all
	-bin/scifi -n var/demo_cycle.nls

demo-all: demo demo-cycle

test: optimize
	bin/test.sh 0 2 1in
	bin/test.sh 1 2 1in
	bin/test.sh 2 4 2in
	bin/test.sh 3 4 2in
	bin/test.sh 4 4 2in
	bin/test.sh 5 8 3in
	bin/test.sh 6 8 3in
	bin/test.sh 7 16 reg_in
	bin/test.sh 8 16 reg_in
	bin/test.sh fullAdd 8 3in
	bin/test.sh minus 18 reg_in
	bin/test.sh opt 12 test_opt.in

clean: clear
	rm -f $(OBJS) $(BIN) $(REPORT)

clear:
	rm -f *~ src/*~ var/*~ bin/*~ tmp/* demo/*.out demo/*~ .depend tmp/.depend
