# file: $isip/scripts/make/compile.make
# this makefile contains a standard set of dependencies and targets
# that are used by all makefiles in the environment.
#

#------------------------------------------------------------------------------
#
# define variables section
#
#------------------------------------------------------------------------------

# create filenames
#
SRCS = $(wildcard $(ISIP_FILES))
OBJS = $(subst .cc,.o,$(SRCS))

# make a dependency search path for header files. first all locally
# checked out code, next the user specified include directories,
# finally the standard isip include directory(ies)
#
HEADER_DIRS = $(subst -I, ,$(ISIP_IFLAGS) $(ISIP_INCLUDE))
HEADER_VPATH = $(CLASS_DIRS) $(HEADER_DIRS) 

# define the diagnostic program's path
#

# initialize all the library variables to null
#
ISIP_OLIB_BASE :=
HOME_OLIB_BASE :=
OLIB_BASE :=
OLIB_DIR := 
OLIB := 

# if an ISIP_OLIB is specified, set the variables
#
ifneq "$(ISIP_OLIB)" ""
	ISIP_OLIB_BASE := $(ISIP_PROTO)/lib
	HOME_OLIB_BASE := $(ISIP_WORK)/lib
	OLIB_BASE := $(ISIP_OLIB_BASE)
	OLIB_DIR = $(OLIB_BASE)/$(ISIP_BINARY)
	OLIB = $(OLIB_DIR)/$(ISIP_OLIB)
	INSTALL_DIRECTIVE := install-actual
else
	INSTALL_DIRECTIVE := install-skip
endif

TEMPLATE_DIAGNOSE_CLASS := 

CLASS_DIRS := ""
LOC_OLIB := 

HDR_FILES = $(notdir $(wildcard *.h)) 
HDR_INST = $(foreach hdr,$(HDR_FILES),$(ISIP_PROTO)/include/$(notdir $(hdr)))

vpath %.h $(HEADER_VPATH)

# define flags for the C++ and C compilers
#
I_FLAGS =  $(ISIP_IFLAGS) $(CLASS_I_FLAGS) $(ISIP_INCLUDE)
CPLUS_FLAGS  = -Wall -ftemplate-depth-50 $(ISIP_CFLAGS) $(filter-out -dmalloc,$(DEBUG)) -c
C_FLAGS      = 

# modify the c_flags from the command line arguments.  notice that
# 'make DEBUG=' will nullify a specified debug flag in the user
# makefile. note that we need to compare the origin of the variable
# with the string undefined instead of just a ifdef DEBUG because
# ifdef DEBUG does not consider a null setting (which is useful to
# disable all debuging/optimization).
#

# if the DEBUG is specified on the command line, ignore what the
# makefile tells us about debugging
#
ifneq "$(origin DEBUG)" "undefined"
	ISIP_CFLAGS	:= $(filter-out -g,$(ISIP_CFLAGS))
	ISIP_CFLAGS	:= $(filter-out -ggdb3,$(ISIP_CFLAGS))
	ISIP_CFLAGS 	:= $(filter-out -dmalloc,$(ISIP_CFLAGS))
endif

# move the debug flags from the makefile's definition of CFLAGS to our
# local variable DEBUG, which is later parsed back into
# CPLUS_FLAGS. this unifies variables set in makefiles and command line
# options.  
#
ifneq "$(filter -g,$(ISIP_CFLAGS))" ""
	ISIP_CFLAGS	:= $(filter-out -g,$(ISIP_CFLAGS))
	DEBUG += -g
endif
ifneq "$(filter -ggdb3,$(ISIP_CFLAGS))" ""
	ISIP_CFLAGS	:= $(filter-out -ggdb3,$(ISIP_CFLAGS))
	DEBUG += -ggdb3
endif
ifneq "$(filter -dmalloc,$(ISIP_CFLAGS))" ""
	ISIP_CFLAGS 	:= $(filter-out -dmalloc,$(ISIP_CFLAGS))
	DEBUG += -dmalloc
endif

# if the optimize is set on the command line, override
#
ifneq "$(origin OPTIMIZE)" "undefined"
	CPLUS_FLAGS	:= $(filter-out -O%,$(CPLUS_FLAGS))
	CPLUS_FLAGS += $(OPTIMIZE)
endif


# define dependencies: note that we include, by default, a makefile
#  located in the current directory, so that changes to the makefile
#  will trigger a make.
#
ALL_DEPS = $(ISIP_PROTO)/class/GNUmakefile ./GNUmakefile $(ISIP_DEPS)

# define suffixes
#
.SUFFIXES: .cc

# make sure we clean up
#  note: this code is executed ALL the time
#
.DONE:

# make everything silent
#
.SILENT:

.PHONY: all clean test_make check install-skip install-actual

#------------------------------------------------------------------------------
#
# define targets and dependencies
#
#------------------------------------------------------------------------------

# make all objects
#
all: $(OBJS)

# include file dependencies (define only the significant relationships)
#
$(OBJS): $(ALL_DEPS)

# define source file dependencies
#
.cc.o:
	echo $(ISIP_CPLUS_COMPILER) $(I_FLAGS) $(CPLUS_FLAGS) $(C_FLAGS) $<
	$(ISIP_CPLUS_COMPILER) $(I_FLAGS) $(CPLUS_FLAGS) $(C_FLAGS) $<

# install into a library
#
install: $(INSTALL_DIRECTIVE)

install-actual: $(ISIP_PROTO)/include $(HDR_INST) $(OBJS) $(OLIB_DIR)
	echo "> installing in" $(OLIB)
	-ar crus $(OLIB) $(OBJS)

install-skip: $(ISIP_PROTO)/include $(HDR_INST) 
	echo "> skipping install"

# make the library directory if necessary
#
$(OLIB_DIR) : $(OLIB_BASE)
	-if ( test ! -d $(OLIB_DIR)); then echo "> creating directory" $(OLIB_DIR); mkdir -p $(OLIB_DIR); fi

$(OLIB_BASE) : 
	-if ( test ! -d $(OLIB_BASE)); then echo "> creating directory" $(OLIB_BASE); mkdir -p $(OLIB_BASE); fi

# make a link from the class to the $isip/include directory, if necessary
#
$(HDR_INST):
	$(MAKE) -f $(ISIP_PROTO)/lib/scripts/make/compile_depend.make

# create the $(ISIP_PROTO)/include directory if necessary
#
$(ISIP_PROTO)/include: 
	-if ( test ! -d $(ISIP_PROTO)/include); then mkdir -p $(ISIP_PROTO)/include; fi

# depend directive: this directive is called by recursive make before
# any other, so header files will be in place before any code is
# compiled.
#
depend: $(ISIP_PROTO)/include $(HDR_INST) $(OLIB_DIR)

# clean up (the -rm lets make continue after errors)
#
clean:
	echo "> removing" $(OBJS) $(EXEC)
	-rm -f $(OBJS) $(EXEC)

test_make:
	echo "debug = " $(DEBUG)
	echo "c_flags = " $(CPLUS_FLAGS)
	echo "hdr_files = " $(HDR_FILES)
	echo "hdr_inst = " $(HDR_INST)

# check existence
#
check: $(SRCS)

#
# end of file

