#
# Makefile for kForth
#
# Copyright (c) 1998--2011 Creative Consulting for Research and Education
# Provided under the terms of the GNU General Public License
#
# Revisions: 
#       2006-02-23
#       2006-04-01  km; added TARGET-CPU and TARGET-OS
#       2006-04-11  km; changed names to TARGET_CPU and TARGET_OS
#	2006-04-15  km; fixed `make snapshot' to preserve links in fast subdir
#       2006-06-12  km; updated to store sample *.dat files in archives and snapshots
#       2006-11-02  km; updated to store *.el files (for Emacs) in archive and snapshot
#	2007-02-14  km; updated version to 1.4.1
#       2007-11-30  km; revised to copy examples directory into archive and 
#                       snapshot files; updated version to 1.4.2
#       2008-03-18  km; added dependency on vm-common.s for vm.s
#       2008-03-21  km; added dependency on ForthWords.h for ForthCompiler.cpp
#       2008-07-05  km; integrated make for fast version; updated version to 1.4.3
#       2008-07-07  km; removed dependency on ForthVM-fast.cpp; use FAST to
#			  pass compiler switch for regular and fast version.
#       2009-05-03  km; remove dependency on vmc-fast.c; updated archive target commands
#       2009-09-20  km; added dependency on kfmacros.h; updated version to 1.4.6
#       2009-09-23  km; added linking to the dynamic link loader library
#	2009-10-01  km; updated version to 1.4.7
#       2009-10-04  km; updated version to 1.5.0
#       2009-10-05  km; revised to store ChangeLog file in snapshot and archive files.
#       2010-04-25  km; updated version to 1.5.1, the new release version
#       2010-12-22  km; removed reference to README.SNAP file (no longer used),
#                       updated version to 1.5.2.
#       2011-03-05  km; revised archive and snapshot rules to exclude version
#                       control subdirectories in the archive files (since we
#                       now using Subversion for source control) 
#
# Possible invocations:
#
#	make		creates dynamically linked release executable
#	make clean	remove all object files belonging to this project
#	make debug	create statically linked debug executable
#	make archive	create a compressed tar file (for release)
#       make snapshot   create a development snapshot
#
# Notes:
#
#   1. If a debug version is being built, always invoke "make clean" 
#      before "make debug".
#
#
# Default debug option creates a release version of the executable.
# Invoke "make debug" if you want to create an executable
# that contains debugging information for the GNU debugger (gdb).

VERSION = 1.5.2
BUILD_DATE=`date +%F`
DEBUG = 
TARGET_CPU = x86
TARGET_OS  = linux

# location of gcc and g++
GCCDIR = /usr/bin

# library path
LIBPATH = /usr/lib:/usr/lib32

CPP = ${GCCDIR}/g++
CC  = ${GCCDIR}/gcc
CPPFLAGS = -c -m32
CFLAGS = -c -m32
FAST = -D__FAST__
OBJS = kforth.o ForthVM.o ForthCompiler.o vm.o vmc.o
FOBJS = kforth-fast.o ForthVM-fast.o ForthCompiler.o vm-fast.o vmc-fast.o
LIBS = -lreadline -lncurses -ldl

all:	
	make kforth
	make kforth-fast


kforth: ${OBJS} 
	${CPP} -m32 -o kforth ${DEBUG} ${OBJS} -L${LIBPATH} ${LIBS}

kforth-fast: ${FOBJS}
	${CPP} -m32 -o kforth-fast ${DEBUG} ${FOBJS} -L${LIBPATH} ${LIBS}

clean:
	- rm -f ${OBJS} ${FOBJS} kforth kforth-fast


archive:
	mkdir kforth-${VERSION}
	cp --target-directory=kforth-${VERSION}/ *.s *.h *.c *.cpp\
	   *.xpm *.el Makefile README ChangeLog
	cp -pR examples kforth-${VERSION}/
	tar --exclude-vcs -czvf kforth-${TARGET_CPU}-${TARGET_OS}-${VERSION}.tar.gz\
           kforth-${VERSION}
	rm -R --force kforth-${VERSION}

snapshot:
	mkdir kforth-${BUILD_DATE}/
	cp -p *.s *.h *.c *.cpp *.xpm *.el Makefile README ChangeLog kforth-${BUILD_DATE}/
	cp -pR examples kforth-${BUILD_DATE}/
	tar --exclude-vcs -czvf kforth-${TARGET_CPU}-${TARGET_OS}-${BUILD_DATE}.tar.gz\
           kforth-${BUILD_DATE}
	rm -R --force kforth-${BUILD_DATE}

debug:
	make kforth "DEBUG = -g"

kforth.o: kforth.cpp ForthVM.h ForthCompiler.h
	${CPP} ${CPPFLAGS} ${DEBUG} -DVERSION=\"${VERSION}\" \
	-DBUILD_DATE=\"${BUILD_DATE}\" kforth.cpp

kforth-fast.o: kforth.cpp ForthVM.h ForthCompiler.h
	${CPP} ${CPPFLAGS} ${DEBUG} -DVERSION=\"${VERSION}\" \
	-DBUILD_DATE=\"${BUILD_DATE}\" ${FAST} -o kforth-fast.o kforth.cpp

ForthCompiler.o: ForthCompiler.cpp ForthCompiler.h fbc.h ForthWords.h
	${CPP} ${CPPFLAGS} ${DEBUG} ForthCompiler.cpp

ForthVM.o: ForthVM.cpp ForthVM.h fbc.h ForthCompiler.h kfmacros.h
	${CPP} ${CPPFLAGS} ${DEBUG} ForthVM.cpp

ForthVM-fast.o: ForthVM.cpp ForthVM.h fbc.h ForthCompiler.h kfmacros.h
	${CPP} ${CPPFLAGS} ${DEBUG} ${FAST} -o ForthVM-fast.o ForthVM.cpp

vmc.o: vmc.c kfmacros.h
	${CC} ${CFLAGS} ${DEBUG} vmc.c

vmc-fast.o: vmc.c kfmacros.h
	${CC} ${CFLAGS} ${DEBUG} ${FAST} -o vmc-fast.o vmc.c

vm.o: vm.s vm-common.s
	as --32 -o vm.o vm.s

vm-fast.o: vm-fast.s vm-common.s
	as --32 -o vm-fast.o vm-fast.s



# end of makefile

