# Makefile for MetaPost in its various incarnations.  It is appropriate
# for the SAS/C Development System Version 6.x by SAS Institute Inc.
# and was written by Andreas Scherer, April 6, 1995.

# With this SMakefile several different versions of MetaPost can be
# created (one at a time!).  The standard is No20MetaPost.  It is as
# close to the Web2C implementation as possible.
#
#     CHANGEFILE = cmp.ch
#     HEADERFILE = mpd.h
#     DEFS       = cpu=ANY data=FAR
#     PS         = no20
#     LOBJ       = lib:c.o
#     LIBRARIES  = lib:scmieee.lib lib:scnb.lib
#
# An improved version for standard Amigas will provide a runtime
# memory configuration and a resident version of IniMP.
#
#     CHANGEFILE = mmp.ch
#     HEADERFILE = mpmemory.h
#     DEFS       = cpu=ANY define=VARMEM
#     PS         = no20
#     LOBJ       = lib:cres.o
#     LIBRARIES  = lib:scmieee.lib lib:sc.lib
#
# Advanced hardware configurations work with IniMP.  This uses the
# runtime memory configuration as well.
#
CHANGEFILE = mmp_20.ch
HEADERFILE = mpmemory.h
DEFS       = cpu=68020 define=SPEEDUP define=VARMEM
PS         =
LOBJ       = lib:cres.o
LIBRARIES  = lib:scmieee.lib lib:sc.lib

SED = sed
SHELL=rx
TIE=tie
WMERGE=wmerge
srcdir = .
VPATH = .

# The missing `configure' mechanism requires this to be done by hand.
bindir = TeXMF:bin/web2c61
configdir = TeXMF:web2c61/config
mppooldir = TeXMF:web2c61/pool

# This is so kpathsea will get remade automatically if you change
# something in it and recompile from the package directory.
kpathsea_parent = ../..
kpathsea_dir = $(kpathsea_parent)/kpathsea
kpathsea = $(kpathsea_dir)/kpathsea.lib
kpathsea_20 = $(kpathsea_dir)/kpathsea_20.lib

# Routines used everywhere.
commondefines = $(srcdir)/../lib/common.defines $(srcdir)/../lib/texmf.defines \
	$(srcdir)/../lib/texmfextras.defines
commonh = $(srcdir)/../lib/andyconfig.h
commono = ../lib/lib.lib $(kpathsea)
commono_20 = ../lib/lib_20.lib $(kpathsea_20)

# Routines used in TeX and Metafont.
extrac = $(srcdir)/../lib/andytexmf.c
extrah = $(srcdir)/../lib/andytexmf.h $(srcdir)/../lib/texmfmem.h $(HEADERFILE)

# Routines used in TeX, Metafont, and BibTeX.
fileioc = $(srcdir)/../lib/andyopeninout.c
fileioo = openinout.o

objs = $(fileioo) mp0.o mp1.o mp2.o mp3.o mp4.o mp5.o mp6.o mp7.o mp8.o \
	mp9.o mp10.o
iobjs = iextra.o inimp.o
vobjs = vextra.o imp.o

# Where the test files are.
mpsrcdir = $(srcdir)/../mp
mpdistdir = $(srcdir)/../mpdist
trapdir = $(mpsrcdir)/MPtrap

program = $(PS)inimp
vprogram = $(PS)virmp

CC=sc
CFLAGS = $(DEFS) math=IEEE code=far stackcheck noicons optimize
CPPFLAGS = define=_STRICT_ANSI define=MP idir=$(srcdir)/../lib idir=../..

CCLD = slink
LN = type

LDFLAGS = noicons $(XLDFLAGS)
libs = $(commono) $(LIBRARIES)
libs_20 = $(commono_20) $(LIBRARIES)

# This code was written by Stefan Scherer especially for
# this Amiga implementation.
speedup = ../mf/64bit/64bit.o

INSTALL = copy
INSTALL_PROGRAM = copy
INSTALL_DATA = copy

# The mems we know how to make.
mems = plain.mem mfplain.mem

# And how to make them.
inimp = MPPOOL=. ./$(PS)inimp -I

.SUFFIXES:
.SUFFIXES: .o .c
.c.o:
	$(CC) $(CPPFLAGS) $(CFLAGS) $*.c

default: all
all: $(program)
triptrap: etrapmp

inimp: $(iobjs) $(objs) $(commono_20) $(speedup)
	$(CCLD) to inimp from $(LOBJ) $(iobjs) $(objs) $(speedup) \
	lib $(libs_20) $(LDFLAGS)

no20inimp: $(iobjs) $(objs) $(commono)
	$(CCLD) to no20inimp from $(LOBJ) $(iobjs) $(objs) \
	lib $(libs) $(LDFLAGS)

# You don't need to make virmp because the change file makes inimp behave
# like virmp when not invoked with a -I switch.  The `virmp' target is provided
# just in case you want a separate executable that is slightly more compact.
virmp: $(vobjs) $(objs) $(commono_20) $(speedup)
	$(CCLD) to virmp from $(LOBJ) $(vobjs) $(objs) $(speedup) \
	lib $(libs_20) $(LDFLAGS)

