PROGRAM = cscope
MULTIPROGRAM = mlcscope
BIN = $(HOME)/bin

# For Solaris, ilinux and cygwin, comment out
#CURSES = -lcurses

# For Solaris, comment out
#LIBS=-lPW

# for System V Release 2 change the U to D:
SVR2=U

# For Solaris remove the #:
CURSES=/usr/ccs/lib/libcurses.a

# for System V Release 4  or  Solaris  remove the #:
LIBS=-lgen

# for Berkeley UNIX based machines change the U to D:
BSD=U

# for Berkeley UNIX based machines remove the #:
#LIBS = -ltermcap

# for Berkeley UNIX 4.2 change the U to D:
BSD42=U

# for Multi-lingual cscope change the U to D:
MULTILINGUAL=U

# for hppa remove the #s:
#HPPA=-Dhppa

# for cygwin remove the #s:
#LEX = /bin/flex -l
#FLEX = /lib/libfl.a

# for flex remove the #s:
# LEX = $(TOOLS)/gnu/bin/flex -l -L
# LEX = $(TOOLS)/gnu/bin/flex -l -d
LEX = $(TOOLS)/gnu/bin/flex -l
FLEX = $(TOOLS)/gnu/lib/libfl.a

# for ilinux remove the #s:
#LINUX=-I/usr/include/ncurses -Dilinux -D__linux__
#CURSES=-lncurses

# for zlinux remove the #s:
#LINUX=-I/usr/include/ncurses -Dilinux -D__linux__ -Dzlinux
#CURSES=-lncurses

# for cygwin remove the #s:
#CYGWIN=-Dcygwin
#EXEEXT=.exe

# for ANSI or cygwin change the U to D:
STDARG=D

# for darwin remove the #s:
#DARWIN=-I/opt/exp/gnu/include -Ddarwin
#CURSES=/opt/exp/gnu/lib/libncurses.a

# for solaris change the U to D:
USEVFS=D

######################################################################
# Parts of standard lex and yacc compatible with scanner.l/scanner.c
# For ilinux, cannot use the native lex and yacc libraries.
# And this is a problem with more and more OSs, so we always
# supply our own.
######################################################################
LEX_YACC_OBJ = reject.o yyless.o yywrap.o
LEX_YACC_SRC = reject.c yyless.c yywrap.c

# for IBM AIX change the U to D:
AIX = U

# for darwin remove the #s:
#TMPDIR = /var/tmp
TMPDIR = /usr/tmp

# If void * prototypes are not supported, change the D to a U
# These are prototypes like:  compare(const void *s1, const void *s2)
# Examples of compilers that don't support this:  hppa, ibm, sparc
VOID_PROTOTYPE=D

######################################################################
# The compiler defines __STDC__ on the following machine types:
#	i486
#	sgi
#	ilinux
#	solaris
#
# __STDC__ is NOT defined on:
#	sparc
#	ibm
#	uts4
#	hppa
#
# If __STDC__ is defined, then generally the ANSI function prototypes
# are supported.
######################################################################

DEBUG   = -O
DFLAGS  = -$(SVR2)SVR2 -$(BSD)BSD -$(BSD42)BSD42 -$(USEVFS)USEVFS \
	  -$(VOID_PROTOTYPE)VOID_PROTOTYPE -$(MULTILINGUAL)MULTILINGUAL \
	  $(HPPA) $(LINUX) $(CYGWIN) $(DARWIN) -$(AIX)AIX -$(STDARG)STDARG \
	  -DTMPDIR=\"$(TMPDIR)\"
CFLAGS	= $(DEBUG) $(DFLAGS) $(PROF)

LDFLAGS = $(DEBUG)
LFLAGS  = -n
LINT	= lint

COBJ	= cscanner.o clookup.o 
JAVAOBJ	= jscanner.o jlookup.o 
SDLOBJ	= sscanner.o slookup.o 
LANGOBJ	= $(COBJ) $(JAVAOBJ) $(SDLOBJ)
MULTIOBJ = lexscanner.o
ORIGOBJ	= scanner.o lookup.o
OBJ	= main.o dir.o crossref.o command.o cache.o ccalls.o \
	  display.o find.o edit.o exec.o help.o history.o input.o menu.o
LIBOBJ	= alloc.o basename.o cgrep.o compath.o fixkeypad.o getwd.o invlib.o \
	  logdir.o mouse.o mygetenv.o mygetwd.o mypopen.o putenv.o regcmp.o regex.o \
	  strtok.o vp.o vpaccess.o vpfopen.o vpinit.o vpopen.o vpstat.o
