# The is the make file to (re-)build the Cintpos System
# Written by Martin Richards (c) 17 August 2007

# Last modified 17 August 2007
# (Renamed most file names to lowercase letters to simplify running on
# Windows machines.)
# Modified the Makefile to allow compilation of the system for the
# GP2X handheld machine when running under Cygwin.

# Before running this makefile check that the environment variables
# BCPLROOT, BCPLHDR, BCPLCIN and PATH are correctly defined as
# described in docs/README

# Just modified to allow cross compilation for the GP2X handheld Linux machine.

# Location of Assembly language source code (architecture dependent)
# for example: SYSM = sysasm/LINUX

# My public HTML directory when not mountable on this machine.
# Remember to call ssh-add before calling make sshpub
SSHPUB = rhee.cl.cam.ac.uk:public_html


# Select the appropriate definition of CC, LIBS and SYSM

# First define the default settings.

AS = as
FASTERP = obj/fasterp.o

# Assume a little ender machine unless otherwise specified
ENDER = litender
GP2XCC = /cygdrive/c/devkitGP2X/bin/arm-linux-gcc -static -O9 -DforGP2X

#####################################################################
# add FASTERP = obj/cintasm.o for architectures having a hand written
# assembly language fast interpreter. This will override the
# default setting of: FASTERP = obj/fasterp.o
#####################################################################


# Version for the Power PC running Mac OS 10.3.9 -- added 25/7/06
#CC = gcc -O9 -DforMacOSPPC
#SYSM = ../cintcode/sysasm/macosppc
#ENDER = bigender

# Version for the Macmini running Intel Mac OS-X 32 bit version -- added 12/01/11
CC = gcc -O9 -m32 -DforMacOSX
ENDER = litender

#CC = gcc -O9 -DforMIPS
#SYSM = sysasm/MIPS

#CC = gcc -O9 -DforLINUX
#SYSM = sysasm/LINUX

# When run under Cygwin make will cross compile the system for
# the GP2X handheld machine, and make gp2xinstall will install Cintpos
# on the GP2X SD memory card assuming is is on drive G:.
# CC = gcc -O9 -DforCYGWIN32

#CC = gcc -O9 -DforSUN4
#SYSM = sysasm/SUN4
#ENDER = bigender

#CC = gcc -O9 -DforSPARC
#SYSM = sysasm/SPARC
#ENDER = bigender

#CC = gcc -O9 -DforALPHA -lbsd
#SYSM = sysasm/ALPHA


#CC = gcc -O9 -DforMSDOS
#SYSM = sysasm/MSDOSobj/

#CC = gcc -O9 -DforOS2
#SYSM = sysasm/OS2

all:	$(ENDER) \
	bin/cintpos cin/syscin/boot cin/syscin/blib cin/syscin/klib \
	cin/syscin/idle cin/syscin/cli cin/syscin/cli_init cin/syscin/debug \
	cin/syscin/cohand cin/syscin/fh0 cin/syscin/mbxhand cin/syscin/tcphand\
	cin/syscin/dlib \
	cin/abort cin/bounce cin/break \
	cin/c cin/changepri \
	cin/endcli cin/echo cin/enlarge \
	cin/hold cin/input cin/newcli \
	cin/logout cin/map cin/newcli cin/prompt \
	cin/unhold cin/repeat cin/run cin/send \
	cin/stack cin/status cin/test1 \
	cin/type cin/wait
	@echo
	./bin/cintpos

xref1:
	cintsys -c c compall xref |\
	egrep " [MSFG]:" |\
        tr -d "\015" |\
	sed -e "s/\./<dot>/g" |\
	sort -u |\
	sed -e "s/<dot>/\./g" > xrefdata

xref:	rawxref
	cintsys -c sortxref rawxref to xrefdata
	rm rawxref

rawxref:
	cintsys -c c compall xref >rawxref

run:	all sane
	./bin/cintpos

