# ******************************
# *       S E T T I N G S      *
# ******************************

# default target
TARGET  ?= linux-release

#AspectC++ source directory
AcSrcDir := ../AspectC++

# ACBase root
ACBASE ?= ACBase

# => set _TARGET, RELEASE, etc.
ifeq ($(filter %-release,$(TARGET)),)
RELEASE := debug
else
RELEASE := release
endif
_TARGET := $(patsubst %-release,%,$(TARGET))
PLATFORM := $(shell echo $(_TARGET) | cut -d _ -f1)
ARCH := $(shell echo $(_TARGET) | cut -d _ -f2-)

# if this is a release build, set the appropriate (specific) flags
ifeq ($(RELEASE),debug)
CFLAGS := -O0 -ggdb3 -fno-inline -fno-default-inline
else
CFLAGS   := -O2 -ggdb3
CPPFLAGS += -DNDEBUG
endif
CFLAGS   += -Wall -pipe -Wno-deprecated-declarations
# if a compile target is specified, add the corresponding flag
ifneq ($(COMPILE_TARGET),)
CFLAGS += -target $(COMPILE_TARGET)
endif
CXXFLAGS += $(CFLAGS) # -fno-rtti -fno-exceptions
CPPFLAGS += $(CPP_OPTFLAGS)

# check whether we compile for Windows: TARGET-Variable starts with 'win'
ifneq ($(filter win%,$(TARGET)),)
	WIN := yes
endif

# compiler settings

ifeq ($(WIN),yes)
	BinExt := .exe
	RES := windres
	RCFILES := win-ag++.rc
endif

CXXFLAGS += $(CPPFLAGS) -std=gnu++17

ifneq ($(PLATFORM),macos)
	ifeq ($(SHARED),)
		LDFLAGS += -static
	endif
endif

ifneq ($(PROFILING),)
	LDFLAGS += -lc_p -lm_p 
	CXXFLAGS += -pg
endif

# use libacbase
LDFLAGS  += -L$(ACBASE)/lib/$(TARGET) -lacbase
ACBASELIB := $(ACBASE)/lib/$(TARGET)/libacbase.a

# ******************************
# *       S O U R C E S        *
# ******************************

SourceFiles := $(wildcard *.cc) 

BuildDir ?= build/$(TARGET)

ObjDir := $(BuildDir)/obj
ObjectFiles := $(addprefix $(ObjDir)/,$(SourceFiles:%.cc=%.o))

DepDir := $(BuildDir)/dep
DepFiles := $(addprefix $(DepDir)/,$(SourceFiles:%.cc=%.d))

BinDir := $(BuildDir)/bin
BinName := ag++$(BinExt)
BinFile := $(BinDir)/$(BinName)


ifeq ($(WIN),yes)
ObjectFiles += $(addprefix $(ObjDir)/,$(RCFILES:%.rc=%.o))
DepFiles += $(addprefix $(DepDir)/,$(RCFILES:%.rc=%.d))
endif




# ******************************
# *     T A R G E T S          *
# ******************************

define _show_target
    @echo "---"
	@echo "Build commands for TARGET=$(TARGET)"
	@echo "  Compiler = $(CXX) $(CXXFLAGS)"
	@echo "  Linker   = $(CXX) $(CXXFLAGS) <objects> $(LDFLAGS)"
    @echo "---"
endef

all: 
	@$(MAKE) -C ACBase
	@$(call _show_target)
	@$(MAKE) $(AcSrcDir)/bin/$(TARGET)/$(BinName)

strip: $(AcSrcDir)/bin/$(TARGET)/$(BinName)
	@strip $<

showtarget:
	@$(call _show_target)

help:
	@echo "  help:          show this help"
	@echo "  all (default): compile, link and copy"
	@echo "  copy:          copy executable into AspectC++ bin folder"
	@echo "  test :         run tests"
	@echo "  test_<XX>:     run test number <XX>"
	@echo "  doc:           build documentation"
	@echo "  showtarget:    show compiler options for selected target"
	@echo "  clean:         remove intermediate files of current TARGET"
	@echo "  distclean:     clean all generated files and directories"


clean: 
	@$(MAKE) -C ACBase clean
	@echo -n "CLEAN    "
	rm -rf $(BuildDir) core core.* 
	@make -C doc clean

distclean: clean
 

test:	all
	@export TARGET=$(TARGET);export EXT=$(EXT);cd tests;./run_tests.sh

test_%: all
	@export TARGET=$(TARGET);export EXT=$(EXT);cd tests; ./run_tests.sh $@

doc:
	@make -C doc


.PHONY: all clean distclean showtarget doc

# ******************************
# *       R U L E S            *
# ******************************

$(BinFile): $(ObjectFiles) $(ACBASELIB)
	@echo "LD        $@"
	@mkdir -p $(BinDir)
	@$(CXX) -o $@ $(CXXFLAGS) $^ $(LDFLAGS)

$(AcSrcDir)/bin/$(TARGET)/$(BinName): $(BinFile)
	@echo "COPY      $< to $@"
	@mkdir -p $(@D)
	@cp $< $@

$(ObjDir)/%.o : %.cc
	@echo "CC        $<"
	@mkdir -p $(DepDir)
	@mkdir -p $(ObjDir)
	@$(CXX) $(CXXFLAGS) -MMD -MP -MT $@ -MT $(DepDir)/$(<:%.cc=%.d) -MF $(DepDir)/$(<:%.cc=%.d) -c -o $@ $< 
# @echo "CC        $@"
#	@mkdir -p $(ObjDir)
#	@$(CXX) $(CXXFLAGS) -c -o $@ $<

ifeq ($(WIN),yes)
$(ObjDir)/%.o : %.rc
	@echo "DEP       $@."
	@mkdir -p $(DepDir)
	@$(CXX) $(CXXFLAGS) -MM -MP -MT $@ -MT $(DepDir)/$(<:%.cc=%.d) -MF $(DepDir)/$(<:%.cc=%.d) -xc++ $< 
	@echo "RES       $@"
	@mkdir -p $(ObjDir)
	@$(RES) $(filter -D%,$(CXXFLAGS)) $< $@
endif

ifeq (,$(findstring clean,$(MAKECMDGOALS)))
ifeq (,$(findstring help,$(MAKECMDGOALS)))
-include $(DepFiles)
endif
endif

.SUFFIXES: .cc .o .h .rc .mk $(SUFFIXES)
