# Makefile
#

DATETIME     := `date +'%y%m%d-%H%M'`
MAKEFILEPATH := $(strip $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))))
ROOTPATH     := $(shell git rev-parse --show-toplevel)
COMMANDLINE  := $(shell ps -o args= $$PPID)

ARCHS        := $(shell ls $(ROOTPATH)/Makefiles/Makefile.conf.* | grep -v base | sed 's/.*Makefile\.conf\.//g')
ARCHS        += any
ARCH         := $(filter $(ARCHS),$(MAKECMDGOALS))
MAKECMDGOALS := $(filter-out $(ARCH),$(MAKECMDGOALS))
ARCHS        := $(filter-out any,$(ARCHS))

ifeq ($(ARCH),)
ARCH         := x86-64
endif

ifeq (,$(findstring -S,$(COMMANDLINE)))
ifneq ($(ARCH),)
$(info ARCH: $(ARCH))
endif
endif

-include $(ROOTPATH)/Makefiles/Makefile.conf.$(ARCH)
-include $(ROOTPATH)/Makefiles/Makefile.conf.base
-include $(ROOTPATH)/Makefile.conf

PID          := $(shell echo $$$$)
PREFIX        = nice -n 19 /usr/bin/time --format='%E (%U)' -o /dev/shm/t-$(PID)
POSTFIX       = && cat /dev/shm/t-$(PID) | xargs -I "%" echo -en '\033[1A\033[100D\033[60C' "%\n" && rm -f /dev/shm/t-$(PID)

ARNAME        = $(notdir $(CURDIR)).a.$(ARCH)_

SUBDIRS      := $(wildcard */.)
SUBDIRS      := $(foreach dir,$(SUBDIRS),$(subst /.,,$(dir)))
FILTER        = crash% doc% include% lib% Libraries% Makefiles%
SUBDIRS      := $(filter-out $(FILTER),$(SUBDIRS))
SUBDIRARS     = $(foreach dir,$(SUBDIRS),$(dir)/$(dir).a)

