# This Makefile made available to his students by 
# Prof. Ronald Moore  
#     https://fbi.h-da.de/personen/ronald-moore/  
#     mailto:ronald.moore@h-da.de
# with no warranties whatsoever


PROGS := halfbaker
SOURCES := main.cpp lexer.cpp parser.cpp astree.cpp
OBJS = $(SOURCES:.cpp=.o)

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

## Add your own CFLAGS if you find them necessary... such as -O3 or so... 
##    -g for debugging
##    -std=<whatever> to select the right C++ Version
##    -fmessage-length=0 disallows line wrapping in error messages
##    (helps some IDEs (still?))
# CPPFLAGS :=  -g -std=c++17 -Wall -fmessage-length=0
CPPFLAGS :=  -g -std=c++20 -Wall

# Should also work with c++17 (c++20 not required)

## 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 :

# 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 clean tests

# This absolutely needs to be the first target (so to be the default target)
all: $(PROGS)

#  Some of the "Automatic Variables" that can be used in Makefiles.
#      Cf. https://www.gnu.org/software/make/manual/  - particularly
# https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html#Automatic-Variables
# $@ = The filename representing the target.
# $< = The filename of the first prerequisite.
# $(*F) = The stem of the filename of the target (i.e. without .o, .cpp...)
# $^ = The names of all the prerequisites, with spaces between them.

## Following magic is used to figure out which dot cpp files depend
# on which headers (dot h files) -- automatically (so that we recompile
# only the ncessary dot cpp files when a header is maodified).
# Magic taken from 
# http://make.mad-scientist.net/papers/advanced-auto-dependency-generation/
#  ... and then fixed, and fixed, and fixed some more.
DEPDIR := .deps
# From URL: DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d
# Simpler, better...
DEPFLAGS = -MMD -MP -MF $(DEPDIR)/$*.d

DEPS := $(OBJS:%.o=$(DEPDIR)/%.d)

#include the deps (minus says don't complain if they don't)
-include $(DEPS)

# %.o  $(DEPDIR)/%.d : %.cpp $(DEPDIR)
%.o : %.cpp $(DEPDIR)
	$(CC) -c $(CPPFLAGS) $(DEPFLAGS) -o $*.o $*.cpp

# Make depdir if it doesn't exist... 
$(DEPDIR): ; @mkdir -p $@

# generate dep files. Note missing "-o" to keep from generating them too
# $(DEPDIR)/%.d :  $(DEPDIR)
#	$(CC) -c $(CPPFLAGS) $(DEPFLAGS) $*.cpp

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

$(PROGS): $(OBJS) 
	$(CC) $(CPPFLAGS) $(OBJS) $(LIBS) -o $@

clean: 
	$(RM) -v *~ *.o $(PROGS) tmp.txt
	$(RM) -fvr $(DEPDIR)
	# starting recursive clean...
	cd tests && $(MAKE) clean

tests: $(PROGS)
	# Going to the tests directory for testing
	cd tests && $(MAKE) tests