#
# VNPForth - Compiled native Forth for IA32 Linux systems
# Copyright (C) 2000  Simon Baldwin (simonb@sco.com)
#
# This program 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-1307, USA.
#
PSTAMP	= "`date '+%b %d %Y %T'`"

#
# Define a standard method of compiling .ft files
#
FORTHC		= ../compiler/forthc
FORTHFLAGS	= -g -O
#FORTHFLAGS	= -g -O -P -S
.SUFFIXES:
.SUFFIXES:	.ft .o
.ft.o:
	$(FORTHC) $(FORTHFLAGS) $<

#
# List of object files built in this directory
#
OBJECTS =	_dlmain.o corestack.o datastack.o retstack.o \
		memory.o logic.o maths.o compare.o basicio.o bitsnbytes.o \
		loop.o compat.o env.o fp.o \
		procenv.o cclinkage.o syscall.o errno.o \
		extsyscall.o

#
# List of source files built into the man page
#
MDOCSOURCES =	_dlmain.ft corestack.ft datastack.ft retstack.ft \
		memory.ft logic.ft maths.ft compare.ft basicio.ft \
		bitsnbytes.ft loop.ft compat.ft env.ft fp.ft \
		procenv.ft cclinkage.ft \
		forthrt1.ft

SDOCSOURCES =	syscall.ft extsyscall.ft
EDOCSOURCES =	errno.ft

default: all

#
# Build the runtime .o, and both the static and shared libraries
#
_dlmain.o: _dlmain.ft
	m4 -DPSTAMP=$(PSTAMP) _dlmain.ft >_dlmain.pp
	$(FORTHC) $(FORTHFLAGS) _dlmain.pp
	rm -f _dlmain.pp

all: forthrt1.o libforth.a libforth.so libforth.3

libforth.a: $(OBJECTS)
	rm -f libforth.a; ar -cr libforth.a $(OBJECTS)
	ranlib libforth.a

libforth.so: $(OBJECTS)
	ld -shared -o libforth.so $(OBJECTS)

#
# Add the documentation to the man page
#
TRIMENTRY =	tr '\t' ' ' |						\
		sed -e 's/  */	/'					\
		    -e 's/	[^(]*(/	(/' -e 's/)[^)]*$$/)/'		\
		    -e 's/^/\\\&/'
libforth.3: libforth.3.m4 $(MDOCSOURCES) $(SDOCSOURCES) $(EDOCSOURCES)
	@egrep -h '^(VARIABLE|CREATE)' 					\
			$(MDOCSOURCES) $(SDOCSOURCES) $(EDOCSOURCES) |	\
		sed -e 's/^VARIABLE[[:space:]]*//' 			\
		    -e 's/^CREATE[[:space:]]*//' 			\
		| $(TRIMENTRY) | sort >forthvariables
	@egrep -h '^(CODE|:)' $(MDOCSOURCES) |				\
		sed -e 's/^CODE[[:space:]]*//' -e 's/^:[[:space:]]*//'	\
		| $(TRIMENTRY) | sort >forthwords
	@egrep -h '^(CODE|:)' $(SDOCSOURCES) |				\
		sed -e 's/^CODE[[:space:]]*//' -e 's/^:[[:space:]]*//'	\
		| $(TRIMENTRY) | sort >extraforthwords
	@egrep -h '^(CODE|:)' $(EDOCSOURCES) |				\
		sed -e 's/^CODE[[:space:]]*//' -e 's/^:[[:space:]]*//'	\
		| $(TRIMENTRY) | sort >>extraforthwords
	m4 libforth.3.m4 >libforth.3
	rm -f forthwords extraforthwords forthvariables

#
# Cleanup targets
#
clean:
	rm -f forthrt1.o libforth.a libforth.so *.s *.p *.o libforth.3
	rm -f _dlmain.pp
	rm -f core
	rm -f forthwords extraforthwords forthvariables

clobber: clean