no20virmp: $(vobjs) $(objs) $(commono)
	$(CCLD) to no20virmp from $(LOBJ) $(vobjs) $(objs) \
	lib $(libs) $(LDFLAGS)

# This target is only for testing arithmetic routines.  Run timemath with
# no arguments and use the results printed on standard output to decide if
# the external routines used when `FIXPT' is not defined in c-auto.h work
# properly and run faster than the standard arithmetic routines.
timemath: time.o cmath.o pmath.o mathtest.o
	$(CC) $(CPPFLAGS) $(CFLAGS) link to timemath \
	from time.o cmath.o pmath.o mathtest.o

time.o: andytime.o
	copy andytime.o time.o
andytime.o: andytime.c andytest_mpd.h $(extrah)
andytime.c: time.c time.ch
	$(WMERGE) time.c time.ch andytime.c

andytest_mpd.h: test_mpd.h test_mpd.hch
	$(WMERGE) test_mpd.h test_mpd.hch andytest_mpd.h

mathtest.o: andymathtest.o
	copy andymathtest.o mathtest.o
andymathtest.o: andymathtest.c andytest_mpd.h $(extrah)
andymathtest.c: mathtest.c mathtest.ch
	$(WMERGE) mathtest.c mathtest.ch andymathtest.c

pmath.o: andypmath.o
	copy andypmath.o pmath.o
andypmath.o: andypmath.c andytest_mpd.h $(extrah)
andypmath.c: pmath.c pmath.ch
	$(WMERGE) pmath.c pmath.ch andypmath.c

cmath.o: andycmath.o
	copy andycmath.o cmath.o
andycmath.o: andympmath.c andytest_mpd.h $(extrah)
	- delete stamp-orig stamp-trap
	copy andytest_mpd.h mpd.h
	copy andympmath.c andycmath.c
	$(CC) $(CPPFLAGS) $(CFLAGS) andycmath.c
andympmath.c: mpmath.c mpmath.ch
	$(WMERGE) mpmath.c mpmath.ch andympmath.c

# trapmp is a shell script that sets environment variables and runs etrapmp
#
check: run-trap
run-trap: $(PS)inimp trapf.tfm # etrapmp
	-copy $(PS)inimp etrapmp
	-execute settrapenv
	-copy $(trapdir)/mtrap.mp .
	-./etrapmp -I mtrap
	-diff $(trapdir)/mtrap.log mtrap.log
	-diff $(trapdir)/mtrap.0 mtrap.0
	-diff $(trapdir)/mtrap.1 mtrap.1
	-diff $(trapdir)/writeo writeo
	-diff $(trapdir)/writeo.2 writeo.2
	-copy $(trapdir)/trap.mp .
	-copy $(trapdir)/trap.mpx .
 	wait # one second and then
	touch trap.mpx # again.
	-./etrapmp -I <$(trapdir)/trap1.in >NIL:
	-copy trap.log trapin.log
	-diff $(trapdir)/trapin.log trapin.log
	-./etrapmp -I <$(trapdir)/trap2.in >trap.fot
	-diff $(trapdir)/trap.log trap.log -l10000
	-diff $(trapdir)/trap.fot trap.fot
	-diff $(trapdir)/trap.5 trap.5
	-diff $(trapdir)/trap.6 trap.6
	-diff $(trapdir)/trap.148 trap.148
	-diff $(trapdir)/trap.149 trap.149
	-diff $(trapdir)/trap.150 trap.150
	-diff $(trapdir)/trap.151 trap.151
	-diff $(trapdir)/trap.197 trap.197
	-diff $(trapdir)/trap.200 trap.200
	../fontutil/tftopl trap.tfm trap.pl
	-diff $(trapdir)/trap.pl trap.pl

trapf.tfm: $(trapdir)/trapf.pl
	../fontutil/pltotf $(trapdir)/trapf.pl trapf.tfm

# Make mem files automatically.
mems: $(mems)

# inimp requires mp.pool to be in one of the directories mentioned in the
# MPPOOL path.  This version should work if the path includes ".".
#
plain.mem: $(PS)inimp
	$(inimp) $(mpdistdir)/mplib/plain dump

mfplain.mem: $(PS)inimp
	$(inimp) $(mpdistdir)/mplib/mfplain dump

# After changing constants, making a trapmp is just like making an inimp.
# 
etrapmp: stamp-trap $(iobjs) $(objs)
	$(CCLD) to etrapmp from $(LOBJ) $(iobjs) $(objs) \
	lib $(libs) $(LDFLAGS)

# Only one of stamp-trap and stamp-orig should exist at any given time.
# They just keep track of whether mpd.h came from trap_mpd.h or orig_mpd.h
#
stamp-trap: stamp-convert
	copy trap_mpd.h mpd.h
	- delete stamp-orig
	echo "" > stamp-trap

stamp-orig: stamp-convert
	copy orig_mpd.h mpd.h
	- delete stamp-trap
	echo "" > stamp-orig

