#
#
#

SHELL	:= /bin/sh

TOP	:= $(PWD)

CROSS	=
CC	= $(CROSS)gcc
LD	= $(CROSS)ld
YACC	= bison
YFLAGS	= -d -y
LEX	= flex
LFLAGS	=
AR	= ar
ARFLAGS	= rcs
OBJCOPY	= $(CROSS)objcopy

# Target name for per target flags
basetarget = $(basename $@)

CFLAGS	+= -g3 -Wall -Wextra -Werror \
	-Wno-unused-parameter -Wno-sign-compare -Wno-format
#CFLAGS	+= -O2 -D_FORTIFY_SOURCE=2
#CFLAGS	+= -fstack-protector-strong -fstack-clash-protection
#CFLAGS	+= -Wformat -Werror=format-security
#CFLAGS	+= -fcf-protection
#CFLAGS	+= -D_FILE_OFFSET_BITS=64 -D_TIME_BITS=64
#CFLAGS	+= -D_GNU_SOURCE
#CFLAGS	+= -Wp,-Wunused-macros
#CFLAGS	+= -ftrivial-auto-var-init=zero
#CFLAGS	+= -fPIE -pie
#CFLAGS	+= -fhardened
# Per target CFLAGS
CFLAGS	+= $(CFLAGS_$(basetarget).o)

#LDFLAGS	+= -Wl,-z,defs,--as-needed
#LDFLAGS	+= -Wl,-z,relro
#LDFLAGS	+= -Wl,-z,now
# Per target LDFLAGS/LIBS
LDFLAGS	+= $(LDFLAGS_$@)
LIBS	+= $(LIBS_$@)

# gcc sanitizers
ifeq ($(ASAN),1)
CFLAGS	+= -fsanitize=address,leak,undefined
endif

AFLAGS	= $(CFLAGS) -D__ASSEMBLY__
# .S default is not noexecstack
AFLAGS	+= -Wa,--noexecstack
AFLAGS	+= $(AFLAGS_$(basetarget).o)

export SHELL TOP CC LD CFLAGS LDFLAGS

# binary or shared library
all-bin		:=
# static library
all-static	:=

all-bin		+= cache-test
cache-test-objs	:= main.o libklib/slab.o

CFLAGS		+= -I$(TOP) 

#all-bin			+= libtest2.so
#libtest2.so-objs	:= test2.o
#CFLAGS_test2.o		+= -fPIC -shared
#LDFLAGS_libtest2.so	+= $(CFLAGS_test2.o) -Wl,-soname,libtest2.so.1

#all-static	+= libtest.a
#libtest.a-objs	:= test.o test2.o

# objects dependency
ALL_TARGETS	:= $(all-bin) $(all-static)
ALL_OBJS	:=

.PHONY: all
all: $(ALL_TARGETS)

define dep_template =
$(1): $$($(1)-objs)
ALL_OBJS += $$($(1)-objs)
endef
$(foreach bin,$(ALL_TARGETS),$(eval $(call dep_template,$(bin))))

# directories includes objects
OBJS_DIRS	:= $(sort $(dir $(ALL_OBJS)))

# dependency generation
DEPDIR    := .deps
DEP_FILES := $(foreach f,$(ALL_OBJS),$(dir $f)$(DEPDIR)/$(notdir $f).d)
DEP_DIRS  := $(addsuffix $(DEPDIR),$(OBJS_DIRS))
MISSING_DEP_DIRS := $(filter-out $(wildcard $(DEP_DIRS)),$(DEP_DIRS))

$(DEP_DIRS):
	$(Q)mkdir -p $@

DEP_FILE = $(dir $@)$(DEPDIR)/$(notdir $@).d
DEP_ARGS = -MF $(DEP_FILE) -MP -MMD
# Take advantage of gcc's on-the-fly dependency generation
# See <http://gcc.gnu.org/gcc-3.0/features.html>.
DEP_FILES_PRESENT := $(wildcard $(DEP_FILES))
-include $(DEP_FILES_PRESENT)

# verbose or quiet
ifeq ($(V),1)
Q :=
cmd =	$(2)
else
Q :=	@
cmd =	@echo "  "$(1); $(2)
endif

FORCE:
#	$(MAKE) -C lib

# binary or shared library rules
$(all-bin):
	$(call cmd,"LINK      $@",\
		$(CC) $(CFLAGS) $(LDFLAGS) $^ $(LIBS) -o $@)

%.o: %.S $(MISSING_DEP_DIRS)
	$(call cmd,"CC        $@",\
		$(CC) $(DEP_ARGS) $(AFLAGS) -c -o $@ $<)

%.o: %.c $(MISSING_DEP_DIRS)
	$(call cmd,"CC        $@",\
		$(CC) $(DEP_ARGS) $(CFLAGS) -c -o $@ $<)

# static library rules
$(all-static):
	$(call cmd,"AR        $@",\
		rm -f $@ && $(AR) $(ARFLAGS) $@ $^)

.PHONY: clean
clean:
	$(call cmd,"CLEAN")
	$(Q)rm -f $(ALL_TARGETS) $(ALL_OBJS)
	$(Q)rm -rf $(DEP_DIRS)
