$(call ShakeNMake.CALL.CLEAN-SET,binding-cpp)
ifneq (1,$(MAKING_CLEAN))
binding-cpp: all
endif
LFPP.MAKEFILE := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
CXXFLAGS += -fPIC -Wall -Werror

DIR.cpp := $(DIR.bindings)/cpp
CPP.SRC := 	$(patsubst %,$(DIR.cpp)/%,\
	Context.cpp \
	Db.cpp \
	Deck.cpp \
	Exception.cpp \
	Fossil.cpp \
	OStream.cpp)

LFPP_OBJECTS := $(patsubst %.cpp,%.o,$(CPP.SRC))
$(LFPP_OBJECTS): $(LFPP.MAKEFILE)
libf++ := $(DIR.cpp)/libfossil++
$(libf++).DLL.OBJECTS := $(LFPP_OBJECTS)
$($(libf++).DLL.OBJECTS): $(libfossil.library)
$(libf++).BIN.LDFLAGS := -lstdc++ -fPIC $(LIBF_CLIENT_LDFLAGS)
$(libf++).LIB.OBJECTS := $($(libf++).DLL.OBJECTS)
$($(libf++).DLL.OBJECTS): $(PACKAGE.C_DEPS.MAKEFILES) $(LFPP.MAKEFILE)
########################################################################
# Shared lib
$(eval $(call ShakeNMake.CALL.RULES.DLLS,$(libf++)))
$($(libf++).DLL): $($(libf++).DLL.OBJECTS)
CLEAN.binding-cpp += $($(libf++).DLL.OBJECTS) $($(libf++).DLL)
all: $($(libf++).DLL)

CPPTest := $(DIR.cpp)/test
$(CPPTest): $(libfossil.library)
$(CPPTest).BIN.OBJECTS := $(CPPTest).o
ifeq (0,1)
$(CPPTest).BIN.LDFLAGS := $($(libf++).BIN.LDFLAGS) $(EXTRA_LIBS) -lstdc++ -L. -lfossil++ @SH_LINKFLAGS@
else
$(CPPTest).BIN.OBJECTS += $($(libf++).DLL.OBJECTS)
$(CPPTest).BIN.LDFLAGS := $(LIBF_CLIENT_LDFLAGS) $(EXTRA_LIBS) -lstdc++ @SH_LINKFLAGS@
endif
$(eval $(call ShakeNMake.CALL.RULES.BINS,$(CPPTest)))
CLEAN.binding-cpp += $($(CPPTest).BIN) $($(CPPTest).BIN.OBJECTS)
all: $($(CPPTest).BIN)

########################################################################
# A quick-n-dirty amalgamation build...
LFPP.AMAL_D := $(DIR.top)
LFPP.AMAL_CPP := $(LFPP.AMAL_D)/libfossil++.cpp
LFPP.AMAL_HPP := $(LFPP.AMAL_D)/libfossil++.hpp
LFPP.AMAL_CONF.H := $(LFPP.AMAL_D)/libfossil-config.h
CLEAN.binding-cpp += $(LFPP.AMAL_HPP) $(LFPP.AMAL_CPP)

LFPP.AMAL_CPP.SRC := \
	$(CPP.SRC)

# ACHTUNG: in LFPP.AMAL_H.SRC, $(LFPP.AMAL_CONF.H) MUST be included first.
LFPP.AMAL_HPP.SRC := \
	$(DIR.include)/fossil-scm/fossil.hpp

$(LFPP.AMAL_HPP.SRC):
$(LFPP.AMAL_CPP.SRC):
$(LFPP.AMAL_HPP): $(LFPP.AMAL_HPP.SRC)
$(LFPP.AMAL_CPP): $(LFPP.AMAL_HPP) $(LFPP.AMAL_CPP.SRC)
$(LFPP.AMAL_CONF.H): $(MAIN_MAKEFILES)

$(LFPP.AMAL_HPP):
	@echo "Creating $@..."
	@{ \
		echo '#if !defined(FSLPP_AMALGAMATION_BUILD)'; \
		echo '#define FSLPP_AMALGAMATION_BUILD 1'; \
		echo '#endif'; \
		echo '#include "$(notdir $(libfossil.h))"'; \
	} > $@
	@{ \
		for i in $(LFPP.AMAL_HPP.SRC); do \
			echo "/* start of file $$i */"; \
			cat $$i; \
			echo "/* end of file $$i */"; \
		done; \
	} | sed  \
		 -e '/[ ]*#[ ]*include[ ]*.*fossil.*\.h[>"]/d' \
		 -e '/[ ]*#[ ]*include[ ]*.*".*config\.h[>"]/d' \
		>> $@

$(LFPP.AMAL_CPP): $(LFPP.AMAL_HPP)
	@echo "Creating $@..."
	@echo '#include "$(notdir $(LFPP.AMAL_HPP))"' > $@
	@{ \
		for i in $(LFPP.AMAL_CPP.SRC); do \
			echo "/* start of file $$i */"; \
			cat $$i; \
			echo "/* end of file $$i */"; \
		done; \
	} | sed \
		 -e '/[ ]*#[ ]*include[ ]*.*fossil.*\.h.*[>"]/d' \
		>> $@

LFPP.AMAL_CPP_FLAGS := -I$(DIR.include) -I$(DIR.top) -I. -I$(DIR.src)# for sqlite3.h :/
.PHONY: amal
amalpp: $(LFPP.AMAL_CPP)
	@ls -ls $(LFPP.AMAL_CPP) $(LFPP.AMAL_HPP)
	@if which g++ >/dev/null; then \
		echo "Trying GCC..."; \
		g++ -c $(LFPP.AMAL_CPP) -pedantic -Wstrict-aliasing -Wall -Werror -Wno-long-long $(LFPP.AMAL_CPP_FLAGS); \
	fi; true
	@if which clang >/dev/null; then \
		echo "Trying clang..."; \
		clang -c $(LFPP.AMAL_CPP) -pedantic -Wstrict-aliasing -Wall -Werror -Wno-long-long $(LFPP.AMAL_CPP_FLAGS); \
	fi; true

# /amalgamation
########################################################################
CLEAN.binding-cpp += $(wildcard $(DIR.cpp)/*~)
