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

# AspectC++ supports alternative frontends: Puma or Clang
# It can be configured the 'make FRONTEND=...'. Default is Puma.
FRONTEND ?= Puma

# PUMA root
PUMA    ?= ../Puma

# LLVM root
LLVM    ?= ../llvm

# 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`
	EXT=.exe
else
	LIBXML2_INC := `xml2-config --cflags` 
	LIBXML2_LIB := `xml2-config --libs` 
endif

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

LDFLAGS += $(LIBXML2_LIB)
ifneq ($(FRONTEND),Puma)
  LDFLAGS += -L$(LLVM)/Release+Asserts/lib -lclangRewriteFrontend -lclangRewriteCore -lclangFrontend -lclangSerialization -lclangDriver -lclangParse -lclangSema -lclangAnalysis -lclangEdit -lclangAST -lclangLex -lclangBasic -lLLVMAsmParser -lLLVMMCParser -lLLVMBitReader -lLLVMTransformUtils -lLLVMCore -lLLVMMC -lLLVMOption -lLLVMSupport -ldl -lpthread -lz
endif

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

CPPFLAGS += -I$(PUMA)/extern -I$(PUMA)/include $(LIBXML2_INC)
ifeq ($(FRONTEND),Puma)
  CPPFLAGS += -DFRONTEND_PUMA
else
  CPPFLAGS += -I$(LLVM)/include -I$(LLVM)/tools/clang/include -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
  CPPFLAGS += -DFRONTEND_CLANG
  CXXFLAGS += -Wno-strict-aliasing # hide annoying warning in Clang header 
endif

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

ifneq ($(PROFILING),)
	CPPFLAGS += -DPROFILING
endif

# use libacmodel
LDFLAGS  += -LACModel/lib/$(TARGET) -lacmodel 


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

BINDIR := bin/$(TARGET)

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

# sources; front-end specific files for the wrong frontend are filtered out
CCSOURCES := $(wildcard *.cc)
ifeq ($(FRONTEND),Puma)
CCSOURCES := $(filter-out Clang%.cc, $(CCSOURCES))
else
CCSOURCES := $(filter-out Puma%.cc, $(CCSOURCES))
endif

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 := -k -v 9

ACEXAMPLES = coverage helloworld modules profiling singleton threads


.PHONY: $(ACEXAMPLES) 


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

all: libacmodel showtarget $(PROG)
#.NOTPARALLEL: all

strip: $(PROG)
	@$(STRIP) $<

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.
	@$(MAKE) -C ACModel clean
	@rm -rf  core core.* *~ $(PROG) $(OBJECTDIR) $(DEPDIR) $(ERROR_FILE)

cleanall:	doxygen-clean clean

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

doxygen:
	@echo "Making AspectC++ documentation with doxygen"
	@doxygen doc/doxygen.conf && echo "=> see doc/doxygen/html/index.html"

doxygen-clean:
	@rm -rf doc/doxygen

regressiontest:
	rm -rf RegressionTests.tar.bz2 RegressionTestsTmp
	mkdir RegressionTestsTmp
	svn export tests/ RegressionTestsTmp/tests/
	sed -i '1iREGRESSIONTEST := 1' RegressionTestsTmp/tests/Makefile
	cd RegressionTestsTmp && tar cvfj RegressionTests.tar.bz2 tests/
	mv RegressionTestsTmp/RegressionTests.tar.bz2 .
	cd ..
	rm -rf RegressionTestsTmp

.PHONY: all test clean cleanall example examplerun exampleclean \
        showtarget doxygen doxygen-clean libacmodel



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




$(DIRS) :
	@mkdir -p $@

libacmodel :
	@$(MAKE) -C ACModel

$(PROG): $(OBJECTS) | libacmodel
	@echo "Linking $@."
	@mkdir -p $(BINDIR)
	@$(CXX) -o $@ $(CXXFLAGS) $^ $(LDFLAGS)


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

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

ifeq ($(_TARGET),win32)
$(OBJECTDIR)/%.o : %.rc
	@echo Compiling Windows resource file
	@mkdir -p $(OBJECTDIR)
	@$(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 ../$<-out; g++ -o $< *.cc -lpthread

%.example_run:	%.example_make
	@echo ""
	@echo ---- Running $* ----
	@cd examples/$*-out; `find . -type f -perm -700`

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