# tangle produces mp.p and mp.pool.
# 
mp.p mp.pool: mp.web $(CHANGEFILE)
	../web/tangle mp.web $(CHANGEFILE)

cmp.ch: $(mpsrcdir)/mp.ch $(mpsrcdir)/mp.chch
	- delete cmp.ch
	$(TIE) -c cmp.ch mp.web mp.ch mp.chch

mmp.ch: $(mpsrcdir)/mp.ch $(mpsrcdir)/mp.chch $(mpsrcdir)/mpmemory.ch
	- delete mmp.ch
	$(TIE) -c mmp.ch mp.web mp.ch mp.chch mpmemory.ch

mmp_20.ch: $(mpsrcdir)/mp.ch $(mpsrcdir)/mp.chch \
	$(mpsrcdir)/mpmemory.ch $(mpsrcdir)/mp_20.ch
	- delete mmp_20.ch
	$(TIE) -c mmp_20.ch mp.web mp.ch mp.chch mp_20.ch mpmemory.ch

# The convert script produces the following files:
#
mp0.c: mp.p andycoerce.add $(commondefines)
	$(SHELL) $(mpsrcdir)/convert.rexx
	touch mpd.h
mp1.c mp2.c mp3.c mp4.c mp5.c mp6.c mp7.c mp8.c mp9.c mp10.c \
imp.c coerce.h mpd.h: mp0.c

mpmemory.h: stamp-convert

stamp-convert: mpd.h
	copy mpd.h mpd.safe
	$(SED) -f $(mpsrcdir)/mpmemory.sed < mpd.h > mpd.temp
	copy mpd.temp mpd.h
	delete mpd.temp
	echo "" > stamp-convert

andycoerce.add: ../mf/coerce.add $(mpsrcdir)/coerce.add.ch
	$(WMERGE) ../mf/coerce.add $(mpsrcdir)/coerce.add.ch andycoerce.add

# The (hand-coded) file $(extrac) and the (generated) file imp.c have
# #ifdefs for INIMP, so we compile them differently.
iextra.o: iextra.c $(commonh) $(extrah)
	$(CC) $(CPPFLAGS) define=INIMP define=INI $(CFLAGS) iextra.c

inimp.o: inimp.c $(commonh) $(extrah)
	$(CC) $(CPPFLAGS) define=INIMP $(CFLAGS) inimp.c

# Avoid compiling files in another directory.
iextra.c: $(extrac)
	- delete iextra.c
	$(LN) $(extrac) >iextra.c

inimp.c: imp.c
	- delete inimp.c
	$(LN) imp.c > inimp.c

vextra.o: vextra.c
vextra.c: $(extrac)
	- delete vextra.c
	$(LN) $(extrac) > vextra.c

$(fileioo): openinout.c
openinout.c: $(fileioc)
	- delete openinout.c
	$(LN) $(fileioc) > openinout.c

$(iobjs) $(vobjs) $(objs): $(commonh) $(extrah)

mp0.o:	mp0.c
mp1.o:	mp1.c
mp2.o:	mp2.c
mp3.o:	mp3.c
mp4.o:	mp4.c
mp5.o:	mp5.c
mp6.o:	mp6.c
	$(CC) $(CPPFLAGS) $(CFLAGS) NOOPT mp6.c # Don't try suicide!
mp7.o:	mp7.c
mp8.o:	mp8.c
mp9.o:	mp9.c
	$(CC) $(CPPFLAGS) $(CFLAGS) NOOPTPEEP mp9.c # Don't try suicide!
mp10.o:	mp10.c

$(speedup): ../mf/64bit/64bit.a
	asm -iINCLUDE: -m2 ../mf/64bit/64bit.a

c-sources: mpd.h
.PHONY: c-sources

install: install-exec install-data

install-exec: all
	$(INSTALL_PROGRAM) $(program) $(bindir)/$(program)
install-data: mp.pool $(srcdir)/../config/mp.mcf
	$(INSTALL_DATA) mp.pool $(mppooldir)
	$(INSTALL_DATA) $(srcdir)/../config/mp.mcf $(configdir)

clean:
	- delete \#?.(bak|o|lnk|log|mem) $(speedup)
	- delete trap.\#? trapin.log trapf.tfm
	- delete andytime.o andympmath.o andymathtest.o andypmath.o andycmath.\#?
	- delete time.o mpmath.o mathtest.o timemath pmath.o cmath.\#?
	- delete mtrap.\#? writeo writeo.2 mp.mcf

veryclean: clean
	- delete \#?.(dvi|pool|p|safe|ckp) andy\#? ?mp.ch ?mp_20.ch
	- delete mp?.c mp??.c imp.c tmp.c mpd.h coerce.h
	- delete iextra.c inimp.c openinout.c
	- delete orig_mpd.h trap_mpd.h

extraclean: veryclean
	- delete \#?.(aux|bbl|blg|orig|pl|rej)
	- delete \#?.(i|s|tfm|vf|vpl) \#?(gf|pk)
	- delete CONTENTS.tex mpout.\#? patch\#? texput.\#? stamp-\#?
	- delete $(program) etrapmp
