#				-*-Fun-*-
# Makefile for GNU e?grep
#

XCFLAGS=
MODEL=C
LDFLAGS=/NOE/STACK:0XA000
LIBS=$(GLOB_LIB)
LIB_OBJS=$(BINMODE) $(SETARGV)

# Add -DUSG for System V.
CFLAGS = -A$(MODEL) -DUSG $(GLOB_FLAG) $(XCFLAGS)

BINMODE=c:\msc\lib\binmode.obj

# To use SETARGV.OBJ
# SETARGV=c:\msc\lib\setargv.obj
# GLOB_FLAG=
# GLOB_LIB=

# To use GLOBARGV.LIB (GNU globbing)
SETARGV=
GLOB_FLAG=-DGNU_GLOBBING
GLOB_LIB=globargv.lib


#
# You may add getopt.o if your C library lacks getopt(); note that
# 4.3BSD getopt() is said to be somewhat broken.
#
# Add alloca.o if your machine does not support alloca().
#
OBJS = dfa.obj regex.obj getopt.obj
GOBJ = grep.obj
EOBJ = egrep.obj


default: egrep.exe grep.exe

all: regress

regress: egrep.exe grep.exe
	cd tests; sh regress.sh

egrep.exe: $(OBJS) $(EOBJ) $(GLOB_LIB)
	link $(LDFLAGS) $(OBJS) $(EOBJ) $(LIB_OBJS),$@,nul,$(LIBS)


egrep.obj: grep.c
	$(CC) $(CFLAGS) -DEGREP -Fo$@ -c grep.c
#	rm -f egrep.c; cp grep.c egrep.c
#	$(CC) $(CFLAGS) -DEGREP -c egrep.c
#	rm -f egrep.c

grep.exe: $(OBJS) $(GOBJ) $(GLOB_LIB)
	link $(LDFLAGS) $(OBJS) $(GOBJ) $(LIB_OBJS),$@,nul,$(LIBS)


# Compile regex.c with enhancements for matching `$' with `\r\n' sequences.
regex.obj: regex.c regex.h
	$(CC) $(CFLAGS) -DDOLLARS_MATCH_CRLF -c regex.c

#clean:
#	rm -f grep egrep *.obj core tests/core tests/tmp.script \
#	tests/khadafy.out egrep.c

RM=rm -fv

cleanbackups:
	@$(RM) *.~ *.bak *.old

clean: cleanbackups
	@$(RM) *.lib *.obj tests\tmp.script tests\khadafy.out
#	@$(RM) egrep.c

clobber: clean
	@$(RM) egrep.exe grep.exe

######################################
# GLOBARGV.LIB (GNU globbing of command line arguments)

# Order is in general significant in GLOB_OBJ
GLOB_OBJ=globargv.obj glob.obj msd_dir2.obj getwd.obj

globargv.lib: $(GLOB_OBJ)
	rm -f $@
	lib $@ $(GLOB_OBJ), nul;

msd_dir2.obj glob.obj: msd_dir.h

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

dfa.obj egrep.obj grep.obj: dfa.h
egrep.obj grep.obj regex.obj: regex.h