sys:	bin/cintpos $(ENDER)
	echo "bin/cintpos is up to date"

bigender:
	cp enderbig/syslib cin/syscin
	cp enderbig/boot cin/syscin
	cp enderbig/blib cin/syscin
	cp enderbig/klib cin/syscin
	cp enderbig/dlib cin/syscin
	cp enderbig/idle cin/syscin
	cp enderbig/cli cin/syscin
	cp enderbig/cli_init cin/syscin
	cp enderbig/debug cin/syscin
	cp enderbig/cohand cin/syscin
	cp enderbig/fh0 cin/syscin
	cp enderbig/mbxhand cin/syscin
	cp enderbig/tcphand cin/syscin
	cp enderbig/tbcpl cin
	cp enderbig/bcpl cin
	cp enderbig/c cin
	cp enderbig/echo cin
	cp enderbig/logout cin
	cp enderbig/cmpltest cin
	cp enderbig/map cin
	cp enderbig/abort cin
	cp enderbig/bcb .
	cp enderbig/bcl .
	cp enderbig/bsb .
	cp enderbig/bsl .
	echo touch >bigender

litender:
	cp enderlit/syslib cin/syscin
	cp enderlit/boot cin/syscin
	cp enderlit/blib cin/syscin
	cp enderlit/klib cin/syscin
	cp enderlit/dlib cin/syscin
	cp enderlit/idle cin/syscin
	cp enderlit/cli cin/syscin
	cp enderlit/cli_init cin/syscin
	cp enderlit/debug cin/syscin
	cp enderlit/cohand cin/syscin
	cp enderlit/fh0 cin/syscin
	cp enderlit/mbxhand cin/syscin
	cp enderlit/tcphand cin/syscin
	cp enderlit/tbcpl cin
	cp enderlit/bcpl cin
	cp enderlit/c cin
	cp enderlit/echo cin
	cp enderlit/logout cin
	cp enderlit/cmpltest cin
	cp enderlit/map cin
	cp enderlit/abort cin
	cp enderlit/bcb .
	cp enderlit/bcl .
	cp enderlit/bsb .
	cp enderlit/bsl .
	echo touch >litender

sane:
	stty sane

pubthreadtest:
	cintsys -c xencode sysc/threadtest.c to sysc/threadtest.cx
	scp sysc/threadtest.c sysc/threadtest.cx trace.txt $(SSHPUB)/threadtest

threadtest:	bin/threadtest
	threadtest

bin/threadtest:	obj/threadtest.o
	$(CC) -o bin/threadtest obj/threadtest.o -lpthread

obj/threadtest.o:	sysc/threadtest.c Makefile
	$(CC) -o obj/threadtest.o -c sysc/threadtest.c


bin/cintpos:	obj/cintpos.o obj/cinterp.o obj/kblib.o \
	$(FASTERP) obj/nullrastlib.o obj/devices.o
	$(CC) -lm -o bin/cintpos obj/cintpos.o obj/cinterp.o \
	obj/kblib.o $(FASTERP) obj/nullrastlib.o obj/devices.o -lpthread


sysc/INT.h:	sysc/mkint-h.c
	$(CC) -o mkint-h sysc/mkint-h.c
	mkint-h >sysc/INT.h
	rm -f mkint-h

obj/cintpos.o:	sysc/cintpos.c sysc/cintpos.h sysc/INT.h Makefile
	$(CC) -o obj/cintpos.o -c sysc/cintpos.c

obj/cinterp.o:	sysc/cinterp.c sysc/cintpos.h sysc/INT.h Makefile
	$(CC) -o obj/cinterp.o -c sysc/cinterp.c

obj/fasterp.o:	sysc/cinterp.c sysc/cintpos.h sysc/INT.h Makefile
	$(CC) -DFASTyes -o obj/fasterp.o -c sysc/cinterp.c

