#
# This file has been modified as part of the FreeMiNT project. See
# the file Changes.MH for details and dates.
#
#
# Makefile for MiNT using the GCC
#

# if you're cross-compiling, define NATIVECC
# to the host's C compiler, NATIVECFLAGS to
# the appropriate flags for it, and NATIVELIBS
# to appropriate libraries
# NATIVECC = cc -DNO_STDLIB
# NATIVECFLAGS = -g
# NATIVELIBS =

NATIVECC = gcc
NATIVECFLAGS = -O
NATIVELIBS = -liio

#
# depending on your version of bison/yacc, you may
# need to change these names, e.g. to
# YACC = yacc
# YTABC = y.tab.c
# YTABH = y.tab.h

#YACC = bison
#YTABC = asm_tab.c
#YTABH = asm_tab.h

YACC = byacc -b y_
YTABC = y_tab.c
YTABH = y_tab.h

# here are defs for the cross compiler
# MiNT must be compiled with 16 bit integers

CC = gcc
AS = gcc
MODEL = -mshort
MODEL030 = -mshort -m68030
#LIBS = -liio16

# add -DDIRSEP_SLASH to make forward slash a path separator in kernel filesystems
# add -DMULTITOS for a MultiTOS kernel
# add -DONLY030 for a version of MiNT that needs a 680x0, x>=3
# add -DCPU040 to make the kernel know its compiled for 040
# add -DCPU060 to make the kernel know its compiled for 060
# add -DDEBUG_INFO for debugging information
# add -DFASTTEXT for the u:\dev\fasttext fast console driver
# add -DVM_DEBUGGING for a VMX kernel with debugging code

DEFS = -DMULTITOS -DDIRSEP_SLASH -DVERBOSE_BOOT
DEFS030 = -DMULTITOS -DDIRSEP_SLASH -DONLY030 -DVERBOSE_BOOT
DEFSVMX = -DMULTITOS -DDIRSEP_SLASH -DONLY030 -DVM_EXTENSION -DVERBOSE_BOOT

#
# if you have an older version of gcc, it won't understand -G;
# that won't hurt anything (it just controls the format of the
# symbol table) so just delete the -G. Do *not* change it into
# -g!!!
#
CFLAGS = -G -Wall -Wmissing-prototypes -O2 -fno-defer-pop -fomit-frame-pointer $(MODEL) $(DEFS)
ASFLAGS = $(MODEL)
CFLAGS030 = -G -Wall -Wmissing-prototypes -O2 -fno-defer-pop -fomit-frame-pointer $(MODEL030) $(DEFS030)
ASFLAGS030 = $(MODEL030)
CFLAGSVMX = -G -Wall -Wmissing-prototypes -O2 -fno-defer-pop -fomit-frame-pointer $(MODEL030) $(DEFSVMX)
ASFLAGSVMX = $(MODEL030)

COBJS = bios.o xbios.o console.o dos.o dosdir.o dosfile.o dosmem.o dossig.o \
	filesys.o main.o mem.o proc.o signal.o timeout.o tty.o util.o \
	biosfs.o pipefs.o procfs.o tosfs.o debug.o rendez.o \
	unifs.o shmfs.o fasttext.o welcome.o nalloc2.o memprot.o realloc.o \
	update.o ssystem2.o

COBJS030 = bios.o0 xbios.o0 console.o0 dos.o0 dosdir.o0 dosfile.o0 dosmem.o0 dossig.o0 \
	filesys.o0 main.o0 mem.o0 proc.o0 signal.o0 timeout.o0 tty.o0 util.o0 \
	biosfs.o0 pipefs.o0 procfs.o0 tosfs.o0 debug.o0 rendez.o0 \
	unifs.o0 shmfs.o0 fasttext.o0 welcome.o0 nalloc2.o0 memprot.o realloc.o0 \
	update.o0 ssystem2.o0

COBJSVMX = bios.o1 xbios.o1 console.o1 dos.o1 dosdir.o1 dosfile.o1 dosmem.o1 dossig.o1 \
	filesys.o1 main.o1 mem.o1 proc.o1 signal.o1 timeout.o1 tty.o1 util.o1 \
	biosfs.o1 pipefs.o1 procfs.o1 tosfs.o1 debug.o1 rendez.o1 \
	unifs.o1 shmfs.o1 fasttext.o1 welcome.o1 nalloc2.o1 memprot.o realloc.o1 \
	update.o1 ssystem2.o1

