#Makefile for Gforth

#Copyright (C) 1995,1996,1997,1998,2000,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015 Free Software Foundation, Inc.

#This file is part of Gforth.

#Gforth is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation, either version 3
#of the License, or (at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program. If not, see http://www.gnu.org/licenses/.

# To change the values of `make' variables: instead of editing Makefiles,
# (1) if the variable is set in `config.status', edit `config.status'
#     (which will cause the Makefiles to be regenerated when you run `make');
# (2) otherwise, pass the desired values on the `make' command line.

# Warning:
# For some stupid reason setting SHELL to bash does not work properly with
# DOS. If you want to use shell-specific things that must run with DOS make
# an external batch file and call it with bash (see versions.bsh).

# ------------- gforth version

VERSION	= 0.7.9_20160410
DOSVERSION=`echo $(VERSION)|sed 's/\.//g'|sed 's/-.*//g'`

# -------------	System specific variables	

machine=amd64
kernel_fi=kernl64l.fi
EXE=.exe

# this is the type of machine
# used to extend the include path with ./arch/$machine
# so we can include a machine specific 
# machine.h file

PATHSEP = :

EC = 
ARCH= @ARCH@
EXT = $(EC)$(EXE)
LA = $(EC).la
EXTRAS= 
HOST= 
EXTRAPREFIX= /usr/local
FAST= no

# ------------	Install Directories

package = $(DESTDIR)
VPATH = .
prefix = /usr/local
exec_prefix = ${prefix}
srcdir = .
bindir = $(package)${exec_prefix}/bin
#read-only architecture-independent files
datarootdir = ${prefix}/share
datadir = $(package)${datarootdir}
#read-only architecture-dependent non-ascii files
libdir = $(package)${exec_prefix}/lib
libccdir = $(subst $(package),,$(libdir)/gforth/$(VERSION)/$(machine)/libcc-named/)
buildccdir = lib/gforth/$(VERSION)/$(machine)/libcc-named/
includedir = $(package)${prefix}/include
infodir = $(package)${datarootdir}/info
htmldir = $(package)${docdir}
mandir = $(package)${datarootdir}/man
man1dir= $(mandir)/man1
man1ext= .1
#older emacses have their site-lisp in $(libdir)/emacs/
emacssitelispdir=$(package)/usr/share/emacs/site-lisp
emacssitestartdir=$(package)
siteforthdir=$(libdir)/gforth/site-forth

# -------------	Utility programs

SHELL	= /bin/sh
RM	= rm
RMTREE	= rm -rf
CP	= cp -p
MV	= mv
TAR	= tar cf -
MKDIR	= mkdir
ACLOCAL_AMFLAGS = -I m4
INSTALL	= /usr/bin/install -c
INSTALL_PROGRAM = ${INSTALL}
MASSAGE_EXE = chcon -t unconfined_execmem_exec_t
INSTALL_SCRIPT = ${INSTALL}
INSTALL_DATA = ${INSTALL} -m 644
INSTALL_INFO = /usr/bin/install-info
LN_S	= ln -s -f
M4      = m4 -s
GCC	= gcc
EMACS   = emacs
LIBTOOL = libtool
PREFORTH	= ./preforth -p ".$(PATHSEP)~+$(PATHSEP)$(srcdir)"
CC	= $(GCC)
EXTRAPATH = $(PATHSEP)/usr/share/gforth/site-forth$(PATHSEP)/usr/local/share/gforth/site-forth
FORTHPATH = .$(PATHSEP)$(siteforthdir)$(PATHSEP)$(datadir)/gforth/site-forth$(PATHSEP)$(libdir)/gforth/$(VERSION)$(PATHSEP)$(datadir)/gforth/$(VERSION)$(EXTRAPATH)
FORTHSIZES = 
FORTHS_ARGS =  -p "$(FORTHPATH)$(PATHSEP)$(srcdir)"
FORTH_ARGS = --die-on-signal $(FORTHS_ARGS)
ENGINE = ./gforth
DITCENGINE = ./gforth-ditc
FORTH	= $(ENGINE) $(FORTH_ARGS)
FORTHS	= $(ENGINE) $(FORTHS_ARGS)
# the (existing) forth system to use for cross compiling and primitives tables
# per default we use the one in this directory for a rebuild. It is also
# possible to override this variable at the command line for an initial build (jaw)
BUILDFORTH = $(ENGINE) --die-on-signal -m 4M -p ".$(PATHSEP)~+$(PATHSEP)$(srcdir)" -i $(kernel_fi)
# the forth system plus flags we use to build
FORTHB = $(BUILDFORTH) exboot.fs -e 'fpath= .|~+|$(srcdir)'
# the Forth system for running prims2x.fs
#FORTHP = $(FORTH)
ENGINE_FAST = ./gforth-fast$(EXT)
FORTH_FAST	= $(ENGINE_FAST) $(FORTH_ARGS)
FORTHKFLAGS= --die-on-signal -p ".$(PATHSEP)~+$(PATHSEP)$(srcdir)" -i $(kernel_fi)
FORTHK	= ./gforthker -p ".$(PATHSEP)~+$(PATHSEP)$(srcdir)"
#FORTHP	= $(ENGINE) --die-on-signal -i ./$(kernel_fi)
#the "-2 image-included-files +!" undoes the change to image-included-files
# in exboot.fs
STARTUP	= exboot.fs startup.fs arch/amd64/asm.fs arch/amd64/disasm.fs
STRIP	= strip

# -------------	Compiler Flags

XCFLAGS	= -g -O2 -pthread
XDEFINES = -DHAVE_CONFIG_H
SWITCHES = $(XCFLAGS) $(XDEFINES) $(OPTDEFINES) #-DNDEBUG #turn off assertions
ENGINE_FLAGS =  -fno-gcse -fcaller-saves -fno-defer-pop -fno-inline -fwrapv -fno-strict-aliasing -fno-cse-follow-jumps -fno-reorder-blocks -fno-reorder-blocks-and-partition -fno-toplevel-reorder -falign-labels=1 -falign-loops=1 -falign-jumps=1 -pthread -fno-defer-pop -fcaller-saves
DEBUGFLAG = 
CFLAGS	= $(DEBUGFLAG) -I$(srcdir)/engine -I$(srcdir)/arch/$(machine) -O3 -Wall $(SWITCHES) -DDEFAULTPATH='"$(FORTHPATH)"'
CPPFLAGS = 

#John Wavrik should use -Xlinker -N to get a writable text (executable)
XLDFLAGS =  -Wl,--export-dynamic -pthread
LDFLAGS	=  $(DEBUGFLAG) $(XLDFLAGS)
LDLIBS	= -lrt -lltdl -lm 

STACK_CACHE_REGS = 1
STACK_CACHE_DEFAULT_FAST = 1

PROFOBJS = 
PROFEXES = 

INSTALLDIRS = kernel kernel-ec doc asm ec test \
	compat unix minos2 \
	arch arch/generic arch/m68k arch/mips \
	arch/386 arch/hppa arch/sparc arch/power \
	arch/alpha arch/4stack arch/misc arch/6502 \
	arch/8086 arch/avr arch/c165 arch/h8 \
	arch/shboom arch/sharc arch/ia64 arch/amd64 \
	arch/arm arch/arm64 arch/r8c arch/lm32
INSTALL_LIBDIRS = unix

INCLUDES = engine/forth.h engine/threaded.h engine/io.h engine/longlong.h

