# BMakefile für CLISP mit GNU-ANSI-C-Compiler
# Jörg Höhle 3.8.98
# Optionen: ADE, C00, C20, TYPC, HIGH, WIDE, FFI, KI, KA, LMEM
#  Versionsauswahl
#   HIGH => C20 -high version
#   WIDE => C20 -wide: 64 bits for use within VMM
#   TYP2 =>     - version with only two type bits
#   C00         -00 for 68000 only
#   ---         -low with 24 address bits
#  Optional: FFI KI KA LMEM
#  Compiler: ADE
# keeping intermediate files implies keeping assembly files
if def(KI)
KA=1
endif

if def(HIGH)
C20=1
endif
if def(WIDE)
C20=1
endif

if ndef(C20)
MPFLAGS=-Dmc68010
MCFLAGS=-m68000
MAFLAGS=-mc68010
else
MPFLAGS=-Dmc68010 -DMC680Y0
MCFLAGS=-m68020
MAFLAGS=-mc68020
endif

WPFLAGS=-DTYPECODES
if def(WIDE)
WPFLAGS=-DWIDE
endif
ifdef(HIGH)
WPFLAGS=-DAMIGA3000 -DTYPECODES
endif
ifdef(C00)
WPFLAGS=-DMC68000
endif
ifdef(TYP2)
WPFLAGS=-DNO_TYPECODES
endif

#IXCFLAGS= >NIL: -a -.
IXCFLAGS= >NIL: -a1 -/0

if def(ADE)
# Use GCC272
# Path ADE:bin/ ADE:lib/gcc-lib/m68k-cbm-amigados/2.7.2/
#IXCONFIG=GNU:ixem/bin/ixconfig
IXCONFIG=GCC258:bin/ixprefs
CC  = gcc
CPP = cpp -undef -D__GNUC__=2 -D__GNUC_MINOR__=7 -lang-c -v
CC1 = T:cc1 -version -freg-struct-return
AS  = as

CPPSTACK = 40000
CC1STACK = 400000
LDSTACK  = 180000

INCPATH=-I. -IAB:usr/include/Amiga31 -IGCC258:os-include -ILISP:amiga/jchlib/include -IGCC258:include
LD     = ld $(LIBPATH) $(CRT0)
LIBGCCPATH=-LGCC258:lib/gcc-lib/amigados/2.5.8/
LIBPATH=-nostdlib -LLISP:amiga/jchlib/lib $(LIBGCCPATH)
LIBS   =-lcclisp -lgcc
CRT0   =LISP:amiga/jchlib/lib/crt0.o

else
# Use GCC258
# Path GCC258:bin/ GCC258:lib/gcc-lib/amigados/2.5.8/
#IXCONFIG=GCC258:gnu/ixem/bin/ixconfig
IXCONFIG=GCC258:bin/ixprefs
CC  = gcc
CPP = cpp -undef -D__GNUC__=2 -D__GNUC_MINOR__=5 -lang-c -v
CC1 = cc1 -version
AS  = as

CPPSTACK = 40000
CC1STACK = 450000
LDSTACK  = 200000

INCPATH=-I. -IAB:usr/include/Amiga31 -IGCC258:os-include -ILISP:amiga/jchlib/include -IGCC258:include
LD     = ld $(LIBPATH) $(CRT0)
#LIBGCCPATH=-LGCC258:lib/gcc-lib/amigados/2.5.8/
LIBGCCPATH=
LIBPATH=-nostdlib -LLISP:amiga/jchlib/lib $(LIBGCCPATH)
LIBS   =-lcclisp -ltinygcc
CRT0   =LISP:amiga/jchlib/lib/crt0.o
endif


