##########################################################################
# Copyright 2002 MBARI                                             
##########################################################################
# Summary  : Makefile for SmartSampler
# Filename : Makefile
# Author   : Rishi Graham
# Project  : 
# Version  : Version 1.0
# Created  : 1/31/11
# Archived :
##########################################################################
# Modification History:
#
##########################################################################


##########################################################################
# Two test modes:
#
# SELF_TEST=YES means we only attach to the gulper and cluster interfaces,
# and use fake stubs to read data from files for the others.
# 
# SELF_TEST=(anything else) means we use the simulation versions for data
# interfaces. These let us test the interfaces, but do not provide any
# sort of real data.
#
##########################################################################

SELF_TEST = NO
SSDBG = NO
DUMP_MEM = NO

CC	    = cc
CCPP	    = cc
LD	    = cc
AR 	    = wlib
DEPEND_TOOL = makedepend

AUV_HOME = $(AUV)
AUV_BIN_DIR = $(AUV_HOME)/bin

EXCEPTION_HANDLING = -WC,-xs
## OPTIMIZE = -4 -Os
OPTIMIZE = 

## MAP_OUTPUT = -S -ms -M
MAP_OUTPUT =

ifeq ($(DUMP_MEM),YES)
MEMFLAG=-DDMEM
DMEM_DIR=$(AUV_HOME)/cluster/dmem
DMEM_LIBS=$(DMEM_DIR)/dmem.lib
else
DMEM_LIBS=
endif

ifeq ($(SELF_TEST),YES)
TESTFLAG=-DSELF_TEST
SSDBG=YES
endif

ifeq ($(SSDBG),YES)
SSDBGFLAG = -DSSDBG
endif

CFLAGS	    = -c $(MAP_OUTPUT) $(OPTIMIZE) -D_QNX -DUNIX $(SSDBGFLAG) $(TESTFLAG) $(MEMFLAG)
CPPFLAGS    = -c $(MAP_OUTPUT) $(OPTIMIZE) -D_QNX -DUNIX \
	         $(EXCEPTION_HANDLING) $(SSDBGFLAG) $(TESTFLAG) $(MEMFLAG)

SSDIR = $(AUV_HOME)/smartSampler
DT_LIST = SimpleThreshold ClusterHMM FrontMomentum GPThreshold
DT_SUBDIRS = $(DT_LIST:%=$(SSDIR)/DecisionTool/%)
DT_DIRS = $(SSDIR)/DecisionTool $(DT_SUBDIRS)
DEV_DIR = $(SSDIR)/devices
FAKEDEV_DIR = $(DEV_DIR)/self_test
define COPY_DT_CONFIGS
	for ONE_DIR in $(DT_SUBDIRS); \
	do \
		cp -f $$ONE_DIR/smartSampler*.cfg $(AUV_CONFIG_DIR) ;\
	done
endef

INCLUDES    = -I.  \
	      -I$(AUV_HOME)/stlport \
	      -I$(AUV_HOME)/utils \
	      -I$(AUV_HOME)/framework \
	      -I$(AUV_HOME)/system \
	      -I$(AUV_HOME)/taskIF \
	      -I$(AUV_HOME)/DataLog \
	      -I$(AUV_HOME)/include \
	      -I$(AUV_HOME)/cluster/dmem \
	      -I$(DEV_DIR) \
	      -I$(FAKEDEV_DIR) \
	      $(foreach dir,$(DT_DIRS),-I$(dir) ) \
	      -I/usr/include

# CLSTR_UTILS_OBJS = utils/Symbol.o utils/ArgList.o \
#              utils/ConfParser.o utils/ConfLexer.o utils/Random.o

LDFLAGS	    = -lmqueue
LIBPATH	    = /usr/lib:$(AUV_HOME)/lib

LIBS        =   $(AUV_HOME)/utils/utils.lib \
		$(AUV_HOME)/taskIF/taskInterfaces.lib \
		$(AUV_HOME)/framework/auvFramework.lib \
		$(AUV_HOME)/DataLog/dataLog.lib \
		$(DMEM_LIBS)

# Here are a few directories which we just pick up all 