KERN_SRC = \
	mach32b.fs mach32l.fs mach64b.fs mach64l.fs \
	machpc.fs.in \
	kernel/aliases0.fs \
	kernel/aliases.fs \
	kernel/args.fs \
	kernel/cond.fs \
	cross.fs \
	kernel/errore.fs \
	kernel/files.fs \
	kernel/require.fs \
	kernel/paths.fs \
	kernel/kernel.fs \
	kernel/main.fs \
	kernel/prim0.fs \
	search.fs \
	kernel/quotes.fs \
	kernel/tools.fs \
	kernel/toolsext.fs \
	kernel/vars.fs \
	kernel/accept.fs \
	kernel/basics.fs \
	kernel/int.fs \
	kernel/comp.fs \
	kernel/recognizer.fs \
	kernel/io.fs \
	kernel/input.fs \
	kernel/license.fs \
	kernel/nio.fs \
	kernel/saccept.fs \
	kernel/doers.fs \
	kernel/getdoers.fs \
	kernel/copydoers.fs \
	kernel/pass.fs \
	kernel/vtables.fs \
	kernel/xchars.fs \
	kernel/string.fs

KERN_EC_SRC = kernel-ec/args.fs kernel-ec/basics.fs kernel-ec/cbr.fs	\
	kernel-ec/cbrpi.fs kernel-ec/cloop.fs kernel-ec/comp.fs		\
	kernel-ec/cond.fs kernel-ec/doers.fs kernel-ec/errore.fs	\
	kernel-ec/getdoers.fs kernel-ec/int.fs kernel-ec/io.fs		\
	kernel-ec/kernel.fs kernel-ec/license.fs kernel-ec/main.fs	\
	kernel-ec/nio.fs kernel-ec/pass.fs kernel-ec/quotes.fs		\
	kernel-ec/saccept.fs kernel-ec/toolsext.fs kernel-ec/tools.fs	\
	kernel-ec/vars.fs kernel-ec/version.fs

EC_SRC = $(KERN_EC_SRC) \
	asm/README \
	asm/bitmask.fs \
	asm/numref.fs \
	chains.fs \
	asm/basic.fs \
	asm/generic.fs \
	asm/target.fs \
	ec/README \
	ec/mirror.fs \
	ec/shex.fs \
	ec/builttag.fs \
	ec/dotx.fs \
	ec/nesting.fs \
	build-ec.in

GFORTH_FI_SRC1=\
	assert.fs \
	backtrac.fs \
	blocked.fb \
	blocks.fs \
	bufio.fs \
	code.fs \
	debug.fs \
	debugs.fs \
	dis-gdb.fs \
	ekey.fs \

GFORTH_FI_SRC2=\
	savesys.fs \
	environ.fs \
	errors.fs \
	exboot.fs \
	except.fs \
	extend.fs \
	float.fs \
	glocals.fs \
	hash.fs \
	history.fs \
	intcomp.fs \
	i18n.fs \
	libcc.fs \
	locals.fs \
	look.fs \
	mkdir.fs \
	prelude.fs \
	quotes.fs \
	quotedstring.fs \
	parsedto.fs \
	search.fs \
	see.fs \
	see-ext.fs \
	simp-see.fs \
	source.fs \
	startup.fs \
	str-exec.fs \
	string.fs \
	struct.fs \
	struct0x.fs \
	struct-val.fs \
	stuff.fs \
	substitute.fs \
	tasker.fs \
	table.fs \
	termsize.fs \
	utf-8.fs \
	vt100.fs \
	vt100key.fs \
	wordinfo.fs \
	gforthrc.fs \
	options.fs \
	arch/386/asm.fs arch/386/disasm.fs \
	arch/amd64/asm.fs arch/amd64/disasm.fs \
	arch/alpha/asm.fs arch/alpha/disasm.fs arch/alpha/testasm.fs\
	arch/arm/asm.fs arch/arm/disasm.fs \
	arch/arm64/asm.fs arch/arm64/disasm.fs \
	arch/arm/testdisasm.fs arch/arm/testdisasm.out arch/arm/Makefile \
	arch/mips/asm.fs arch/mips/disasm.fs arch/mips/insts.fs \
	arch/mips/testasm.fs arch/mips/testdisasm.fs \
	arch/power/asm.fs arch/power/disasm.fs arch/power/inst.fs \
	arch/lm32/asm.fs arch/lm32/testasm.fs

# exclude envos.fs from list of distributed sources; don't include compat again
GFORTH_FI_DIST_SRC = \
	$(GFORTH_FI_SRC1) \
	$(GFORTH_FI_SRC2)

GFORTH_FI_SRC = \
	$(GFORTH_FI_SRC1) \
	envos.fs \
	$(GFORTH_FI_SRC2) \
	compat/caseext.fs \
	compat/strcomp.fs

SWIGMODULES =  xlib.fs wingdi.fs winuser.fs gles.fs egl.fs
SWIGLIBS =  x.fs gdi.fs user.fs win32.fs opengles.fs
SWIG_WRAP = $(patsubst %.fs, unix/%-fsi.c, $(SWIGMODULES))

LIBCC_SRC = cstr.fs unix/socket.fs unix/pthread.fs unix/mmap.fs unix/serial.fs \
	unix/filestat.fs unix/libc.fs unix/time.fs $(patsubst %, unix/%, $(SWIGLIBS))

UNIXLIBS_SRC = unix/x.fs unix/opengl.fs unix/opengles.fs unix/opengles3.fs \
	unix/jnilib.fs unix/soillib.fs unix/soil2lib.fs unix/freetype-gllib.fs \
	unix/android.fs unix/openmax.fs unix/cpufeatureslib.fs unix/png.fs

SWIG_SRC = unix/androidlib.i unix/egl.i unix/gles.i unix/gles3.i	\
	unix/gl.i unix/glx.i unix/jni.i unix/omxal.i unix/openvg.i	\
	unix/pnglib.i unix/soil.i unix/soil2.i unix/xlib.i		\
	unix/ios-gles.i unix/ios-gles3.i unix/freetype-gl.i		\
	unix/wingdi.i unix/winuser.i unix/cpufeatures.i

EXTRA_DOC = code.fs objects.fs oof.fs moofglos.fs regexp.fs fft.fs \
	 $(LIBCC_SRC) mkdir.fs cilk.fs

LIBCC_DIST_SRC = $(SWIG_SRC) unix/test.i unix/Makefile.in unix/doexec

LIBCC_FORTH_SRC = libffi.fs fflib.fs $(LIBCC_SRC) $(UNIXLIBS_SRC)
LIBCC_GEN_SRC = $(patsubst %, unix/%, $(SWIGMODULES))

LIBCC_BUILD_SRC =  libffi.fs $(LIBCC_SRC)
LIBCC_BUILD_LA = $(patsubst %.fs,$(buildccdir)/libgf%.la,$(shell echo $(LIBCC_BUILD_SRC) | sed -e s,unix/,,g))

LIBCC_BUILD_NOEC =  $(LIBCC_BUILD_SRC)

MINOS2_SRC = minos2/gl-helper.fs minos2/gl-terminal.fs \
	minos2/jpeg-exif.fs minos2/linux-gl.fs minos2/soil-texture.fs \
	minos2/gl-sample.fs minos2/gl-slideshow.fs minos2/ftgl-helper.fs \
	minos2/ftgl-sample.fs minos2/widgets.fs minos2/widgets-test.fs \
	minos2/mts-tools.fs minos2/mkv-tools.fs minos2/omx-example.fs \
	minos2/mkv-tags.fs minos2/ascii.png minos2/button.png \
	minos2/button2.png minos2/button3.png

