##########################################################################
# Copyright 2008 MBARI                                             
##########################################################################
# Summary  : Makefile for the camera driver/server/test
# Filename : Makefile
# Author   : K Headley, Rob McEwen
# Project  : 
# Version  : Version 1.0
# Created  : 1 May 2008
# Modified : 
# Archived :
##########################################################################
# Modification History:
# $Header$
#
##########################################################################

BICAM_DIR = .
COMMON_DIR=${BICAM_DIR}
XPORT_DIR=${BICAM_DIR}
NMEA_DIR=${BICAM_DIR}
AUV_BIN_DIR = ../bin
HW_CFG_FLAGS = -DLANTRONIX_MATCHPORT
#HW_CFG_FLAGS = -DLANTRONIX_XPORT

########################
# Determine architecture
ARCH_TEST=${shell uname -s}
ARCH=unknown
QNX=${findstring QNX,$(ARCH_TEST)}
CYGWIN=${findstring CYGWIN,$(ARCH_TEST)}
ifeq (${QNX},QNX)
ARCH=QNX
endif
ifeq (${CYGWIN},CYGWIN)
ARCH=CYGWIN
endif

###############################
# Architecture-dependent macros
ifeq (${ARCH},QNX)
CC	    = cc
CCPP	    = cc
CFLAGS	    = -c $(DEBUG_FLAGS) $(MAP_OUTPUT) $(OPTIMIZE) -D_QNX -DUNIX $(HW_CFG_FLAGS) 
CPPFLAGS    = -c $(DEBUG_FLAGS) $(MAP_OUTPUT) $(OPTIMIZE) -D_QNX -DUNIX $(HW_CFG_FLAGS) \
	         $(EXCEPTION_HANDLING)
LDFLAGS	    = -g
INCLUDES    = -I.  \
	      -I.. \
	      -I${BICAM_DIR} \
	      -I../utils \
	      -I../framework \
	      -I../taskIF \
	      -I../DataLog \
	      -I../LayeredControl \
	      -I../Simulator \
	      -I../Navigation \
	      -I/usr/include           

LD	    = cc
AR 	    = wlib
DEPEND_TOOL = makedepend
MKDEP_FLAGS = ${DEBUG_FLAGS} -D_QNX -DUNIX
EXCEPTION_HANDLING = -WC,-xs
## OPTIMIZE = -4s -Os
OPTIMIZE = 
## MAP_OUTPUT = -S -ms -M
MAP_OUTPUT = -S -ms -M
LIBPATH	    = /usr/lib:../lib
LIBS        =   ../utils/utils.lib \
		../taskIF/taskInterfaces.lib \
		../DataLog/dataLog.lib \
		../framework/auvFramework.lib
else
CC=gcc
CCPP=g++
CFLAGS=-c -Wall ${DEBUG_FLAGS} $(HW_CFG_FLAGS) 
CPPFLAGS= -O -Wall ${DEBUG_FLAGS} $(HW_CFG_FLAGS) 
LDFLAGS=
# If the other packages are moved, need
# to add paths to those headers.
# Ditto if PThreads is added
INCLUDES= -I${BICAM_DIR} \
	  -I/usr/include
endif

##############
# Debug Flags
# default is no debug output
# use make "DEBUG=-DDEBUG" to enable debug output
DEBUG_FLAGS= -g ${DEBUG}

#########################################
# Source, Object, Executable Definitions

UT_SRC    =  ${COMMON_DIR}/Utils.cc
UT_OBJ    =  ${UT_SRC:.cc=.o}

XP_SRC    =  ${XPORT_DIR}/XPort.cc 
XP_OBJ    =  ${XP_SRC:.cc=.o}

NMEA_SRC  =  ${NMEA_DIR}/NMEA.cc ${NMEA_DIR}/GPGGA.cc ${NMEA_DIR}/GPRMC.cc 
NMEA_OBJ  =  ${NMEA_SRC:.cc=.o}

CIF_SRC   =  ${BICAM_DIR}/XPortGPIO.cc ${BICAM_DIR}/BicamGPS.cc 
CIF_OBJ   =  ${UT_OBJ} ${XP_OBJ} ${NMEA_OBJ} ${CIF_SRC:.cc=.o}
CIF_EXE   =  bicam

TCIF_SRC  =  ${UT_SRC} ${XP_SRC} ${NMEA_SRC} ${CIF_SRC} ${BICAM_DIR}/bicamTest.cc
TCIF_OBJ  =  ${TCIF_SRC:.cc=.o}
TCIF_EXE  =  bicamTest

