# vi: set ts=8 shiftwidth=8 noexpandtab:
#
# VNPForth - Compiled native Forth for IA32 Linux systems
# Copyright (C) 2005  Simon Baldwin (simon_baldwin@yahoo.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.
#

INSTALL		= /usr/bin/install -c
INSTALL_PROGRAM	= ${INSTALL}
INSTALL_DATA	= ${INSTALL} -m 644
prefix		= /usr/local
exec_prefix	= ${prefix}
includedir	= ${exec_prefix}/include
libdir		= ${exec_prefix}/lib
mandir		= ${prefix}/man
GZIP		= /usr/bin/gzip
AWK		= /usr/bin/awk
RANLIB		= /usr/bin/ranlib

# Define a standard method of compiling .ft files.  Since the modules in
# this directory generally go into a shared library, use -fPIC.  Some
# modules containing CODE words require this, in any case.
FORTHC		= ../compiler/forthc
FORTHFLAGS	= -g -fPIC -O -P -S
.SUFFIXES:
.SUFFIXES:	.ft .o
.ft.o:
	$(FORTHC) $(FORTHFLAGS) $<

OBJECTS = _dlmain.o corestack.o datastack.o retstack.o memory.o logic.o \
	  maths.o compare.o coreio.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 coreio.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
DOCSOURCES  =	$(MDOCSOURCES) $(SDOCSOURCES) $(EDOCSOURCES)

# List of source files built into the C header file
HSOURCES    =	$(DOCSOURCES)

default: all

# Build the runtime .o, and both the static and shared libraries
_dlmain.o: _dlmain.ft
	$(AWK) -vDATETIME="`date '+%b %d %Y %T'`"			\
		'{ gsub (/__DATETIME__/, DATETIME); print $$0 }'	\
		_dlmain.ft >_dlmain.pp
	$(FORTHC) $(FORTHFLAGS) _dlmain.pp
	rm -f _dlmain.pp

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

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
libforth.3: libforth.3.m4 $(DOCSOURCES)
	$(AWK) -f libdoc.awk $(DOCSOURCES) \
		| $(AWK) -f libtables.awk >forthvariables
	$(AWK) -f libdoc.awk $(MDOCSOURCES) \
		| $(AWK) -vmode=word -f libtables.awk >forthwords
	$(AWK) -f libdoc.awk $(EDOCSOURCES) \
		| $(AWK) -vmode=word -f libtables.awk >extraforthwords
	$(AWK) -f include.awk <libforth.3.m4 >libforth.3
	rm -f forthwords extraforthwords forthvariables

# Build the forthlib.h C header from the sources
forthlib.h: forthlib.h.m4 $(HSOURCES)
	$(AWK) -f libdoc.awk $(HSOURCES) \
		| $(AWK) -f libnames.awk > fnames
	cat fnames | xargs $(FORTHC) -M > mnames
	$(AWK) -f libdoc.awk $(HSOURCES) \
		| $(AWK) -vfnames='fnames' -vmnames='mnames' -f libheader.awk \
		| $(AWK) -vsymbol='fdpush' -vargs='(int)' -f libhpatch.awk \
		| $(AWK) -vsymbol='fdpop'  -vtype='int'   -f libhpatch.awk \
		>cheader
	$(AWK) -f include.awk <forthlib.h.m4 >forthlib.h
	rm -f cheader fnames mnames

install:
	$(INSTALL) -d $(includedir) $(libdir) $(mandir)/man3
	$(INSTALL_DATA) forthlib.h $(includedir)/forthlib.h
	$(INSTALL_DATA) libforth.a $(libdir)/libforth.a
	$(INSTALL_PROGRAM) libforth.so $(libdir)/libforth.so
	$(INSTALL_DATA) forthrt1.o $(libdir)/forthrt1.o
	$(INSTALL_DATA) libforth.3 $(mandir)/man3/libforth.3
	$(GZIP) -f -9 $(mandir)/man3/libforth.3

install-strip:
	$(MAKE) INSTALL_PROGRAM='$(INSTALL_PROGRAM) -s' install

uninstall:
	rm -f $(includedir)/forthlib.h
	rm -f $(libdir)/libforth.a
	rm -f $(libdir)/libforth.so
	rm -f $(libdir)/forthrt1.o
	rm -f $(mandir)/man3/libforth.3 $(mandir)/man3/libforth.3.gz

clean:
	rm -f forthrt1.o libforth.a libforth.so *.s *.p *.o
	rm -f libforth.3 forthlib.h
	rm -f forthwords extraforthwords forthvariables cheader fnames mnames
	rm -f _dlmain.pp
	rm -f core

clobber: clean
distclean: clean
maintainer-clean: distclean
