# This is the Makefile for the native code version of 64 bit BCPL
# running on 64 bit hardware such as an AMD64 machine.

# Implemented by Martin Richards (c) June 2013

# Last updated 10 Jun 2024

# Change history

#10/06/2024
# Trying to make natbcpl work after years of neglect.
# These changes have only just started.


# natbcpl uses the BCPL to SIAL compiler bcpl2sial and various
# SIAL to native code codegenerators, such as:

# sial-386 sial-alpha sial-vax sial-arm sial-amd64b64

# sial-arm is currently under development and will be tested
# on the Raspberry-Pi


# "../" below is included to simplify compilation for a
# different architectures sharing the same filing system.
# We assume that the current directory is BCPL/natbcpl
# The definitions below give the locations of various directories
# of the standard BCPL distribution.

CINTCODE = ../cintcode
BCPLPROGS = ../bcplprogs
DEMOS = $(BCPLPROGS)/demos
EDSAC = $(BCPLPROGS)/edsac
SORT = $(BCPLPROGS)/sort
TESTS = $(BCPLPROGS)/tests
COM = $(CINTCODE)/com
SYSB = $(CINTCODE)/sysb
SYSC = $(CINTCODE)/sysc
RASPI = $(BCPLPROGS)/raspi

# Choose the architecture directory

# ARCH = amd64b32
ARCH = amd64b64
# ARCH = alpha
# ARCH = arm
# ARCH = raspi32
# ARCH = raspi64

# Choose between 32 and 64 bit cintsys

# CINTSYS = cintsys
CINTSYS = cintsys64

# LIBS is the collection compiled modules required native code cmpilatios
#of BCPL programs. They are all compilations of C programs except for
# mlib.o whose source is hand writtens assembly language.

LIBS =  obj/mlib.o obj/clib.o obj/blib.o obj/dlibsys.o obj/kblib.o obj/sdlfn.o


# Uncomment some of the lines below for the syatem and architecture
# you wish to build.

# SYSM gives the location of machine specific code (eg amd64b64/mlib.s)

CC = gcc -O3 -DforAMD64b64
MLIB = amd64b64/mlib.s
CLIB = sysc/clib64.c
NB = nbamd64b64

#CC = gcc -O9 -DforMAC
#NB = nbmac

#CC = gcc -O9 -DforMIPS
#NB = nbmips

# For ARM Linux without the SDL feature
#CC = gcc -O2 -DforARM
#MLIB = arm/mlib.s
#SYSM = ../cintcode/sysasm/arm
#NB = nbarm

# For ARM Linux with the SDL feature
#CC = gcc -O4 -DforARM -DSDLavail `sdl-config --cflags`
#SDLLIBS =  `sdl-config --libs`
#MLIB = arm/mlib.s
#SYSM = ../cintcode/sysasm/arm
#NB = nbarm

# For Linux without SDL
# CC = gcc -O9 -DforLINUX
# MLIB = i386/mlib.s
# NB = nb386
#NB = nb686

# For Linux with SDL
#SDLLIBS =  `sdl-config --libs`
#CC = gcc -O4 -DforLINUX -DSDLavail `sdl-config --cflags`
#MLIB = i386/mlib.s
#NB = nb386

# For Linux with GL
#SDLLIBS =  `sdl-config --libs`
#CC = gcc -O4 -DforLINUX -DSDLavail -DGLavail `sdl-config --cflags`
#MLIB = i386/mlib.s
#NB = nb386

#CC = gcc -O9 -DforARM
#MLIB = arm/mlib.s
#NB = nbarm

#CC = gcc -O9 -DforVAX
#NB = nbvax

#CC = gcc -O9 -DforSUN4
#NB = nbsun4

#CC = gcc -O9 -DforSPARC
#NB = nbsparc

#CC = gcc -O9 -DforALPHA
#NB = nbalpha

#CC = gcc -O9 -DforMSDOS
#NB = nbmsdos

#CC = gcc -O9 -DforOS2
#NB = nbos2



#run:	prog bench100 cmpltest crdemo edsac natbcpl b2sial
run:	c1
	./c1
	
