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

# PUMA root
PUMA    ?= ../Puma

# default target
TARGET  ?= linux-release

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

# libxml2 settings
ifeq ($(_TARGET),win32)
	LIBXML2_INC := `mingw32-xml2-config --cflags` 
	LIBXML2_LIB := `mingw32-xml2-config --libs` -lwsock32
	EXT=.exe
else
	LIBXML2_INC := `xml2-config --cflags` 
	LIBXML2_LIB := `xml2-config --libs` 
endif

# linker settings
ifneq ($(_TARGET),macosx)
        ifeq ($(SHARED),)
                LDFLAGS += -static
        endif
	LDFLAGS += -Wl,-Bstatic -L$(PUMA)/lib/$(TARGET) -lPuma 
ifneq ($(SHARED),)
	LDFLAGS += -Wl,-Bdynamic
endif
endif

LDFLAGS += $(LIBXML2_LIB)

# compiler settings
ifeq ($(_TARGET),win32)
        CPPFLAGS += -I $(PUMA)/src/win32/regex
	RES := mingw32-windres
	RCFILES := win-ac++.rc
endif

CPPFLAGS += -I$(PUMA)/include $(LIBXML2_INC)

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





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

BINDIR := bin/$(TARGET)

#programm to compile
PROG := $(BINDIR)/ac++$(EXT)

CCSOURCES := $(wildcard *.cc) 

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

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

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

# ******************************
# *  E X A M P L E  / T E S T  *
# ******************************

# tool selection
export AC := $(shell pwd)/$(PROG)
MAKE ?= make

# ac++ settings for tests
ACFLAGS := -v 9

ACEXAMPLES = FigureObserver Instances Trace CFlow ID Action Cache IconCache \
	     Order


.PHONY: $(ACEXAMPLES) 


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

all: showtarget dirs $(PROG)

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




clean:  testclean exampleclean
	@echo Making it clean.
	@rm -rf  core core.* *~ $(PROG) $(OBJECTDIR) $(DEPDIR) $(ERROR_FILE)

dirs: $(DIRS)

test: $(PROG)$(EXT) testall

example: $(PROG)$(EXT) $(addsuffix .example_make, $(ACEXAMPLES))

examplerun: example $(addsuffix .example_run, $(ACEXAMPLES))

exampleclean:
	@rm -rf examples/*-out
	@rm -rf examples/*.acp

.PHONY: all test  clean  example  examplerun  exampleclean \
        showtarget



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




$(DIRS) :
	@mkdir -p $@



$(PROG): $(OBJECTS)
	@echo "Linking $@."
	@$(CXX) -o $@ $(CXXFLAGS) $^ $(LDFLAGS)


$(OBJECTDIR)/%.o : %.cc
	@echo Making object file $@.
	@$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c -o $@ $<

# -Wold-style-cast to find classic C casts "a = (A*)b;"

$(DEPDIR)/%.d: %.cc
	@echo Making dependency file $@.
	@mkdir -p $(DEPDIR)
	@$(CXX) $(CPPFLAGS) $(CXXFLAGS) -MM -MT $(OBJECTDIR)/$(<:%.cc=%.o) -MT $@ $< >$@ 

ifeq ($(_TARGET),win32)
$(OBJECTDIR)/%.o : %.rc
	@echo Compiling Windows resource file
	@$(RES) $(filter -D%,$(CPPFLAGS)) $< $@

endif

test%: 
	$(MAKE) -C tests -s  $*

%.example_make: %
	cd examples/$< ; \
	$(AC) $(ACFLAGS) -I. -p . -d ../$<-out \
	-r ../$<.acp ; \
	cp Makefile ../$<-out/Makefile; \
	cd ../..

%.example_run:	%
	@mkdir examples/$<-out || true
	cd examples/$<-out; $(MAKE); `find . -type f -perm -700`

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