FORTH_SRC = $(KERN_SRC) $(GFORTH_FI_DIST_SRC) $(EC_SRC) $(LIBCC_FORTH_SRC) \
	$(MINOS2_SRC) ans-report.fs ansi.fs answords.fs \
	colorize.fs comp-i.fs complex.fs \
	depth-changes.fs dosekey.fs doskey.fs ds2texi.fs \
	envos.dos envos.os2 etags.fs fft.fs filedump.fs fi2c.fs \
	fsl-util.4th fsl-util.fs glosgen.fs gray.fs httpd.fs install-tags.fs \
	make-app.fs doc/makedoc.fs locate.fs more.fs onebench.fs fft-bench.fs \
	other.fs prims2x.fs prims2x0.6.2.fs proxy.fs random.fs \
	regexp.fs sokoban.fs string.fs table.fs tags.fs \
	tt.fs quotations.fs \
	unbuffer.fs wordsets.fs 2012words.fs \
	test/tester.fs test/ttester.fs \
	test/coretest.fs test/postpone.fs test/dbltest.fs \
	test/string.fs test/float.fs test/search.fs test/gforth.fs \
	test/other.fs test/signals.fs test/checkans.fs \
	test/primtest.fs test/coreext.fs test/deferred.fs \
	test/coremore.fs test/gforth-nofast.fs test/libcc.fs \
	test/macros.fs \
	bubble.fs siev.fs matrix.fs fib.fs \
	oof.fs oofsampl.fs objects.fs objexamp.fs mini-oof.fs moof-exm.fs \
	moofglos.fs fixpath.fs mini-oof2.fs moof2-example.fs user-object.fs \
	callable.fs add.fs lib.fs oldlib.fs sieve.fs list.fs \
	endtry-iferror.fs recover-endtry.fs smartdots.fs \
	unix/terminal-server.fs unix/jni-helper.fs unix/jni-tools.fs \
	unix/jni-media.fs unix/jni-location.fs unix/sensors.fs \
	unix/gdi.fs unix/user.fs unix/win32.fs \
	date.fs script.fs wf.fs traceall.fs rec-scope.fs rec-env.fs \
	notfound.fs utf16.fs archive.fs cilk.fs

COMPAT = compat/README \
	compat/anslocal.fs \
	compat/assert.fs \
	compat/caseext.fs \
	compat/control.fs \
	compat/defer.fs \
	compat/exception.fs \
	compat/execute-parsing.fs \
	compat/loops.fs \
	compat/macros.fs \
	compat/required.fs \
	compat/strcomp.fs \
	compat/struct.fs \
	compat/vocabulary.fs

GFORTH_TEXI =  doc/gforth.texi doc/version.texi

VMGEN_TEXI = doc/vmgen.texi doc/version.texi doc/fdl.texi

ALLSUBDIRS = engine

ARCHS =	\
	arch/generic/machine.h \
	arch/m68k/machine.h \
	arch/mips/machine.h \
	arch/mips/check_prim.c \
	arch/386/machine.h \
	arch/386/android/AndroidManifest.xml.in \
	arch/386/android/config.sh \
	arch/386/android/build.sh \
	arch/386/android/starta.fs \
	arch/386/android/res \
	arch/386/android/src \
	arch/hppa/machine.h \
	arch/hppa/cache.c \
	arch/sparc/machine.h \
	arch/power/machine.h \
	arch/power/_sync_cache_range.c \
	arch/alpha/machine.h \
	arch/arm/machine.h \
	arch/arm/cacheflush0.c \
	arch/arm/cacheflush-linux.c \
	arch/arm/android/AndroidManifest.xml.in \
	arch/arm/android/config.sh \
	arch/arm/android/build.sh \
	arch/arm/android/starta.fs \
	arch/arm/android/res/drawable-hdpi/ic_launcher.png \
	arch/arm/android/res/drawable-ldpi/ic_launcher.png \
	arch/arm/android/res/drawable-mdpi/ic_launcher.png \
	arch/arm/android/res/drawable-xhdpi/ic_launcher.png \
	arch/arm/android/res/values/strings.xml \
	arch/arm/android/src/gnu/gforth/Gforth.java \
	arch/arm/ev3/config.sh \
	arch/ia64/machine.h \
	arch/ia64/flush_icache_block.c \
	arch/amd64/machine.h \
	arch/arm64/android/AndroidManifest.xml.in \
	arch/arm64/android/config.sh \
	arch/arm64/android/build.sh \
	arch/arm64/android/starta.fs \
	arch/arm64/android/res \
	arch/arm64/android/src \
	arch/4stack/README \
	arch/4stack/asm.fs \
	arch/4stack/mach.fs \
	arch/4stack/prim.fs \
	arch/4stack/mach.sh \
	arch/4stack/relocate.fs \
	arch/misc/README \
	arch/misc/asm.fs \
	arch/misc/mach.fs \
	arch/misc/optcmove.fs \
	arch/misc/prim.fs \
	arch/misc/sim.fs \
	arch/misc/sokoban.fs \
	arch/misc/tt.fs \
	arch/6502/prim.fs \
	arch/6502/mach.fs \
	arch/shboom/asm.fs \
	arch/shboom/compiler.fs \
	arch/shboom/dis.fs \
	arch/shboom/mach.fs \
	arch/shboom/prim.fs \
	arch/shboom/dis2.fs \
	arch/shboom/sh.p \
	arch/shboom/doers.fs \
	arch/sharc/mach.fs \
	arch/sharc/machine.h \
	arch/sharc/compile.sharc \
	arch/sharc/systypes.h \
	arch/sharc/types.h \
	arch/sharc/g21k-3.3.4-bp1.diff \
	arch/r8c/asm.fs \
	arch/r8c/mach.fs \
	arch/r8c/errors.fs \
	arch/r8c/prim.fs \
	arch/r8c/asm-test.fs \
	arch/r8c/errors.fs \
	arch/r8c/lauflicht.fs \
	arch/r8c/tasker.fs \
	arch/r8c/terminal.fs \
	arch/r8c/tt.fs \
	arch/sharc/unistd.h \
	arch/lm32/mach.fs \
	arch/lm32/mach.sh \
	arch/lm32/prim.fs \
	arch/lm32/Makefile \
	arch/lm32/testasm.disas-ok

#not included in distribution until distribution terms are included:
ARCHS_NO_DIST = \
	arch/6502/asm.fs \
	arch/6502/cold.fs \
	arch/6502/softuart.fs \
	arch/6502/zero.fs \
	arch/8086/asm.fs \
	arch/8086/mach.fs \
	arch/8086/mach.sh \
	arch/8086/prim.fs \
	arch/avr/asm.fs \
	arch/c165/asm.fs \
	arch/c165/mach.fs \
	arch/c165/prim.fs \
	arch/h8/asm.fs \
	arch/r8c/tt.fs

# temporary change.  DO NOT COMMIT
ARCHS += $(ARCHS_NO_DIST)

VMGEN_EX = vmgen-ex/Makefile vmgen-ex/README vmgen-ex/disasm.c \
	vmgen-ex/engine.c vmgen-ex/fib.mini \
	vmgen-ex/mini-inst.vmg vmgen-ex/mini-super.vmg vmgen-ex/mini.h \
	vmgen-ex/mini.l vmgen-ex/mini.y vmgen-ex/peephole-blacklist \
	vmgen-ex/peephole.c vmgen-ex/profile.c vmgen-ex/seq2rule.awk \
	vmgen-ex/simple.mini vmgen-ex/stat.awk vmgen-ex/support.c \
	vmgen-ex/test.mini vmgen-ex/test.out

VMGEN_EX2 = $(VMGEN_EX:vmgen-ex/%=vmgen-ex2/%)

ENGINE_SOURCES = engine/Makefile.in engine/config.h.in \
	engine/engine.c engine/main.c engine/libmain.c \
	engine/io.c engine/support.c engine/signals.c engine/libcc.h.in \
	engine/profile.c engine/128bit.h \
	engine/getopt.c engine/getopt1.c engine/getopt.h engine/select.c \
	engine/ecvt_r.c engine/memcmp.c engine/rint.c \
	engine/strtol.c engine/strtoul.c \
	engine/ansidecl.h engine/memmove.c \
	engine/pow10.c engine/atanh.c engine/cleanalign.c \
	engine/strerror.c engine/strsignal.c engine/dblsub.c \
	engine/fnmatch.h engine/fnmatch.c \
	engine/zexpand.c engine/androidmain.c engine/sincos.c