TXP_SRC   =  ${UT_SRC} ${XP_SRC} ${XPORT_DIR}/xportTest.cc
TXP_OBJ   =  ${TXP_SRC:.cc=.o}
TXP_EXE   =  xportTest

TNMEA_SRC =  ${UT_SRC} ${NMEA_SRC} ${NMEA_DIR}/nmeaTest.cc
TNMEA_OBJ =  ${TNMEA_SRC:.cc=.o}
TNMEA_EXE =  nmeaTest

XPSIM_SRC =  ${UT_SRC} ${XP_SRC} ${XPORT_DIR}/XPortSim.cc
XPSIM_OBJ =  ${XPSIM_SRC:.cc=.o}
XPSIM_EXE =  xpsim

XPIO_SRC  = ${UT_SRC} ${XP_SRC} ${XPORT_DIR}/xportIO.cc
XPIO_OBJ  = ${XPIO_SRC:.cc=.o}
XPIO_EXE  = xpio

GWIZ_SRC  = ${UT_SRC} ${XP_SRC} ${BICAM_DIR}/XPortGPIO.cc  ${XPORT_DIR}/GWiz.cc
GWIZ_OBJ  = ${GWIZ_SRC:.cc=.o}
GWIZ_EXE  = gwiz

SVR_SRC   = CameraServer.cc CameraOutput.cc _cameraServer.cc 
SVR_OBJ   = ${SVR_SRC:.cc=.o}
SVR_EXE   = cameraServer

DRV_SRC   = ${UT_SRC} ${XP_SRC} ${NMEA_SRC} ${CIF_SRC} \
	    CameraDriver.cc CameraLog.cc CameraOutput.cc _cameraDriver.cc
DRV_OBJ   = ${DRV_SRC:.cc=.o}
DRV_EXE   = cameraDriver

CAMT_SRC  = ${UT_SRC} ${XP_SRC} ${NMEA_SRC} ${CIF_SRC} _cameraTest.cc 
CAMT_OBJ  = ${CAMT_SRC:.cc=.o}
CAMT_EXE  = cameraTest

SOBJS	    =	CameraServer.o \
		CameraOutput.o

DOBJS	    =	CameraDriver.o \
		CameraLog.o \
		CameraOutput.o

AOBJS	    =   _cameraServer.o _cameraDriver.o _cameraTest.o

ifeq (${ARCH},QNX)
TARGETS	    =	camctl cameraDriver cameraServer cameraTest 
INSTALL_EXECS = cameraDriver cameraServer cameraTest
OBJS	    =   ${TCIF_OBJ} ${TXP_OBJ} ${TNMEA_OBJ} ${XPSIM_OBJ} ${XPIO_OBJ} ${GWIZ_OBJ} \
		$(SVR_OBJ) $(DRV_OBJ) ${CAMT_OBJ}
CLEAN_EXECS =   ${TCIF_EXE} ${TXP_EXE} ${XPSIM_EXE} ${TNMEA_EXE} ${XPIO_EXE} ${GWIZ_EXE} cameraDriver cameraServer cameraTest
CLEAN_OBJS  =  ${CIF_OBJ} XPortSim.o bicamTest.o nmeaTest.o xportTest.o xportIO.o GWiz.o \
		${SOBJS} ${SOBJS:.o=.d} ${DOBJS} ${DOBJS:.o=.d} ${AOBJS} ${AOBJS:.o=.d}
else
TARGETS	    =	camctl 
OBJS	    =   ${TCIF_OBJ} ${TXP_OBJ} ${TNMEA_OBJ} ${XPSIM_OBJ} ${XPIO_OBJ}
INSTALL_EXECS = camctl
CLEAN_EXECS =   ${TCIF_EXE}.exe ${TXP_EXE}.exe ${XPSIM_EXE}.exe ${TNMEA_EXE}.exe ${XPIO_EXE}.exe ${GWIZ_EXE}.exe
CLEAN_OBJS  =  ${CIF_OBJ} XPortSim.o bicamTest.o nmeaTest.o xportTest.o xportIO.o GWiz.o
endif

