#==============================================================================
#
#  CoStLy Version 2.0.4                                                051017
#  Makefile 
#
#==============================================================================
#
#  C++ compiler
#
CXX = c++
#
#  Interval library path
#
#  Modify the following line for using the C-XSC library
#
CXSCDIR = /usr/local/cxsc2p0
#
#  Modify the following line for using the filib++ library
#
FILIBDIR = /usr/local/filib++
#
#  Default installation path
#
OWN ?= root
GRP ?= users
PREFIX ?= /usr/local/CoStLy
#
#---------------------------------------------
#
AR = ar
INSTALL = /usr/bin/install
INCLUDE = -I$(DIR)/include/
SOURCES	= s2i.cpp cinterval.cpp cimath.cpp
OBJECTS = $(patsubst %.cpp,%.o,$(SOURCES))
#
#---------------------------------------------
#
start: info

# Information
PHONY:   info
.SILENT: info
info:
	@printf "\n\n"
	@printf " ------------------------------------------------------------------------------\n\n"
	@printf	"   CoStLy library Makefile\n"
	@printf "   =======================\n\n"
	@printf "   Possible make targets are:\n\n"
	@printf "   - lib-cxsc  :  Build CoStLy library using the C-XSC interval library.\n"
	@printf "   - lib-filib :  Build CoStLy library using the filib++ interval library.\n"
	@printf "   - install   :  Install the library.\n"
	@printf "   - clean     :  Remove object files.\n\n"
	@printf " ------------------------------------------------------------------------------\n"
	@printf "\n"
#
#  Creating the library based on C-XSC
#
PHONY:   lib-cxsc
.SILENT: lib-cxsc
lib-cxsc: OPTIONS  = -DCXSC_VERSION
lib-cxsc: DIR     ?= $(CXSCDIR)
lib-cxsc: TEXT     = C-XSC
lib-cxsc: text1 lib text2
	mv libcostly.a libcostly_cxsc.a

#
#  Creating the library based on filib++
#
PHONY:   lib-filib
.SILENT: lib-filib
lib-filib: OPTIONS  = -DFILIB_VERSION
lib-filib: DIR     ?= $(FILIBDIR)
lib-filib: TEXT     = filib
lib-filib: text1 lib text2
	mv libcostly.a libcostly_filib.a
#
#  Output Text 1
#
PHONY:   text1
.SILENT: text1
text1:
	echo
	echo "********************************************************************************"
	echo
	echo "Building CoStLy library using "$(TEXT)"."
	echo "------------------------------------"
	echo
	echo $(TEXT)" library path: " $(DIR)
	echo
	echo "Compiling source code:"
	echo
#
#  Output Text 2
#
PHONY:   text2
.SILENT: text2
text2:
	echo
	echo "Done!"
	echo
	echo "********************************************************************************"
	echo
#
#  Creating CoStLy
#
PHONY:   lib
.SILENT: lib
lib: libcostly.a 

libcostly.a: $(OBJECTS)
	$(AR) rs $@ $?

$(OBJECTS): %.o: %.cpp Makefile
	$(CXX) $(OPTIONS) $(INCLUDE) -c $< -o $@

s2i.o: s2i.cpp s2i.h adaptintval.h
cinterval.o: cinterval.cpp cinterval.h adaptintval.h
cimath.o: cimath.cpp cimath.h cinterval.h error.h adaptintval.h
#
#  Installing the library
#
install: 
	${INSTALL} -d  -g ${GRP} -o ${OWN} -m 755     ${PREFIX}
	${INSTALL} -d  -g ${GRP} -o ${OWN} -m 755     ${PREFIX}/lib
	${INSTALL} -d  -g ${GRP} -o ${OWN} -m 755     ${PREFIX}/include
	${INSTALL}     -g ${GRP} -o ${OWN} -m 444 *.h ${PREFIX}/include
	${INSTALL}     -g ${GRP} -o ${OWN} -m 444 *.a ${PREFIX}/lib
#
#  Cleaning the directory
#
.PHONY:  clean
.SILENT: clean
clean: 
	find . -name "*~" -print0 | xargs -0 rm 2>/dev/null; echo Removing temporary files; true
	find . -name "*.o" -print0 | xargs -0 rm 2>/dev/null; echo Removing object files; true