obj/devices.o:	sysc/devices.c sysc/cintpos.h sysc/INT.h Makefile
	$(CC) -o obj/devices.o -c sysc/devices.c

obj/cintasm.o:	$(SYSM)/cintasm.s sysc/INT.h Makefile
	$(CC) -o obj/cintasm.o -c $(SYSM)/cintasm.s

obj/kblib.o:	sysc/kblib.c sysc/cintpos.h sysc/INT.h Makefile
	$(CC) -o obj/kblib.o -c sysc/kblib.c

obj/nullrastlib.o:	sysc/nullrastlib.c sysc/cintpos.h sysc/INT.h Makefile
	$(CC) -o obj/nullrastlib.o -c sysc/nullrastlib.c





# Rules to compile the GP2X version

gp2x:	cintpos.gpu

gp2xinstall:	cintpos.gpu
	rm -rf G:/cintpos
	cp -rp ../../Cintpos G:/cintpos
	rm -f G:/Bin/cintpos.gpu
	cp -r cintpos.gpu G:/Bin/cintpos
	rm -f G:/Bin/cintpos.gpe
	cp -r ../../GP2X/gp2x/cintpos.gpe G:/Bin/cintpos.gpe

cintpos.gpu:	\
	obj/gp2xcintpos.o obj/gp2xcinterp.o obj/gp2xkblib.o \
	obj/gp2xfasterp.o obj/gp2xnullrastlib.o obj/gp2xdevices.o
	$(GP2XCC) -o cintpos.gpu obj/gp2xcintpos.o \
	obj/gp2xcinterp.o obj/gp2xkblib.o \
	obj/gp2xfasterp.o obj/gp2xnullrastlib.o obj/gp2xdevices.o -lpthread

obj/gp2xcintpos.o:	sysc/cintpos.c sysc/cintpos.h Makefile
	$(GP2XCC) -o obj/gp2xcintpos.o -c sysc/cintpos.c

obj/gp2xcinterp.o:	sysc/cinterp.c sysc/cintpos.h Makefile
	$(GP2XCC) -o obj/gp2xcinterp.o -c sysc/cinterp.c

obj/gp2xfasterp.o:	sysc/cinterp.c sysc/cintpos.h Makefile
	$(GP2XCC) -DFASTyes -o obj/gp2xfasterp.o -c sysc/cinterp.c

obj/gp2xdevices.o:	sysc/devices.c sysc/cintpos.h Makefile
	$(GP2XCC) -o obj/gp2xdevices.o -c sysc/devices.c

obj/gp2xcintasm.o:	$(SYSM)/cintasm.s Makefile
	$(GP2XCC) -o obj/gp2xcintasm.o -c $(SYSM)/cintasm.s

obj/gp2xkblib.o:	sysc/kblib.c sysc/cintpos.h Makefile
	$(GP2XCC) -o obj/gp2xkblib.o -c sysc/kblib.c

obj/gp2xnullrastlib.o:	sysc/nullrastlib.c sysc/cintpos.h Makefile
	$(GP2XCC) -o obj/gp2xnullrastlib.o -c sysc/nullrastlib.c


# Rules to compile the BCPL source

cin/syscin/boot:	sysb/boot.b Makefile g/libhdr.h
	cintsys -c "c bs boot"

cin/syscin/blib:	sysb/blib.b Makefile g/libhdr.h g/manhdr.h
	cintsys -c "c bs blib"

cin/syscin/dlib:	sysb/dlib.b Makefile g/libhdr.h g/manhdr.h
	cintsys -c "c bs dlib"

cin/syscin/klib:	sysb/klib.b Makefile g/libhdr.h
	cintsys -c "c bs klib"

cin/syscin/idle:	sysb/idle.b Makefile g/libhdr.h
	cintsys -c "c bs idle"

cin/syscin/cli:	sysb/cli.b Makefile g/libhdr.h
	cintsys -c "c bs cli"

cin/syscin/cli_init:	sysb/cli_init.b Makefile g/libhdr.h
	cintsys -c "c bs cli_init"

