# documentation here
# https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html
# http://www.partow.net/programming/makefile/index.html

CXX = gcc
GCXX_DEBUG = -ggdb3 -DDEBUG -g -fstack-protector-all -Wstack-protector -fno-omit-frame-pointer
CFLAGS = -pedantic-errors -Wall -Wextra --std=gnu18 # -Werror
LDFLAGS = # $(shell pkg-config --libs libcrypto jsoncpp)
INCLUDE = -Iinclude/

# path #
BIN_DIR = ./bin
OBJ_DIR = ./obj
SRC_DIR = ./src
SRC_EXT = .c
TARGET = out.ex

# code lists #
# List all source files in the source directory
SRC :=								   	\
   $(wildcard $(SRC_DIR)/module1/*$(SRC_EXT))	\
   $(wildcard $(SRC_DIR)/*$(SRC_EXT))         	\

# Set the object file names, with the source directory stripped
# from the path, and the build path prepended in its place
OBJECTS = $(SRC:$(SRC_DIR)/%.$(SRC_EXT)=$(OBJ_DIR)/%.o)

all: $(BIN_DIR)/$(TARGET)

$(OBJ_DIR)/%.o: %$(SRC_EXT)
	$(CXX) $(CFLAGS) $(INCLUDE) -c $< -o $@ $(LDFLAGS)

$(BIN_DIR)/$(TARGET): $(OBJECTS)
	$(CXX) $(CFLAGS) -o $(BIN_DIR)/$(TARGET) $^ $(LDFLAGS)

.PHONY: all debug release valgrind clean markdown doxygenconf

debug: CFLAGS += $(GCXX_DEBUG)
debug: all

release: CFLAGS += -O2
release: all

# debug target
valgrind:
	@valgrind --leak-check=full --show-leak-kinds=all --trace-children=no --track-origins=yes $(BIN_DIR)/$(TARGET) /bin/ls -la /bin

run:
	$(BIN_DIR)/$(TARGET) /bin/ls -la /

clean:
	@rm -f $(OBJ_DIR)/*.o
	@rm -f $(BIN_DIR)/*.ex

markdown:
	@pandoc --defaults=pandoc-config.yaml

doxygenconf:
	@doxygen -g doxygen.conf

doc: doxygen.conf
	@doxygen doxygen.conf

busybox_setup:
	@tar xvf alpine-minirootfs-3.12.0-x86.tar.gz --directory=./busybox
