# Makefile for TeX 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, March 23, 1995.

# With this SMakefile several different versions of TeX can be
# created (one at a time!).  The standard is No20IniTeX/No20VirTeX.
# It is as close to the Web2C implementation as possible.
#
#     CHANGEFILE = ctex.ch
#     HEADERFILE = texd.h
#     DEFS       = cpu=ANY data=FAR
#     PS         = No20
#     LOBJ       = lib:c.o
#     LIBRARIES  = lib:scmieee.lib lib:scnb.ib
#
# An improved version for standard Amigas will provide a runtime
# memory configuration and resident versions of IniTeX and VirTeX.
#
#     CHANGEFILE = mtex.ch
#     HEADERFILE = texmemory.h
#     DEFS       = cpu=ANY define=VARMEM
#     PS         = No20
#     LOBJ       = lib:cres.o
#     LIBRARIES  = lib:scmieee.lib lib:sc.lib
#
# Advanced hardware configurations work with IniTeX/VirTeX.  These use
# the runtime memory configuration as well.
#
CHANGEFILE = mtex.ch
HEADERFILE = texmemory.h
DEFS       = cpu=68020 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
texpooldir = 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
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) tex0.o tex1.o tex2.o tex3.o tex4.o tex5.o tex6.o \
  tex7.o tex8.o tex9.o
iobjs = iextra.o initex.o
vobjs = vextra.o itex.o

# Where the test files are.
texsrcdir = $(srcdir)/../tex
tripdir = $(texsrcdir)/TeXtrip

program = $(PS)initex $(PS)virtex patgen

# The -DTeX here is used for patgen, too, but that doesn't hurt anything.
CC = sc
CFLAGS = $(DEFS) math=IEEE stackcheck noicons optimize
CPPFLAGS = define=_STRICT_ANSI define=TeX idir=$(srcdir)/../lib idir=../..

CCLD = slink
LN = type

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

INSTALL = copy
INSTALL_PROGRAM = copy
INSTALL_DATA = copy

# spiff -r5e-8 gives cleaner results than just `diff', but because it
# also produces backspaces and other verbiage, I haven't taught
# configure about it.
DIFF = diff
DIFFFLAGS = -l10000 -w

# The formats we know how to make.
fmts = amslatex.fmt amstex.fmt etex.fmt inrstex.fmt latex.fmt \
  picplus.fmt slitex.fmt tex.fmt texinfo.fmt 

# And how to make them.
initex = TEXPOOL=. ./initex

# Need the web rules for patgen.
.SUFFIXES:
.SUFFIXES: .o .c .p .ch
.p.c:
	$(SHELL) $(srcdir)/../lib/convert.rexx $*.p $*.c
.ch.p:
	../web/tangle $*.web $*.ch
.c.o:
	$(CC) $(CPPFLAGS) $(CFLAGS) $*.c

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

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

virtex: $(vobjs) $(objs) $(commono_20)
	$(CCLD) to virtex from $(LOBJ) $(vobjs) $(objs) \
	lib $(libs_20) $(LDFLAGS)

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

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

check: run-trip
run-trip: $(PS)initex # triptex
	-copy $(PS)initex triptex
	-delete quiet trip.tex
	-copy $(tripdir)/trip.tex .
	-execute settripenv
	../fontutil/pltotf $(tripdir)/trip.pl trip.tfm
	../fontutil/tftopl ./trip.tfm trip.pl
	-diff $(tripdir)/trip.pl trip.pl
        -./triptex < $(tripdir)/trip1.in >NIL:
	-diff $(tripdir)/tripin.log trip.log
	-./triptex < $(tripdir)/trip2.in > trip.fot
# 
# We use $(DIFF) instead of `diff' only for those files where there
# might actually be legitimate numerical differences.
	-$(DIFF) $(DIFFFLAGS) $(tripdir)/trip.log trip.log
	-diff $(tripdir)/trip.fot trip.fot
	../dviutil/dvitype trip.dvi < $(tripdir)/dvitype.in > trip.typ
	-$(DIFF) $(DIFFFLAGS) $(tripdir)/trip.typ trip.typ

# Make format files automatically.
fmts: $(fmts)

# AMSLaTeX requires that you
# 1) move Lamport's lfonts.tex out of TeX's way (I use the lfonts.tex I
# distribute with the sauter CM parameter files, and call it lfonts.sauter),
# 2) say `lfonts.new' when lplain.tex can't find lfonts.tex,
# 3) choose a ``fontdef'' file (I use fontdef.ori),
# 4) a ``preload'' file, (I use preload.ori),
# 5) and a ``basefont'' file.  (I use basefont).
# How automatic, huh?  I can hardly wait for LaTeX 3.
amslatex.fmt: initex
	$(initex) lplain \\dump
	mv lplain.fmt amslatex.fmt
	mv lplain.log amslatex.log

# As of AMSTeX 2.1, the initialization file is named `amstex.ini'.
# Because it explicitly reads plain.tex, we cannot use &./tex; that
# produces a `Too late for \patterns' error.
amstex.fmt: initex
	$(initex) amstex.ini

etex.fmt: tex.fmt
	touch eplain.aux # Makes cross-reference warnings work right.
	$(initex) \&./tex eplain \\dump
	mv eplain.fmt etex.fmt
	mv eplain.log etex.log

