# File:    Makefile
# Author:  Johan Bevemyr
# Created: Thu Oct 24 13:19:35 MET 1991
# Purpose: Makefile for Reform and Luther


TOPDIR= /home/groucho/csd/bevemyr/Luther2

LIBDIR= ${TOPDIR}/NewLibrary

WAMLIB= ${LIBDIR}/builtin

######################################################################
# 
# Define the proper compiler etc. 
#

CC= gcc
#CC= /usr/gnu/bin/gcc2

YACC= bison
LEX= flex

LDFLAGS= -lm

DATE= `date '+%y-%m-%d-%H.%M'`

VERSIONDATE= `date`
VERSION= 0
SUBVERSION= `cat version.txt`

M4 = /usr/bin/m4

SED= sed

######################################################################
#
# Not all systems have gnu-make. Uncomment the proper one for 
# your system.
#

MAKE= gnu-make -j 4
#MAKE= make 
#MAKE= gmake  -j 10
#MAKE= gmake -j 3
#MAKE= gmake 
#MAKE= gnu-make -j 4

######################################################################
#
# OPT is the optimization level to be used. Uncomment the desired
# optimization level for your system.
#

#OPT= -fast
#OPT= -O +Obb1100
#OPT= -O
#OPT= -O2
#OPT= -O3
#OPT= -O4
OPT= -pipe -O4
#OPT= -pipe -O3
#OPT= -pipe -O2
#OPT= -pipe -O
#OPT= -g -O2
#OPT= -g -O -Wall
#OPT= -g -pipe -O
#OPT= -g -Wall
#OPT= -pipe -g


######################################################################
#
# OS_TYPE is the type of operation system the target system has. 
# Uncomment the proper one.
#
# APOLLO	Used when compiling for Apollo Domain OS
# HP_UX		Used when compiling for HP 9000/720 
# SUN		Used when compiling for SUN 
# SEQUENT	Used when compiling for SEQUENT
#

OS_TYPE= -DSUN
#OS_TYPE= -DSUN
#OS_TYPE= -DHP_UX
#OS_TYPE= -DAPOLLO
#OS_TYPE= -DSEQUENT

######################################################################
#
# The following flags are avaliable
#
# DEBUG         Compile with wamdebugger.
# STATISTICS	Statistics of the number of calls, fails, and calls to 
#               builtin predicates
# LOAD		Define load/1 so that strait WAM code can be read.
# NEW_READ      Used when compiling with the new read schema.
# NEW_READ_NOC  Used when compiling with the new read schema, WITHOUT counting.
# NEW_WRITE     Used when compiling with the new write schema.
# ANSI		Used when compiling with prototypes
# E_CALL	Used when compiling with extended call (var_call)
# ANSI_C        Used when the compiler understands prototypes.
# TIMESTAMP     Extends each variable with an extra 'create time' word.
# LOCK          Mutually exclusive locks (parallel version).
# CCSWAP        When compiling parallel version with cc (compared to gcc).
# SEMILOCK      
# IND_THREAD    Inderect treaded code. Only gcc 2.0 can do this.
# PARALLEL	Used when compiling a reform parallel version
#		(defines SEMAPHORE, LOCK, SHARED, TIMESTAMP (see config.h))

YACC_LOAD= -DLOAD
NEW_READ= -DNEW_READ
NEW_WRITE= -DNEW_WRITE
ANSI= -DANSI_C
VARDISPLAY= -DVARDISPLAY
P_WALL= -DPARALLEL_WALL
IND_THREAD= -DTHREADED_IND
PREFETCH= -DPREFETCH
JUMP_CALL= -DJUMP_CALL
PARALLEL= -DPARALLEL

#	WAM_DEBUG= -DDEBUG
#	COPY_GC=-DCOPY_GC
#	STAT_AWAIT= -DSTAT_AWAIT
#	WAM_STAT= -DSTATISTICS
#	UNBOUND= -DUNBOUND
#	BOUNDED_Q= -DBOUNDED_Q
#	PARALLEL_BQ= -DPARALLEL_BQ
#	SPIN_LOCK= -DSPIN_LOCK
#	CACHE_G= -DCACHE_G
#	TRAIL_ALL= -DTRAIL_ALL
#	LOCALPRED= -DLOCALPRED
#	CCSWAP= sun4.il
#	NEW_READ_NOC= -DNEW_READ_NOC
#	E_CALL= -DEXTENDED_CALL
#	SEMILOCK= -DSEMILOCK
#       DIS= -DDISABLE_HEAP_RECLAIM 

COMMON_CFLAGS= $(WAM_DEBUG) $(WAM_STAT) $(YACC_LOAD) $(NEW_READ) $(NEW_WRITE) $(ANSI) $(NEW_READ_NOC) $(PARALLEL) $(E_CALL) $(SEMILOCK) $(LOCALPRED) $(BOUNDED_Q) $(IND_THREAD) $(PREFETCH) $(SPIN_LOCK) $(TRAIL_ALL) $(VARDISPLAY) $(P_WALL) $(UNBOUND) $(STAT_AWAIT) $(CACHE_G) $(PARALLEL_BQ) $(JUMP_CALL) $(COPY_GC) $(DIS)

CFLAGS= $(OPT) $(OS_TYPE) $(COMMON_CFLAGS) $(CCSWAP) 

# These defs, objs and src should be used if you want the system to
# be able to read strait WAM code. 

WDEFS=	
WOBJ=   parser.yy.o  parser.tab.o 
WSRC=   parser.y.m4 parser.l.m4
 