OBJECTS = $(OBJ) $(ORIGOBJ) $(LIBOBJ) $(LEX_YACC_OBJ)
MULTIOBJECTS = $(OBJ) $(LANGOBJ) $(LIBOBJ) $(MULTIOBJ)
CSRC	= cscanner.l clookup.c
JAVASRC	= jscanner.l jlookup.c
SDLSRC	= sscanner.l slookup.c
LANGSRC	= $(CSRC) $(JAVASRC) $(SDLSRC)
MULTISRC = lexscanner.c
ORIGSRC = scanner.l lookup.c
LEXSRC  = cscanner.c jscanner.c sscanner.c scanner.c
SRC	= constants.h global.h library.h version.h \
	  main.c dir.c crossref.c command.c cache.c ccalls.c \
	  display.c find.c edit.c exec.c help.c history.c input.c
LIBSRC	= alloc.c basename.c cgrep.c compath.c fixkeypad.c ndir.h getwd.c \
	  invlib.c invlib.h logdir.c menu.c mouse.c mouse.h mygetenv.c \
	  mygetwd.c mypopen.c putenv.c regcmp.c regex.c strtok.c \
	  vp.h vp.c vpaccess.c vpfopen.c vpinit.c vpopen.c vpstat.c
WIN32SRC = dirent.c dirent.h sleep.c
SOURCE	= $(SRC) $(ORIGSRC) $(LIBSRC) $(LEX_YACC_SRC) $(WIN32SRC)
MULTISOURCE = $(LANGSRC) $(MULTISRC)
CSCALLS = cscalls.sh cscalls.1
BORLAND	= cscope.dsk cscope.mak cscope.prj egrep.y cscope95.mak vcwin32.mak
OTHER	= COPYING README makefile emacs.e gmacs.ml notes.dos notes.w95 notes.win32 notes.cygwin fixScanner.pl stdscan.c homepage/index.html homepage/cscope.html homepage/mlcscope.html homepage/cscalls.html 
FILES	= $(OTHER) $(BORLAND) $(SOURCE) $(MULTISOURCE) $(MULTIPROGRAM).1 $(PROGRAM).1 $(CSCALLS) $(EXTRA)

