# make file for "flex" tool

# $Header: e:/gnu/flex/RCS/makefile 2.3.6.1 90/10/27 22:27:33 tho Exp $

# the first time around use "make first_flex"

BINDIR	= c:/bin
LIBDIR	= d:/usr/lib
INCDIR	= d:/usr/include

vpath %.c $(LIBDIR)
vpath %.h $(INCDIR)

BUILD	= debug
MODEL	= L
MSC	= 600

INSTALL = cp -f
DISK	= b
ZIPFILE = flex23

SKELETON_FILE = $(LIBDIR)/flex.skel
SKELFLAGS = -DDEFAULT_SKELETON_FILE=\\"$(SKELETON_FILE)\\"
DEFS = -A$(MODEL) -W$(WARNINGLEVEL) -DUSG -DMS_DOS -Za -DSTDC_HEADERS

ifeq ($(MSC),600)
WARNINGLEVEL = 4
else
WARNINGLEVEL = 3
endif

ifeq ($(BUILD),debug)
CFLAGS	= -Od -Zi $(DEFS)
CRIPPLED_CFLAGS = $(CFLAGS)
LDFLAGS	= /co/st:8000/noe/far setargv
else
CFLAGS	= -Ox $(DEFS)
ifeq ($(MSC),600)
CRIPPLED_CFLAGS = -Ocegit -Gs $(DEFS)
else
CRIPPLED_CFLAGS = $(CFLAGS)
endif
LDFLAGS = /e/st:8000/noe/far setargv
endif

COMPRESSION =
FLEX_FLAGS = -ist8 -Sflex.ske

# which "flex" to use to generate scan.c from scan.l
FLEX	= flex

OBJS	= ccl.obj dfa.obj ecs.obj gen.obj main.obj misc.obj nfa.obj \
	  parse.obj scan.obj sym.obj tblcmp.obj yylex.obj $(LIBS)
SRCS	= ccl.c dfa.c ecs.c gen.c main.c misc.c nfa.c \
	  parse.y  scan.l sym.c tblcmp.c yylex.c
INCS	= flexdef.h
DOCS	= flex.1 flexdoc.1 flex.man flexdoc.man

LIBS	= _cwild.obj
LOADLIBES= gnulib_$(MODEL)

LIBOBJS	= libmain.obj
FLEXLIB	= flexlib.lib

RCSFILES= changes flex.ske makefile $(SRCS) $(INCS) initscan.c
MISC	= readme readme.dos $(DOCS) tar.-tv \
	  misc/atari.pat misc/makefile.vms misc/msdos.not \
	  misc/readme misc/turbo-c.not

.PHONY: all
all: flex.exe $(FLEXLIB)

flex.exe: $(OBJS)
	$(LINK) $(LDFLAGS) $^, $@, nul, $(LOADLIBES),,

.PHONY: first_flex
first_flex: initscan.c
	cp $^ scan.c
	$(MAKE) $(MFLAGS) flex.exe

.PHONY: install
install: flex.exe flex.ske flexdef.h
	$(INSTALL) flex.exe $(BINDIR)
	$(INSTALL) flex.ske $(LIBDIR)
	$(INSTALL) flexdef.h $(INCDIR)

parse.h parse.c : parse.y
	$(YACC) -d parse.y
	@mv -f y_tab.c parse.c
	@mv -f y_tab.h parse.h

scan.c : scan.l ./flex.ske
	$(FLEX) $(FLEX_FLAGS) $(COMPRESSION) scan.l >scan.c

scan.obj : scan.c parse.h flexdef.h


# MSC 6.0's loop optimizations break this.
tblcmp.obj : tblcmp.c flexdef.h
	$(CC) $(CRIPPLED_CFLAGS) $(CPPFLAGS) -c $<

# Ugly kludge to work around MSC 6.0 environment parsing bug...
# (works only with my GNU make)
main.obj : main.c flexdef.h
	CL $(CFLAGS) -c $(SKELFLAGS) $(CPPFLAGS) main.c

ccl.obj : ccl.c flexdef.h
dfa.obj : dfa.c flexdef.h
ecs.obj : ecs.c flexdef.h
gen.obj : gen.c flexdef.h
misc.obj : misc.c flexdef.h
nfa.obj : nfa.c flexdef.h
parse.obj : parse.c flexdef.h
sym.obj : sym.c flexdef.h
yylex.obj : yylex.c flexdef.h

$(FLEXLIB) : $(LIBOBJS)
	$(AR) $(FLEXLIB) $(ARFLAGS) $^,,,,

tags: $(SRCS)
	etags *.c

.PHONY: clean veryclean
clean:
	$(RM) $(OBJS) scan.c parse.c parse.h errs tags

veryclean: clean
	$(RM) flex.exe patches flex.0[0-9]
	rcsclean $(RCSFILES)

include make.inc

.PHONY: test bigtest
test : flex.exe
	flex $(FLEX_FLAGS) $(COMPRESSION) scan.l | diff -s scan.c -

bigtest :
	rm -f scan.c
	$(MAKE) COMPRESSION="-C" test
	rm -f scan.c
	$(MAKE) COMPRESSION="-Ce" test
	rm -f scan.c
	$(MAKE) COMPRESSION="-Cm" test
	rm -f scan.c
	$(MAKE) COMPRESSION="-Cfe" test
	rm -f scan.c
	$(MAKE) COMPRESSION="-CFe" test
	rm -f scan.c
	$(MAKE) COMPRESSION="-Cf" test
	rm -f scan.c
	$(MAKE) COMPRESSION="-CF" test
	rm -f scan.c
	$(MAKE)


PACKETSIZE = 58

.PHONY: dist
dist: flex.01

flex.01: copying readme.dos makefile flex.exe patches
	shar -Cb16 -F -an'GNUish MS-DOS flex' \
		-L$(PACKETSIZE) -o $(basename $@) $^

# It would be easier if I really kept it in RCS ...

patches: flex.ske flexdef.h misc.c initscan.c
	rcsdiff -c -r2.3.0.3 flex.ske flexdef.h > $@
	rcsdiff -c -r2.3.6.1 misc.c >> $@
	rcsdiff -c -r2.3 initscan.c >> $@
	rcsdiff -c -r2.3 parse.y >> $@
