SHELL:=/bin/bash

LTO_PARAM:=
ifeq ($(USE_LTO),1)
LTO_PARAM:=-flto
endif

MARCH_PARAM:=
ifneq ($(USE_MARCH),)
MARCH_PARAM:=-march=$(USE_MARCH)
endif

define NEW_LINE


endef

export NEW_LINE

CXX_FLAGS := -Wall -pedantic -Wextra -Werror -Wshadow -Wpointer-arith -Wcast-qual -Wdelete-non-virtual-dtor -Wredundant-decls

INCLUDE_PATHS:=$(addprefix -I, $(realpath $(addsuffix /src, $(addprefix ../, $(LINK_LIBRARIES) $(LIBRARY)))) $(SYSTEM_INCLUDE_PATHS))

DEBUG_LINK_PATHS:=$(addsuffix /lib-debug, $(addprefix ../, $(LINK_LIBRARIES)))

LDFLAGS_DEBUG:=-Wl,--no-as-needed $(addprefix -L, $(DEBUG_LINK_PATHS)) -rdynamic $(addprefix -l, $(LINK_LIBRARIES) $(SYSTEM_LIBRARIES)) -Wl,-rpath,.

RELEASE_LINK_PATHS:=$(addsuffix /lib-release, $(addprefix ../, $(LINK_LIBRARIES)))

LDFLAGS_RELEASE:=-Wl,--no-as-needed $(addprefix -L, $(RELEASE_LINK_PATHS)) -rdynamic $(addprefix -l, $(LINK_LIBRARIES) $(SYSTEM_LIBRARIES)) $(LTO_PARAM) -Wl,-rpath,.

OBJECTS_DEBUG:=$(patsubst src/%.cpp,obj-debug/%.o, $(shell find src/ -name *cpp))

OBJECTS_RELEASE:=$(patsubst src/%.cpp,obj-release/%.o, $(shell find src/ -name *cpp))

.PHONY: all build-debug clean-debug doc

all:
	@echo "What to do master?"

FORCE:

# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# make subdir makefile

%/makefile: ../makefile-binary ../build.conf makefile makefile.conf
	if [ ! -w $(dir $@) ] && [ $(USE_RAMDISK) -eq 1 ]; then\
		ln -s /tmp/`date +'%s%N'`-$(dir $@) $(subst /,, $(dir $@)) 2>/dev/null;\
	fi;\
	if [ -L $(subst /,, $(dir $@)) ]; then\
		mkdir -p `readlink $(subst /,, $(dir $@))`;\
	else\
		mkdir -p $(dir $@);\
	fi
	cp ../makefile-rec $@

# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# final lib/bin construction

bin-debug/$(EXECUTABLE): obj-debug/ $(OBJECTS_DEBUG)
	if [ ! -w $(dir $@) ] && [ $(USE_RAMDISK) -eq 1 ]; then\
		ln -s /tmp/`date +'%s%N'`-$(dir $@) $(subst /,, $(dir $@)) 2>/dev/null;\
	fi;\
	if [ -L $(subst /,, $(dir $@)) ]; then\
		mkdir -p `readlink $(subst /,, $(dir $@))`;\
	else\
		mkdir -p $(dir $@);\
	fi
	$(CXX) $(OBJECTS_DEBUG) -o $@ $(LDFLAGS_DEBUG)

bin-release/$(EXECUTABLE): obj-release/ $(OBJECTS_RELEASE)
	if [ ! -w $(dir $@) ] && [ $(USE_RAMDISK) -eq 1 ]; then\
		ln -s /tmp/`date +'%s%N'`-$(dir $@) $(subst /,, $(dir $@)) 2>/dev/null;\
	fi;\
	if [ -L $(subst /,, $(dir $@)) ]; then\
		mkdir -p `readlink $(subst /,, $(dir $@))`;\
	else\
		mkdir -p $(dir $@);\
	fi
	$(CXX) $(OBJECTS_RELEASE) -o $@ $(LDFLAGS_RELEASE)

# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# subdir make calls

obj-debug/: FORCE | obj-debug/makefile
	$(MAKE) -C $@ OBJECTS_BASE_DIR=$(realpath obj-debug) SOURCES_BASE_DIR=$(realpath src) CXX_OTHER_FLAGS="-g -Og -DDEBUG $(CXX_FLAGS) $(INCLUDE_PATHS)"

obj-release/: FORCE | obj-release/makefile
	$(MAKE) -C $@ OBJECTS_BASE_DIR=$(realpath obj-release) SOURCES_BASE_DIR=$(realpath src) CXX_OTHER_FLAGS="-O3 -DNDEBUG -DRELEASE $(LTO_PARAM) $(MARCH_PARAM) $(CXX_FLAGS) $(INCLUDE_PATHS)"

# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# objects dependencies

$(OBJECTS_DEBUG): obj-debug/

$(OBJECTS_RELEASE): obj-release/

# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# standalone main targets

build-code-debug: bin-debug/$(EXECUTABLE)

build-code-release: bin-release/$(EXECUTABLE)

# -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# do all main targets

debug:
	$(MAKE) build-code-debug

release:
	$(MAKE) build-code-release

clean: clean-debug clean-release
	$(RM) -r doc

clean-debug:
	if [ -L obj-debug ]; then\
		$(RM) -r `readlink obj-debug`;\
	fi
	if [ -L bin-debug ]; then\
		$(RM) -r `readlink bin-debug`;\
	fi
	$(RM) -r *.o *.d bin-debug obj-debug

clean-release:
	if [ -L obj-release ]; then\
		$(RM) -r `readlink obj-release`;\
	fi
	if [ -L lib-release ]; then\
		$(RM) -r `readlink lib-release`;\
	fi
	$(RM) -r *.o *.d bin-release obj-release

doc:
	doxygen