#
# StuBS: ESSO (Extensible Shell with Scheme Orientation)
# Matthias K"oppe <mkoeppe@cs.uni-magdeburg.de>
#

#DEBUG=1
#LOWLEVEL=1
DEBUGOPTLEVEL=1
NORMALOPTLEVEL=1

# USESHM means: Use shared memory. This will slow down everything,
# but it is required for task-force to work properly.
USESHM=1
OWNEXCEPTIONS=1

#USELIBGXX=1

INCLUDES=-I . -I /usr/include/g++ -I ../../include

ifdef LOWLEVEL
PREDEFINES= -DNDEBUG -D_LowLevel -DUSESHM
LIBES=
else
ifdef USESHM
PREDEFINES= -DNDEBUG -DHEAVY -DUSESHM
else
PREDEFINES= -DNDEBUG 
endif
ifdef USELIBGXX
LIBES=-lg++ -lstdc++ -lc
else
LIBES=-lc -lg++
endif
endif

ifdef DEBUG
DEFINES = $(PREDEFINES) -ggdb3 
#-pg
OPTLEVEL = $(DEBUGOPTLEVEL)
else
DEFINES = $(PREDEFINES) 
OPTLEVEL = $(NORMALOPTLEVEL)
endif
ifneq "$(OPTLEVEL)" "0"
OPTEXCEPT=1	
endif

ifdef OWNEXCEPTIONS
EXCEPTIONS=-DOWNEXCEPTIONS
else
EXCEPTIONS=-fhandle-exceptions
endif

.phony: all essoll clean

all: esso esso.dvi

# Low-level target
essoll: lisp.o essoapp.o mmop.o libfake.o gets.o

gets.o: gets.c
	gcc -c -I../../include -Wall  -o $@ $<

%.ci: %.scm
	sed 's/\\/\\\\/g;s/"/\\"/g;s/^/"/g;s/$$/\\n"/g;' $< > $@

PLUSOPT =
#PLUSOPT = -fstrength-reduce -fexpensive-optimizations 

%.o : %.cc
	gcc -c $(INCLUDES) $(DEFINES) -O$(OPTLEVEL) $(PLUSOPT) -o $@ $<

%.s : %.cc
	gcc -S $(INCLUDES) $(DEFINES) -O$(OPTLEVEL) $(EXCEPTIONS) $(PLUSOPT) -o $@ $<

essoapp.s : essoapp.cc essolib.ci
	gcc -S $(INCLUDES) $(DEFINES) -O$(OPTLEVEL) $(EXCEPTIONS) $(PLUSOPT) -o $@ $<

ifdef OWNEXCEPTIONS
lisp.o : lisp.s
	gcc -c -O$(OPTLEVEL)  -o lisp.o lisp.s
esso.o  : esso.s
	gcc -c -O$(OPTLEVEL)  -o esso.o esso.s
essoapp.o  : essoapp.s 
	gcc -c -O$(OPTLEVEL)  -o essoapp.o essoapp.s
else
ifdef OPTEXCEPT
# This script will patch up the gcc_except_table of lisp.s a bit,
# removing references to non-existing labels (which have been thrown
# out by -O3).
lisp.o : lisp.s
	./fix-s.sh lisp.s 
	gcc -c -O$(OPTLEVEL)  -o lisp.o lisp.s
esso.o  : esso.s
	./fix-s.sh esso.s 
	gcc -c -O$(OPTLEVEL)  -o esso.o esso.s
essoapp.o  : essoapp.s
	./fix-s.sh essoapp.s 
	gcc -c -O$(OPTLEVEL)  -o essoapp.o essoapp.s
else	
lisp.o : lisp.cc lisp.h 
	gcc -c $(INCLUDES) $(DEFINES) -O0 $(EXCEPTIONS)  -o $@ $<
esso.o : esso.cc lisp.h 
	gcc -c $(INCLUDES) $(DEFINES) -O0 $(EXCEPTIONS)  -o $@ $<
essoapp.o : essoapp.cc lisp.h 
	gcc -c $(INCLUDES) $(DEFINES) -O0 $(EXCEPTIONS)  -o $@ $<
endif	
endif

ifdef LOWLEVEL
SYSOBJ = mmop.o
else
ifdef USESHM
ifndef USELIBGXX
SYSOBJ = mmop.o libfake.o
else
SYSOBJ = mmop.o
endif
endif
endif

mmop.o : mmop.cc
	gcc -c $(INCLUDES) $(DEFINES) -O3  $(PLUSOPT) -o $@ $<

esso: lisp.o esso.o $(SYSOBJ)
	gcc $(LIBES) $(DEFINES) lisp.o esso.o $(SYSOBJ) -o esso

SCFILES = essolib.scm buf-test.scm ext-test.scm essoslow.scm esso.sci \
	info.scm frame.scm \
	feature.scm feature.sci \
	profile.scm profile.sci \
	loops.scm loops.sci \
	quizquiz.scm

ESSOFILES = $(SCFILES) lisp.h string pipebase.h stpipe.h esso.txt esso.tex Makefile fix-s.sh filt.sh lisp.cc esso.cc bool assert mmop.cc libfake.cc mmserv.inc asmmacro.h bpipe.h cmailbox.h debug.h defalloc.h essoapp.cc mboxpipe.h gets.c README

tgz: 
	tar cfz esso`date '+%m%d'`.tgz $(ESSOFILES)

clean:
	rm *.o *.s

#
# Docu
#

LATEX=latex
MAKEINDEX=makeindex
DVIPS=dvips
LATEX2HTML=latex2html -no_subdir -html_version 3.1 -address mkoeppe@csmd.cs.uni-magdeburg.de -prefix esso

%.ps: %.dvi
	-$(DVIPS) $*

esso.dvi: esso.tex 
	-$(LATEX) "\batchmode \input{esso.tex}"
	-$(LATEX) "\batchmode \input{esso.tex}"

esso.html: esso.tex
	sed 's/"=/-/g' esso.tex > esso.txl
	-$(LATEX2HTML) esso.txl
