#Makefile for the C part of Gforth

#Copyright (C) 1995,1996,1997,1998,2000,2003 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 2
#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, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.

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

VERSION =0.5.9
DOSVERSION=`echo $VERSION|sed 's/\.//g'`
EXE=.exe

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

machine=386
kernel_fi=kernl32l.fi
include_fi=gforth.fi
# this is the type of machine
# used to extend the include path with ./arch/$machine
# so we could include a machine specific
# machine.h file

PATHSEP = ;

osclass = dos

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

SHELL	= bash
RM	= del
RMTREE	= del -rf
CP	= copy -p
TAR	= tar cf -
GCC	= gcc
CC	= $(GCC)
STRIP	= strip

FORTHPATH = .$(PATHSEP)$(libdir)/gforth/site-forth$(PATHSEP)$(datadir)/gforth/site-forth$(PATHSEP)$(libdir)/gforth/$(VERSION)$(PATHSEP)$(datadir)/gforth/$(VERSION)

# ------------	Install Directorys

VPATH = .
prefix = /usr/local
exec_prefix = ${prefix}
srcdir = .
bindir = $(exec_prefix)/bin
#read-only architecture-independent files
datadir = $(prefix)/share
#read-only architecture-dependent non-ascii files
libdir = $(prefix)/lib
infodir = $(prefix)/info
mandir = $(prefix)/man
man1dir= $(mandir)/man1
man1ext= .1

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

XCFLAGS	= 
XDEFINES = -DHAVE_CONFIG_H
SWITCHES = $(XCFLAGS) $(XDEFINES)
#use -fno-inline to avoid register problems with asinh, atanh on gcc-3.3 on 386
ENGINE_FLAGS =  -fno-defer-pop -fcaller-saves -fno-inline
DEBUGFLAG = 
CFLAGS	= $(DEBUGFLAG) -I$(srcdir)/../arch/$(machine) -I. -Wall $(SWITCHES) -DDEFAULTPATH='"~+\""
CFLAGS2	= $(DEBUGFLAG) -I$(srcdir)/../arch/$(machine) -I. -Wall $(SWITCHES) -DDEFAULTPATH='"~+\""
FORTHKFLAGS= --die-on-signal -p "..$(PATHSEP)$(srcdir)" -i ../$(kernel_fi)
FORTHK	= ../gforth $(FORTHKFLAGS)

#John Wavrik should use -Xlinker -N to get a writable text (executable)
XLDFLAGS = 
LDFLAGS =  $(DEBUGFLAG) $(XLDFLAGS)
LDLIBS  = -lm 

AOBJECTS = io.o signals.o support.o  pow10.o strsignal.o ecvt.o atanh.o getopt.o getopt1.o

OBJECTS = engine.o engine2.o main.o
OBJECTS_NATIVE = engine-native.o engine-native2.o engine-native3.o main-native.o
OBJECTS_FAST = engine-fast.o engine-fast2.o main-fast.o
OBJECTS_ITC = engine-itc.o main-itc.o
OBJECTS_DITC = engine-ditc.o main-ditc.o
OBJECTS_PROF = engine-prof.o main-prof.o
OBJECTS_FI =  engine.o main-fi.o

# In engine subdirectory there are (or should be) only files that belong to
# our engine, so we can make life easy
DEPS =  config.h $(srcdir)/../arch/$(machine)/machine.h
ENGINE_DEPS = engine.c $(DEPS) prim_lab.i prim.i
MAIN_DEPS = main.c $(DEPS) prim_superend.i prim_num.i prim_grp.i costs.i super2.i

