#
# Sequoia: Database clustering technology.
# Copyright (C) 2005 Emic Networks
# Contact: sequoia@continuent.org
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
# http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Initial developer(s): Gilles Rayrat
# Contributor(s): 
#

# Makefile for the Carob C++ API and tests
#

#General
LIB_MAJOR_VERSION	= 1

#Tools

RM					= /bin/rm -f
RMDIR				= /bin/rm -rf
LN					= /bin/ln -s
DOXYGEN				= /usr/bin/doxygen

#Dirs
INCDIR  			= include
SRCDIR  			= src

#Carob C++ lib
OBJS    			= ${SRCDIR}/Common.o\
					  ${SRCDIR}/JavaSocket.o\
					  ${SRCDIR}/DriverSocket.o\
					  ${SRCDIR}/CarobException.o\
					  ${SRCDIR}/ConnectionParameters.o\
					  ${SRCDIR}/ControllerPinger.o\
					  ${SRCDIR}/ControllerPool.o\
					  ${SRCDIR}/Connection.o\
					  ${SRCDIR}/Request.o\
					  ${SRCDIR}/RequestWithResultSetParameters.o\
					  ${SRCDIR}/ResultSetMetaData.o\
					  ${SRCDIR}/DriverResultSet.o\
					  ${SRCDIR}/Field.o\
					  ${SRCDIR}/Statement.o\
					  ${SRCDIR}/ParameterStatement.o\
					  ${SRCDIR}/BigDecimal.o\
					  ${SRCDIR}/SQLDataSerialization.o\
					  ${SRCDIR}/StringCodecs.o
# _GLIBCXX_GTHREAD_USE_WEAK

# optional CXXFLAGS
CXXFLAGS			=  -g3 -Wall -O0 -fPIC
# mandatory CXXFLAGS
override CXXFLAGS	+= -I${INCDIR}

# Uncomment this line to use poll function instead of select
# Or using the command line: make CXXFLAGS=-DCAROB_USE_POLL
# (Warning, optional feature not deeply tested)
# override CXXFLAGS		+= -DCAROB_USE_POLL

# LDFLAGS is not used for _static_ libcarob.a
# optional LDFLAGS
LDFLAGS				= -lpthread

# mandatory LDFLAGS
override LDFLAGS	+= -fPIC -shared
# for GMP
override LDFLAGS	+= -lgmp

ARFLAGS				= rcs
LIB_CAROB			= carob
LIB_CAROB_LIB_SHORT = lib${LIB_CAROB}.so
LIB_CAROB_LIB		= ${LIB_CAROB_LIB_SHORT}.${LIB_MAJOR_VERSION}
LIB_CAROB_STATIC	= lib${LIB_CAROB}.a
# Uncomment these 2 lines to use log4cxx instead of internal logger
#CXXFLAGS            += -DCAROB_USE_LOG4CXX
#LDFLAGS				+= -llog4cxx

#Doc
# DOC_DIR is duplicated in Doxyfile
DOC_DIR				= doxygen
DOC_OUT				= ${DOC_DIR}/html
DOXYFILE			= ${DOC_DIR}/Doxyfile
DOC_HTML			= ${DOC_OUT}/index.html

all: lib static
lib: ${LIB_CAROB_LIB}
static: ${LIB_CAROB_STATIC}
test: all
	$(MAKE) -C test

${LIB_CAROB_LIB}: checkld ${OBJS}
	${CXX} ${OBJS} ${LDFLAGS} -o ${LIB_CAROB_LIB}
	- ${LN} ${LIB_CAROB_LIB} ${LIB_CAROB_LIB_SHORT}

${LIB_CAROB_STATIC}: ${OBJS}
	ar ${ARFLAGS} ${LIB_CAROB_STATIC} ${OBJS}	

checkld:
ifneq (,$(findstring 2.14,$(shell 2>&1 $(CXX) -Wl,--version)))
	@echo "A bug in ld version 2.14 prevents Carob to work correctly"
	@echo "Upgrade binutils to version 2.15 or later"
	@echo "See CAROB-102 for more info."
	@exit 1
endif

clean: clean-lib clean-test clean-doc
clean-lib:
	${RM} ${SRCDIR}/*.o ${LIB_CAROB_LIB} ${LIB_CAROB_LIB_SHORT} ${LIB_CAROB_STATIC}
clean-test:
	$(MAKE) -C test clean
clean-doc:
	${RMDIR} ${DOC_OUT}

doc: ${DOC_HTML}
${DOC_HTML}: ${DOXYFILE}
	${DOXYGEN} ${DOXYFILE}

# compile each include/*.hpp, isolated
checkheaders:
	cd include ; \
	for h in *.hpp; do ln -sf $$h someheader.cpp; \
	printf "compiling $$h\n"; \
	${CXX} -c ${CXXFLAGS} someheader.cpp; done



# To create "carob-foo.tar.gz" from carob version tag r12_34,
# simply type this:
#
#   $  DIR_SUFFIX=foo CVS_TAG=r12_34 make dist
#
# alternatively, typing:
#
#   $  CVS_DATE=2055-09-22 make dist
#
# will create "carob-2055-09-22.tar.gz"
# Default value is the current YYYYMMDD date

dist:
	./publisher.sh buildsrctarball

publishdoc:
	./publisher.sh publishdoc