TAR_SRC     =	_cameraDriver.cc _cameraServer.cc _cameraTest.cc \
		CameraDriver.cc CameraDriver.h CameraLog.cc CameraLog.h \
		CameraOutput.cc CameraOutput.h CameraServer.cc CameraServer.h \
		${UT_SRC} ${XP_SRC} ${NMEA_SRC} ${CIF_SRC} \
		${NMEA_DIR}/nmeaTest.cc ${XPORT_DIR}/XPortSim.cc ${XPORT_DIR}/xportIO.cc ${XPORT_DIR}/GWiz.cc \
		${XPORT_DIR}/xportTest.cc ${BICAM_DIR}/bicamTest.cc \
		${XPORT_DIR}/XPort.h ${NMEA_DIR}/NMEA.h ${BICAM_DIR}/Utils.h \
		${BICAM_DIR}/Bicam.h ${BICAM_DIR}/XPortGPIO.h  ${BICAM_DIR}/BicamConsts.h ${BICAM_DIR}/qnx_port.h \
		Makefile ${BICAM_DIR}/Makefile

TAR_FILE    = 	camera-src.tar

ifeq (${ARCH},QNX)
-include ../autonomy.make # this file contains the general
                       # modifications we want to apply to
                       # all the modules 
endif

.PHONY: camctl tar

all:		$(OBJS) $(TARGETS)

cameraDriver:  ${DRV_OBJ} ${LIBS} 
	${LD} ${LDFLAGS} ${DRV_OBJ} ${LIBS} -o $@

cameraServer:  ${SVR_OBJ} ${LIBS} 
	${LD} ${LDFLAGS} ${SVR_OBJ} ${LIBS} -o $@

cameraTest: ${CAMT_OBJ}  ${LIBS} 
	${LD} ${LDFLAGS} ${CAMT_OBJ} ${LIBS} -o $@

camctl: xport nmea utils xportTest nmeaTest bicamTest xpsim xpio gwiz

utils: ${UT_OBJ}

xport: ${XP_OBJ}

nmea: ${NMEA_OBJ}

bicam: ${CIF_OBJ}

bicamTest: ${TCIF_OBJ}
	${CCPP} ${LDFLAGS} ${TCIF_OBJ} -o $@ ${LIBS}

xportTest: ${TXP_OBJ}
	${CCPP} ${LDFLAGS} ${TXP_OBJ} -o $@ ${LIBS}

nmeaTest: ${TNMEA_OBJ}
	${CCPP} ${LDFLAGS} ${TNMEA_OBJ} -o $@ ${LIBS}

xpsim: ${XPSIM_OBJ}
	${CCPP} ${LDFLAGS} ${XPSIM_OBJ} -o $@ ${LIBS}

xpio: ${XPIO_OBJ}
	${CCPP} ${LDFLAGS} ${XPIO_OBJ} -o $@ ${LIBS}

gwiz: ${GWIZ_OBJ}
	${CCPP} ${LDFLAGS} ${GWIZ_OBJ} -o $@ ${LIBS}

install:
		cp -fnv ${INSTALL_EXECS} ${AUV_BIN_DIR}/. 

tar:
	dos2unix ${TAR_SRC}
	tar cvf ${TAR_FILE} ${TAR_SRC};\
	gzip ${TAR_FILE}

##########################################################################
# Do not modify anything below this line unless absolutely necessary
ifeq (${ARCH},QNX)
.SUFFIXES:
.SUFFIXES:	.o .c .cc .d .idl

%.d: %.c
	    @echo Generating dependencies for $?
	    @${DEPEND_TOOL} ${INCLUDES} ${MKDEP_FLAGS} $? -f $@.tmp
	    @mv $@.tmp $@
%.d: %.cc
	    @echo Generating dependencies for $?
	    @${DEPEND_TOOL} ${INCLUDES} ${MKDEP_FLAGS} $? -f $@.tmp
	    @mv $@.tmp $@

%.o: %.c 
	    ${CC} ${CFLAGS} ${INCLUDES} $< -o $@	
	    @echo
%.o: %.cc
	    ${CCPP} ${CPPFLAGS} ${INCLUDES} $< -o $@
	    @echo
else
.SUFFIXES:
.SUFFIXES:      .o .c .cc .cpp
.c.o:
	${CC} -c ${CFLAGS} ${INCLUDES}  $< -o $*.o
.cpp.o:
	${CXX} -c ${CPPFLAGS} ${INCLUDES} $< -o $*.o

.cc.o:
	${CXX} -c ${CPPFLAGS} ${INCLUDES} $< -o $*.o
endif
DEST	    = .

MAKEFILE    = Makefile

ifeq (${ARCH},QNX)
include       $(OBJS:.o=.d)
endif
depend:	

clean:
	rm -f core ${CLEAN_OBJS} ${CLEAN_OBJS:.o=.d} ${CLEAN_EXECS}\
	*.lib *.yy *.tab.* *.S 

update:
	cvs update ${CC_SRCS} $MAKEFILE

commit:
	cvs commit ${CC_SRCS} $MAKEFILE

##########################################################################









