###############################################################################
#                                                                             #
#   Copyright 2005 University of Cambridge Computer Laboratory.               #
#                                                                             #
#   This file is part of Nprobe.                                              #
#                                                                             #
#   Nprobe is free software; you can redistribute it and/or modify            #
#   it under the terms of the GNU General Public License as published by      #
#   the Free Software Foundation; either version 2 of the License, or         #
#   (at your option) any later version.                                       #
#                                                                             #
#   Nprobe is distributed in the hope that it will be useful,                 #
#   but WITHOUT ANY WARRANTY; without even the implied warranty of            #
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             #
#   GNU General Public License for more details.                              #
#                                                                             #
#   You should have received a copy of the GNU General Public License         #
#   along with Nprobe; if not, write to the Free Software                     #
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA #
#                                                                             #
###############################################################################

#
# Where to find various stuff
#

# Archive root
NPROBE_ROOT = ../..

# Import all relative paths defined at top level
include $(NPROBE_ROOT)/Makefile.paths

#
# ***NB***
# If making any changes which result in the below lists of paths changing 
#  in length be sure to edit setup_raw target below accordingly
#

# All tools dirs
PY_TOOLS_DIRS = $(ANAL_TOOL_DIR) $(PLOTTER_DIR) $(ANAL_NUT_BOLT_DIR) \
		$(PY_TOOLS_DIR)

# Additions to $PATH required to run correct Python and find tools
NP_ANAL_PATHS = $(PY_DIR) $(ANAL_SCRIPT_DIR) $(PY_TOOLS_DIRS) $(ANAL_UTILITY_DIR)
#NP_ANAL_PATHS = $(ANAL_SCRIPT_DIR) $(PY_TOOLS_DIRS) $(ANAL_UTILITY_DIR)

# Pypath components to allow Python runtime to find modules
NP_PYPATHS = $(ANAL_DIR) $(ANAL_UTILITY_DIR) $(PLOTTER_DIR) $(PY_INSTALL_DIR) \
		$(PY_TOOLS_DIR)

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

#
# Run make depend if building on different architecture
#

#ifeq ($(OS), $(ARCH))
#CHANGE_ARCH=
#else
#CHANGEARCH=changearch
#endif

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

# No built-in constants for ia64 gcc - define compilation switch const
#
ARCH_FIND_IA64 = $(findstring ia64, $(OS))
ifeq ($(findstring ia64, $(OS)), ia64)
SWIG_ARCH_DEF=-D__ia64__
else
SWIG_ARCH_DEF=
endif

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

#
# Define trace level for memory allocation debugging
#

TRACE_MALLOC_LEVEL=
#TRACE_MALLOC_LEVEL=-DREC_MALLOC_MED
#TRACE_MALLOC_LEVEL=-DREC_MALLOC_MAX

#TRACE_MALLOC=-DREC_MALLOC $(TRACE_MALLOC_LEVEL)
#TRACE_MALLOC=-DREC_MALLOC

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

#
# Defines for analysis module compilation
#
NPROBE_DEFINES = -DPROBE_FED -DBIN_REPORT -DDUMP_DEBUG -DCAM_CONFIG

SWIG_DEFINES = -DWREAD -DSWIG

DEFINES = $(SWIG_DEFINES) $(NPROBE_DEFINES) -DCPRINTF $(TRACE_MALLOC)

SWIG_ONLY_DEFINES = -python -shadow -DSWIG_ONLY $(SWIG_ARCH_DEF)

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

#
# cc options and include directives
#
CCOPT = -Dmain=oldmain

INCLUDES = -I. -I$(COMMON_HDR_DIR) -I$(NPROBE_DIR) -I$(NPROBE_MODULE_DIR) \
	$(PYTHON_INCLUDES)

###############################################################################
#
# Standard CFLAGS
#
CFLAGS = $(CCOPT) $(DEFINES) $(INCLUDES)

CC = gcc -g -Wall -fpic

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

AS = as
MAKE = make

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

# 
# Where to find source not in this directory
#
vpath %.c $(COMMON_SRC_DIR)
vpath %.h $(COMMON_HDR_DIR)

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

#
# Common C src and headers
#