CPPFLAGS = -Wcomment -nostdinc $(INCPATH) -D__OPTIMIZE__ -DSTDC_HEADERS -Damiga -DNO_CLISP_H -DNO_GETTEXT $(MPFLAGS) $(WPFLAGS)
CC1FLAGS = -quiet -O2 -W -Wimplicit -Wreturn-type -Wswitch -Wcomment -Wpointer-arith\
 -ffixed-a4 -fomit-frame-pointer -fno-strength-reduce $(MCFLAGS)
ASFLAGS  = $(MAFLAGS)
LDFLAGS  =
CFLAGS   = $(LDFLAGS) $(CPPFLAGS) $(CC1FLAGS)

TMPDIR   = T:
FSTMPDIR = RAM:

MV=mv
RM=delete
AR=ar
RANLIB=ranlib
COMMENT  = lisp:utils/comment5
CVTMSGS  = lisp:utils/cvtmsgs
ANSIDECL = lisp:utils/ansidecl
PRESTACK = 20000

# Remote file prefix
R=/src/

D_INC_MACHINE = amiga.d
D_INC_SPECIAL = amiga2.d
D_INC_FURTHER = avl.d sort.d ari68000.d ari68020.d arilev1e.d arilev1i.d bytecode.d erramiga.d
D_INC_NORMAL = lispbibl.d fsubr.d subr.d subrkw.d pseudofun.d constsym.d \
 constobj.d constpack.d aridecl.d arilev0.d $(D_INC_MACHINE)
D_INC_ALL = $(D_INC_NORMAL) $(D_INC_SPECIAL) $(D_INC_FURTHER)
#D_LISPARIT = arilev1c.d arilev1e.d arilev1i.d arlev1m.d $(D_ARI)
if def(C20)
D_AMODULES = ari68020.d
else
D_AMODULES = ari68000.d
endif
if ndef(FFI)
D_EMODULES = affi.d
else
D_EMODULES = affi.d foreign.d
endif
D_CMODULES_SPECIAL = rexx.d
D_CMODULES = misc.d spvw.d spvwtabf.d spvwtabs.d spvwtabo.d \
 eval.d control.d pathname.d stream.d io.d error.d time.d \
 array.d hashtabl.d list.d package.d record.d sequence.d \
 charstrg.d debug.d predtype.d symbol.d \
 lisparit0.d modules.d amigaaux.d $(D_CMODULES_SPECIAL) $(D_EMODULES)
#D_MODULES = $(D_CMODULES) $(D_AMODULES)
DFILES_OTHERARIT = \
 aridecl.d arilev0.d arilev1.d intelem.d intlog.d intplus.d \
 intcomp.d intbyte.d intmal.d intdiv.d intgcd.d int2adic.d \
 intsqrt.d intprint.d intread.d rational.d sfloat.d ffloat.d \
 dfloat.d lfloat.d flo_konv.d flo_rest.d realelem.d realrand.d \
 realtran.d compelem.d comptran.d
RDFILES_OTHERARIT = $(addprefix $(R),$(DFILES_OTHERARIT))

C_INC_SPECIAL = $(subst .d,.c,$(D_INC_SPECIAL))
C_INC_NORMAL = $(subst .d,.c,$(D_INC_NORMAL))
C_INC_ALL = $(subst .d,.c,$(D_INC_ALL))
C_DIST = modules.c genclisph.c

OBJECTS_SPECIAL = $(subst .d,.o,$(D_CMODULES_SPECIAL))
OBJECTS_C = $(subst .d,.o,$(D_CMODULES))
OBJECTS_A = $(subst .d,.o,$(D_AMODULES))
OBJECTS = $(OBJECTS_C) $(OBJECTS_A)
LIBOBJECTS = $(filter-out modules.o,$(OBJECTS))

E_LSPFILES =
if def(FFI)
E_LSPFILES =foreign1.lisp
endif
LSPFILES = \
 init.lisp defseq.lisp backquote.lisp defmacro.lisp macros1.lisp macros2.lisp \
 defs1.lisp places.lisp floatprint.lisp type.lisp defstruct.lisp format.lisp \
 user1.lisp user2.lisp trace.lisp macros3.lisp config.lisp compiler.lisp \
 timezone.lisp defs2.lisp clos.lisp condition.lisp defs3.lisp gstream.lisp \
 screen.lisp loop.lisp rexx1.lisp affi1.lisp $(E_LSPFILES)