DEFS=   builtin.h debug.h error.h inout.h storage.h c.h display.h include.h \
        instrdef.h term.h constants.h display_code.h initial.h unify.h \
	database.h engine.h inline.h statistics.h time.h qload.h \
	atom_table.h event.h expand_file_name.h \
	wam-main.h config.h gc.h assert.h labelsort.h array.h lock.h sun.h \
        semaphore.h load.h think.h load.h sequent.h unix.h worker.h deref.h \
	bind.h trail.h instructions.h engine_threaded.c signal.h parallel.h \
	version.h

OBJ=    error.o inout.o storage.o database.o builtin.o time.o \
	display_code.o initial.o term.o debug.o engine.o \
	inline.o unify.o expand_file_name.o statistics.o \
	display.o qload.o atom_table.o sequential-main.o wam-main.o \
	gc.o assert.o instrdef.o labelsort.o $(WOBJ) alloca.o array.o sun.o \
	semaphore.o think.o load.o sequent.o unix.o parallel.o \
	copy-gc-struct.o signal.o

SRC= 	builtin.c display.c error.c inout.c storage.c database.c \
	display_code.c initial.c term.c debug.c engine.c inline.c \
	unify.c expand_file_name.c statistics.c time.c qload.c \
	atom_table.c event.c wam-main.c sequential-main.c \
	gc.c assert.c instrdef.c labelsort.c $(WSRC) alloca.c array.c sun.c \
	semaphore.c think.c load.c sequent.c unix.c parallel.c \
	copy-gc-struct.c signal.c

MISC=	Makefile version.txt Makefile.orig configure.sh

.c.o:
	${CC} ${CFLAGS} -c $*.c 

all:
	$(MAKE) luther

configure:
	sh configure.sh

remake: clean all

luther:	objectfiles lut $(WAMLIB)2.wam
	${CC} $(OBJ) $(LDFLAGS) -o luther
	chmod a+xr luther

lut: 
	echo `pwd`/luther -wam $(WAMLIB)2.wam $$\* > lut
	chmod a+x lut

objectfiles:$(P) $(OBJ)

$(OBJ): $(DEFS)

m4_defs : 
	echo $(OS_TYPE) $(COMMON_CFLAGS) |\
	$(SED) 's:-D\([a-zA-Z_]*\):define(\1,1):g' |\
	tr ' ' '\012' > m4_defs

$(WAMLIB)2.wam : $(WAMLIB).wam
	cat $(WAMLIB).wam | $(SED) 's:##libdir##:$(LIBDIR):g' > $(WAMLIB)2.wam

parser.y : parser.y.m4 m4_defs
	$(M4) m4_defs parser.y.m4 > parser.y

parser.l : parser.l.m4 m4_defs
	$(M4) m4_defs parser.l.m4 > parser.l

parser.yy.c: parser.l parser.tab.c
	$(LEX) $(LFLAGS) parser.l 
	mv lex.yy.c parser.yy.c

parser.tab.c parser.tab.h: parser.y 
	$(YACC) $(YFLAGS) -d parser.y

parser.tab.bin: parser.tab.c
	$(CC) $(CFLAGS) -c parser.tab.c

newversion: version clean all

version:
	mv version.txt version.tmp
	cat version.tmp | awk '$$1 { print $$1+1 }' > version.txt
	rm version.tmp

version.h: version.txt 
	rm -f version.h
	echo '#define LUTHER_VERSION "'$(VERSION)":"$(SUBVERSION)'"'>version.h
	echo '#define LUTHER_COMPILE_DATE "'$(VERSIONDATE)'"' >> version.h

TAGS: $(SRC) $(DEFS)
	etags $(DEFS) $(SRC) 

remsem:
#	removesem $(USER)

clean: remsem
	/bin/rm -f .luther_tmp parser.y parser.l
	/bin/rm -f $(OBJ) $(WOBJ)
	/bin/rm -f m4_defs version.h

realclean: clean
	/bin/rm -f parser.yy.c parser.tab.h parser.tab.c lut 
	/bin/rm -f $(WAMLIB)2.wam

wipeout: realclean
	/bin/rm -f luther opcode opcode.pl 
	/bin/rm -f transputer.h xmakefile core errs TAGS

opcode : opcode.c instrdef.h qload.h
	${CC} ${CFLAGS} -DOPCODE_TABLE opcode.c -o opcode

removed: removed.c
	cc removed.c -o removed

backup: 
	mkdir SaveLibrary
	cp -r $(LIBDIR) SaveLibrary
	tar cf - $(MISC) $(DEFS) $(SRC) SaveLibrary | \
	    compress > Backup-$(DATE).tar.Z
	/bin/rm -r SaveLibrary

macback: parser.y parser.l
	mkdir SaveLibrary
	cp -r $(LIBDIR) SaveLibrary
	tar cf - $(MISC) $(DEFS) $(SRC) parser.l parser.y \
	    SaveLibrary | compress > Backup-$(DATE).tar.Z
	/bin/rm -r SaveLibrary

sicsbackup : 
	mkdir SaveLibrary
	cp -r $(LIBDIR) SaveLibrary
	tar cf - $(MISC) $(DEFS) $(SRC) SaveLibrary | \
	    compress > tmp.sics.backup.Z
	/bin/rm -r SaveLibrary
	echo "binary #cd LutherBackup #put tmp.sics.backup.Z " \
	     "Backup-"$(DATE)".tar.Z #bye #" | tr '#' '\012' | \
	     ftp sics.se
	/bin/rm tmp.sics.backup.Z
	
distribution:
	mv -f $(WAMLIB)2.wam tmptmp
	mkdir Distribution
	cp -r $(LIBDIR) Distribution
	mkdir Distribution/Emulator
	cp $(MISC) $(DEFS) $(SRC) Distribution/Emulator
	tar cf - Distribution | compress > Dist-$(DATE).tar.Z
	/bin/rm -r Distribution
	mv -f tmptmp $(WAMLIB)2.wam 