#	./tst
#	$(CINTSYS) -c "sial-sasm sial/fcmpltest.sial"
#	./tstfloat
#	./cmpltest
#	./crdemo
#	./b2sial $(DEMOS)/queens.b to queens.sial
#	time natbcpl $(CINTCODE)/com/bcpl.b to junk
#	time ./bench100
#	./prog y yyyyy x xxxxx
#	@echo
#	@echo "To run an Edsac program type: rcq"
#	@echo
#	./edsac

sysc/defines.h:	sysc/mkdefines-h.c
	gcc -o mkdefines-h sysc/mkdefines-h.c
	./mkdefines-h >sysc/defines.h
	rm -f mkdefines-h

sysc/mkdefines-h.c:	$(SYSC)/mkdefines-h.c
	cp $(SYSC)/mkdefines-h.c sysc/mkdefines-h.c

tempc/defines.h:	sysc/defines.h
	cp sysc/defines.h tempc

tempc/bcpl.h:	sysc/bcpl.h
	cp sysc/bcpl.h tempc

# bench100

bench100.gpu: obj/bench100.o
	$(CC) -O2 -Wall -Werror \
	obj/bench100.o \
	-o bench100.gpu \
	-L$(DEVKITGP2X)/lib -Wl,-rpath,$(DEVKITGP2X)/lib \
	-static -lpthread -lSDL -lutil

bench100:	obj/initbench100.o obj/bench100.o $(LIBS)
	echo "linking bench100"
	$(CC) -o bench100 obj/initbench100.o obj/bench100.o $(LIBS) -lm 

obj/bench100.o:	temps/bench100.s
	$(CC) -o obj/bench100.o -c temps/bench100.s

temps/bench100.s:	$(TESTS)/bench100.b tempc/INT.h tempc/bcpl.h Makefile
	$(CINTSYS) -c "c $(NB) $(TESTS) bench100"
	echo bench100.b compiled to temps/bench100.s

obj/initbench100.o:	tempc/initbench100.c tempc/bcpl.h tempc/INT.h
	$(CC) -o obj/initbench100.o -c tempc/initbench100.c

tempc/initbench100.c:	$(TESTS)/bench100.b
	$(CINTSYS) -c "makeinit $(TESTS)/bench100.b to tempc/initbench100.c"

c1:	obj/c1.o $(LIBS)
	$(CC) -o c1 \
	obj/clib.o \
	obj/kblib.o \
	-lm
#	obj/c1.o 
#	obj/sdlfn.o \
#	obj/blib.o \
#	obj/dlibsys.o \
	
obj/c1.o:    c1.c $(LIBS)
	     $(CC) -o obj/c1.o -c c1.c
	     
# t0

t0:	obj/initt0.o obj/t0.o $(LIBS)
	$(CC) -o t0 obj/initt0.o obj/t0.o $(LIBS) -lm

obj/t0.o:	temps/t0.s sysc/defines.h
	$(CC) -o obj/t0.o -c temps/t0.s

temps/t0.s:	t0.b Makefile sysc/defines.h
	$(CINTSYS) -c "c $(NB) . t0"
	echo t0.b compiled to temps/t0.s

obj/initt0.o:	tempc/initt0.c tempc/defines.h tempc/bcpl.h
	$(CC) -o obj/initt0.o -c tempc/initt0.c

tempc/initt0.c:	t0.b sysc/defines.h
	$(CINTSYS) -c "makeinit t0.b to tempc/initt0.c"

# prog

prog:	obj/initprog.o obj/prog.o $(LIBS) sysc/defines.h
	$(CC) -o prog obj/initprog.o obj/prog.o $(LIBS) -lm

obj/prog.o:	temps/prog.s
	$(CC) -o obj/prog.o -c temps/prog.s

temps/prog.s:	prog.b Makefile
	$(CINTSYS) -c "c $(NB) . prog"
	echo prog.b compiled to temps/prog.s

obj/initprog.o:	tempc/initprog.c tempc/defines.h tempc/bcpl.h
	$(CC) -o obj/initprog.o -c tempc/initprog.c