FASFILES = $(subst .lisp,.fas,$(LSPFILES))
RFASFILES = $(addprefix $(R),$(FASFILES))

IMEMFILE = interpreted.mem
HMEMFILE = halfinterp.mem
CMEMFILE = lispinit.mem
CLISPEXE = lisp.run
DMEMFILE = compiler.mem
SMEMFILE = small.mem

%.h:
# No need to make .d or .lisp files
%.d:

%.lisp:

#%.c: $(R)%.d

all: init allc allo lisp.run

init: .NEVER

allc: $(C_INC_ALL)

allo: $(OBJECTS)

nope: .NEVER
	echo trying to get rid of bmake bug.

# Various dependencies
nope $(OBJECTS_C): $(C_INC_NORMAL)

nope $(OBJECTS_SPECIAL): $(C_INC_SPECIAL)

nope spvw.o: avl.c sort.c

nope eval.o: bytecode.c

nope modules.o: $(R)modules.h

nope error.o: erramiga.c

nope lisparit0.o: ari68000.c ari68020.c arilev1e.c arilev1i.c

.PHONY: all init allc allo

$(R)lisparit1.d: $(R)lisparit.d
	head >$@ -19 $<
$(R)lisparit2.d: $(R)lisparit.d
	tail >$@ +53 $<
$(R)lisparit0.d: $(R)lisparit1.d $(RDFILES_OTHERARIT) $(R)lisparit2.d
	join $(R)lisparit1.d $(RDFILES_OTHERARIT) $(R)lisparit2.d as $@

# In order to save diskspace, we don't keep intermediary .c, .i or .s files

# Make all include files
$(C_INC_ALL) $(C_DIST): $(R)$*.d
	stack $(PRESTACK)
#	Named pipes make it slower!
#	run $(COMMENT) $(R)$*.d >FIFO:prepclisp/mwek
#	$(ANSIDECL) <FIFO:prepclisp/r >$@
#	run $(COMMENT) $(R)$*.d >PIPE:prepclisp
#	$(ANSIDECL) <PIPE:prepclisp/r >$@
	$(COMMENT) $(R)$*.d $(FSTMPDIR)$*.cd
	$(CVTMSGS) <$(FSTMPDIR)$*.cd >$(FSTMPDIR)$*.dc
	$(RM) $(FSTMPDIR)$*.cd
	$(ANSIDECL) <$(FSTMPDIR)$*.dc >$@
	$(RM) $(FSTMPDIR)$*.dc

# Make all assembly objects
# Here only when .c is available
$(OBJECTS_A): $*.c
	stack $(CPPSTACK)
	$(CPP) -DASM_UNDERSCORE $(CPPFLAGS) $*.c $(TMPDIR)$*.s
	stack $(CC1STACK)
	$(AS) $(ASFLAGS) -o $@ $(TMPDIR)$*.s
	$(RM) $(TMPDIR)$*.s

# Make all C objects
$(OBJECTS_C) testd.o testn.o: $(R)$*.d
	@date
	stack $(PRESTACK)
	$(COMMENT) $(R)$*.d $(FSTMPDIR)$*.cd
	$(CVTMSGS) <$(FSTMPDIR)$*.cd >$(FSTMPDIR)$*.dc
	$(RM) $(FSTMPDIR)$*.cd
	$(ANSIDECL) <$(FSTMPDIR)$*.dc >$(TMPDIR)$*.c
	$(RM) $(FSTMPDIR)$*.dc
if def(LMEM)
	@avail >nil: flush
	@$(IXCONFIG) $(IXCFLAGS)