inrstex.fmt: initex
	$(initex) inrstex \\dump

latex.fmt: initex
	$(initex) lplain \\dump
	mv lplain.fmt latex.fmt
	mv lplain.log latex.log

picplus.fmt: tex.fmt
	$(initex) \&./tex picplus \\dump

slitex.fmt: initex
	$(initex) splain \\dump
	mv splain.fmt slitex.fmt
	mv splain.log slitex.log

tex.fmt: initex
	$(initex) plain \\dump
	mv plain.fmt tex.fmt
	mv plain.log tex.log

# Texinfo changes the escape character from `\' to `@'.
texinfo.fmt: tex.fmt
	$(initex) \&./tex texinfo @dump

# We change texd.h instead of the change file, to avoid retangling.
# This target never gets executed on the Amiga.  It is only kept
# for reference.
triptex: stamp-trip
	$(MAKE) $(makeargs) initex
	mv initex triptex

stamp-trip: texd.h $(texsrcdir)/trip.sed
	rm -f *.o texd.h.bak temp
	sed -f $(texsrcdir)/trip.sed < texd.h > temp
	mv texd.h texd.h.bak
	mv temp texd.h
	touch stamp-trip

clean-trip:
	rm -f *.o texd.h triptex stamp-trip
# Don't reinstall a texd.h with the test constants.
	if grep 'memmax 2999' texd.h.bak >/dev/null 2>&1; then \
          rm -f texd.h*; else mv texd.h.bak texd.h; fi

tex.p tex.pool: tex.web $(CHANGEFILE)
	../web/tangle tex.web $(CHANGEFILE)

# If you're building mltex, reportedly have to add:
#        sed -e 's/xiiix/(/;s/xtttx/)/' tex.p >tex.pp
#        mv tex.pp tex.p

ctex.ch: $(texsrcdir)/tex.ch $(texsrcdir)/tex.chch ../lib/texmfhistory.ch
	- delete ctex.ch
	$(WMERGE) $(texsrcdir)/tex.ch $(texsrcdir)/tex.chch andytex.ch
	$(TIE) -c ctex.ch tex.web andytex.ch ../lib/texmfhistory.ch
mtex.ch: $(texsrcdir)/tex.ch $(texsrcdir)/tex.chch \
	../lib/texmfhistory.ch $(texsrcdir)/texmemory.ch
	- delete mtex.ch
	$(WMERGE) $(texsrcdir)/tex.ch $(texsrcdir)/tex.chch andytex.ch
	$(TIE) -c mtex.ch tex.web andytex.ch ../lib/texmfhistory.ch \
	$(texsrcdir)/texmemory.ch

# The convert script produces the following. Touch texd.h to ensure its
# timestamp is later than the rest.
tex0.c: tex.p andycoerce.add $(commondefines)
	$(SHELL) $(texsrcdir)/convert.rexx
	touch texd.h
tex1.c tex2.c tex3.c tex4.c tex5.c tex6.c tex7.c tex8.c tex9.c \
itex.c coerce.h texd.h: tex0.c

texmemory.h: stamp-convert

stamp-convert: texd.h
	copy texd.h texd.safe
	$(SED) -f ../tex/texmemory.sed < texd.h > texd.temp
	copy texd.temp texd.h
	delete texd.temp
	echo "" > stamp-convert

andycoerce.add: coerce.add coerce.add.ch
	$(WMERGE) coerce.add coerce.add.ch andycoerce.add

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

initex.o: initex.c $(commonh) $(extrah)
	$(CC) $(CPPFLAGS) define=INITEX $(CFLAGS) initex.c

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

initex.c: itex.c
	- delete initex.c
	$(LN) itex.c > initex.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)

tex0.o: tex0.c
tex1.o: tex1.c
tex2.o: tex2.c
tex3.o: tex3.c
tex4.o: tex4.c
tex5.o: tex5.c
tex6.o: tex6.c
tex7.o: tex7.c
tex8.o: tex8.c
tex9.o: tex9.c

patgen: patgen.o $(commono)
	$(CC) link to patgen $(CPPFLAGS) $(CFLAGS) data=far patgen.o $(commono)
patgen.o: patgen.c $(commonh)
	$(CC) $(CPPFLAGS) $(CFLAGS) data=far $*.c
patgen.c: patgen.p $(commondefines)
patgen.p: patgen.web andypatgen.ch
	../web/tangle patgen.web andypatgen.ch
andypatgen.ch: patgen.ch patgen.chch
	$(WMERGE) patgen.ch patgen.chch andypatgen.ch

c-sources: texd.h patgen.c
.PHONY: c-sources

install: install-exec install-data

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

clean:
	- delete \#?.(bak|o|lnk|log|fmt)

veryclean: clean
	- delete \#?.(dvi|pool|c|p|h|safe) andy\#? ctex.ch mtex.ch

extraclean: veryclean
	- delete \#?.(aux|bbl|blg|orig|pl|rej)
	- delete \#?.(i|s|tfm|vf|vpl)
	- delete CONTENTS.tex texput.\#? patch\#? mfput.\#? stamp-\#?
	- delete $(program) triptex trip.tex tex.mcf tripos.tex \
	- delete 8terminal.tex trip.fot trip.typ
