# Declaration of variables
C = gcc
COMMON_FLAGS = -Wall -MMD -fprofile-arcs -ftest-coverage
C_FLAGS = $(COMMON_FLAGS)
CC = g++
CC_FLAGS = $(COMMON_FLAGS) -std=c++17 -O0 -g
LD_FLAGS = -lgcov
INCLUDES =

# File names
EXEC = run
CSOURCES = $(wildcard *.c)
COBJECTS = $(CSOURCES:.c=.o)
SOURCES = $(wildcard *.cpp)
OBJECTS = $(SOURCES:.cpp=.o)

# Main target
$(EXEC): $(COBJECTS) $(OBJECTS)
	$(CC) $(COBJECTS) $(OBJECTS) -o $(EXEC) $(LD_FLAGS)

# To obtain object files
%.o: %.cpp
	$(CC) $(INCLUDES) $(CC_FLAGS) -o $@ -c $<

# To obtain object files
%.o: %.c
	$(C) $(INCLUDES) $(C_FLAGS) -o $@ -c $<

-include $(SOURCES:%.cpp=%.d)
-include $(CSOURCES:%.c=%.d)

# To generate the documentation
doc:
	doxygen Doxyfile

# To generate human-readable coverage data
cov:
	gcov *.gcda

# To remove all the files generated by gcov
cleancov:
	rm -f *.gcov *.gcda

# To remove generated files
clean: cleancov
	rm -f $(COBJECTS) $(OBJECTS) $(SOURCES:%.cpp=%.d) $(CSOURCES:%.c=%.d) *.gcno

cleaner: clean
	rm -f $(EXEC)