#some makes don't do the -o $@ correctly, so we help them
.c.o:
	$(GCC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<

support.o: support.c config.h forth.h

gforth$(EXE):	$(OBJECTS) $(AOBJECTS)
	$(GCC) $(LDFLAGS) $(OBJECTS) $(AOBJECTS) $(LDLIBS) -o $@

gforth-native$(EXE):	$(OBJECTS_NATIVE) $(AOBJECTS)
	$(GCC) $(LDFLAGS) $(OBJECTS_NATIVE) $(AOBJECTS) $(LDLIBS) -o $@

gforth-fast$(EXE):	$(OBJECTS_FAST) $(AOBJECTS)
	$(GCC) $(LDFLAGS) $(OBJECTS_FAST) $(AOBJECTS) $(LDLIBS) -o $@

gforth-itc$(EXE):	$(OBJECTS_ITC) $(AOBJECTS)
	$(GCC) $(LDFLAGS) $(OBJECTS_ITC) $(AOBJECTS) $(LDLIBS) -o $@

gforth-ditc$(EXE): $(OBJECTS_DITC) $(AOBJECTS)
	$(GCC) $(LDFLAGS) $(OBJECTS_DITC) $(AOBJECTS) $(LDLIBS) -o $@

gforth-prof$(EXE): $(OBJECTS_PROF) $(AOBJECTS) profile.o
	$(GCC) $(LDFLAGS) $(OBJECTS_PROF) $(AOBJECTS) profile.o $(LDLIBS) -o $@

gforth-fi$(EXE):	$(OBJECTS_FI) $(AOBJECTS)
	$(GCC) $(LDFLAGS) $(OBJECTS_FI) $(AOBJECTS) $(LDLIBS) -o $@

engine.s:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -S $(srcdir)/engine.c

engine.o:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DGFORTH_DEBUGGING -o $@ -c $(srcdir)/engine.c

engine2.o:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DGFORTH_DEBUGGING -DENGINE=2 -o $@ -c $(srcdir)/engine.c

engine-native.o:$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DNO_IP -o $@ -c $(srcdir)/engine.c

engine-native2.o:$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DNO_IP -DENGINE=2 -o $@ -c $(srcdir)/engine.c

engine-native3.o:$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DNO_IP -DENGINE=3 -o $@ -c $(srcdir)/engine.c

engine-fast.o:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -o $@ -c $(srcdir)/engine.c

engine-fast2.o:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DENGINE=2 -o $@ -c $(srcdir)/engine.c

engine-itc.o:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DINDIRECT_THREADED -DGFORTH_DEBUGGING -o $@ -c $(srcdir)/engine.c

engine-ditc.o:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DDOUBLY_INDIRECT -o $@ -c $(srcdir)/engine.c

engine-prof.o:	$(ENGINE_DEPS)
		$(GCC) $(CFLAGS2) $(ENGINE_FLAGS) -DVM_PROFILING -o $@ -c $(srcdir)/engine.c

main.o:		$(MAIN_DEPS) 
		$(GCC) $(CFLAGS) -DGFORTH_DEBUGGING -o $@ -c $(srcdir)/main.c

main-native.o:	$(MAIN_DEPS) 
		$(GCC) $(CFLAGS) -DNO_IP -o $@ -c $(srcdir)/main.c

main-fast.o:	$(MAIN_DEPS) 
		$(GCC) $(CFLAGS) -o $@ -c $(srcdir)/main.c

main-itc.o:	$(MAIN_DEPS) 
		$(GCC) $(CFLAGS) -DINDIRECT_THREADED -DGFORTH_DEBUGGING -o $@ -c $(srcdir)/main.c

main-ditc.o:	$(MAIN_DEPS)
		$(GCC) $(CFLAGS) -DDOUBLY_INDIRECT -o $@ -c $(srcdir)/main.c

main-prof.o:	$(MAIN_DEPS)
		$(GCC) $(CFLAGS) -DVM_PROFILING -o $@ -c $(srcdir)/main.c

main-fi.o:	$(MAIN_DEPS)
		$(GCC) $(CFLAGS) -DINCLUDE_IMAGE -o $@ -c $(srcdir)/main.c

peephole.o:	peephole.c peephole.i $(DEPS)
		$(GCC) $(CFLAGS) -o $@ -c $(srcdir)/peephole.c

profile.o:	profile.c profile.i $(DEPS)
		$(GCC) $(CFLAGS) -o $@ -c $(srcdir)/profile.c

#The next two rules seem to be superfluous:

# engine:		$(OBJECTS) $(OBJECTS0) ;

# engine_ditc:	$(OBJECTS_DITC) $(OBJECTS0) ;

#NeXTstep hack

termios.o:	/usr/lib/libposix.a
	ar x /usr/lib/libposix.a termios.o

#duplicated rules to avoid too many recursive make invocations
# !! No any forth stuff should be done in ../Makefile.
# !! I added the dependencies on prim.i and prim_lab.i in the main Makefile, jens

#I commented out the following rules because they are no longer up-to-date. - anton

#prim.i:		../prim.b ../prims2x.fs
#		$(FORTHK) prims2x.fs -e "s\" ../prim.b\" ' output-c process-file bye" >$@-
#		$(CP) $@- $@
#		$(RM) $@-
#
#prim_lab.i:	../prim.b ../prims2x.fs
#		$(FORTHK) prims2x.fs -e "s\" ../prim.b\" ' output-label process-file bye" >$@-
#		$(CP) $@- $@
#		$(RM) $@-

config.h:	stamp-h
stamp-h:	
		
		echo timestamp > stamp-h

image.c:	../fi2c.fs ../$(include_fi)
		$(FORTHK) fi2c.fs -e "s\" ../$(include_fi)\" fi2c bye" >$@

../$(include_fi):	FORCE
		cd .. && $(MAKE) $(include_fi)

#phony targets depend on FORCE; this is more portable than .PHONY
FORCE:

%.o:	%.c
		$(GCC) $(CFLAGS) -c $<
