# makefile for pForth
# Portable Forth written in 'C'
# by Phil Burk
# For more info visit http://www.softsynth.com/pforth/
#
# See "help" target below.

.POSIX:

# Options include: PF_SUPPORT_FP PF_NO_MALLOC PF_NO_INIT PF_DEBUG
# See "docs/pf_ref.htm" file for more info.

TOPSRC       = $(shell cd ../..; pwd)
include      $(TOPSRC)/target.mk

FULL_WARNINGS =  \
#        -c89 \
        -fsigned-char \
        -fno-builtin \
        -fno-unroll-loops \
        -fpeephole \
        -fno-keep-inline-functions \
        -pedantic \
        -Wcast-qual \
        -Wall \
        -Wwrite-strings \
        -Winline  \
        -Wmissing-prototypes \
        -Wmissing-declarations

CCOPTS = -DPF_SUPPORT_FP -O2 -g $(FULL_WARNINGS)

IO_SOURCE = pf_io_posix.c
#IO_SOURCE = pf_io_stdio.c

EMBCCOPTS = -DPF_STATIC_DIC

#######################################
PFBASESOURCE = pf_cglue.c pf_clib.c pf_core.c pf_inner.c \
	pf_io.c pf_io_none.c pf_main.c pf_mem.c pf_save.c \
	pf_text.c pf_words.c pfcompil.c pfcustom.c
PFSOURCE = $(PFBASESOURCE) $(IO_SOURCE)

XCPPFLAGS = $(CDEFS) -I.

COMPILE = $(CC) $(CCOPTS) $(CFLAGS) $(XCPPFLAGS) $(CPPFLAGS)
LINK = $(CC) $(LDFLAGS)

.SUFFIXES: .c .o .eo

PFOBJS     = $(PFSOURCE:.c=.o)
PFEMBOBJS  = $(PFSOURCE:.c=.eo)

.c.o:
	$(COMPILE) -c -o $@ $<

.c.eo:
	$(COMPILE) $(EMBCCOPTS) -c -o $@ $<

all: pforth_standalone

# Build pforth by compiling 'C' source.
pforth: $(PFOBJS)
	$(LINK) -o $@ $(PFOBJS)

# Build basic dictionary image by running newly built pforth and including "system.fth".
pfdicdat.h: pforth
	wd=$$(pwd); (cd fth; $${wd}/pforth -i system.fth)
	mv fth/pforth.dic pforth.dic
	mv fth/pfdicdat.h $@

pforth_standalone: pfdicdat.h $(PFEMBOBJS)
	$(LINK) -o $@ $(PFEMBOBJS) -lm
	mv pforth ..
        mv pforth.dic ..
	@echo ""
	@echo "Standalone pForth executable written to pforth_standalone"


# target aliases
pfdicapp: pforth

pfdicdat: pfdicdat.h

pforthapp: pforth_standalone

help:
	@echo "Use 'make all' to build standalone pForth executable."
	@echo "PForth can be built in several stages using these targets:"
	@echo "   pfdicapp = executable pForth with minimal dictionary. All from 'C'."
	@echo "   pfdicdat = image of full dictionary build by compiling Forth code."
	@echo "   pforthapp = executable with embedded dictionary image. DEFAULT 'all' target."
	@echo ""
	@echo "   The file 'pfdicdat.h' is generated by pForth. It contains a binary image of the Forth dictionary."
	@echo "   It allows pForth to work as a standalone image that does not need to load a dictionary file."

install:
	install -s -m 755 pforth ${DESTDIR}/bin/pforth
	install -c -o bin -g bin -m 444 pforth.dic ${DESTDIR}/bin/pforth.dic
	install -c -o bin -g bin -m 444 pfdicdat.h ${DESTDIR}/bin/pfdicdat.h

test:
	wd=$$(pwd); (cd fth; $${wd}/pforth_standalone -q t_corex.fth)
	wd=$$(pwd); (cd fth; $${wd}/pforth_standalone -q t_strings.fth)
	wd=$$(pwd); (cd fth; $${wd}/pforth_standalone -q t_locals.fth)
	wd=$$(pwd); (cd fth; $${wd}/pforth_standalone -q t_alloc.fth)
	wd=$$(pwd); (cd fth; $${wd}/pforth_standalone -q t_floats.fth)

clean:
	rm -f *.o *~ pforth_standalone pfdicdat.h ../pforth.dic ../pforth