tempc/initprog.c:	prog.b
	$(CINTSYS) -c "makeinit prog.b to tempc/initprog.c"

# tstfloat

tstfloat:	obj/inittstfloat.o obj/tstfloat.o $(LIBS) sysc/defines.h
	$(CC) -o tstfloat obj/inittstfloat.o obj/tstfloat.o $(LIBS) -lm

obj/tstfloat.o:	temps/tstfloat.s
	$(CC) -o obj/tstfloat.o -c temps/tstfloat.s

temps/tstfloat.s:	$(COM)/tstfloat.b Makefile
	$(CINTSYS) -c "c $(NB) $(COM) tstfloat"
	echo $(COM)/tstfloat.b compiled to temps/tstfloat.s

obj/inittstfloat.o:	tempc/inittstfloat.c tempc/defines.h tempc/bcpl.h
	$(CC) -o obj/inittstfloat.o -c tempc/inittstfloat.c

tempc/inittstfloat.c:	$(COM)/tstfloat.b
	$(CINTSYS) -c "makeinit $(COM)/tstfloat.b to tempc/inittstfloat.c"

# tst

tst:	obj/inittst.o obj/tst.o $(LIBS)
	$(CC) -o tst obj/inittst.o obj/tst.o $(LIBS) -lm

tst.s:	tempc/tst.c tempc/bcpl.h tempc/defines.h
	$(CC) -O5 -S -c tempc/tst.c

obj/callstart.o:	sysc/callstart.c sysc/bcpl.h sysc/defines.h
	$(CC) -o obj/callstart.o -c sysc/callstart.c

callstart.s:	sysc/callstart.c sysc/bcpl.h sysc/defines.h
	$(CC) -O5 -S -c sysc/callstart.c

obj/tst.o:	temps/tst.s
	$(CC) -o obj/tst.o -c temps/tst.s

tst.txt:	temps/tst.s
	$(CC) -c -Wa,-alh temps/tst.s

temps/tst.s:	$(COM)/tst.b Makefile
	$(CINTSYS) -c "c $(NB) $(COM) tst"
	echo tst.b compiled to temps/tst.s
	$(CINTSYS) -c "sial-sasm sial/tst.sial"

obj/inittst.o:	tempc/inittst.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/inittst.o -c tempc/inittst.c

tempc/inittst.c:	$(COM)/tst.b
	$(CINTSYS) -c "makeinit $(COM)/tst.b to tempc/inittst.c"

# cmpltest

cmpltest:	obj/initcmpltest.o obj/cmpltest.o $(LIBS)

	$(CC) -o cmpltest obj/initcmpltest.o obj/cmpltest.o $(LIBS) -lm

obj/cmpltest.o:	temps/cmpltest.s
	$(CC) -o obj/cmpltest.o -c temps/cmpltest.s

temps/cmpltest.s:	$(COM)/cmpltest.b Makefile
	$(CINTSYS) -c "c $(NB) $(COM) cmpltest"
	echo cmpltest.b compiled to temps/cmpltest.s

obj/initcmpltest.o:	tempc/initcmpltest.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initcmpltest.o -c tempc/initcmpltest.c

tempc/initcmpltest.c:	$(COM)/cmpltest.b
	$(CINTSYS) -c "makeinit $(COM)/cmpltest.b to tempc/initcmpltest.c"

# fcmpltest

fcmpltest:	obj/initfcmpltest.o obj/fcmpltest.o $(LIBS)
	$(CC) -o fcmpltest obj/initfcmpltest.o obj/fcmpltest.o $(LIBS) -lm

obj/fcmpltest.o:	temps/fcmpltest.s
	echo "assembling temps/fcmpltest.s"
	ls -lrt temps/fcmpltest.s
	date
	$(CC) -o obj/fcmpltest.o -c temps/fcmpltest.s

temps/fcmpltest.s:	$(COM)/fcmpltest.b Makefile
	$(CINTSYS) -c "c $(NB) $(COM) fcmpltest"
	echo fcmpltest.b compiled to temps/fcmpltest.s
	$(CINTSYS) -c "sial-sasm sial/fcmpltest.sial"

