###############################################################################
#
# Copyright (c) 2005 by Turku PET Centre 
#
# Purpose: Makefile for PET Modelling C library (libtpcmodel).
#
# 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 and archiving added.
#	Htlm creation with doxygen requires the Doxyfile.
#
# 2005-06-09 Kaisa Sederholm
#       Changes needed because of addition of robust functions to this 
#       library
#
# 2005-10-31 Vesa Oikonen
#       Removed version numbers from PRVERS and ADDDATE.
#
###############################################################################
# --------------------  Settings  ---------------------------------------------#
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. -ltpcmodel -lm
INCLUDE_LOCAL	= -I./ ${INCLUDE}
CFLAGS 		= -s -Wall -O2 ${INCLUDE_LOCAL}

# --------------------  Library related variables -----------------------------#
LIB		= libtpcmodel
LIB_CFILES 	= aic.c gaussdev.c integr.c nnls.c powell.c simplex.c \
			tgo.c bootstrap.c hholder.c llsqwt.c pearson.c qr.c \
			simulate.c libtpcmodelv.c lms.c lts.c median.c mestim.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 libtpcmodel.c:

ROBUSTEST       = 1

# --------------------  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 -----------------------------------------------#
test: test_robust

test_robust:
	./${LIB_EXEC} -v -f${ROBUSTEST}

# ------------------- 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 html 

