# This is source code to CASL (Custom Audit Scripting Language)
#
# Copyright 1998 Secure Networks, Inc.
# Copyright 1999 Network Associates, Inc.
# All Rights Reserved
#
# Redistribution and use are governed by the terms detailed in the
# license document ("LICENSE.TXT") included with this source.  If the
# document is missing, it may be obtained from Network Associates, Inc.

# SET THIS
SUFFIX=

# if not normal GNU system...

CPP=		cpp
CPPCMD=		cpp -

MAKE=make

# overrideable by environmental variable
ifndef PCAP
PCAP=	libpcap-0.4
endif

# UNCOMMENT SECOND ONE IF YOU'RE COMPILING RELEASE VER

#RELEASE=
RELEASE=	-DRELEASE

# SET THESE IF YOU'RE DEBUGGING THE INTERPRETER. 

#TPATH= `pwd`/build
TARGET= casl

# Lex flags

LFLAGS= -ocasl-lex.c

# Yacc flags

YFLAGS= -d

# Linker flags

MISCLDFLAGS=

# Compiler flags

MISCFLAGS=  #-DHAVE_LIBGC

OSFLAGS= -D_FREEBSD -U_LINUX -D_BSD

# turned off in release compile

WARNS= -Wall
#PARSE_DEBUG= -DYYDEBUG=1
#DEBUG= -g -DTRACE_CALLS -DHAVE_DEBUG #-DALLOCATION_TRACE

# turned off in debug compile

#OPTIMIZATIONS=
OPTIMIZATIONS= -O3

# enable automatic gdb execution on assert() failure, as well as the
# "debug" keyword in the CASL language 

#DEBUG+= -D__EXECUTABLE_FILE__="\"${TPATH}/${TARGET}\""
#DEBUG+= -D__ASSERT_AUTO_UNSUSPEND__

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

INCLUDE_PATH= 	-Iinclude 
INCLUDE_PATH+=	-I${PCAP}

CFLAGS= ${INCLUDE_PATH} 	\
	${PARSE_DEBUG} 		\
	${DEBUG} 		\
	${OPTIMIZATIONS} 	\
	${WARNS}

CFLAGS+= ${MISCFLAGS}
CFLAGS+= ${OSFLAGS}
CFLAGS+= ${RELEASE}

CFLAGS+= -DCASL

ifdef CPP
CFLAGS+= -DCPPFILE="\"${CPP}\""
endif

ifdef CPPCMD
CFLAGS+= -DCPPCOMMAND="\"${CPPCMD}\""
endif

LIBS= -lpcap -lreadline -ltermcap

LDFLAGS= -L${PCAP} ${MISCLDFLAGS}

DEPENDFILE= build/dependancies

include arch.mk

OBJS=  	main.o 				\
	statements.o 			\
	expressions.o 			\
	functions.o 			\
	structures.o 			\
	asr-nodes.o 			\
	asr-list.o 			\
	asr-arity.o 			\
	asr-alloc.o			\
       	symbols.o 			\
	utils.o 			\
	builtins-gen.o 			\
	casl-lex.o			

LOBJS= table.o strutil.o setbits.o memutils.o list.o \
       strsep.o setenv.o getopt.o snprintf.o

CAPEOBJS= 	support.o 		\
		arp.o 			\
		cape-output.o 		\
		cape-input.o 		\
		ether_addr.o		\
		inetaddrs.o		\
		nettab.o		\
		ostab.o

BTOBJS= fileio.o random.o stepxstep.o
CPPOBJS= cccp.o cexp.o
SRCS= $(OBJS:.o=.c) $(LOBJS:.o=.c) $(CAPEOBJS:.o=.c)
OBJECTS= ${OBJS} ${LOBJS} ${CAPEOBJS} ${BTOBJS}
INCID= parse.tab.o

vpath %.c lib cape misc-builtins gnu-cpp os/${SNI_OSTYPE}
vpath %.d lib cape misc-builtins gnu-cpp

all: ${TARGET} cpp

${TARGET} : ${OBJECTS} parse.tab.y ${PCAP}/libpcap.a
	${CC} ${STATIC} ${CFLAGS} -o $@ ${OBJECTS} ${INCID} ${LDFLAGS} ${LIBS}
	mv $@${SUFFIX} build/${TARGET}${SUFFIX}
	rm -f parse.tab.c y.tab.c y.tab.h casl-lex.c

${TARGET}-dynamic : ${OBJECTS} parse.tab.y ${PCAP}/libpcap.a
	${CC} ${CFLAGS} -o $@ ${OBJECTS} ${INCID} ${LDFLAGS} ${LIBS}
	mv $@${SUFFIX} build/${TARGET}${SUFFIX}
	rm -f parse.tab.c y.tab.c y.tab.h casl-lex.c

builtins-gen.o : builtins.c build/gen-builtins
	build/gen-builtins 
	${CC} ${CFLAGS} -c builtins-gen.c

build/gen-builtins : gen-builtins.c 
	${CC} -g -o $@ gen-builtins.c lib/strsep.c

cpp: $(CPPOBJS)
	${CC} ${STATIC} ${CFLAGS} -o $@ ${CPPOBJS}
	rm -f cexp.[ch]
	mv $@${SUFFIX} build

.PHONY: depend clean new build sortaclean count
depend :
	chmod a+x build/depends
	cd build; ./depends

-include ${DEPENDFILE}

casl-lex.o : casl-lex.l parse.tab.y
	${YACC} ${YFLAGS} parse.tab.y
	chmod a+x build/yysed
	build/yysed y.tab.c
	mv -f y.tab.c parse.tab.c
	${CC} ${CFLAGS} -c parse.tab.c

	${LEX} ${LFLAGS} -t casl-lex.l > casl-lex.c
	build/yysed casl-lex.c
	${CC} ${CFLAGS} -c casl-lex.c

cexp.o : gnu-cpp/cexp.y
	${YACC} ${YFLAGS} gnu-cpp/cexp.y
	mv y.tab.c cexp.c; mv y.tab.h cexp.h
	${CC} ${CFLAGS} -c cexp.c

${PCAP}/libpcap.a : ${PCAP}/Makefile
	cd ${PCAP} && ${MAKE}

${PCAP}/Makefile :
	cd ${PCAP} && ./configure

clean :
	rm -f ${TARGET}${SUFFIX} *.o lib/*.o 				 \
		misc-builtins/*.o cape/*.o *.d *.core casl-lex.c \
		lex.yy.c y.tab.h build/*.core build/casl build/cpp \
		build/gen-builtins builtins-gen.c
	-cd ${PCAP} && ${MAKE} clean

sortaclean:
	rm -f *.o lib/*.o *.d *.core casl-lex.c lex.yy.c y.tab.h

distclean:
	rm -f ${TARGET}${SUFFIX} *.o lib/*.o 				 \
		misc-builtins/*.o cape/*.o *.d *.core casl-lex.c \
		lex.yy.c y.tab.h build/*.core build/casl build/cpp \
		build/gen-builtins builtins-gen.c
	-cd ${PCAP} && ${MAKE} distclean

build: ${TARGET} sortaclean

new : clean ${TARGET}

fresh : clean build

count :
	find . -name "*.[chyl]" | xargs wc -l 

parser : parse.tab.o casl-lex.o 