endif
	stack $(CPPSTACK)
	$(CPP) $(CPPFLAGS) $(TMPDIR)$*.c $(TMPDIR)$*.i
if ndef(KI)
	$(RM) $(TMPDIR)$*.c
endif
if def(LMEM)
	@avail >nil: flush
	@$(IXCONFIG) $(IXCFLAGS)
endif
	stack $(CC1STACK)
	-$(CC1) $(CC1FLAGS) -o $(TMPDIR)$*.s $(TMPDIR)$*.i
if ndef(KI)
	$(RM) $(TMPDIR)$*.i
endif
if def(YES_HIGH)
	copy $(TMPDIR)$*.s to $(FSTMPDIR)$*.es
#	Some lines are too long for Edit
	sed >$(TMPDIR)$*.s -e "s/^I.even/^I.align 2/" $(FSTMPDIR)$*.es
	$(RM) $(FSTMPDIR)$*.es
endif
	$(AS) $(ASFLAGS) -o $@ $(TMPDIR)$*.s
if ndef(KA)
	$(RM) $(TMPDIR)$*.s
endif

lisp.run: $(OBJECTS)
	@date
	stack $(LDSTACK)
	$(LD) $(LDFLAGS) -o $@ $(OBJECTS) $(LIBS)

testd: testd.o
	stack $(LDSTACK)
	$(LD) $(LDFLAGS) -o $@ $*.o $(LIBS)

testn: testn.o
	stack 300000
	$(CC) -v -o $@ $*.o -lc

# make an interpreted world
$(IMEMFILE):
if def(LMEM)
	@avail >nil: flush
endif
	@date
	$(CLISPEXE) -x (cd"/src/")(load"init.lisp")(saveinitmem)(exit)
	@date
	$(MV) $(R)lispinit.mem $@

# compile LISP files
$(RFASFILES): $*.lisp
if def(LMEM)
	@avail >nil: flush
endif
	@date
	$(CLISPEXE) -M $(DMEMFILE) -c $*.lisp
	@date

# make a useful bootstrap world, set DMEMFILE=interpreted.mem
$(HMEMFILE): $(addprefix $(R),compiler.fas)
if def(LMEM)
	@avail >nil: flush
endif
	@date
	$(CLISPEXE) -x (cd"/src/")(load"init.lisp")(saveinitmem)(exit)
	@date
	$(MV) $(R)lispinit.mem $@

# make a compiled world, set DMEMFILE=halfinterp.mem when bootstrapping
$(CMEMFILE): $(RFASFILES)
if def(LMEM)
	@avail >nil: flush
endif
	@date
	$(CLISPEXE) -x (cd"/src/")(load"init.fas")(saveinitmem)(exit)
	@date
	$(MV) $(R)lispinit.mem $@

# small image without compiler, disassem, loop, clos, conditio, defs3, gstream, screen
# init-small.fas is init-small.fas with these lines commented out (near end of file)
$(SMEMFILE): $(RFASFILES)
if def(LMEM)
	@avail >nil: flush
endif
	@date
	$(CLISPEXE) -x (cd"/src/")(load"init-small.fas")(saveinitmem)(exit)
	@date
	$(MV) $(R)lispinit.mem $@

clean:
	$(RM) $(C_INC_ALL) $(OBJECTS)
force:

libclisp.a: $(LIBOBJECTS)
	-$(RM) $@
	stack $(LDSTACK)
	$(AR) qv $@ $(LIBOBJECTS)
	$(RANLIB) $@

# generate it using the normal libnix/ixemul environment
#gcc -v -O2 -m68020 -DAMIGA3000 -DHAVE_FFI -I. -c genclisph.c
genclisph.o: genclisph.c
	$(CC) $(WPFLAGS) $(MCFLAGS) -I. -o $@ $*.c

#gcc -v -m68020 -Wl,-defsym,asciz_out=printf
genclisph: genclisph.o
	$(CC) $(MCFLAGS) -o $@ $<
