#
# Makefile for C++
#
# Copyright (C) 2019-2020 Christos Choutouridis <christos@choutouridis.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

# ============== Project settings ==============
# Project's name
PROJECT         := kNN
# Excecutable's name
TARGET          := knn
# Source directories list(space seperated). Makefile-relative path, UNDER current directory.
# Note: To support parent relative paths uncomment abspath line in main body below 
SRC_DIR_LIST    := src
# Include directories list(space seperated). Makefile-relative path.
INC_DIR_LIST    := inc
# Exclude files list(space seperated). Filenames only.
# EXC_FILE_LIST := bad.cpp old.cpp

# Build directories
BUILD_DIR       := bin
OBJ_DIR         := $(BUILD_DIR)/obj
DEP_DIR         := $(BUILD_DIR)/.dep


# ========== Compiler settings ==========
# Compiler flags for debug and release
DEB_CFLAGS      := -DDEBUG -g3 -Wall -Wextra
REL_CFLAGS      := -Wall -Wextra -O2
# Pre-defines
# PRE_DEFS := MYCAB=1729 SUPER_MODE

# ============== Linker settings ==============
# Linker flags (example: -pthread -lm)
LDFLAGS         := -lm -lopenblas
# Map output file
MAP_FILE        := output.map
MAP_FLAG        := -Xlinker -Map=$(BUILD_DIR)/$(MAP_FILE)

# ============== Docker settings ==============
# We need:
#  - Bind the entire project directory(the dir that icludes all the code) as volume.
#  - In docker instance, change to working directory(where the makefile is).
DOCKER_VOL_DIR  := $${PWD}
DOCKER_WRK_DIR  :=
DOCKER_RUN      := docker run --rm
DOCKER_FLAGS    := -v $(DOCKER_VOL_DIR):/usr/src/$(PROJECT) -w /usr/src/$(PROJECT)/$(DOCKER_WRK_DIR)

# docker invoke mechanism (edit with care)
#   note:
#   Here, `DOCKER` variable is empty. Rules can assign `DOCKER := DOCKER_CMD` when docker
#   functionality is needed.
DOCKER_CMD      = $(DOCKER_RUN) $(DOCKER_FLAGS) $(IMAGE)
DOCKER          :=

# ============== Tool selection ==============
# compiler and compiler flags.
CSIZE           := size
CFLAGS          := $(DEB_CFLAGS)
CXX             := g++


#
# =========== Main body and Patterns ===========
#

# Uncomment to support Win file extension for local builds
#ifeq ($(OS), Windows_NT)
#	TARGET := $(TARGET).exe
#endif
INC     := $(foreach dir,$(INC_DIR_LIST),-I$(dir))
DEF     := $(foreach def,$(PRE_DEFS),-D$(def))
EXC     := $(foreach fil,$(EXC_FILE_LIST),                              \
               $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/$(fil))) \
           )
# source files. object and dependencies list
# recursive search into current and source directories
SRC     := $(wildcard *.cpp)
SRC     += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/*.cpp))
SRC     += $(foreach dir,$(SRC_DIR_LIST),$(wildcard $(dir)/**/*.cpp))
SRC     := $(filter-out $(EXC),$(SRC))
# Uncomment to support parent relative source paths
#SRC     := $(abspath $(SRC))

OBJ     := $(foreach file,$(SRC:%.cpp=%.o),$(OBJ_DIR)/$(file))
DEP     := $(foreach file,$(SRC:%.cpp=%.d),$(DEP_DIR)/$(file))


# Make Dependencies pattern.
# This little trick enables recompilation only when dependencies change
# and it does so for changes both in source AND header files ;)
# 
# It is based on Tom Tromey's method.
# 
# Invoke cpp to create makefile rules with dependencies for each source file
$(DEP_DIR)/%.d: %.cpp
	@mkdir -p $(@D)
	@$(DOCKER) $(CXX) -E $(CFLAGS) $(INC) $(DEF) -MM -MT $(OBJ_DIR)/$(<:.cpp=.o) -MF $@ $<

# objects depent on .cpp AND dependency files, which have an empty recipe 
$(OBJ_DIR)/%.o: %.cpp $(DEP_DIR)/%.d
	@mkdir -p $(@D)
	$(DOCKER) $(CXX) -c $(CFLAGS) $(INC) $(DEF) -o $@ $<

# empty recipe for dependency files. This prevents make errors
$(DEP):

# now include all dependencies, after all they are makefile dependency rules ;)
include $(wildcard $(DEP))

# main target rule
$(BUILD_DIR)/$(TARGET): $(OBJ)
	@mkdir -p $(@D)
	@echo Linking to target: $(TARGET)
	@echo $(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) '$$(OBJ)'
	@$(DOCKER) $(CXX) $(LDFLAGS) $(MAP_FLAG) -o $(@D)/$(TARGET) $(OBJ)
	@echo
	@echo Print size information
	@$(CSIZE) $(@D)/$(TARGET)
	@echo Done

.PHONY: clean
clean:
	@echo Cleaning build directories
	@rm -rf $(OBJ_DIR)
	@rm -rf $(DEP_DIR)
	@rm -rf $(BUILD_DIR)


#
# ================ Local build rules =================
# Use cases:
# - local builds in terminal
# 	example:
# 		make debug
# - build inside container
#   example in a yml:
#       image: gcc:8
#       build:
#          stage: build
#          - make release
#          ... etc
#       test:
#          stage: test
#          - bin/out
#          ... etc
#
debug: CFLAGS += $(DEB_FLAGS)
debug: $(BUILD_DIR)/$(TARGET)

release: CFLAGS += $(REL_FLAGS)
release: $(BUILD_DIR)/$(TARGET)

.PHONY: all
all: release

#
# ================ Docker based rules ================
# Use case:
# - build localy using via terminal or script in order
#   to compile for various compilers, versions, etc...
#   example in bash:
#       #!/bin/bash
#       for im in gcc:8 gcc:9 myImage; do
#          make IMAGE="$im" dock
#       done
# 
dock: DOCKER := $(DOCKER_CMD)
dock: $(BUILD_DIR)/$(TARGET)