NPROBE_CSRC = wread_util.c  http_util.c \
	report.c counters.c \
	print_util.c print_type.c errstring.c \
	cprintf.c malloc_rec.c

PYMOD_NPROBE_CSRC = $(addprefix $(COMMON_SRC_DIR)/, $(NPROBE_CSRC))

NPROBE_HDRS = http_errdefs.h basic_defs.h service.h\
	counters.h np_file.h tcp.h \
	http.h flows.h content_t.h \
	http_util.h udp.h \
	sundry_records.h udp_ns.h \
	icmp.h malloc_rec.h procstat.h

PYMOD_NPROBE_HDRS = $(addprefix $(COMMON_HDR_DIR)/, $(NPROBE_HDRS))

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

#
# C src and hdrs specific to analysis
#

PYMOD_ANAL_CSRC = flow_util.c  except.c  cfilter.c

PYMOD_ANAL_HDRS =  except.h flow_util.h cfilter.h

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

#
# All the C src and hdrs
#

PYMOD_CSRC = $(PYMOD_NPROBE_CSRC) $(PYMOD_ANAL_CSRC)

PYMOD_HDRS = $(PYMOD_NPROBE_HDRS) $(PYMOD_ANAL_HDRS)

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

#
# .o files required to build analysis interface
#

PYMOD_NPROBE_OBJ = wread_util.o flow_util.o report.o counters.o print_util.o \
	 http_util.o errstring.o except.o nprobe_wrap.o cprintf.o cfilter.o \
	malloc_rec.o print_type.o

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

CSRC = $(PYMOD_CSRC)  

HDR = $(PYMOD_HDRS)

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

#
# Stuff to copy to install directories
#

# Included modules, shared libs -> install/lib
ANAL_LIB = nprobec.so aanonc.so nprobe.py aanon.py $(filter-out np_bitmaps.py nprobe.py aanon.py, $(shell ls *.py)) np_bitmaps.py

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

#
# How to build it all
#

generate:
	@if [ ! -f $(NPROBE_DIR)/probe_config.h ]; \
		then \
			$(MAKE) -C $(NPROBE_DIR) $(MFLAGS) config_probe; \
	fi
	@if [ ! -f $(COMMON_HDR_DIR)/content_t.h ]; \
		then \
			 $(MAKE) -C $(NPROBE_DIR) $(MFLAGS) content_t; \
	fi


# Explicit dependency rule for *.o where *.c resides in other dir
%.o : %.c
	rm -f $@;
	$(CC) $(CFLAGS) -c $< -o $@

#all: Pymod_nprobe Pymod_aanon install
all: nprobe.py nprobec.so aanon.py aanonc.so update_install

#changearch: clean depend
#	echo $(OS) > last_build.arch

# swig-generate and compile analysis interface C and python files
nprobe_wrap.o nprobe.py: nprobe.i $(PYMOD_HDRS)
	$(SWIG) $(SWIG_ONLY_DEFINES) -DWREAD -I$(COMMON_HDR_DIR) nprobe.i
	$(CC) $(CFLAGS) -DSWIG_ONLY -c nprobe_wrap.c

nprobec.so: $(PYMOD_NPROBE_OBJ)
	gcc -shared -o $@ $(PYMOD_NPROBE_OBJ)

# swig-generate and compile analysis interface anonymisation C and python files
aanon_wrap.o aanon.py: aanon.i $(AANON_HDRS)
	$(SWIG) $(SWIG_ONLY_DEFINES) -I$(COMMON_HDR_DIR) aanon.i
	$(CC) $(CFLAGS) -c aanon_wrap.c

# build interface anonymisationlibrary
aanonc.so: aanon_wrap.o
	gcc -shared -o aanonc.so aanon_wrap.o

np_bitmaps.py: np_bitmaps.pyt $(NP_BITMAPS_GEN)
	 $(NP_BITMAPS_GEN)  np_bitmaps.pyt np_bitmaps.py $(NP_BITMAP_DIR)

