################################################################################
#
#  Copyright (c) 2005-2007 by Turku PET Centre
#
#  Purpose: Makefile for PET Image processing C library (libtpcimgp).
#
#  Environment variables:
#	OS = 'operating system'
#	INCLUDE = 'include path'
#	LDFLAGS = 'library path'
#	PET_BIN = 'where to install binaries'
#	PET_LIB = 'where to install libraries'
#	PET_ARCHIVE = 'where to archive sources'
#
# Required files:
#	Doxyfile
#
#  Main targets:
#	compile: compile library and executable
#	test: run tests on library
#	install: install library and executable
#	distribution: archiving sources and html docs for backup and distribution
#	clean & clobber: clean the directory
#
#  Compile with: make -e
#
#  Date:
#  2005-05-19 Jarkko Johansson
#  2005-11-07 Calle Laakkonen
#  2005-11-29 Jarkko Johansson
#  2006-02-09 Seppo Tyrvainen
#  2006-06-20 Jarkko Johansson
#  2006-07-13 Vesa Oikonen
#    Added the option to line "PRVERS = progvers -opt=-b".
#    Replaced the rule html by distribution in the list above.
#  2006-10-02 VO
#    Added -std=gnu99 to CFLAGS.
#  2006-10-30 VO
#    Added new C files.
#  2007-02-02 VO
#    Added new C files.
#    imgproc.c removed (contents splitted).
#    Added target 'test'.
#
################################################################################
# ------------- General settings:----------------------------------------------#
LIBRARY		= libtpcimgp
SOURCES 	= imgarithm.c imgmatch.c imgscanner.c imgflip.c \
                  imgtransform.c point.c imgfilter.c \
                  imgthrs.c imgframe.c libtpcimgp.c libtpcimgpv.c
OBJECTS 	= ${SOURCES:.c=.o}
LIBARCHIVE	= ${LIBRARY}.a
DOXYGEN		= doxygen
CC		= gcc
CFLAGS 		= -std=gnu99 -s -Wall -O ${INCLUDE}
LDFLAGS		= -L.
LIBS 		= ${LDFLAGS} -ltpcimgp -ltpcimgio -lm
ZIP		= zip
PRVERS		= progvers -opt=-b
ADDDATE		= adddate
PDOC2HTML	= pdoc2htm -L

# ------------- OS/terminal dependent settings:--------------------------------#
ifeq ($(OS),Windows_NT)
SUF		= 
EXT		= .exe
else
SUF		= ./
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}  ${CFLAGS} ${LIBS}

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

# ------------- Testing: ------------------------------------------------------#
test: ${LIBEXEC}
	${SUF}${LIBRARY} -f 1
	${SUF}${LIBRARY} -f 2

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

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

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

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

execinstall: ${LIBEXEC}
	${ADDDATE} -i=${LIBRARY}.version -o=${PET_BIN} -nodate -copy ${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}

libsrcarchive: clobber
	make_libsrcarchive.sh ${LIBRARY}

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

clobber: clean
	rm -f *~ *.exe

