# Very rudimentary Makefile for the llvm tutorial
# See http://llvm.org/docs/tutorial/
# See also http://koichitamura.blogspot.de/2011/01/since-i-went-to-held-several-weeks-ago.html
# for a necessary "fix" (!)
#
# This Makefile written by Prof. R. C. Moore, fbi.h-da.de

PROGS := ModuleMaker fibonacci toy2 toy3 toy4 toy5 toy6 toy7 toy8 toy9

# Uncomment only one of the next two lines (choose your c++ compiler)
# CC=g++
CC := clang++

# Now, how to compile in LLVM?
# LLVM VERSION 3.9.1 (2017) and possibly LLVM Version 4
# The Tutorial (Chapter 3) does it like this:
# clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core` -o toy
# based on that, and some experimentation, we got, for LLVM 3.6...
# CFLAGS=-std=c++11 `llvm-config --cxxflags --ldflags --system-libs --libs all` -rdynamic -O3
# Now, for llvm >= 3.7.1, further experience has brought us to....
# At one time, we piped the output of llvm-config to sed s/no-maybe/no/ .
# Hopefully this is no longer necessary... ?
# (sed was used to hide an incompatibility between llvm-config and clang)

LLVM_FLAGS := `llvm-config --cxxflags --ldflags --system-libs --libs all `

CFLAGS :=  $(LLVM_FLAGS) -rdynamic -O3


## More preliminaries
# See https://www.gnu.org/software/make/manual/html_node/Special-Targets.html
# In this makefile, we want to keep going even if we find errors
.IGNORE :

# Further, we do not want multiple things built at once, even if make called with -j2
.NOTPARALLEL :

# Tell make that the following "targets" are "phony"
# Cf. https://www.gnu.org/software/make/manual/html_node/Phony-Targets.html#Phony-Targets
.PHONY : all tests clean testclean

##  Now, the targets -- the things that will get made!

all: $(PROGS)

$(PROGS): %: %.cpp
	$(CC) -g $< $(CFLAGS) -o $@

clean: testclean
	$(RM) -fv *~ $(PROGS) ModuleMaker.bc fib test8 test8.o test9

testclean:
	rm -fv *~ *.output test8.o test9

testModuleMaker: ModuleMaker
	echo && echo "Testing ModuleMaker (expect 5)...\n"
	@./ModuleMaker >ModuleMaker.bc
	@lli ModuleMaker.bc || echo $$?

# WARNING: testFibonacci broken (with LLVM and clang 3.7.1)
testFibonacci: fibonacci
	echo && echo "\n===================> Testing fibonacci (expect 46368)...\n"
	-./fibonacci

test2: toy2 tests/test2.kal
	echo && echo "\n===================> Testing toy2.. (expecting 1 error)\n"
	-./toy2 <tests/test2.kal

test3: toy3 tests/test3.kal
	echo && echo "\n===================> Testing toy3...\n"
	-./toy3 <tests/test3.kal

test4: toy4 tests/test4.kal
	echo && echo "\n===================> Testing toy4...\n"
	-./toy4 <tests/test4.kal

test5: toy5 tests/test5.kal
	echo && echo "\n===================> Testing toy5...\n"
	-./toy5 <tests/test5.kal

test6: toy6 tests/test6.kal
	echo && echo "\n===================> Testing toy6...\n"
	-./toy6 <tests/test6.kal

test7: toy7 tests/test7.kal
	echo && echo "\n===================> Testing toy7...\n"
	-./toy7 <tests/test7.kal

test8: toy8 tests/test8.kal toy8.cpp
	echo && echo "\n===================> Testing toy8... (expect 42, of course)\n"
	-./toy8 <tests/test8.kal
	-clang++ tests/test8.cpp test8.o -o test8
	-./test8

test9: toy9 tests/test9.kal
	echo && echo "\n===================> Testing toy9...\n"
	-./toy9 <tests/test9.kal |& clang -x ir - -o test9
	-./test9

# WARNING: test9 removed since broken (with LLVM and clang <= 3.9)
# NOTE: fibonacci added BACK to tests since it's working again!
tests: testModuleMaker testFibonacci test2 test3 test4 test5 test6 test7 test8