SOURCES	= $(ENGINE_SOURCES) compat Makefile.in Makedist.in unix/Makefile.in \
	gforthmi.in gforthmi.sh.in vmgen.in preforth.in libforth.in gforthker.in gfgen README.vmgen NEWS.vmgen \
	autogen.sh configure.ac configure config.sub config.guess \
	elisp-comp stamp-h.in envos.fs.in kernel/version.fs.in \
	iss.sh wininst.sh modpath.iss \
	install-sh INSTALL INSTALL.BINDIST NEWS README ToDo BUGS \
	BUILD-FROM-SCRATCH model \
	COPYING COPYING.LIB COPYING.DOC AUTHORS ChangeLog Benchres aclocal.m4 \
	doc/Makefile.in doc/gforth.texi.in doc/gforth.1 doc/version.texi.in \
	doc/vmgen.texi doc/fdl.texi doc/gpl.texi \
	gforth.el \
	$(ARCHS) \
	mkinstalldirs siteinit.fs \
	versions.bsh \
	glosgen.glo doc/glossaries.doc \
	$(INCLUDES) $(FORTH_SRC) $(LIBCC_DIST_SRC) $(LIBCC_GEN_SRC) \
	$(COMPAT) $(VMGEN_EX) $(VMGEN_EX2) \
	timings.sc \
	test/coretest.out test/checkans.out test/gforth.out \
	$(wildcard cache*.vmg) peeprules.vmg prim \
	debian/rules debian/control.in debian/changelog.in debian/compat \
	gforth.ico .travis.yml install-swig.sh $(SWIG_WRAP)

RCS_FILES =  ToDo model high-level

ENGINES = gforth$(EXT)  gforth-ditc$(EXT) gforth-itc$(EXT) $(PROFEXES)

ENGINES_FAST =  gforth-fast$(OPT)$(EXT) #gforth-native$(OPT)$(EXT) 

GEN = $(ENGINES) $(ENGINES_FAST) gforth.elc

# things that need a working forth system to be generated
FORTH_GEN_ENGINE=engine/prim.i engine/prim_lab.i engine/prim_names.i \
	engine/prim_superend.i engine/profile.i \
	engine/prim_num.i engine/prim_grp.i \
	engine/costs.i engine/super2.i
FORTH_GEN_ENGINE_FAST= \
	engine/prim-fast.i engine/prim_lab-fast.i engine/prim_names-fast.i \
	engine/prim_superend-fast.i engine/profile-fast.i \
	engine/prim_num-fast.i engine/prim_grp-fast.i \
	engine/costs-fast.i engine/super2-fast.i
FORTH_GEN_PRIMB = $(FORTH_GEN_ENGINE) kernel/aliases.fs kernel/prim.fs
FORTH_GEN0 = prim.b $(FORTH_GEN_PRIMB)
FORTH_GEN =  $(FORTH_GEN0) kernl32l.fi kernl32b.fi kernl64l.fi kernl64b.fi gforth.fi
# this is used for antidependences,
FORTH_GEN1 = $(FORTH_GEN0) kernl64l.fi build-ec

#kernel dependencies
KERN_DEPS = $(KERN_SRC) kernel/version.fs machpc.fs $(FORTH_GEN0) compat/strcomp.fs

#distributed documentation
DOCDIST = doc/gforth.info $(wildcard doc/gforth.info-*) doc/gforth.ps \
	  doc/vmgen.info doc/vmgen.ps

BKERNLS = kernl32b$(EC).fi kernl32l$(EC).fi \
	  kernl64b$(EC).fi kernl64l$(EC).fi

KERNLS = kernl32b$(EC).fi- kernl32l$(EC).fi- \
	 kernl64b$(EC).fi- kernl64l$(EC).fi-

# -------------        Make forth images
GEN_PRECIOUS = $(FORTH_GEN) $(KERNLS) \
	doc/gforth.texi doc/gforth.dvi doc/gforth.ps \
	doc/vmgen.dvi doc/vmgen.ps \
	Makefile Makedist engine/Makefile configure

#search path patterhn

vpath %.fs $(srcdir) $(srcdir)/unix

#standards.info recommends this:
.SUFFIXES:
.SUFFIXES: .c .o

all: kernel/version.fs more  check extras

# use this dependency for phony targets just as mostlyclean,...
FORCE: ;

#targets of failed commands should be deleted:
.DELETE_ON_ERROR:

#this rule avoids remaking everything after minor changes in Makefile.in
#disabled now, as it doesn't have the wanted effect
#version:	Makefile.in configure.ac
#		echo $(VERSION) > $@

# With dos we use normal dos echo
# we cannot pipe the output to engine/version.h directly because
# of the "/ and \" problem. Copying works because we use the
# shell und file utilities.

#kernel/version.fs:	version
#	echo ": version-string s\" $(VERSION)\" ;" > kernel/version.fs

more:	$(ENGINES) $(FORTH_GEN) $(GEN) libgforths build-libcc-named

#from the gcc Makefile: 
#"Deletion of files made during compilation.
# There are four levels of this:
#   `mostlyclean', `clean', `distclean' and `realclean'.
# `mostlyclean' is useful while working on a particular type of machine.
# It deletes most, but not all, of the files made by compilation.
# It does not delete libgcc.a or its parts, so it won't have to be recompiled.
# `clean' deletes everything made by running `make all'.
# `distclean' also deletes the files made by config.
# `realclean' also deletes everything that could be regenerated automatically."

mostlyclean:	FORCE
		-$(RM) -rf engine/*.s gforth.fi *.fi~ *.fi- kernel/version.fs \
		*TAGS gforth~ \
		doc/crossdoc.fd doc/doc.fd doc/gforth.texi doc/gforth.fns \
		doc/gforth.aux doc/gforth.cp doc/gforth.cps \
		doc/gforth.dvi doc/gforth.fn doc/gforth.ky doc/gforth.log \
		doc/gforth.pg \
		doc/gforth.toc doc/gforth.tp doc/gforth.vr html \
		gforth-$(VERSION).tar.gz
		(cd unix && $(MAKE) clean)

# Just the stuff needed to rebuild the documentation nac03feb1999
docclean:	FORCE
		-$(RM) -rf doc/crossdoc.fd doc/doc.fd doc/gforth.texi doc/gforth.fns \
		doc/gforth.aux doc/gforth.cp doc/gforth.cps \
		doc/gforth.dvi doc/gforth.fn doc/gforth.ky doc/gforth.log \
		doc/gforth.pg \
		doc/gforth.toc doc/gforth.tp doc/gforth.vr html

clean:		mostlyclean
	-$(RM) -rf $(GEN) *.o engine/*.o engine/*.lo engine/*.la \
	engine/.libs engine/libgforth* libgforth* arch/*/*.o \
	gforth-itc-noll$(EXT)  engine/gforth-itc-noll$(EXT)  \
	gforth-ditc-noll$(EXT) engine/gforth-ditc-noll$(EXT) \
	gforth-prof-noll$(EXT) engine/gforth-prof-noll$(EXT) \
	engine/prim-s.i
	for i in gforth gforth-fast gforth-native; do for j in $(OPTS); do rm -f engine/$$i$${j}$(EXT) $$i$${j}$(EXT); done; done
	-$(RM) -rf $(buildccdir)

distclean:	clean
		-$(RM) -rf config.cache config.log config.status \
		engine/config.h Makefile Makedist engine/Makefile \
		stamp-h engine/stamp-h \
		doc/version.texi gforthmi vmgen preforth \
		prim-fast.b $(FORTH_GEN_ENGINE_FAST)

#realclean is useless, but dangerous, so it's commented out
realclean:	distclean
		-$(RM) -rf $(GEN_PRECIOUS) `cat .cvsignore`