# produce command to set up nprobe analysis paths etc.
paths_raw: Makefile
	@echo
	@echo '***************************************************************'
	@echo To set up Nprobe runtime/analysis using archive tree execute: \
	\". $(PATHS_SETUP) 1 7 5 $(TCL_TK_LIB) \
	$(subst $(NPROBE_ROOT), $(ABS_ROOT_PATH), \
	$(NP_ANAL_PATHS) $(NP_PYPATHS))\"
	@echo '***************************************************************'
	@echo

# produce command to set up nprobe analysis paths etc. for installed version
paths: Makefile
	@echo
	@echo '***************************************************************'
	@echo To set up Nprobe runtime/analysis using installed versions execute: \
	\". $(PATHS_SETUP) 1 2 1 $(TCL_TK_LIB) \
	$(INSTALL_BIN_DIR) $(PY_DIR) \
	$(INSTALL_LIB_DIR)\"
	@echo '***************************************************************'
	@echo

# copy everything useful to install directories, or update as necessary
install: install_dirs $(ANAL_LIB)
	@cp -uvf $(ANAL_LIB) $(INSTALL_LIB_DIR)

setup setup_raw:
	$(MAKE) -C $(ANAL_ROOT) $(MFLAGS) $@

update_install: install_dirs install_lib

#install_bin: $(ANAL_SCRIPTS)
#	@cp -fv $? $(INSTALL_BIN_DIR)
#	@touch install_bin

install_lib: $(ANAL_LIB)
	@cp -fv $? $(INSTALL_LIB_DIR)
	@touch install_lib

# Create install dirs if necessary
install_dirs: 
	@if [ ! -d $(INSTALL_LIB_DIR) ]; \
		then \
			mkdir -p $(INSTALL_LIB_DIR); \
			echo Created $(INSTALL_LIB_DIR); \
	fi
	@if [ ! -d $(INSTALL_BIN_DIR) ]; \
		then \
			mkdir -p $(INSTALL_BIN_DIR); \
			echo Created $(INSTALL_BIN_DIR); \
	fi


tidy:
	rm -f *.o

clean: tidy
	rm -f *wrap* *.so *.pyc nprobe.py aanon.py np_bitmaps.py

test_clean:
	if [ -f $(REP_TEST_DATA).gz ];\
		then \
			rm -vf $(REP_TEST_DATA); \
	fi 
	rm -vf $(REP_TEST_DATA) $(REP_TEST_DATA).counters \
	$(REP_TEST_DATA).r* \
	$(TEST_DATA_REP_DIR)/delayed_servers*  $(TEST_DATA_REP_DIR)/server*
	rm -vrf $(TEST_DATA_REP_DIR)/*np_rets

very_clean: clean test_clean
	rm -vf $(DEPENDENCY_FILE)

clobber: very_clean
	@rm -vf $(addprefix $(INSTALL_BIN_DIR)/, $(ANAL_SCRIPTS))
	@rm -vf $(addprefix $(INSTALL_BIN_DIR)/, $(addsuffix .pyc, $(basename $(ANAL_SCRIPTS)))) 
	@rm -vf $(addprefix $(INSTALL_LIB_DIR)/, $(ANAL_LIB)) 
	@rm -vf $(addprefix $(INSTALL_LIB_DIR)/, $(addsuffix .pyc, $(basename $(ANAL_LIB))))

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

#
# Build dependencies in three stages:
# (i) As normal for C src
# (ii) Append for interface hdrs - maintain .h suffix
# (iii) Remove unwanted path prefixes
#

DEPENDENCY_FILE = nprobe_anal.d

$(DEPENDENCY_FILE) :
	if [ ! -f $(DEPENDENCY_FILE) ]; then touch $(DEPENDENCY_FILE); fi

depend: $(DEPENDENCY_FILE) generate
	makedepend -f$(DEPENDENCY_FILE) -- $(DEFINES) -- $(INCLUDES) $(CSRC)
	makedepend -f$(DEPENDENCY_FILE) -o .h -a -- $(DEFINES) -- $(INCLUDES) $(HDR)
	sed -e 's/$(subst /,\/,$(COMMON_SRC_DIR))\///' $(DEPENDENCY_FILE) > deps.tmp
	mv deps.tmp $(DEPENDENCY_FILE)

include $(DEPENDENCY_FILE)
