# $Header: /a/rathlin/disk/src/master/edml/EDML4/fam.src/UPTODATE/RCS/Makefile,v 5.1 91/09/05 18:22:26 edml Exp $
# $Log:	Makefile,v $
# Revision 5.1  91/09/05  18:22:26  edml
# Version 4.2 of EdML
# This version does not yet work on machines with large pointers
# (e.g. DECmips / SGI) - conficts between ASSIGNED/ZERO/MARKBIT macros
# 
# Revision 4.6  90/11/27  16:32:26  rjg
# Branches for native code/srgp
# 
#
.SUFFIXES: .h 
#
# 	You will need to configure the CFLAGS and LIBFLAGS variables 
#	depending on what features you regquire in the fam
#
#       The GNU Readline library (distributed with bash) may be used to 
#	allow line editing.  If you have this software available, 
#	add CFLAGS_RD to CFLAGS and LIBFLAGS_RD to LIBFLAGS
#       See the man pag for details on features available with Readline lib.
#
#
CFLAGS_RD = -DREADLINE -I/usr/local/include
LIBFLAGS_RD = -L/usr/local/lib -lreadline -ltermcap

#       The srgp library may be used if you want to use simple X 
#	window graphics.  Uncomment the relevant fields in CFLAGS and
# 	LIBFLAGS

CFLAGS_SRGP = -DSRGP_GRAPHICS -I/usr/local/include
LIBFLAGS_SRGP = -L/usr/local/lib -L/usr/local/lib/X11R4 -lsrgp -lX11

# uncomment if you want readline and/or srgp
# The PROFILE symbol allows use of -d flag to give 
# runtime profiling of function usage.
# Sun3 requires -J flag for large switch statements

CFLAGS =   #-J #$(CFLAGS_RD)    -DPROFILER #$(CFLAGS_SRGP) -g
LIBFLAGS = -lm #$(LIBFLAGS_RD)  	         #$(LIBFLAGS_SRGP)
LDFLAGS = 

# Uncomment this (and change sun3 to correct arch) if needed
# At present, only sun3 implemented
#ARCH=sun3

.h.o:
	sed -e "s/extern//" -e "s/\/\*\///g" < $*.h > $*.c
	$(CC) -c $(CFLAGS) $*.c
	rm $*.c
.c.s:
	$(CC) -S $(CFLAGS) $*.c

.s.o:
	$(AS) -o $*.o $*.s

.c.o:
	$(CC) -c $(CFLAGS) $*.c

#
#	Define the list of all the primary targets which this makefile
#	creates.  This is most useful when it is used by clobber.
#
ALL = debug profile fam 
OTHER = _dumpcode.c _debug.c _version.c
FAM = fam.o interp.o io.o store.o system.o _dumpcode.o _debug.o _version.o

#
#	How to build the ML interpreter.
#
fam:	$(FAM)
	$(CC) -o fam $(CFLAGS) $(LDFLAGS) $(FAM) $(LIBFLAGS)
	rm -f _*.c _*.o

all:	$(ALL)

#
#	Clean all extraneous files from this directory.
#
clean:
	rm -f *.o
	rm -f ncglobs.h
#
#	Clean all generated files from this directory.
#
clobber:	clean
	rm -f $(ALL) $(OTHER)
#
# NICK: No action, other than generating _*.c
#
export:	$(OTHER)


#
#	How to make a debugging version of the ML interpreter.
#
debug:
	make "CFLAGS=$(CFLAGS)" clean debug2
#
#	How to make a debugging version without starting from scratch.
#
debug2:
	make "CFLAGS=$(CFLAGS) -gx -DDEBUGinterp=1 -DNOTRACE" fam
	mv fam debug

#
#	How to make a debugging version of the ML interpreter.
#
tdebug:
	make "CFLAGS=$(CFLAGS)" clean tdebug2
#
#	How to make a debugging version without starting from scratch.
#
tdebug2:
	make "CFLAGS=$(CFLAGS) -g -DDEBUGinterp=1" fam
	mv fam debug

#
#	How to make a profiling version of the interpreter.
#
profile:
	make "CFLAGS=-p" fam
	mv fam profile

_version.o: _version.c
	$(CC) -c $(CFLAGS) _version.c
	rm -f _version.c

_version.c: *.c *.h
# sccs	grep "@(#)" *.c *.h > _1
	grep "\$$Header" *.c *.h > _1
# sccs	sed 's/.*\(@(#)\) \(.*\)@(#).*/"\1\2",/' < _1 > _2
	sed 's/.*\/\([^\/]*\),v \([^ ]*\).*/"\1 \2",/' < _1 > _2
	echo "char VersionId[][80] = {" | cat - _2 > _version.c
	echo '"@(#)CFLAGS = $(CFLAGS)",' >> _version.c
	echo '"@(#)LDFLAGS = $(LDFLAGS)",' >> _version.c
	echo '"" } ;' >> _version.c
	rm -f _1 _2

#
#	Create the routine which will print a profile of the FAM
#	opcodes.  Note that this routine is built automatically from
#	the master list of opcodes.
#
_debug.o:_debug.c
	$(CC) -c $(CFLAGS) _debug.c

_debug.c:debug.h
	sed -e "s/extern//" -e "s/\/\*\///g" < debug.h > _debug.c

_dumpcode.o:	_dumpcode.c
	$(CC) -c $(CFLAGS) _dumpcode.c

_dumpcode.c:	dumpcode.END bcodes.h
	echo "#ifdef DEBUGinterp" > _dumpcode.c
	echo "char opstrings[][40] = {" >> _dumpcode.c
	sed -n 's/^#[ 	]*define[ 	]*\(Op[^ 	]*\)[ 	]*\(.*\)/"\1",/p' < bcodes.h >> _dumpcode.c
	echo '"INVALID_OPCODE"} ;' >> _dumpcode.c
	echo "int opcode[] = {" >> _dumpcode.c
	sed -n "s/^#[ 	]*define[ 	]*\(Op[^ 	]*\)[ 	]*\(.*\)/\2,/p" < bcodes.h >> _dumpcode.c
	echo '-1 } ;' >> _dumpcode.c
	cat dumpcode.END >> _dumpcode.c

# 
# multiple native code versions control for the deps defs
# use nonenc.h for no Native Code Option
#
ncglobs.h: 
	if test -f $(ARCH)nc.h ; then ln -s $(ARCH)nc.h ncglobs.h ; else ln -s nonenc.h ncglobs.h ; fi

#
#	Automatically build the list of dependencies between source
#	files and header files.  Note that this routine automatically
#	handles nested includes.
#
makefile:Makefile
	rm -f makefile
	cp Makefile makefile
	grep '^#[ 	]*include[ 	]*"' *.[chyl] | \
	sed -e "s/[cyl]:/o:/" \
		-e 's/#[ 	]*include[ 	]*"//' \
		-e 's/"[ 	]*//' >> makefile
	chmod -w makefile




