CXX_STD=c++1y
GCC_PATH=/usr/local/gcc
GTEST_ROOT=/opt/googletest
GMOCK_ROOT=/opt/googlemock

WITH_GTEST ?= 0
WITH_CPPUT ?= 0
WITH_BUTF ?= 0

BOOST_ROOT=$(GCC_PATH)

ifeq ($(BOOST_ROOT),$(GCC_PATH))
BOOST_IPATH=${GCC_PATH}/include
else # BOOST_ROOT
ifeq ($(BOOST_ROOT),/opt/boost)
BOOST_IPATH=${BOOST_ROOT}
BOOST_LDPATH=${BOOST_ROOT}/bin.v2/libs
BOOST_TOOLCHAIN=gcc-$(GCC_VERSION)/debug
BOOST_LDFLAGS= \
    $(BOOST_MODULES:%=-L$(BOOST_LDPATH)/%/$(BOOST_TOOLCHAIN)) \
    $(BOOST_MODULES:%=-Wl,-rpath,$(BOOST_LDPATH)/%/$(BOOST_TOOLCHAIN))
else
BOOST_IPATH=${BOOST_ROOT}/include
BOOST_LDFLAGS=L$(BOOST_ROOT)/lib -Wl,-rpath,$(BOOST_ROOT)/lib
endif # BOOST_ROOT
endif # BOOST_ROOT
BOOST_LDLIBS=$(BOOST_MODULES:%=-lboost_%)

CPPUNIT_PATH=$(GCC_PATH)
CPPUT_LDLIBS=-lcppunit

WITH_BOOST ?= $(WITH_BUTF)
ifneq ($(WITH_BOOST),0)
CPPFLAGS += -I$(BOOST_IPATH)
LDFLAGS+=$(BOOST_LDFLAGS)
LDLIBS+=$(BOOST_LDLIBS)
endif # WITH_BOOST

WARN_CXXFLAGS=-Wall -Werror
CXX=$(GCC_PATH)/bin/g++
OPT_CXXFLAGS=-g -O2
CPPFLAGS=-I../include -DWITH_BOOST=$(WITH_BOOST) $(AUX_CPPFLAGS)

CXXFLAGS=-std=$(CXX_STD) $(WARN_CXXFLAGS) $(OPT_CXXFLAGS) $(AUX_CXXFLAGS)
LDFLAGS=-Wl,-rpath,$(GCC_PATH)/lib64:$(GCC_PATH)/lib32:$(GCC_PATH)/lib
LINK.o=$(LINK.cc)

CLEAN ?=
REALCLEAN ?=

ifneq ($(WITH_GTEST), 0)
CXXFLAGS += -pthread
CPPFLAGS += -I$(GTEST_ROOT)/include -I$(GMOCK_ROOT)/include

GT_MODULES=eval
GT_LDLIBS=libgtest.a
GT_DEPS=gt_main.o $(GT_LDLIBS)

CLEAN += $(GT_MODULES:%=gt_%.o)
REALCLEAN += $(GT_MODULES:%=gt_%)

REALCLEAN += gt
gt: $(foreach m,$(GT_MODULES),gt_$(m).o) $(GT_DEPS)
	$(LINK.cc) -o $@ $^

REALCLEAN += $(GT_DEPS)
libgtest.a:
	$(COMPILE.cc) -iquote $(GTEST_ROOT) $(GTEST_ROOT)/src/gtest-all.cc
	$(COMPILE.cc) -iquote $(GMOCK_ROOT) $(GMOCK_ROOT)/src/gmock-all.cc
	$(AR) rv $@ gtest-all.o gmock-all.o
	rm gtest-all.o gmock-all.o

gt_statistics.o: gt_statistics.cc ../include/pabutils/statistics.hpp
gt_statistics: gt_statistics.o $(GT_DEPS)
	$(LINK.cc) -o $@ $^

gt_string_view.o: gt_string_view.cc
gt_string_view: gt_string_view.o $(GT_DEPS)
	$(LINK.cc) -o $@ $^
endif # WITH_GTEST

ifneq ($(WITH_BUTF),0)
BUTF_LDLIBS=-lboost_unit_test_framework

BUTF_MODULES=eval
CLEAN += butf_main.o

CLEAN += butf_eval.o
REALCLEAN += cpput_eval
butf_eval: butf_main.o butf_eval.o $(BUTF_LDLIBS)

REALCLEAN += butf
butf: butf_main.o $(foreach m,$(BUTF_MODULES),butf_$(m).o)
	$(LINK.cc) $^ $(LDLIBS) $(BUTF_LDLIBS) -o $@
endif # WITH_BUTF

ifneq ($(WITH_CPPUT),0)

CPPUT_MODULES=eval
CLEAN += cpput_main.o

CLEAN += cpput_eval.o
cpput_eval.o: cpput_eval.cc
REALCLEAN += cpput_eval
cpput_eval: cpput_main.o cpput_eval.o

REALCLEAN += cpput
cpput: cpput_main.o $(foreach m,$(CPPUT_MODULES),cpput_$(m).o)
	$(LINK.cc) $^ $(LDLIBS) $(CPPUT_LDLIBS) -o $@

endif # WITH_CPPUT

.PHONY: clean realclean
clean:
	-rm -rf $(CLEAN)

realclean: clean
	-rm -rf $(REALCLEAN)