###############################################################################
#
# Copyright (c) 2005 by Turku PET Centre 
#
# Purpose: Makefile for PET Image Image Processing C library (libtpcimgp).
#
# 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-03-04 JJ
#	Targets for library testing included in this Makefile:
#		TEST_IMGARITHM (1): test image arihtmetics functions
#		TEST_IMGTRANSFORM (2): test image transformation functions
#
# 2005-04-19 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-02 Kaisa Sederholm
#       Added mestim.c
#
# 2005-06-09 KS
#       Added test_robust to test-target
#
# 2005-06-09 KS
#       Changes following from removal of robust functions to library
#       libtpcmodel 
#
###############################################################################
#---------- Settings:      --------#
CC 		= gcc
#AR 		= gar
DOXYGEN		= doxygen
ZIP		= zip
PRVERS		= progvers_0_3_0
ADDDATE		= adddate_0_3_3
ifeq ($(OS),Windows_NT)
endif
ifeq ($(OSTYPE),msys)
WD_NAME		= `basename ${PWD}`
endif
ifeq ($(OSTYPE),solaris)
WD_NAME		= `basename ${PWD}`
endif

#---------- Compilation parameters:      --------#
LIBS  		= -L. -ltpcimgp -ltpcimgio_1 -lm
INCLUDE		= -Iinclude -I../libtpcimgio/include -I../libtpcmisc/include
CFLAGS 		= -s -Wall -O2 ${INCLUDE}

#---------- Library related variables:      --------#
LIB		= libtpcimgp
LIB_ARCHIVE	= ${LIB}.a
LIB_VERS_CFILES	= ${LIB}v.c
LIB_EXEC_CFILES	= ${LIB}.c
LIB_CFILES      = imgproc.c imgtransform.c point.c ${LIB_VERS_CFILES}
LIB_OBJECTS 	= ${LIB_CFILES:.c=.o}
LIB_EXEC_OBJECTS= ${LIB_EXEC_CFILES:.c=.o}
ifeq ($(OS),Windows_NT)
LIB_EXEC	= ${LIB}.exe
else
LIB_EXEC	= ${LIB}
endif

#---------- Function serial numbers for testing the library functions;         --------#
#---------- these should correspond to serial numbers defined in libtpcimgp.c: --------#
IMGARITHM 	= 1
IMGTRANSFORM	= 2

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

${LIB_ARCHIVE}: ${LIB_OBJECTS} ${LIB_VERS_CFILES:.c=.o}
	${AR} -rvs ${LIB_ARCHIVE} ${LIB_OBJECTS}

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

${LIB_VERS_CFILES:.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} -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_imgarithm test_imgtransform 

test_imgarithm:
	${LIB_EXEC} -f${IMGARITHM}

test_imgtransform:
#	./${LIB_EXEC} -f ${IMGTRANSFORM} -v -z 2
#	./${LIB_EXEC} -f ${IMGTRANSFORM} -v -z 3
	${LIB_EXEC} -f ${IMGTRANSFORM} -v -z 6


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

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

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