obj/initfcmpltest.o:	tempc/initfcmpltest.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initfcmpltest.o -c tempc/initfcmpltest.c

tempc/initfcmpltest.c:	$(COM)/fcmpltest.b
	$(CINTSYS) -c "makeinit $(COM)/fcmpltest.b to tempc/initfcmpltest.c"

# xcmpltest

xcmpltest:	obj/initxcmpltest.o obj/xcmpltest.o $(LIBS)
	$(CC) -o xcmpltest obj/initxcmpltest.o obj/xcmpltest.o $(LIBS) -lm

obj/xcmpltest.o:	temps/xcmpltest.s
	echo "assembling temps/xcmpltest.s"
	ls -lrt temps/xcmpltest.s
	date
	$(CC) -o obj/xcmpltest.o -c temps/xcmpltest.s

temps/xcmpltest.s:	$(COM)/xcmpltest.b Makefile
	$(CINTSYS) -c "c $(NB) $(COM) xcmpltest"
	echo xcmpltest.b compiled to temps/xcmpltest.s
	$(CINTSYS) -c "sial-sasm sial/xcmpltest.sial"

obj/initxcmpltest.o:	tempc/initxcmpltest.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initxcmpltest.o -c tempc/initxcmpltest.c

tempc/initxcmpltest.c:	$(COM)/xcmpltest.b
	$(CINTSYS) -c "makeinit $(COM)/xcmpltest.b to tempc/initxcmpltest.c"

# crdemo

crdemo:	obj/initcrdemo.o obj/crdemo.o $(LIBS)
	$(CC) -o crdemo obj/initcrdemo.o obj/crdemo.o $(LIBS) -lm

obj/crdemo.o:	temps/crdemo.s
	$(CC) -o obj/crdemo.o -c temps/crdemo.s

temps/crdemo.s:	$(DEMOS)/crdemo.b Makefile
	$(CINTSYS) -c "c $(NB) $(DEMOS) crdemo"
	echo crdemo.b compiled to temps/crdemo.s

obj/initcrdemo.o:	tempc/initcrdemo.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initcrdemo.o -c tempc/initcrdemo.c

tempc/initcrdemo.c:	$(DEMOS)/crdemo.b
	$(CINTSYS) -c "makeinit $(DEMOS)/crdemo.b to tempc/initcrdemo.c"

# edsac

edsac:	obj/initedsac.o obj/edsac.o $(LIBS)
	$(CC) -o edsac obj/initedsac.o obj/edsac.o $(LIBS) -lm

obj/edsac.o:	temps/edsac.s
	$(CC) -o obj/edsac.o -c temps/edsac.s

temps/edsac.s:	$(EDSAC)/edsac.b Makefile
	$(CINTSYS) -c "c $(NB) $(EDSAC) edsac"
	echo edsac.b compiled to temps/edsac.s

obj/initedsac.o:	tempc/initedsac.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initedsac.o -c tempc/initedsac.c

tempc/initedsac.c:	$(EDSAC)/edsac.b
	$(CINTSYS) -c "makeinit $(EDSAC)/edsac.b to tempc/initedsac.c"

# medfind

medfind:	obj/initmedfind.o obj/medfind.o $(LIBS)
	$(CC) -o medfind obj/initmedfind.o obj/medfind.o $(LIBS) -lm

obj/medfind.o:	temps/medfind.s
	$(CC) -o obj/medfind.o -c temps/medfind.s

temps/medfind.s:	$(SORT)/medfind.b Makefile
	$(CINTSYS) -c "c $(NB) $(SORT) medfind"
	echo medfind.b compiled to temps/medfind.s

obj/initmedfind.o:	tempc/initmedfind.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initmedfind.o -c tempc/initmedfind.c

tempc/initmedfind.c:	$(SORT)/medfind.b
	$(CINTSYS) -c "makeinit $(SORT)/medfind.b to tempc/initmedfind.c"

# b2sial