#mostlyclean, but also remove some of the stuff that is distributed
virtualclean:	mostlyclean
		-$(RM) -rf gforth.fns gforth.texi gforth.ps gforth.info* \
		gforth-$(VERSION).tar.gz config.cache *~ */*~

maintainer-clean: realclean

#Some makes (Ultrix, SunOS, IRIX) are so broken, they cannot read the
#Makefile if it contains our dist rules.  Therefore we have put these
#rules in Makedist (you can use them with GNU make on these systems).

dist:		Makedist FORCE
		$(MAKE) -f Makedist d$@

dosdist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

srcdist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

srconlydist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

docdist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

htmldist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

bindist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

binonlydist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

debdist:	Makedist FORCE
		$(MAKE) -f Makedist d$@

setup-debdist:	doc Makedist FORCE
		$(MAKE) -f Makedist d$@

#HPUX make breaks the cycle differently when the dates are equal, so
# touch some of the files if it acts up
hpux-workaround:
	touch $(FORTH_GEN_PRIMB)

#strip gforth, because the debugging stuff is hardly useful once
# gforth manages to execute more than a few primitives.

#install does not depend on gforth.info, because that would require
#supplying a lot of files that can be easily generated (only info is
#hard to generate).
#we rebuild gforth.fi, because it contains some path names.
#we delete $build/gforth.fi and $build/install.TAGS after installation because of ownership.
normal-install:
		$(NORMAL_INSTALL)

INSTEXES=gforth gforth-fast gforth-itc gforth-ditc
INSTSCRIPTS=gforthmi vmgen
INSTSUFFIX="" -$(VERSION) -$(machine) -$(VERSION)-$(machine)

rm-install:
		-for i in $(INSTEXES); do \
		    for j in $(INSTSUFFIX); do \
		        $(RM) $(bindir)/$$i$$j$(EXT); \
		    done \
		done
		-for i in $(INSTSCRIPTS); do \
		    for j in $(INSTSUFFIX); do \
		        $(RM) $(bindir)/$$i$$j; \
		    done \
		done

install-progs:
		for i in $(INSTEXES); do \
		    $(INSTALL_PROGRAM) $$i$(EXT) $(bindir)/$$i-$(VERSION)-$(machine)$(EXT); \
		    $(MASSAGE_EXE) $(bindir)/$$i-$(VERSION)-$(machine)$(EXT) || true; \
		done
		$(INSTALL_SCRIPT) gforthmi $(bindir)/gforthmi-$(VERSION)-$(machine)
		$(INSTALL_SCRIPT) vmgen $(bindir)/vmgen-$(VERSION)
		for i in $(INSTEXES) gforthmi; do \
		    for j in "" -$(VERSION) -$(machine); do \
		        (cd $(bindir) && $(LN_S) $$i-$(VERSION)-$(machine)$(EXT) $$i$$j$(EXT)); \
		    done \
		done
		(cd $(bindir) && $(LN_S) vmgen-$(VERSION) vmgen)

install-sources:
		-for i in doc/gforth.info* doc/vmgen.info*; do $(INSTALL_DATA) $$i $(infodir); done
		for i in $(FORTH_SRC) $(COMPAT) prim; do \
			$(INSTALL_DATA) $(srcdir)/$$i $(datadir)/gforth/$(VERSION)/$$i; \
		done

install-gensources:
		for i in $(LIBCC_GEN_SRC) envos.fs; do \
			$(INSTALL_DATA) $$i $(libdir)/gforth/$(VERSION)/$$i; \
		done
		touch $(siteforthdir)/siteinit.fs

install-images:
		-$(RM) gforth.fi
		$(INSTALL_DATA) $(kernel_fi) $(libdir)/gforth/$(VERSION)
		GFORTHD="$(DITCENGINE)$(EXT) -p $(siteforthdir)$(PATHSEP)$(siteforthdir)$(PATHSEP)$(datadir)/gforth/$(VERSION)$(PATHSEP)$(libdir)/gforth/$(VERSION) -i $(kernel_fi)" GFORTH="$(DITCENGINE)$(EXT) -p $(siteforthdir)$(PATHSEP)$(siteforthdir)$(PATHSEP)$(datadir)/gforth/$(VERSION)$(PATHSEP)$(libdir)/gforth/$(VERSION) --die-on-signal -i $(kernel_fi) $(STARTUP)" includedir=$(subst $(package),,$(includedir)) bindir=$(subst $(package),,$(bindir)) libccdir=$(libccdir) GFORTHPATH=.:"$$GFORTHPATH" GFORTHDESTDIR="$(package)" ./gforthmi gforth.fi $(FORTHSIZES) $(STARTUP)
		$(INSTALL_DATA) gforth.fi $(libdir)/gforth/$(VERSION)

install-include:
		$(srcdir)/mkinstalldirs $(includedir)/gforth/$(VERSION)/$(machine)
		$(INSTALL_DATA) engine/config.h engine/libcc.h $(includedir)/gforth/$(VERSION)/$(machine)

install-libcc:
		if test -n "$(LIBTOOL)"; then for i in $(LIBCC_BUILD_SRC); do \
		   $(LIBTOOL) --silent --mode=install $(INSTALL) $(buildccdir)/libgf`basename $$i .fs`.la `(cd $(package)$(libccdir); pwd)`/libgf`basename $$i .fs`.la; \
		done; fi

install-man:
		-$(INSTALL_DATA) $(srcdir)/doc/gforth.1 $(man1dir)

install-info:
		$(INSTALL_INFO) --info-dir=$(infodir) $(infodir)/gforth.info
		$(INSTALL_INFO) --info-dir=$(infodir) $(infodir)/vmgen.info

install-tags:	install.TAGS
		$(INSTALL_DATA) install.TAGS $(datadir)/gforth/$(VERSION)/TAGS

install-elc:
		@if test -n "/usr/share/emacs/site-lisp"; then \
			$(MKDIR) -p $(emacssitelispdir); \
			for i in gforth.el gforth.elc; do \
				$(INSTALL_DATA) $$i $(emacssitelispdir); \
			done; \
		else \
			echo '>>>>>Please install gforth.{el,elc} in your .../emacs/site-lisp directory'; \
		fi
		@if test -n ""; then \
			$(MKDIR) -p $(emacssitestartdir); \
			for i in start-gforth.el; do \
				$(INSTALL_DATA) $$i $(emacssitestartdir); \
			done; \
		else \
			echo '>>>>>Please install start-gforth.{el,elc} in your .../emacs/.../site-start.d directory'; \
		fi

installdeps:	gforth$(EXT) $(FORTH_SRC) $(kernel_fi) gforthmi vmgen doc/gforth.1 prim gforth.fi start-gforth.el

install-indep:	installdeps installdirs-indep install-sources install-man install-info install-elc

install-arch:	installdeps installdirs-arch normal-install rm-install install-progs install-gensources install-images install-include install-libcc install-tags libinstall extra-install

install-seq:	install-indep install-arch

post-inst:
		$(RM) install.TAGS gforth.fi start-gforth.el
		$(POST_INSTALL)

install:	installdeps install-seq post-inst

installnodep:	installdirs install-seq post-inst

install-strip:	install

libinstall:	$(ENGINES:%$(EXT)=lib%$(EXT)) $(ENGINES_FAST:%$(EXT)=lib%$(EXT))
		for i in $(ENGINES:%$(EXT)=lib%$(EC)) $(ENGINES_FAST:%$(EXT)=lib%$(EC)); do \
		   $(LIBTOOL) --silent --mode=install $(INSTALL) engine/$$i.la `(cd $(libdir); pwd)`/$$i.la; \
		done

installdirs:	installdirs-arch installdirs-indep

installdirs-arch: 	mkinstalldirs
		$(NORMAL_INSTALL)
		for i in $(bindir) $(libdir)/gforth/$(VERSION) $(siteforthdir) $(siteforthdir) $(package)$(libccdir); do \
			$(srcdir)/mkinstalldirs $$i; \
		done
		for i in $(INSTALL_LIBDIRS); do \
			$(srcdir)/mkinstalldirs $(libdir)/gforth/$(VERSION)/$$i; \
		done

installdirs-indep: 	mkinstalldirs
		$(NORMAL_INSTALL)
		for i in $(man1dir) $(infodir) $(datadir)/gforth/$(VERSION) $(datadir)/gforth/site-forth; do \
			$(srcdir)/mkinstalldirs $$i; \
		done
		for i in $(INSTALLDIRS); do \
			$(srcdir)/mkinstalldirs $(datadir)/gforth/$(VERSION)/$$i; \
		done

#deinstall all files specific to this version of gforth
#to uninstall version foo, type `make uninstall VERSION=foo'
uninstall:	FORCE
		$(NORMAL_UNINSTALL)
		-$(RM) -rf $(libdir)/gforth/$(VERSION) $(datadir)/gforth/$(VERSION) $(bindir)/gforth-$(VERSION)$(EXT) $(bindir)/gforth-fast-$(VERSION)$(EXT) $(bindir)/gforth-itc-$(VERSION)$(EXT) $(bindir)/gforthmi-$(VERSION) $(bindir)/vmgen-$(VERSION) $(includedir)/gforth/$(VERSION)/$(machine)
		@echo -e "To remove Gforth completely, type\n$(RM) -rf $(bindir)/gforth$(EXT) $(bindir)/gforth-fast$(EXT) $(bindir)/gforth-itc$(EXT) $(bindir)/gforthmi $(bindir)/vmgen $(man1dir)/gforth.1 $(infodir)/gforth.info* $(datadir)/gforth $(libdir)/gforth"

$(buildccdir)/libgf%.la: %.fs $(FORTH_GEN) $(GEN) $(LIBCC_SRC)
		-./libforth $(package)$(libccdir) $< -e bye

build-libcc-named: build-libcc-swig $(LIBCC_BUILD_LA) libccdir

libccdir:	$(buildccdir)

$(buildccdir):
		$(MKDIR) -p $(buildccdir)

build-libcc-swig:
		(cd unix; $(MAKE))

check:		gforths	gforth.fi 
		$(MAKE) checkone check-nofast ENGINE="./gforth$(EXT) --no-dynamic" >/dev/null 2>&1
		$(MAKE) checkone check-nofast ENGINE="./gforth-itc$(EXT)" >/dev/null 2>&1
		$(MAKE) checkone check-nofast ENGINE="./gforth-ditc$(EXT)" >/dev/null 2>&1
		$(MAKE) checkone ENGINE="./gforth-fast$(EXT) --no-dynamic" >/dev/null 2>&1
		$(MAKE) checkone check-nofast ENGINE="./gforth$(EXT)" >/dev/null 2>&1
		$(MAKE) checkone ENGINE="./gforth-fast$(EXT)" >/dev/null 2>&1
		@echo "*** Check successful ***"
		./gforth-fast$(EXT) --diag -e bye

checkone test: gforth.fi engine/prim-s.i
		$(FORTH) test/tester.fs test/coretest.fs test/postpone.fs test/dbltest.fs test/string.fs test/float.fs test/deferred.fs test/coreext.fs test/search.fs -e bye 2>/dev/null | tr -d '\015' | diff -c - $(srcdir)/test/coretest.out
		$(FORTH) test/other.fs -e bye
		$(FORTHS) test/signals.fs -e bye
		$(FORTHS) test/coremore.fs test/gforth.fs test/macros.fs -e bye 2>&1 | tr -d '\015' | diff -c - $(srcdir)/test/gforth.out
		$(FORTH) code.fs test/checkans.fs -e bye | tr -d '\015' | diff -c - $(srcdir)/test/checkans.out
		$(FORTH) prims2x.fs -e \
		  "c-flag on s\" prim.i\" out-filename 2! s\" prim.b\" ' output-c ' output-c-combined process-file bye"| \
		  tr -d '\015' | grep -v '^#line '|diff -c - engine/prim-s.i

check-nofast: gforth.fi
		$(FORTHS) test/gforth-nofast.fs -e bye

test/primtest.fi: test/primtest.fs mach32l.fs cross.fs
		$(FORTHB) -e 's" mach32l.fs"' $(srcdir)/test/primtest.fs -e "save-cross $@ $(bindir)/gforth-$(VERSION) bye"

primtest:	gforth gforth-native test/primtest.fi FORCE
		(echo rs|ENVVAR=bla ./gforth -i test/primtest.fi) && (echo rs|ENVVAR=bla ./gforth-native -i test/primtest.fi); echo $$?

bench:		gforth-fast$(EXT) gforth.fi
		@echo 'Each benchmark takes about 30s on a 486-66 (gcc-2.6.3 -DFORCE_REG)'
		time $(FORTH_FAST) siev.fs -e "main bye"
		time $(FORTH_FAST) bubble.fs -e "main bye"
		time $(FORTH_FAST) matrix.fs -e "main bye"
		time $(FORTH_FAST) fib.fs -e "main bye"
		time $(FORTH_FAST) fft-bench.fs -e "main bye"

onebench one-bench:
	$(FORTH_FAST) onebench.fs

all-bench allbench:
	./gforth-fast$(EXT) --dynamic onebench.fs
	./libgforth-fast$(EXT) --dynamic onebench.fs
	./gforth-fast$(EXT) --no-dynamic onebench.fs
	./libgforth-fast$(EXT) --no-dynamic onebench.fs
	./gforth$(EXT) --dynamic onebench.fs
	./libgforth$(EXT) --dynamic onebench.fs
	./gforth$(EXT) --no-dynamic onebench.fs
	./libgforth$(EXT) --no-dynamic onebench.fs
	./gforth-itc$(EXT) onebench.fs
	./libgforth-itc$(EXT) onebench.fs
	./gforth-ditc$(EXT) onebench.fs
	./libgforth-ditc$(EXT) onebench.fs

EXTRAVARS="GCC=$(GCC)" "CFLAGS=$(XCFLAGS)" "LIBTOOL=$(LIBTOOL)" "HOST=$(HOST)" "SRC=$(shell pwd)" "ENGINE=$(DITCENGINE) -i $(shell pwd)/gforth.fi" "LIBCCNAMED=$(shell pwd)/$(buildccdir)" "libccdir=$(libccdir)" "package=$(package)" "PREFIX=$(EXTRAPREFIX)" "datadir=$(datadir)" "ARCH=" "FAST=$(FAST)"

extras: $(EXTRAS) gforth.fi
	@for i in $(EXTRAS); do (cd $$i; $(MAKE) -f ./Makefile configs all $(EXTRAVARS)); done

extra-install: $(EXTRAS) gforth.fi
	for i in $(EXTRAS); do (cd $$i; $(MAKE) -f ./Makefile extra-install $(EXTRAVARS)); done

# -------------	Make forth images

# How to make new images:
# 1. Produce an image called kernlXYZ.fi-
#    the original kernel.fi is not touched because it's needed for creation
# 2. copy old kernlXYZ.fi to kernlXYZ.fi~
#    that's a backup copy in case the new kernels don't work
# 3. copy new kernels to kernlXYZ.fi
#    these are the ones we want to use now

kernl32l$(EC).fi-:	$(KERN_DEPS) mach32l.fs
		$(PREFORTH) -e 's" mach32l.fs"' $(srcdir)/kernel/main.fs -e "save-cross kernl32l$(EC).fi- $(bindir)/gforth-$(VERSION) bye"

kernl32b$(EC).fi-:	$(KERN_DEPS) mach32b.fs
		$(PREFORTH) -e 's" mach32b.fs"' $(srcdir)/kernel/main.fs -e "save-cross kernl32b$(EC).fi- $(bindir)/gforth-$(VERSION) bye"

kernl64l$(EC).fi-:	$(KERN_DEPS) mach64l.fs
		$(PREFORTH) -e 's" mach64l.fs"' $(srcdir)/kernel/main.fs -e "save-cross kernl64l$(EC).fi- $(bindir)/gforth-$(VERSION) bye"

kernl64b$(EC).fi-:	$(KERN_DEPS) mach64b.fs
		$(PREFORTH) -e 's" mach64b.fs"' $(srcdir)/kernel/main.fs -e "save-cross kernl64b$(EC).fi- $(bindir)/gforth-$(VERSION) bye"

#Solaris make does not like that:
kernl-%.fi:	arch/%/mach.fs arch/%/prim.fs arch/%/asm.fs $(KERN_SRC) kernel/version.fs $(FORTH_GEN0)
		$(PREFORTH) -e 's" $<"' $(srcdir)/kernel/main.fs -e "save-cross $@- $(bindir)/gforth-$(VERSION) bye"
		if [ -f `echo $< | sed s/fs/sh/` ]; \
		then sh `echo $< | sed s/fs/sh/` $@; \
		else $(CP) $@- $@; \
		fi

#SunOS make does not like that
#arch/%/mach.fs:	arch/%/prim.fs arch/%/asm.fs

kernl32b$(EC).fi:	$(KERNLS)
		-$(CP) kernl32b$(EC).fi kernl32b$(EC).fi~
		-$(CP) kernl32b$(EC).fi- kernl32b$(EC).fi

kernl32l$(EC).fi:	$(KERNLS)
		-$(CP) kernl32l$(EC).fi kernl32l$(EC).fi~
		-$(CP) kernl32l$(EC).fi- kernl32l$(EC).fi

kernl64b$(EC).fi:	$(KERNLS)
		-$(CP) kernl64b$(EC).fi kernl64b$(EC).fi~
		-$(CP) kernl64b$(EC).fi- kernl64b$(EC).fi

kernl64l$(EC).fi:	$(KERNLS)
		-$(CP) kernl64l$(EC).fi kernl64l$(EC).fi~
		-$(CP) kernl64l$(EC).fi- kernl64l$(EC).fi

#kernl%.fi:	kernl%.fi- $(KERNLS)
#		-$(CP) $@ $@~
#		-$(CP) $< $@

gforth.fi:	$(kernel_fi) gforthmi gforth-ditc$(EXT) $(GFORTH_FI_SRC) comp-i.fs
		GFORTHD="$(DITCENGINE)$(EXT) -p .$(PATHSEP)$(srcdir)" GFORTH="$(DITCENGINE)$(EXT) --die-on-signal -p .$(PATHSEP)$(srcdir) -i $(kernel_fi) $(STARTUP)" includedir=`pwd`/include bindir=`pwd` libccdir=`pwd`/$(buildccdir) ./gforthmi gforth.fi $(FORTHSIZES) $(FORTHKFLAGS) $(STARTUP)

# -------------	Make c-engine

prim.b:		prim cache0.vmg
		(cd $(srcdir) && $(M4) -Dcondbranch_opt=0 prim) >$@
		sleep 1 #should make hpux-workaround unnecessary

prim-fast.b:	prim cache-fast$(STACK_CACHE_DEFAULT_FAST).vmg cache-regs$(STACK_CACHE_REGS).vmg peeprules.vmg Makefile
		(cd $(srcdir) && $(M4) -Dcondbranch_opt=1 -DSTACK_CACHE_FILE=cache-fast$(STACK_CACHE_DEFAULT_FAST).vmg -DSTACK_CACHE_REGS=cache-regs$(STACK_CACHE_REGS).vmg prim) >$@
		sleep 1 #should make hpux-workaround unnecessary

$(FORTH_GEN_ENGINE_FAST): prim-fast.b prims2x.fs
		GFORTH="$(PREFORTH)" $(srcdir)/gfgen -fast

$(FORTH_GEN_ENGINE): prim.b prims2x.fs
		GFORTH="$(PREFORTH)" $(srcdir)/gfgen

engine/prim-s.i: engine/prim.i
	grep -v '^#line ' $(srcdir)/engine/prim.i >$@

kernel/aliases.fs:	prim.b prims2x.fs kernel/aliases0.fs
		$(CP) $(srcdir)/kernel/aliases0.fs $@-
		$(PREFORTH) prims2x.fs -e "forth-flag on s\" prim.b\" ' output-alias ' noop process-file bye" >>$@-
		$(CP) $@- $@
		$(RM) $@-

kernel/prim.fs:	prim.b prims2x.fs kernel/prim0.fs
		$(CP) $(srcdir)/kernel/prim0.fs kernel/prim.fs-
		$(PREFORTH) prims2x.fs -e "forth-flag on s\" prim.b\" ' output-forth ' output-forth-combined process-file bye" >>$@-
		$(CP) $@- $@
		$(RM) $@-


OPTS =  -ll-reg -noll-reg -ll -noll

MAKELINE-ll-reg   = OPT=-ll-reg OPTDEFINES="-DFORCE_LL -DFORCE_REG" OPTOBJECTS=
MAKELINE-noll-reg = OPT=-noll-reg OPTDEFINES=-DFORCE_REG OPTOBJECTS=dblsub.o
MAKELINE-ll       = OPT=-ll OPTDEFINES=-DFORCE_LL OPTOBJECTS=
MAKELINE-noll     = OPT=-noll OPTDEFINES= OPTOBJECTS=dblsub.o

gforth$(OPT)$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/config.h $(kernel_fi)
	if test -z "$(OPT)"; then \
		for i in $(OPTS); do $(MAKE) optgforth OPT=$$i && $(CP) gforth$${i}$(EXT) $@ && break; done; \
	else \
		( cd engine && $(MAKE) $@ ) && \
		$(CP) engine/$@ $@ && \
		($(MASSAGE_EXE) $@ || true); \
		true; \
		 $(MAKE) checkone check-nofast ENGINE=./engine/$@; \
	fi

optgforth:
	$(MAKE) gforth$(OPT)$(EXT) $(MAKELINE$(OPT))

gforth-fast$(OPT)$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE_FAST) engine/config.h
	if test -z "$(OPT)"; then \
		for i in $(OPTS); do $(MAKE) optgforth-fast OPT=$$i && $(CP) gforth-fast$${i}$(EXT) $@ && break; done; \
	else \
		(cd engine && $(MAKE) $@ ) && \
		$(CP) engine/$@ $@ && \
		($(MASSAGE_EXE) $@ || true); \
		true; \
		 $(MAKE) checkone ENGINE=./engine/$@; \
	fi

optgforth-fast:
	$(MAKE) gforth-fast$(OPT)$(EXT) $(MAKELINE$(OPT))

gforth-native$(OPT)$(EXT):	engines-fast
		$(CP) engine/$@ $@
		-$(MASSAGE_EXE) $@
#@GFORTHX_EXE@

gforth-itc$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/Makefile engine/config.h
		cd engine && $(MAKE) gforth-itc-noll$(EXT) $(MAKELINE-noll)
		$(CP) engine/gforth-itc-noll$(EXT) $@
		true

gforth-ditc$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/Makefile engine/config.h
		cd engine && $(MAKE) gforth-ditc-noll$(EXT) $(MAKELINE-noll)
		$(CP) engine/gforth-ditc-noll$(EXT) $@
		true

gforth-prof$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/Makefile engine/config.h
		cd engine && $(MAKE) gforth-prof-noll$(EXT) $(MAKELINE-noll)
		$(CP) engine/gforth-prof-noll$(EXT) $@

gforths: $(ENGINES) $(ENGINES_FAST)

#For now, we have no way to check if the engines build correctly
libgforth$(OPT)$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/config.h $(kernel_fi)
	if test -z "$(OPT)"; then \
		for i in $(OPTS); do $(MAKE) optlibgforth OPT=$$i && $(MAKE) optlibgforth2 OPT=$$i && $(LN_S) engine/$@ $@ && break; done; \
	else \
		(cd engine && $(MAKE) $@ ); \
		 $(MAKE) checkone check-nofast ENGINE=./engine/$@; \
	fi

optlibgforth:
	$(MAKE) libgforth$(OPT)$(EXT) $(MAKELINE$(OPT))

optlibgforth2:
	(cd engine && $(MAKE) libgforth$(EXT) $(MAKELINE$(OPT)))

libgforth-fast$(OPT)$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE_FAST) engine/config.h
	if test -z "$(OPT)"; then \
		for i in $(OPTS); do $(MAKE) optlibgforth-fast OPT=$$i && $(MAKE) optlibgforth-fast2 OPT=$$i && $(LN_S) engine/$@ $@ && break; done; \
	else \
		(cd engine && $(MAKE) $@ ); \
		 $(MAKE) checkone ENGINE=./engine/$@; \
	fi

optlibgforth-fast:
	$(MAKE) libgforth-fast$(OPT)$(EXT) $(MAKELINE$(OPT))

optlibgforth-fast2:
	(cd engine && $(MAKE) libgforth-fast$(EXT) $(MAKELINE$(OPT)))

libgforth-itc$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/Makefile engine/config.h
		cd engine && $(MAKE) libgforth-itc$(EXT) $(MAKELINE-noll)
		$(LN_S) engine/$@ $@
		-$(MASSAGE_EXE) $@

libgforth-ditc$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/Makefile engine/config.h
		cd engine && $(MAKE) libgforth-ditc$(EXT) $(MAKELINE-noll)
		$(LN_S) engine/$@ $@
		-$(MASSAGE_EXE) $@

libgforth-prof$(EXT): $(ENGINE_SOURCES) $(FORTH_GEN_ENGINE) engine/Makefile engine/config.h
		cd engine && $(MAKE) libgforth-prof$(EXT) $(MAKELINE-noll)
		$(LN_S) engine/$@ $@
		-$(MASSAGE_EXE) $@

libgforths: $(ENGINES:%=lib%) $(ENGINES_FAST:%=lib%)

# ------------- additional C primitives

.SUFFIXES:	.c .pri

.pri.c:		$< prim2cl.fs
		$(FORTHK) prim2cl.fs -e "file $< altogether bye" >$@

.c.so:		$<
		$(GCC) -shared $(CFLAGS) $(CPPFLAGS) $< -o $@

# -------------	Make Documentation

#TAGS is a GNU standard target
TAGS:		gforth.fi gforth.TAGS
		$(CP) gforth.TAGS $@

tags:		gforth.fi gforth.tags
		$(CP) gforth.tags $@

install.TAGS:	TAGS install-tags.fs
		sed 's;^\$(srcdir)/;;' TAGS|$(PREFORTH) -e 's" '${datarootdir}/gforth/$(VERSION)/'"' $(srcdir)/install-tags.fs -e bye >install.TAGS

gforth.TAGS:	kernl64l.fi gforth$(EXT) $(GFORTH_FI_SRC) $(LIBCC_BUILD_SRC) prim.TAGS kernel.TAGS
		$(FORTHK) etags.fs except.fs startup.fs $(LIBCC_BUILD_NOEC) -e bye
		cat TAGS prim.TAGS kernel.TAGS >gforth.TAGS
		rm TAGS

gforth.tags:	kernl64l.fi gforth$(EXT) $(GFORTH_FI_SRC) prim.tags kernel.tags
		$(FORTHK) tags.fs except.fs startup.fs -e bye
		cat tags prim.tags kernel.tags >gforth.tags
		rm tags

prim.TAGS: 	prim.b prims2x.fs
		$(PREFORTH) prims2x.fs -e "s\" prim.b\" ' output-tag dup process-file bye"|sed 's#^./prim#prim#' >$@-
		$(CP) $@- $@
		$(RM) $@-

prim.tags: 	prim.b prims2x.fs
		$(PREFORTH) prims2x.fs -e "s\" prim.b\" ' output-vi-tag dup process-file bye" >$@-
		$(CP) $@- $@
		$(RM) $@-

kernel.TAGS:
		$(RM) kernl32l$(EC).fi-; $(MAKE) kernl64l.fi

kernel.tags:
		$(RM) kernl32l$(EC).fi-; $(MAKE) kernl64l.fi

#elc files
gforth.elc:	gforth.el
		-$(EMACS) --batch --no-site-file -f batch-byte-compile gforth.el

start-gforth.el:	gforth.el
		echo > $@
		$(EMACS) --batch -l autoload --eval "(setq generated-autoload-file \"$$PWD/$@\")" -f batch-update-autoloads $(dir $<)

#Documentation

doc/doc.fd:	doc/makedoc.fs $(GFORTH_FI_SRC) $(EXTRA_DOC)
		$(FORTHK) -e "s\" doc/doc.fd\"" doc/makedoc.fs except.fs startup.fs $(EXTRA_DOC) -e bye

doc/crossdoc.fd:	$(KERN_SRC) kernel/version.fs $(FORTH_GEN0)
		$(FORTHK) -e 's" mach32l.fs"' kernel/main.fs -e bye

doc/gforth.texi: doc/gforth.texi.in prim.b ds2texi.fs prims2x.fs \
		doc/doc.fd doc/crossdoc.fd
		$(PREFORTH) ds2texi.fs prims2x.fs -e "s\" prim.b\" ' register-doc ' noop process-file" doc/crossdoc.fd doc/doc.fd -e "s\" $(srcdir)/doc/gforth.texi.in\" r/o open-file throw ds2texi bye" >$@-
		$(CP) $@- $@
		$(RM) $@-

checkdoc:	doc/gforth.texi.in prim.b ds2texi.fs prims2x.fs doc/doc.fd doc/crossdoc.fd answords.fs doc/gforth.texi
		$(FORTH) ds2texi.fs prims2x.fs -e "s\" prim.b\" ' register-doc ' noop process-file" doc/crossdoc.fd doc/doc.fd answords.fs -e bye
		-grep unknown doc/gforth.texi
		-grep doc- doc/gforth.texi| grep -v '^@c'

doc pdf dvi txt ps:	gforth.fi TAGS $(GFORTH_TEXI) $(VMGEN_TEXI) doc/Makefile
		-cd doc; $(MAKE) $@

# For an explanation of the following Makefile rules, see node
# `Automatic Remaking' in GNU Autoconf documentation.

#Note: no target "$(srcdir)/configure", because that does not trigger 
#unless $(srcdir)!="."
configure: configure.ac aclocal.m4
	cd $(srcdir) && autoconf

aclocal.m4:	configure.ac
		aclocal $(ACLOCAL_AMFLAGS)

# autoheader might not change config.h.in, so touch a stamp file.
engine/config.h.in: stamp-h.in
stamp-h.in: configure.ac aclocal.m4
	cd $(srcdir) && autoheader
	echo timestamp > $(srcdir)/stamp-h.in

engine/config.h: stamp-h
stamp-h: engine/config.h.in config.status stamp-h.in
	./config.status

Makefile Makedist engine/Makefile doc/Makefile gforthmi vmgen preforth libforth gforthker machpc.fs doc/version.texi envos.fs build-ec gforthmi.sh kernel/version.fs:	Makefile.in Makedist.in engine/Makefile.in doc/Makefile.in unix/Makefile.in gforthmi.in vmgen.in preforth.in libforth.in gforthker.in machpc.fs.in doc/version.texi.in config.status configure.ac envos.fs.in build-ec.in engine/libcc.h.in gforthmi.sh.in kernel/version.fs.in
	./config.status

config.status: configure
	./config.status --recheck