CFILES = bios.c xbios.c console.c dos.c dosdir.c dosfile.c dosmem.c dossig.c \
	filesys.c main.c mem.c proc.c signal.c timeout.c tty.c util.c \
	biosfs.c pipefs.c procfs.c tosfs.c debug.c rendez.c \
	unifs.c shmfs.c fasttext.c welcome.c nalloc2.c memprot.c realloc.c \
	update.c ssystem2.c

HFILES = assert.h atarierr.h basepage.h cookie.h ctype.h debug.h fasttext.h \
	file.h inline.h loadave.h mem.h mint.h proc.h proto.h signal.h sproto.h \
	types.h version.h xbra.h buildtime.h

SFILES = context.spp cpu.spp intr.spp syscall.spp quickzer.spp \
	quickmov.spp quickswa.spp vm5.spp magic.i

SOBJS = context.o intr.o syscall.o quickzer.o quickmov.o quickswa.o cpu.o vm5.o
SOBJS030 = context.o0 intr.o0 syscall.o0 quickzer.o0 quickmov.o0 quickswa.o0 cpu.o0 vm5.o0
SOBJSVMX = $(SOBJS030)

OBJS = $(COBJS) $(SOBJS)
OBJS030 = $(COBJS030) $(SOBJS030)
OBJSVMX = $(COBJSVMX) $(SOBJSVMX)

all: mint.prg mint030.prg mintvmx.prg

mint.prg: buildtime $(OBJS)
	$(CC) $(CFLAGS) -o mint.prg $(OBJS) $(LIBS)
#	strip mint.prg

mint030.prg: buildtime $(OBJS030)
	$(CC) $(CFLAGS030) -o mint030.prg $(OBJS030) $(LIBS)
#	strip mint030.prg

mintvmx.prg: buildtime $(OBJSVMX)
	$(CC) $(CFLAGSVMX) -o mintvmx.prg $(OBJSVMX) $(LIBS)
#	strip mintvm.prg

$(SOBJS): proc.h
$(COBJS): mint.h proc.h file.h debug.h

$(SOBJS030): proc.h
$(COBJS030): mint.h proc.h file.h debug.h

$(SOBJSVMX): proc.h
$(COBJSVMX): mint.h proc.h file.h debug.h

main.o: version.h
welcome.o: version.h
bios.o: inline.h
proc.o dos.o: loadave.h
ssystem2.o: buildtime.h

memprot.o: memprot.c mint.h proc.h file.h
	$(CC) $(CFLAGS) -m68030 -c memprot.c

main.o0: version.h
welcome.o0: version.h
bios.o0: inline.h
proc.o0 dos.o0: loadave.h
ssystem2.o0: buildtime.h

main.o1: version.h
welcome.o1: version.h
bios.o1: inline.h
proc.o1 dos.o1: loadave.h
ssytem2.o1: buildtime.h

#
# assembler source files are now handled in a radically different
# fashion. We build a pre-processor program, asmtrans, that
# takes the .spp files, merges them with an include file that
# gives various offsets into structures of interest, and produces
# the .s files as output. This has two major advantages:
# (1) it lets us use the same source for both the Lattice and
#     gcc assembler files (the translator will convert), and
# (2) if we change the CONTEXT or PROC structures, we don't
#     have to dig through the source code looking for
#     magic numbers

# the asm translator program
# Note that this must be compiled with the native CC of whatever
# system you're using; see the definitions at the top of this
# file.

ATRANSOBJ = asmtab.o trutil.o trans.o
ATRANS = asmtrans.ttp
ATRANSSRC = asm.y asmtab.c asmtab.h asmtrans.h trutil.c trans.c

$(ATRANS): $(ATRANSOBJ)
	$(NATIVECC) $(NATIVECFLAGS) -o $(ATRANS) $(ATRANSOBJ) \
		$(NATIVELIBS)

asmtab.o: asmtab.c asmtrans.h
	$(NATIVECC) $(NATIVECFLAGS) -o $@ -c asmtab.c

