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


# PUMA root
PUMA    ?= ../Puma

# default target
TARGET  ?= linux-release

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

# include the PUMA configuration variables
# => set _TARGET, RELEASE, CFLAGS, CXXFLAGS, CPPFLAGS, etc.
include $(PUMA)/vars.mk

#CXX  := /usr/bin/g++-3.3
#CXX := ag++  -p. -p../aspects --Xcompiler

# compiler settings
ifeq ($(_TARGET),win32)
        CXXFLAGS += -I $(PUMA)/src/win32/regex
	EXT :=.exe
	RES := mingw32-windres
endif

CXXFLAGS += $(CPPFLAGS) -I$(PUMA)/include
ifneq ($(_TARGET),macosx)
	ifeq ($(SHARED),)
		LDFLAGS += -static
	endif

LDFLAGS += -Wl,-Bstatic -L$(PUMA)/lib/$(TARGET) -lPuma

ifneq ($(SHARED),)
LDFLAGS += -Wl,-Bdynamic
endif

endif

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

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

BINDIR := bin/$(TARGET)
PROG := $(BINDIR)/ag++$(EXT)

CCSOURCES := $(wildcard *.cc) 

OBJECTDIR := ObjFiles/$(TARGET)
OBJECTS := $(addprefix $(OBJECTDIR)/,$(CCSOURCES:%.cc=%.o))

DEPDIR := DepFiles/$(TARGET)
DEPS := $(addprefix $(DEPDIR)/,$(CCSOURCES:%.cc=%.d))

ifeq ($(_TARGET),win32)
RCFILES := win-ag++.rc
OBJECTS += $(addprefix $(OBJECTDIR)/,$(RCFILES:%.rc=%.o))
DEPS += $(addprefix $(DEPDIR)/,$(RCFILES:%.rc=%.d))
endif


DIRS := $(OBJECTDIR) $(DEPDIR) $(BINDIR)



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

all: showtarget dirs $(AC_SRC_DIR)/$(PROG)

showtarget:
	@echo "---"
	@echo "Making AspectC++ for TARGET=$(TARGET)"
	@echo "  AC++     = $(ACXX) $(ACXXFLAGS) <objects> "
	@echo "  Compiler = $(CXX) $(CXXFLAGS)"
	@echo "  Linker   = $(CXX) $(CXXFLAGS) <objects> $(LDFLAGS)"
	@echo "---"

clean: 
	@echo -n "CLEAN    "
	rm -rf $(PROG) core core.* *~ $(DIRS)

cleanalltarget:
	rm -rf $(DIRS)


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

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


dirs:  $(DIRS)


.PHONY: all clean showtarget dirs 

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

$(PROG): $(OBJECTS) 
	@echo "LD        $@"
	@$(CXX) -o $@ $^ $(LDFLAGS)

$(AC_SRC_DIR)/bin/$(TARGET):
	@mkdir -p $@

$(AC_SRC_DIR)/$(PROG): $(PROG) $(AC_SRC_DIR)/bin/$(TARGET)
	@echo "COPY      $< to $@"
	@cp $< $@

$(OBJECTDIR)/%.o : %.cc 
	@echo "CC        $@"
	@$(CXX) $(CXXFLAGS) -c -o $@ $<

ifeq ($(_TARGET),win32)
$(OBJECTDIR)/%.o : %.rc
	@echo "RES       $@"
	@$(RES) $(filter -D%,$(CXXFLAGS)) $< $@

$(DEPDIR)/%.d: %.rc
	@echo "DEP       $@."
	@mkdir -p $(DEPDIR)
	@$(CXX) $(CXXFLAGS) -MM -MT $(OBJECTDIR)/$(notdir $(@:%.d=%.o)) -MT $@  -MF $@ -xc++ $< 
endif

$(DEPDIR)/%.d: %.cc 
	@echo "DEP       $@."
	@mkdir -p $(DEPDIR)
	@$(CXX) $(CXXFLAGS) -MM -MT $(OBJECTDIR)/$(notdir $(@:%.d=%.o)) -MT $@ -MF $@ $< 

$(DIRS):
	@mkdir -p $@


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

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

