################################################################################
#
#  Makefile for ROI C library (libtpcroi).
#  For compiling 32-bit library in 64-bit system.
#
#  Environment variables:
#	OS = 'operating system'
#	PET_INCLUDE = 'include path'
#	PET_ARCHIVE = 'where to archive sources'
#	PET_LIBPATH32 = 'where to install 32-bit libraries'
#	PET_BIN32 = 'where to install 32-bit binaries'
#
#  Main targets:
#	compile: compile library and executable
#	install: install library and executable
#	distribution: archiving sources and html docs for backup and distribution
#	clean & clobber: clean the directory
#
#  Required files:
#	Doxyfile
#
#  Usage:
#       Compile with: make -e -f Makefile32
#
#  Modification date:
#  2013-02-19 Vesa Oikonen
#
################################################################################
# ------------- General settings:----------------------------------------------#
LIBPATH		= -L. -L/${PET_LIBPATH32}
INCLUDE		= -I. -I./include -I/${PET_INCLUDE}
LIBRARY		= libtpcroi
SOURCES 	= roi.c oldroi.c libtpcroiv.c
OBJECTS 	= ${SOURCES:.c=.o}
LIBARCHIVE	= ${LIBRARY}.a
DOXYGEN		= doxygen
CC		= gcc
CFLAGS 		= -std=gnu99 -s -m32 -Wall -O3 ${INCLUDE}
LIBS 		= ${LIBPATH} -ltpcroi -ltpcimgio -ltpcmisc -lm
ZIP		= zip
PRVERS		= progvers -opt=-b
ADDDATE		= adddate
PDOC2HTML	= pdoc2htm -L


# ------------- OS/terminal dependent settings:--------------------------------#
ifeq ($(OS),Windows_NT)
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} -f 1
#	${SUF}${LIBRARY} -f 2

# ------------- 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_LIBPATH32} -nodate -copy ${LIBARCHIVE}

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

finalinstall: install
	cp -f ${LIBARCHIVE} ${PET_LIBPATH32}/${LIBARCHIVE}
	cp -f ${LIBEXEC} ${PET_BIN32}/${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
	make_libsrcarchive.sh ${LIBRARY}

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

clobber: clean