trutil.o: trutil.c asmtrans.h
	$(NATIVECC) $(NATIVECFLAGS) -o $@ -c trutil.c

trans.o: trans.c asmtrans.h
	$(NATIVECC) $(NATIVECFLAGS) -o $@ -c trans.c


asmtab.c asmtab.h:	asm.y
	$(YACC) -d asm.y
	mv $(YTABC) asmtab.c
	mv $(YTABH) asmtab.h

# the magic number include file is generated automagically
#
# magic.i only needs to be updated if the structures in proc.h
# change; otherwise, you can use the supplied magic.i.
#
# When cross-compiling:
# If you do need to generate a new magic.i, cross-compile genmagic.ttp
# and use `cnm' and the supplied `perl' script `genmagic.pl' to
# extract the values.

#for native compiling

GENMAGICPRG=genmagic.ttp

$(GENMAGICPRG): genmagic.c proc.h file.h
	$(CC) $(CFLAGS) -DGENMAGIC -o $(GENMAGICPRG) genmagic.c

magic.i: $(GENMAGICPRG)
	$(GENMAGICPRG) $@

# for cross-compiling use this: (assumes you have perl and cnm)
#
#magic.i: $(GENMAGICPRG)
#	perl genmagic.pl > $@

# for build time

MAKEBUILDTIME=makebuildtime

$(MAKEBUILDTIME): makebuildtime.c
	$(CC) -s -o $(MAKEBUILDTIME) makebuildtime.c

buildtime: $(MAKEBUILDTIME)
	date > date.out
	$(MAKEBUILDTIME)

#
# assembler source files
#
.SUFFIXES: .spp .o0 .o1

.spp.o:
	$(ATRANS) $(DEFS) -gas -o $*.s $<
	$(AS) $(ASFLAGS) -o $@ -c $*.s
	$(RM) $*.s
.spp.o0:
	$(ATRANS) $(DEFS030) -gas -o $*.s $<
	$(AS) $(ASFLAGS0) -o $@ -c $*.s
	$(RM) $*.s
.c.o0:
	$(CC) $(CFLAGS030) -o $@ -c $<

.c.o1:
	$(CC) $(CFLAGSVMX) -o $@ -c $<

context.o: context.spp magic.i $(ATRANS)
intr.o: intr.spp magic.i $(ATRANS)
syscall.o: syscall.spp magic.i $(ATRANS)
quickzer.o: quickzer.spp $(ATRANS)
quickmov.o: quickmov.spp $(ATRANS)
cpu.o: cpu.spp $(ATRANS)
vm5.o: vm5.spp $(ATRANS)

context.o0: context.spp magic.i $(ATRANS)
intr.o0: intr.spp magic.i $(ATRANS)
syscall.o0: syscall.spp magic.i $(ATRANS)
quickzer.o0: quickzer.spp $(ATRANS)
quickmov.o0: quickmov.spp $(ATRANS)
vm5.o0: vm5.spp $(ATRANS)

#
# mkptypes generates prototypes from C source code. If you don't have it,
# you'll have to add/delete function prototypes by hand.
# also: Sozobon users will have to edit proto.h by hand to change the
#    #if defined(__STDC__) || defined(__cplusplus)
# line into
#    #if __STDC__
#
proto.h: $(CFILES)
	mkptypes $(CFILES) >proto.h

#
# macros for cleaning up
#
GENFILES= $(OBJS) $(OBJS030) $(OBJSVMX) $(ATRANSOBJ) $(ATRANS) $(GENMAGICPRG)
EXTRAS= asmtab.c asmtab.h mint.prg mint030.prg mintvm.prg

clean:
	$(RM) $(GENFILES)

realclean:
	$(RM) $(GENFILES) $(EXTRAS)

#
# misc. files and subdirectories to include in the distribution
#
MISCFILES = copying readme.src changes makefile makefile.lcc mintlc.prj \
	genmagic.c purec asmtrans.ttp

#
# macros for making distribution packages
#

dist:
	zoo a//h mintsrc.zoo $(MISCFILES) $(CFILES) $(HFILES) $(SFILES) \
	 $(ATRANSSRC)
