################################################################################
#
#  Makefile for Imade-Derived Input C library (libtpcidi).
#
#  Environment variables:
#	OS = 'operating system'
#	PET_INCLUDE = 'include path'
#	PET_ARCHIVE = 'where to archive sources'
#	PET_LIBPATH = 'where to install libraries'
#	PET_BIN = 'where to install binaries'
#
#  Main targets:
#	compile: compile library and executable
#	test: run tests on library
#	install: install library and executable
#	distribution: archiving sources and docs for backup and distribution
#	clean & clobber: clean the directory
#
#  Required files:
#	Doxyfile
#  This library is independent on other (TPC) libraries.
#
#  Usage:
#	Compile with: make -e
#       GCC option -mfpmath=387 can be used to set FLT_EVAL_METHOD to 2;
#       it is by default 2 on 32-bit x86, but 0 on 64-bit x86. However, keeping
#       the default may result in considerable speed enhancement.
#
#  Version:
#  2012-10-22 Vesa Oikonen
#    First created.
#  2012-12-10 VO
#    Additional source file.
#  2012-12-12 VO
#    TIFF files created by testing are removed before archiving.
#    Additional source file.
#  2012-12-20 VO
#    Additional source files.
#  2013-01-28 VO
#    Additional source files.
#  2013-02-19 VO
#    Changed path environment variables.
#
#
################################################################################
# ------------- General settings:----------------------------------------------#
LIBPATH		= -L. -L/${PET_LIBPATH}
INCLUDE		= -I. -I./include -I/${PET_INCLUDE}
LIBRARY		= libtpcidi
SOURCES 	= circle.c heartcor.c heart.c mask.c peak.c recovery.c \
		  vessel.c libtpcidiv.c
OBJECTS 	= ${SOURCES:.c=.o}
LIBARCHIVE	= ${LIBRARY}.a
DOXYGEN		= doxygen
CC		= gcc
CFLAGS 		= -std=gnu99 -s -Wall -O3 ${INCLUDE}
LIBS		= ${LIBPATH} -ltpcidi -ltpcmodext -ltpcsvg -ltpcimgp \
		  -ltpcmodel -ltpcimgio -ltpccurveio -ltpcmisc -lm
ZIP		= zip
PRVERS		= progvers -opt=-b
ADDDATE		= adddate
PDOC2HTML	= pdoc2htm -L

# ------------- OS/terminal dependent settings:--------------------------------#
ifeq ($(OS),Windows_NT)
SUF		= 
EXT		= .exe
endif

LIBEXEC 	= ${LIBRARY}${EXT}

# ------------- Compilation: --------------------------------------------------#
compile: ${LIBARCHIVE} ${LIBEXEC}

# Compile library
${LIBARCHIVE}: ${OBJECTS}
	${AR} -rvs ${LIBARCHIVE} ${OBJECTS}

# Compile executable
${LIBEXEC}: ${LIBARCHIVE} ${LIBRARY}.c
	${CC} ${LIBRARY}.c -o ${LIBEXEC} ${LIBPATH} ${CFLAGS} ${LIBS}

ifneq ($(OS),Windows_NT)
	 chmod a+x ${LIBEXEC}
endif

# ------------- Testing: ------------------------------------------------------#
test: ${LIBEXEC}
	${SUF}${LIBRARY} -v -f 0

# ------------- Version information: ------------------------------------------#
libversion: ${LIBRARY}.version

${LIBRARY}.version: ${LIBEXEC}
	${PRVERS} ${LIBEXEC} ${LIBRARY}.version

# ------------- Installation: -------------------------------------------------#
install: libversion libinstall execinstall

libinstall: ${LIBARCHIVE}
	${ADDDATE} -i=${LIBRARY}.version -o=${PET_LIBPATH} -nodate -copy ${LIBARCHIVE}

execinstall: ${LIBEXEC}
	${ADDDATE} -i=${LIBRARY}.version -o=${PET_BIN} -nodate -copy ${LIBEXEC}

finalinstall: install
	cp -f ${LIBARCHIVE} ${PET_LIBPATH}/${LIBARCHIVE}
	cp -f ${LIBEXEC} ${PET_BIN}/${LIBEXEC}

# ------------- Distribution: -------------------------------------------------#
distribution: libversion doxyapi libbinarchive libsrcarchive

doxyapi: ${LIBARCHIVE} ${LIBEXEC}
	./${LIBEXEC} -R > Readme
	./${LIBEXEC} -H > History
	make_doxyapi.sh ${LIBRARY}

libbinarchive: ${LIBARCHIVE} ${LIBEXEC}
	make_libbinarchive.sh ${LIBRARY} ${LIBEXEC}

libsrcarchive: clobber
	rm -f *.tif
	make_libsrcarchive.sh ${LIBRARY}

# ------------- Cleanup: ------------------------------------------------------#
clean:
	rm -f ${LIBRARY}${EXT} ${OBJECTS} ${LIBRARY}.a

clobber: clean