cin/syscin/debug:	sysb/debug.b Makefile g/libhdr.h
	cintsys -c "c bs debug"

cin/syscin/cohand:	sysb/cohand.b Makefile g/libhdr.h g/manhdr.h
	cintsys -c "c bs cohand"

cin/syscin/fh0:	sysb/fh0.b Makefile g/libhdr.h g/manhdr.h
	cintsys -c "c bs fh0"

cin/syscin/mbxhand:	sysb/mbxhand.b Makefile g/libhdr.h g/manhdr.h
	cintsys -c "c bs mbxhand"

cin/syscin/tcphand:	sysb/tcphand.b Makefile g/libhdr.h g/manhdr.h
	cintsys -c "c bs tcphand"
 
cin/abort:	com/abort.b Makefile g/libhdr.h
	cintsys -c "c bc abort"

cin/bounce:	com/bounce.b Makefile g/libhdr.h
	cintsys -c "c bc bounce"

cin/break:	com/break.b Makefile g/libhdr.h
	cintsys -c "c bc break"

cin/c:	com/c.b Makefile g/libhdr.h
	cintsys -c "c bc c"

cin/changepri:	com/changepri.b Makefile g/libhdr.h
	cintsys -c "c bc changepri"

cin/echo:	com/echo.b Makefile g/libhdr.h
	cintsys -c "c bc echo"

cin/endcli:	com/endcli.b Makefile g/libhdr.h
	cintsys -c "c bc endcli"

cin/enlarge:	com/enlarge.b Makefile g/libhdr.h
	cintsys -c "c bc enlarge"

cin/hold:	com/hold.b Makefile g/libhdr.h
	cintsys -c "c bc hold"

cin/input:	com/input.b Makefile g/libhdr.h
	cintsys -c "c bc input"

cin/logout:	com/logout.b Makefile g/libhdr.h
	cintsys -c "c bc logout"

cin/map:	com/map.b Makefile g/libhdr.h
	cintsys -c "c bc map"

cin/newcli:	com/newcli.b Makefile g/libhdr.h
	cintsys -c "c bc newcli"

cin/prompt:	com/prompt.b Makefile g/libhdr.h
	cintsys -c "c bc prompt"

cin/unhold:	com/unhold.b Makefile g/libhdr.h
	cintsys -c "c bc unhold"

cin/repeat:	com/repeat.b Makefile g/libhdr.h
	cintsys -c "c bc repeat"

cin/run:	com/run.b Makefile g/libhdr.h
	cintsys -c "c bc run"

cin/send:	com/send.b Makefile g/libhdr.h
	cintsys -c "c bc send"

cin/stack:	com/stack.b Makefile g/libhdr.h
	cintsys -c "c bc stack"

cin/status:	com/status.b Makefile g/libhdr.h
	cintsys -c "c bc status"

cin/test1:	com/test1.b Makefile g/libhdr.h
	cintsys -c "c bc test1"

cin/type:	com/type.b Makefile g/libhdr.h
	cintsys -c "c bc type"

cin/wait:	com/wait.b Makefile g/libhdr.h
	cintsys -c "c bc wait"

env:
	@echo
	@echo "To set the Cintpos environment variables"
	@echo "read, edit and run the script ./setenv, which is:"
	@echo 
	cat ./setenv
	@echo
# Alternatively put something like the following in your .profile file.
#	export POSROOT=/homes/mr10/distribution/Cintpos/cintpos
#	export POSPATH=$CINTPOS/cin
#	export POSHDRS=$CINTPOS/g
#	export PATH=$PATH:$CINTPOS:.

clean:
	rm -f OCODE COMFILE litender bigender
	rm -f bin/cintpos bin/cintpos.exe
	rm -f ../*~ *~ */*~
	rm -rf obj cin cin/syscin
	mkdir obj cin cin/syscin

vclean:	clean




