###############################################################################
#
# Copyright (c) 2005 by Turku PET Centre
#
# Purpose: Makefile for Time Activity Curve IO C library (libtpccurveio).
#
# Environment variables:
#	OS = 'operating system'
#	INCLUDE = 'include path' (optional)
#	LDFLAGS = 'library path' (optional)
#	PET_BIN = 'where to install binaries'
#	PET_LIB = 'where to install libraries'
#	PET_ARCHIVE = 'where to archive sources'
#	PET_WWW_ARCHIVE = 'where to put www content' (used in Doxyfile)
#
# Required files:
#	Doxyfile
#
# Main targets:
#	compile: compile the library and the test program
#	install: install the library and the test program
#	html: create the html documentation with doxygen
#	archive: archive the source code
#	test: run tests on library
#	clean & clobber: clean the directory
#	upgrade: clean compile test install clobber html
#
# Version:
#  2005-22-02 Jarkko Johansson
#  2005-03-03 JJ
#	Set environment variables INCLUDE and LDFLAGS to location of header
#	files and libraries to be linked (linking for the test program).
#
# 2005-04-25 JJ
#	Targets for installation and htlm creation added.
#	Htlm creation with doxygen requires the Doxyfile.
#
# 2005-05-04 JJ
#	Target for archiving added (works only in UNIX/Linux).
#
# 2005-06-17 Kaisa Sederholm
#       Added "remove" into html target
#
# 2005-10-31 Vesa Oikonen
#       Removed version numbers from PRVERS and ADDDATE.
#       Added version numbers to other libraries in INCLUDE.
#
# 2006-01-31 VO
#       Updated libtpcimgio version to 1.1.4.
#
# 2006-06-03 VO
#       Updated libtpcmisc version to 1.1.2.
#
###############################################################################
#--------      Programs to use:      -------#
CC 		= gcc
#AR 		= gar
DOXYGEN		= doxygen
ZIP		= zip
PRVERS		= progvers
ADDDATE		= adddate
ifneq ($(OS),Windows_NT)
WD_NAME		= `basename ${PWD}`
endif

#--------      Compilation parameters:      -------#
LIBS  		= -L. -ltpccurveio
INCLUDE		= -Iinclude -I../libtpcimgio_1_1_4/include -I../libtpcmisc_1_1_2/include
CFLAGS 		= -s -Wall -O2 ${INCLUDE}

#--------      Library related variables:      -------#
LIB		= libtpccurveio
LIB_CFILES 	= dft.c mathfunc.c ncifile.c result.c libtpccurveiov.c cpt.c idwc.c if.c
LIB_OBJECTS 	= ${LIB_CFILES:.c=.o}
LIB_ARCHIVE	= ${LIB}.a
ifeq ($(OS),Windows_NT)
LIB_EXEC	= ${LIB}.exe
else
LIB_EXEC	= ${LIB}
endif
LIB_EXEC_CFILES	= ${LIB}.c
LIB_EXEC_OBJECTS= ${LIB_EXEC_CFILES:.c=.o}


#--------      Function serial numbers for testing the library functions;
#--------      these should correspond to serial numbers defined in libtpccurveio.c:


#--------      Library compilation rules:      -------#
compile: ${LIB_ARCHIVE} ${LIB_EXEC}

${LIB_ARCHIVE}: ${LIB_OBJECTS}
	${AR} -rvs ${LIB_ARCHIVE} ${LIB_OBJECTS}

${LIB_EXEC}: ${LIB_OBJECTS} ${LIB_EXEC_OBJECTS}
	${LINK.c} -o ${LIB_EXEC} ${LIB_EXEC_OBJECTS} ${LIBS}

${LIB_VERSION:.c=.o}: ${LIB_CFILES}

#------      Library version rule (create or update the version information file):
libversion: ${LIB}.version

${LIB}.version: ${LIB_CFILES}
	${PRVERS} ${LIB_EXEC} ${LIB}.version

#--------      Installation rules:      -------#
install: install_lib install_lib_exec

install_lib: libversion
	${ADDDATE} -i=${LIB}.version -o=${PET_LIB} -nodate -copy ${LIB_ARCHIVE}

install_lib_exec: libversion
	${ADDDATE} -i=${LIB}.version -o=${PET_BIN} -nodate -copy ${LIB_EXEC}


#--------      Html creation rules:      -------#
html: ${LIB_EXEC}
	rm -rf $(PET_WWW_ARCHIVE)/doc/${LIB}
	./${LIB_EXEC} -R > Readme
	./${LIB_EXEC} -H > History
	${DOXYGEN}


#--------      Archiving rules:      -------#
archive: libversion clean
ifdef WD_NAME
	${ZIP} -r ../${LIB}.zip ../${WD_NAME}
	${ADDDATE} -i=${LIB}.version -o=${PET_ARCHIVE}/lib/${LIB} ../${LIB}.zip
else
	echo "Run 'make archive' only on UNIX/Linux"
endif

#--------      Testing rules:      -------#

#--------      Cleaning rules:      -------#
clean:
	rm -f ${LIB_OBJECTS} ${LIB_EXEC_OBJECTS}
	rm -f *~ ./include/*~ History Readme
	rm -rf ./html

clobber: clean
	rm -f ${LIB_ARCHIVE} ${LIB_EXEC}


#------      Upgrade rules; do all:      ------#
upgrade: clean compile test install clobber html
