#*****************************************************************************
#
#			      PilotForth
#
#      Copyright (C) 1997, Ivan A. Curtis.  All rights reserved.
#
#			       based on
# C-FORTH: a portable, C-coded figFORTH interpreter by Allan Pratt April 1985.
#
# This program may not be redistibuted in part or in whole without this
# Copyright Message being included at the top of each file.
#
#   This is free software, licensed under the GNU Public License V2.
#		  See the file COPYING for details.
#
#******************************************************************************
#******************************************************************************
#
# Filename:	Forth/bootstrap/Makefile
#
# Description:	Makefile for PilotForth bootstrap
#
# Update History:   (most recent first)
#   I. Curtis  15-May-97 20:59 -- v0.1 Released.
#******************************************************************************

CC = gcc
CFLAGS = -Wall -O2
IFLAGS = -I..

all : VocabDB.pdb BlockDB.pdb

###################################################################
##
##               Maintain Bootstrap Support Tools
##
###################################################################

##
## buildpdb is used to build pilot database files
##
buildpdb : buildpdb.c
	${CC} ${CFLAGS} -o buildpdb buildpdb.c

##
## nf is the forth bootstrap compiler
##
nf :	nf.o lex.yy.o
	${CC} ${IFLAGS} -o nf nf.o lex.yy.o -lfl

nf.o :	nf.c forth.lex.h ../common.h
	${CC} ${IFLAGS} -c nf.c

lex.yy.o : lex.yy.c forth.lex.h
	${CC} ${IFLAGS} -c lex.yy.c

lex.yy.c : forth.lex
	lex forth.lex
	/bin/rm -f lex.tmp
	sed "s/static int input/int input/g" lex.yy.c > lex.tmp
	/bin/mv -f lex.tmp lex.yy.c

##
## l2b : convert a line file to a block file. Usage: l2b < linefile > blockfile
##
l2b :	l2b.c
	$(CC) -o l2b l2b.c
##
## b2l: convert a block file to a line file. Usage: b2l < blockfile > linefile
##
b2l :	b2l.c
	$(CC) -o b2l b2l.c

###################################################################
##
##                 Maintain Bootstrap Databases
##
###################################################################

##
## Build the forth vocabulary database
## First build the forth core file by compiling forth.dict with nf
## Then use buildpdb to make the pilot database
## Note that this DB contains shorts, hence -w 2 for little Endians.
## You BIG Endians should use -w 1
##
forth.core : nf forth.dict
	nf < forth.dict

VocabDB.pdb : forth.core buildpdb
	buildpdb -c "i4th" -t "fVoc" -w 2 -n "VocabDB" -r 2048 \
		-s `wc -c forth.core | gawk '{print $$1}'` < forth.core \
		> VocabDB.pdb.fail
	/bin/mv -f VocabDB.pdb.fail VocabDB.pdb

##
## Make the forth Block database
##
## First make the block file from forth.line using l2b
## Then use buildpdb to make the pilot database.
## Note that this DB contains chars, hence -w 1
##
forth.block : forth.line l2b
	l2b < forth.line > forth.block

BlockDB.pdb : forth.block
	buildpdb -c "i4th" -t "fBlk" -w 1 -n "BlockDB" -r 1024 \
		-s `wc -c forth.block | gawk '{print $$1}'` < forth.block \
		> BlockDB.pdb.fail
	/bin/mv -f BlockDB.pdb.fail BlockDB.pdb

##
## Housekeeping
##
clean::
	/bin/rm -f core *~ *.fail *.o forth.core lex.yy.c lex.tmp
	/bin/rm -f forth.dump forth.map

clobber:: clean
	/bin/rm -f *.pdb buildpdb nf b2l l2b forth.block
