# Adapt this file to your project as needed.
# CppUTest manual: https://cpputest.github.io/manual.html


############
# settings #
############

# CppUTest directory (path relative to this Makefile)
# Only necessary, if CppUTest was NOT system installed via e.g. apt-get.
CPPUTEST_HOME = CppUTest

# code directories
SRC_DIR = ./src
TEST_DIR = ./test

# file names
MAIN = $(SRC_DIR)/main
MODULE = $(SRC_DIR)/module
UNITTESTS = $(TEST_DIR)/ModuleTest
TEST = $(TEST_DIR)/AllTests

# general compiler options
CFLAGS  = -g
CFLAGS += -Wall
CFLAGS += -Wextra
#CFLAGS += -std=c99
#CFLAGS += -pedantic
#CFLAGS += -Werror
#CFLAGS += -Wshadow
#CFLAGS += -Wstrict-overflow

# compiler options for CppUTest
CPPFLAGS = -I$(CPPUTEST_HOME)/include

# Linker options that add CppUTest libraries to the linker flags.
LD_LIBRARIES  = -L$(CPPUTEST_HOME)/lib
LD_LIBRARIES += -lCppUTest
# This flag is only needed, if you want to use extensions such as mocking.
LD_LIBRARIES += -lCppUTestExt


#################
# build targets #
#################

# default target
all : $(MAIN).out $(TEST).out

# dependence on the include files
$(SRC_DIR)/%.o : $(SRC_DIR)/%.c $(SRC_DIR)/*.h
	$(CC) -o $@ $< -c $(CFLAGS)

# application
$(MAIN).out : $(MAIN).o $(MODULE).o
	$(CC) -o $@ $^ $(CFLAGS)

# unit tests
$(TEST_DIR)/%.o : $(TEST_DIR)/%.cc $(MODULE).o
	$(CXX) -o $@ $< -c $(CPPFLAGS) $(LD_LIBRARIES)

# test
$(TEST).out : $(TEST).o $(MODULE).o $(UNITTESTS).o
	$(CXX) -o $@ $^ $(CPPFLAGS) $(LD_LIBRARIES)


###############
# run targets #
###############

.PHONY: main test

# application
main : $(MAIN).out
	@$^

# unit tests
test : $(TEST).out
	@$^ -c


################
# misc targets #
################

.PHONY: clean format help

# remove all files generated by make
clean :
	rm -f $(MAIN).out $(TEST).out $(SRC_DIR)/*.o $(TEST_DIR)/*.o

# ensure consistent source code formatting
format :
	@astyle --options=.astylerc "./*.c" "./*.h" "./*.cc"

# display synopsis
help :
	@echo "make [all]     compile everything, i.e application and tests"
	@echo "make clean     remove all files generated by make"
	@echo ""
	@echo "make main      compile and run application"
	@echo "make test      compile and run tests"
	@echo ""
	@echo "make format    ensure consistent source code formatting"