LOCALSOURCES  = $(wildcard *.cpp *.cc)
LOCALOBJECTS := $(LOCALSOURCES:.cpp=.o.$(ARCH))
LOCALOBJECTS := $(LOCALOBJECTS:.cc=.o.$(ARCH))
LOCALDEPENDS  = $(LOCALOBJECTS:.o.$(ARCH)=.d.$(ARCH))
SOURCES       = $(wildcard *.cpp */*.cpp */*/*.cpp */*/*/*.cpp)
FILTER        = Libraries%
SOURCES      := $(filter-out $(FILTER),$(SOURCES))
OBJECTS       = $(SOURCES:.cpp=.o.$(ARCH))
DEPENDS       = $(OBJECTS:.o.$(ARCH)=.d.$(ARCH))
TARGETS       =

define build_target_library_arch
	$(eval $@_DATETIME := $(DATETIME))
	@echo -n " [AR] $(1)\n"
	@$(PREFIX) $(AR) -crs $(1) $(2) $(POSTFIX)
	@mkdir -p $(ROOTPATH)/lib/$(ARCH)
	@mv $(1) $$(echo $(ROOTPATH)/lib/$(ARCH)/lib$(1) | sed 's/\.$(ARCH)//g')
endef

define build_target_arch
	$(eval $@_DATETIME := $(DATETIME))
	@echo -n " [LD] $(1)\n"
	@$(PREFIX) $(CC) -o $(1) $(2) $(LFLAGS) $(CFLAGS) $(POSTFIX)
	@$(OBJCOPY) --only-keep-debug $(1) $(DEBUGDIR)/$(1)-$($@_DATETIME).debug
	@$(STRIP) $(SFLAGS) $(1)
	@$(OBJCOPY) --add-gnu-debuglink="$(DEBUGDIR)/$(1)-$($@_DATETIME).debug" $(1)
	@chmod -x $(DEBUGDIR)/$(1)-$($@_DATETIME).debug
endef

define build_target
	@if [ "$(ARCH)" = "any" ]; then \
		for arch in $(ARCHS); do \
			echo "ARCH: $$arch"; \
			$(MAKE) -s -S $(1) $$arch; \
		done; \
		true; \
	else \
		$(MAKE) -s -S $(1).$(ARCH) $(ARCH); \
	fi
endef

$(LOCALOBJECTS): | $(SUBDIRS)
$(OBJECTS): | $(SUBDIRS)

artifacts: $(LOCALOBJECTS)

.DEFAULT_GOAL := artifacts

-include Makefile.target

ifneq ($(MAKECMDGOALS),clean)
-include $(LOCALDEPENDS)
endif

%.d.$(ARCH): %.cpp
	@if [ "$(ARCH)" != "any" ]; then \
		depth=$(MAKELEVEL); \
		while [ $${depth} -gt 0 ] ; do \
			echo -n " "; \
			depth=`expr $$depth - 1`; \
		done; \
		true; \
		echo -n " [MM] $@\n"; \
		$(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
	fi

%.o.$(ARCH): %.cpp
	@depth=$(MAKELEVEL); \
	while [ $${depth} -gt 0 ] ; do \
		echo -n " "; \
		depth=`expr $$depth - 1`; \
	done; \
	true
	@echo -n " [CC] $@\n"
	@$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)

%.d.$(ARCH): %.cc
	@if [ "$(ARCH)" != "any" ]; then \
		depth=$(MAKELEVEL); \
		while [ $${depth} -gt 0 ] ; do \
			echo -n " "; \
			depth=`expr $$depth - 1`; \
		done; \
		true; \
		echo -n " [MM] $@\n"; \
		$(PREFIX) $(CC) $(CFLAGS) $< -MM -MT $(@:.d.$(ARCH)=.o.$(ARCH)) >$@ $(POSTFIX); \
	fi

%.o.$(ARCH): %.cc
	@depth=$(MAKELEVEL); \
	while [ $${depth} -gt 0 ] ; do \
		echo -n " "; \
		depth=`expr $$depth - 1`; \
	done; \
	true
	@echo -n " [CC] $@\n"
	@$(PREFIX) $(CC) -c $(CFLAGS) $< -o $@ $(POSTFIX)

$(ARNAME): $(SUBDIRS) $(LOCALOBJECTS)
	@depth=$(MAKELEVEL); \
	while [ $${depth} -gt 0 ] ; do \
		echo -n " "; \
		depth=`expr $$depth - 1`; \
	done; \
	true
	@echo -n " [AR] $@\n"
	@$(PREFIX) $(AR) rcuT $@ $(LOCALOBJECTS) $(SUBDIRARS) $(POSTFIX)

.PHONY: $(SUBDIRS)
$(SUBDIRS):
	@if [ -e "$@/Makefile" ]; then \
		depth=$(MAKELEVEL); \
		while [ $${depth} -gt 0 ] ; do \
			echo -n " "; \
			depth=`expr $$depth - 1`; \
		done; \
		true; \
		echo " <$@>"; \
		$(MAKE) -s -S -C $@ artifacts $(ARCH); \
		depth=$(MAKELEVEL); \
		while [ $${depth} -gt 0 ] ; do \
			echo -n " "; \
			depth=`expr $$depth - 1`; \
		done; \
		true; \
		echo " </$@>"; \
	fi

.PHONY: all
all: $(TARGETS)

.PHONY: clean
clean:
	@if [ "$(ARCH)" = "any" ]; then \
		for arch in $(ARCHS); do \
			echo "ARCH: $$arch"; \
			$(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
		done; \
		true; \
	else \
		for dir in $(SUBDIRS); do \
			if [ -e "$$dir/Makefile" ]; then \
				depth=$(MAKELEVEL); \
				while [ $${depth} -gt 0 ] ; do \
					echo -n " "; \
					depth=`expr $$depth - 1`; \
				done; \
				echo " <$$dir>"; \
				$(MAKE) -s -S -C $$dir $@ $(ARCH); \
				while [ $${depth} -gt 0 ] ; do \
					echo -n " "; \
					depth=`expr $$depth - 1`; \
				done; \
				echo " </$$dir>"; \
			fi \
		done; \
		for target in $(TARGETS); do \
			rm -f $$target.$(ARCH); \
		done; \
		rm -f lib/$(ARCH)/*; \
		true; \
	fi
	@rm -f $(LOCALOBJECTS) $(LOCALDEPENDS) || true

.PHONY: cleandeps
cleandeps:
	@if [ "$(ARCH)" = "any" ]; then \
		for arch in $(ARCHS); do \
			echo "ARCH: $$arch"; \
			$(MAKE) -s -S $(MAKECMDGOALS) $$arch; \
		done; \
		true; \
	else \
		for dir in $(SUBDIRS); do \
			if [ -e "$$dir/Makefile" ]; then \
				depth=$(MAKELEVEL); \
				while [ $${depth} -gt 0 ] ; do \
					echo -n " "; \
					depth=`expr $$depth - 1`; \
				done; \
				echo " <$$dir>"; \
				$(MAKE) -s -S -C $$dir $@ $(ARCH); \
				while [ $${depth} -gt 0 ] ; do \
					echo -n " "; \
					depth=`expr $$depth - 1`; \
				done; \
				echo " </$$dir>"; \
			fi \
		done; \
		for target in $(TARGETS); do \
			rm -f $$target.$(ARCH); \
		done; \
		true; \
	fi
	@rm -f $(LOCALDEPENDS) || true

.PHONY: $(ARCH)
$(ARCH):
	@if [ "$(words $(MAKECMDGOALS))" = "0" ]; then \
		if [ "$(ARCH)" = "any" ]; then \
			for arch in $(ARCHS); do \
				echo "ARCH: $$arch"; \
				$(MAKE) -s -S $(.DEFAULT_GOAL) $$arch; \
			done; \
			true; \
		else \
			$(MAKE) -s -S $(.DEFAULT_GOAL) $(ARCH); \
		fi \
	fi