$(PROGRAM)$(EXEEXT): $(OBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(OBJECTS) $(CURSES) $(LIBS)

$(MULTIPROGRAM)$(EXEEXT): $(MULTIOBJECTS)
	$(CC) $(LDFLAGS) -o $@ $(MULTIOBJECTS) $(CURSES) $(FLEX) $(LIBS)

$(MULTIOBJECTS): constants.h global.h
$(OBJECTS): constants.h global.h
cgrep.o:	library.h
ccompath.o:	library.h
dir.o:		ndir.h vp.h
display.o:	version.h
find.o:		vp.h
getwd.o:	ndir.h
invlib.o:	invlib.h library.h constants.h
main.o:		version.h
main.o:		vp.h
mouse.o:	mouse.h
vp.o:		vp.h
vpaccess.o:	vp.h
vpfopen.o:	vp.h
vpinit.o:	vp.h
vpopen.o:	vp.h
vpstat.o:	vp.h
jscanner.o:	jscanner.c
sscanner.o:	sscanner.c
cscanner.o:	cscanner.c
scanner.o:	scanner.c
stdscan.c:	scanner.c

######################################################################
# Special rules needed to get past oddball buffering problem in the
# lex-generated scanner.  Basically, yytext[] is declared with YYLMAX
# and this declaration happens BEFORE our new & improved value for
# YYLMAX is included (for some versions of lex.  Not all versions).
#
# So, we prepend our header, strip out the old declaration of YYLMAX
# from the output of lex and we have the working scanner.c
#
# Also, some compilers don't like the #pragma or #ident controls.
######################################################################
jscanner.c:	jscanner.l global.h constants.h
	echo '#ifdef MULTILINGUAL' > jscanner.c
	echo '#include "global.h" /* for YYLMAX, STMTMAX, ... */' >> jscanner.c
	$(LEX) -Pjyy -t jscanner.l | \
	egrep -i -v '^#pragma|^#ident' |\
	egrep -v '^#[ 	]*define[ 	]*YYLMAX[ 	]' >> jscanner.c
	echo '#endif' >>jscanner.c

sscanner.c:	sscanner.l global.h constants.h
	echo '#ifdef MULTILINGUAL' > sscanner.c
	echo '#include "global.h" /* for YYLMAX, STMTMAX, ... */' >> sscanner.c
	$(LEX) -Psyy -t sscanner.l | \
	egrep -i -v '^#pragma|^#ident' |\
	egrep -v '^#[ 	]*define[ 	]*YYLMAX[ 	]' >> sscanner.c
	echo '#endif' >>sscanner.c

cscanner.c:	cscanner.l global.h constants.h
	echo '#ifdef MULTILINGUAL' > cscanner.c
	echo '#include "global.h" /* for YYLMAX, STMTMAX, ... */' >> cscanner.c
	$(LEX) -Pcyy -t cscanner.l | \
	egrep -i -v '^#pragma|^#ident' |\
	egrep -v '^#[ 	]*define[ 	]*YYLMAX[ 	]' >> cscanner.c
	echo '#endif' >>cscanner.c

scanner.c:	scanner.l global.h constants.h
	echo '#ifndef MULTILINGUAL' > scanner.c
	echo '#include "global.h" /* for YYLMAX, STMTMAX, ... */' >> scanner.c
	lex -n -t scanner.l | \
	egrep -i -v '^#pragma|^#ident' |\
	egrep -v '^#[ 	]*define[ 	]*YYLMAX[ 	]' >> scanner.c
	echo '#endif' >>scanner.c

stdscan.c:	scanner.c fixScanner.pl
	# cp scanner.c stdscan.c
	chmod +x fixScanner.pl
	# This handles changes in STDIO for RedHat 7.0+
	./fixScanner.pl < scanner.c > stdscan.c

cscalls:	cscalls.sh
	cat cscalls.sh >cscalls
	chmod +x cscalls

######################################################################
# This is only needed for defective AT&T Curses library.  It doesn't
# even compile on modern versions of Curses.
######################################################################
fixkeypad.o:
	$(CC) $(CFLAGS) -c -I/usr/src/lib/libcurses/screen fixkeypad.c

emacs: emacs.e
	-ecomp $@

all: $(PROGRAM)$(EXEEXT) $(MULTIPROGRAM)$(EXEEXT)

install: 1.$(PROGRAM) 1.$(MULTIPROGRAM)
	cp $(PROGRAM)$(EXEEXT) $(BIN)/$(PROGRAM)$(EXEEXT)
	cp 1.$(PROGRAM) $(HOME)/man/1.$(PROGRAM)
	cp $(MULTIPROGRAM)$(EXEEXT) $(BIN)/$(MULTIPROGRAM)$(EXEEXT)
	cp 1.$(MULTIPROGRAM) $(HOME)/man/1.$(MULTIPROGRAM)

cpio: $(PROGRAM).cpio

$(PROGRAM).cpio: $(FILES)
# needed for Solaris 5.x systems
	if [ `uname -r | cut -c1` = 5 ] ; then \
	ls $(FILES) | sort | cpio -oH odc >$@; else \
	ls $(FILES) | sort | cpio -oc >$@; fi

uncpio:	uncompress
	cpio -icumdv <$(PROGRAM).cpio

compress:	$(PROGRAM).cpio
	compress -f $(PROGRAM).cpio

uncompress:	$(PROGRAM).cpio.Z
	uncompress -f $(PROGRAM).cpio.Z

lint:	
	$(LINT) $(LFLAGS) $(DFLAGS) $(SOURCE) $(MULTISOURCE)

check:	checkcscope checkmlcscope

checkcscope:
	if [ -f $(PROGRAM)$(EXEEXT) ] ; then \
	./$(PROGRAM)$(EXEEXT) -q -b 2>&1; \
	./$(PROGRAM)$(EXTEXT) -d -L -0main; \
	ls -l $(PROGRAM)$(EXEEXT) $(PROGRAM).out $(PROGRAM).*.out; \
	fi

checkmlcscope:
	if [ -f $(MULTIPROGRAM)$(EXEEXT) ] ; then \
	./$(MULTIPROGRAM)$(EXEEXT) -q -b 2>&1; \
	./$(MULTIPROGRAM)$(EXTEXT) -d -L -0main; \
	ls -l $(MULTIPROGRAM)$(EXEEXT) $(PROGRAM).out $(PROGRAM).*.out; \
	fi

chkversion:	cscalls
	if [ "`./cscope$(EXEEXT) -V 2>&1 | cut -f3 -d' '`" != $(VERSION) ] ; \
	then echo "Need to update VERSION=$(VERSION) in Makefile"; \
	fi; \
	if [ "`./cscalls -V 2>&1 | cut -f3 -d' '`" != $(VERSION) ] ; \
	then echo "Need to update VERSION=$(VERSION) in cscalls.sh"; \
	fi

clean:
	rm -f *.o *.out *.cpio *.cpio.Z core ctc_*

distclean: clean cleanlex

clobber: distclean 
	rm -f $(PROGRAM)$(EXEEXT) $(MULTIPROGRAM)$(EXEEXT)
	rm -f 1.$(PROGRAM) 1.$(MULTIPROGRAM)

cleanlex:
	rm -f $(LEXSRC)

1.$(PROGRAM):	$(PROGRAM).1
	tbl < $(PROGRAM).1 | nroff -Tlp -man -rO0 | col -x > 1.$(PROGRAM)

1.$(MULTIPROGRAM):	$(MULTIPROGRAM).1
	tbl < $(MULTIPROGRAM).1 | nroff -Tlp -man -rO0 | col -x > 1.$(MULTIPROGRAM)

libl.a:
	cd libLY/libl; make -f libl.mk libl.a; ln lib/libl.a ../../.

liby.a:
	cd libLY/liby; make -f liby.mk liby.a; ln liby.a ../../.
