# Makefile for MetaPost.

SHELL = /bin/sh
srcdir = @srcdir@
VPATH = @srcdir@

# 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.a

# Routines used everywhere.
commondefines = $(srcdir)/../lib/common.defines $(srcdir)/../lib/texmf.defines
commonh = $(srcdir)/../lib/config.h
commono = ../lib/lib.a $(kpathsea)

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

# Routines used in TeX, Metafont, and BibTeX.
fileioc = $(srcdir)/../lib/openinout.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 mpmath.o 
iobjs = iextra.o inimp.o
vobjs = vextra.o imp.o

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

program = inimp virmp

CC = @CC@
CFLAGS = -g
CPPFLAGS = -DMP -I$(srcdir)/../lib -I../..

CCLD = $(CC)
RANLIB = @RANLIB@
LN = @LN_S@

LDFLAGS = $(CFLAGS) $(XLDFLAGS)
libs = $(commono) $(LIBS)

LIBS = @LIBS@

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@


# The mems we know how to make.
mems = 

# And how to make them.
inimp = MPPOOL=. ./inimp


# What to pass to submakes.
makeargs=SHELL='$(SHELL)' CC='$(CC)' CFLAGS='$(CFLAGS)' \
LDFLAGS='$(LDFLAGS)' LIBS='$(libs)' RANLIB='$(RANLIB)' LN='$(LN)'


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


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


# MetaPost doesn't need window support.
inimp: $(iobjs) $(objs) $(commono)
	$(CCLD) -o inimp $(LDFLAGS) $(iobjs) $(objs) $(libs)

virmp: $(vobjs) $(objs) $(commono)
	$(CCLD) -o virmp $(LDFLAGS) $(vobjs) $(objs) $(libs)

check: run-trap
run-trap: trapmp trapf.tfm
	rm -f mtrap.mp
	-$(LN) $(trapdir)/mtrap.mp .
	-$(SHELL) -c '. ./trapenv; \
	  ./trapmp mtrap > /dev/null 2>&1'
	-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
	rm -f trap.mp
	rm -f trap.mpx
	-$(LN) $(trapdir)/trap.mp .
	-$(LN) $(trapdir)/trap.mpx .
	touch trap.mpx
	-$(SHELL) -c '. ./trapenv; \
	 ./trapmp < $(trapdir)/trap1.in >/dev/null 2>&1'
	mv trap.log  trapin.log
	-diff $(trapdir)/trapin.log trapin.log
	-$(SHELL) -c '. ./trapenv; \
	 ./trapmp < $(trapdir)/trap2.in > trap.fot'
	-diff $(trapdir)/trap.log trap.log
	-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: 
	../fontutil/pltotf $(trapdir)/trapf.pl trapf.tfm
        

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

mp.mem: inimp
	$(inimp) 'plain; dump'
	mv plain.mem mp.mem
	mv plain.log mp.log

mfmp.mem: inimp
	$(inimp) 'mfplain; dump'
	mv mfplain.mem mfmp.mem
	mv mfplain.log mfmp.log


# The `ini_to_trap' script changes mpd.h, instead of the change file, so
# retangling is not necessary.  After changing constants, making a
# trapmp means making an inimp.
# 
trapmp: stamp-trap
	$(MAKE) $(makeargs) inimp
	mv inimp trapmp

stamp-trap: mpd.h $(mpsrcdir)/trap.sed
	rm -f *.o temp
	sed -f $(mpsrcdir)/trap.sed < mpd.h > temp
	mv mpd.h mpd.h.bak
	mv temp mpd.h
	touch stamp-trap

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

mp.p mp.pool: mp.web cmp.ch
	../web/tangle mp.web cmp.ch

cmp.ch: $(mpsrcdir)/mp.ch
	rm -f cmp.ch
	cp $(mpsrcdir)/mp.ch cmp.ch

# The convert script produces the following files:
mp0.c 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: mp.p coerce.add $(commondefines)
	$(SHELL) $(mpsrcdir)/convert
	touch mpd.h

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

inimp.o: inimp.c $(commonh) $(extrah)
	$(CC) $(CPPFLAGS) -DINIMP $(CFLAGS) -c inimp.c 

# MetaPost specific stuff in openinout.c needs some extra symbols:
# (No longer needed, instead using MPXCMDPATH defined in texmf.cnf.)
# openinout.o: openinout.c
#	$(CC) $(CPPFLAGS) -DMPXCOMMAND='"$(bindir)/makempx"' \
#	-DTROFFMPX='"$(bindir)/troffmpx"' $(CFLAGS) -c openinout.c

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

inimp.c: imp.c
	rm -f inimp.c
	$(LN) imp.c inimp.c

vextra.c: $(extrac)
	rm -f vextra.c
	$(LN) $(extrac) vextra.c

openinout.c: $(fileioc)
	rm -f openinout.c
	$(LN) $(fileioc) openinout.c

$(iobjs) $(vobjs) $(objs): $(commonh) $(extrah)
mpmath.o: mpd.h $(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
mp7.o: mp7.c
mp8.o: mp8.c
mp9.o: mp9.c
mp10.o: mp10.c


Makefile: Makefile.in ../config.status
	(cd ..; sh config.status)

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

install: install-exec install-data

install-exec: all
	for p in $(program); do $(INSTALL_PROGRAM) $$p $(bindir)/$$p; done

install-data: mp.pool
	$(SHELL) $(srcdir)/../../mkdirchain $(mppooldir)
	-if test -s mp.pool; then \
	  $(INSTALL_DATA) mp.pool $(mppooldir)/mp.pool; else true; fi

TAGS: *.c *.h
	etags -t *.c *.h


mostlyclean::
	rm -f *.o $(program) $(lib) $(programs)

clean:: mostlyclean
	rm -f *.dvi *.pool

distclean:: clean
	rm -f Makefile config.status c-auto.h

# Although we can remake configure and c-auto.h.in, we don't remove
# them, since many people may lack Autoconf.  Use configclean for that.
realclean:: distclean
	rm -f TAGS *.info*

extraclean::
	rm -f *.aux *.bak *.bbl *.blg *.dvi *.log *.orig *.pl *.rej
	rm -f *.i *.s *.tfm *.vf *.vpl *\#* *gf *pk *~
	rm -f CONTENTS.tex a.out core mpout.* patch* texput.*

configclean:
	rm -f configure c-auto.h.in c-auto.h


# Prevent GNU make 3 from overflowing arg limit on system V.
.NOEXPORT:

# Be careful not to remove trap.* as we want to keep trap.sed.
mostlyclean::
	rm -f stamp-* trapmp
	rm -f mpd.h mpd.h.bak coerce.h *.log *.mem
	rm -f trapf.tfm mtrap.mp mtrap.0 mtrap.1 writeo writeo.2
	rm -f trap.mp trap.mpx trap.fot trap.tfm trap.pl trap.ps
	rm -f trap.0 trap.5 trap.6 trap.95 trap.96 trap.97 
	rm -f trap.98 trap.99 trap.100 trap.101 trap.102 trap.103 
	rm -f trap.104 trap.105 trap.106 trap.107 trap.108 trap.109
	rm -f trap.148 trap.149 trap.150 trap.151 trap.197 trap.200 

# Be careful not to remove *.c *.h as we want to keep mpmath.c.
clean::
	rm -f *.p *.h
	rm -f imp.c inimp.c iextra.c vextra.c openinout.c mp0.c mp1.c 
	rm -f mp2.c mp3.c mp4.c mp5.c mp6.c mp7.c mp8.c mp9.c mp10.c 

distclean::
	rm -f cmp.ch

extraclean::