# Generic function to generate object names out of all .cc files in a directory
# Tried to make a generic CCTOOBJS function, but for some reason the $(call ...)
# function doesn't seem to work here.
DT_OBJS = $(foreach dir,$(DT_DIRS),$(patsubst %.cc,%.o,$(wildcard $(dir)/*.cc)))

# devices directory contains stuff for interfacing with other devices
DEV_BASE = SSDLogger SmartSamplerData SmartSamplerInterfaces
FAKEDEV_BASE = FakeCTD FakeHS2 FakeIsus FakeProfile FakeDepthSensor
DEV_OBJS = $(foreach bname,$(DEV_BASE),$(DEV_DIR)/$(bname).o) \
	$(foreach bname,$(FAKEDEV_BASE),$(FAKEDEV_DIR)/$(bname).o)

DEPENDS = ConfigTable.hh SmartSamplerInput.hh SmartSamplerOutput.hh dtConfigList.hh

SAMPLER_OBJS = SmartSampler.o _smartSampler.o $(DEV_OBJS) $(DT_OBJS)
SERVER_OBJS = SmartSamplerServer.o _smartSamplerServer.o
TEST_OBJS = _smartSamplerTest.o SmartSamplerServer.o SmartSampler.o $(DEV_OBJS) $(DT_OBJS)

TARGETS =	smartSampler smartSamplerServer smartSamplerTest

-include ../autonomy.make # this file contains the general
                       # modifications we want to apply to
                       # all the modules 


all: $(TARGETS)

smartSamplerTest: $(TEST_OBJS) $(DEPENDS) $(LIBS)
	$(LD) $(LDFLAGS) $(TEST_OBJS) $(LIBS) -o $@

smartSampler: $(SAMPLER_OBJS) $(DEPENDS) $(LIBS)
	$(LD) $(LDFLAGS) $(SAMPLER_OBJS) $(LIBS) -o $@

smartSamplerServer: $(SERVER_OBJS) $(DEPENDS) $(LIBS)
	$(LD) $(LDFLAGS) $(SERVER_OBJS) $(LIBS) -o $@


install:
	cp -f $(TARGETS) $(AUV_BIN_DIR)/.

install_cfg: 
	cp -f smartSampler*.cfg $(AUV_CONFIG_DIR)
	$(COPY_DT_CONFIGS)

list_dts:
	@echo DIRS=$(DT_DIRS)
	@echo OBJS=$(DT_OBJS)

##########################################################################
# Do not modify anything below this line unless absolutely necessary

.SUFFIXES:
.SUFFIXES:	.o .c .cc .d .idl
.PHONY: my_libs

$(DMEM_DIR)/dmem.lib: my_libs

my_libs: 
	( cd $(DMEM_DIR) ; $(MAKE) ) 

%.d: %.cc
	    @echo Generating dependencies for $?
	    @touch $?
	    @$(DEPEND_TOOL) $(INCLUDES) $? -f $@ 

%.o: %.cc
	    $(CCPP) $(CPPFLAGS) $(INCLUDES) $< -o $@
	    @echo

DecisionTool/HMM.o: DecisionTool/HMM.cc DecisionTool/HMM.hh ConfigTable.hh

depend:	$(SAMPLER_OBJS:.o=.d) $(SERVER_OBJS:.o=.d)

DEST	    = .

MAKEFILE    = Makefile

include       $(SAMPLER_OBJS:.o=.d)
include       $(SERVER_OBJS:.o=.d)

CLEAN_BASE = $(SAMPLER_OBJS) $(SERVER_OBJS) $(TEST_OBJS)
CLEAN_OBJS = $(CLEAN_BASE) $(CLEAN_BASE:.o=.d) $(CLEAN_BASE:.o=.d.bak)

clean:
	@echo "removing all generated files"
	@rm -f core $(CLEAN_OBJS) $(TARGETS) *~

update:
	cvs update $(CC_SRCS) $MAKEFILE

commit:
	cvs commit $(CC_SRCS) $MAKEFILE

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









# DO NOT DELETE THIS LINE -- make depend depends on it.