b2sial:	obj/initbcpl2sial.o obj/bcpl2sial.o $(LIBS)
	$(CC) -o b2sial obj/initbcpl2sial.o obj/bcpl2sial.o $(LIBS) -lm

obj/bcpl2sial.o:	temps/bcpl2sial.s
	$(CC) -o obj/bcpl2sial.o -c temps/bcpl2sial.s

temps/bcpl2sial.s:	$(COM)/bcpl2sial.b $(CINTCODE)/g/sial.h Makefile
	$(CINTSYS) -c "c $(NB) $(COM) bcpl2sial"
	echo bcpl2sial.b compiled to temps/bcpl2sial.s

obj/initbcpl2sial.o:	tempc/initbcpl2sial.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initbcpl2sial.o -c tempc/initbcpl2sial.c

tempc/initbcpl2sial.c:	$(COM)/bcpl2sial.b
	$(CINTSYS) -c "makeinit $(COM)/bcpl2sial.b to tempc/initbcpl2sial.c"

# natbcpl

natbcpl:	obj/initbcpl.o obj/bcpl.o $(LIBS)
	$(CC) -o natbcpl obj/initbcpl.o obj/bcpl.o $(LIBS) -lm

obj/bcpl.o:	temps/bcpl.s
	$(CC) -o obj/bcpl.o -c temps/bcpl.s

temps/bcpl.s:	$(COM)/bcpl.b Makefile
	$(CINTSYS) -c "c $(NB) $(COM) bcpl"
	echo "$(COM)/bcpl.b compiled to temps/bcpl.s"

obj/initbcpl.o:	tempc/initbcpl.c tempc/bcpl.h tempc/defines.h
	$(CC) -o obj/initbcpl.o -c tempc/initbcpl.c

tempc/initbcpl.c:	$(COM)/bcpl.b
	$(CINTSYS) -c "makeinit $(COM)/bcpl.b to tempc/initbcpl.c"



# The libraries

obj/blib.o:	temps/blib.s
	$(CC) -o obj/blib.o -c temps/blib.s

temps/blib.s:	sysb/blib.b Makefile
	$(CINTSYS) -c "c $(NB) sysb blib"
	echo blib.b compiled to temps/blib.s

obj/dlibsys.o:	temps/dlibsys.s
	$(CC) -o obj/dlibsys.o -c temps/dlibsys.s

temps/dlibsys.s:	sysb/dlibsys.b Makefile
	$(CINTSYS) -c "c $(NB) sysb dlibsys"
	echo dlibsys.b compiled to temps/dlib.s

sysb/dlibsys.b:	       $(CINTCODE)/sysb/dlibsys.b
	cp $(CINTCODE)/sysb/dlibsys.b sysb/dlibsys.b
		       
obj/clib.o:	sysc/clib64.c sysc/bcpl64.h sysc/defines.h
	$(CC) -o obj/clib.o -c sysc/clib64.c

clib.s:	sysc/clib.c sysc/bcpl.h sysc/defines.h
	$(CC) -S -c sysc/clib.c

obj/kblib.o:	sysc/kblib.c sysc/bcpl.h sysc/defines.h
	$(CC) -o obj/kblib.o -c sysc/kblib.c

obj/sdlfn.o:	sysc/sdlfn.c sysc/bcpl.h sysc/defines.h
	$(CC) -o obj/sdlfn.o -c sysc/sdlfn.c

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

obj/mlib.o:	$(MLIB)
	$(CC) -o obj/mlib.o -c $(MLIB)


demo.txt:	arm/demo.s
	$(CC) -c -Wa,-alh arm/demo.s >demo.txt


gp2xinstall:	tst
	rm -f $(SD)/Bin/tst
	cp -r tst $(SD)/Bin/tst


clean:
	rm -f core OCODE junk*
	rm -f *~ */*~ */*/*~
	rm -f sysc/mkdefines-h.c sysc/defines.h tempc/defines.h
	rm -f  obj/* sial/*
	rm -f temps/*
	rm -f tempc/*
	rm -f cmpltest fcmpltest xcmpltest crdemo edsac medfind natbcpl
	rm -f bench100 prog tstfloat bucket tiger tst






