#
# Makefile
#
# This file is subject to the terms and conditions of the GNU General Public
# License. See the file COPYING in the main directory of this archive for more
# details.

include Makefile.inc

CC		= mipsel-linux-gcc
AS		= mipsel-linux-as -mcpu=r4400 -mips3
AR		= mipsel-linux-ar
LD		= mipsel-linux-ld
MAKE		= make
#
# Don't strip executables - there is a bug in mipsel-linux-strip that changes
# the type of executable from impure to paged. Unfortunately the Deskstations
# doesn't accept paged executables like the Acer Pica.
#
STRIP		= mipsel-linux-strip
M4		= m4

.c.s:
	$(CC) $(CFLAGS) -S $<
.s.o:
	$(AS) $(ASFLAGS) -o $*.o $<
	$(STRIP) --discard-all $*.o
.c.o:
	$(CC) $(CFLAGS) -c $<
	$(STRIP) --discard-all $*.o
.S.s:
	$(CPP) $(CFLAGS) $< -o $*.s
.S.o:
	$(CC) $(CFLAGS) -c $< -o $*.o
	$(STRIP) --discard-all $*.o

#
# Use the following options for Acer PICA 1 machines.
# Seems to be ok for Deskstation rPC44, too.
#
LOADADDR	= 0xa0600000

#
# Use the following options for Deskstation Tynes.
#
#LOADADDR	= 0x80600000

KERNELBASE       = 0x80000000

#
# Set this to 1 to compile Milo with extra debugging code
#
DEBUG		= -DDEBUG=0

CFLAGS		= -Wall -I./include -I/usr/mipsel-linux/include -O2 -mips2 -Wa,-mips3 \
		  -mcpu=r4400 -D__KERNEL__ -DLOADADDR=$(LOADADDR) \
		  -DKERNELBASE=$(KERNELBASE) -DVERSION=$(VERSION) $(DEBUG)

LDFLAGS		= -N -oformat ecoff-littlemips -Ttext $(LOADADDR)

SUBDIRS		= lib man

DISTFILES	= README TODO COPYING KNOWN_BUGS ChangeLog Makefile milo \
		  Makefile.inc hello vmlinux pandora milo.c identify.c \
		  acer_pica_61.c hello.c vmlinux.c crt0.S launch.S a.out.c \
		  elf.c debugger.c parser.c commands.c dskst_tyne.c \
		  olv_70010.c \
		  \
		  lib/Makefile lib/__main.c lib/cachectl.S lib/console.c \
		  lib/ctype.c lib/dumptlb.c lib/fflush.c lib/fprintf.c \
		  lib/getopt.c lib/getopt1.c lib/printf.c lib/puts.c \
		  lib/mkargs.c lib/stdfd.c lib/strerror.c lib/vsprintf.c \
		  lib/string.c lib/strcasecmp.c lib/strncscmp.c \
		  lib/C-ctype_ct.c lib/ctype-info.c lib/ISO88591-ctype.c \
		  lib/ISO88591_ct.c lib/ISO88591_mb.c lib/8x16.fnt \
		  lib/creat.c lib/atob.c lib/strtol.c lib/fopen.c \
		  lib/fclose.c lib/__sflags.c lib/__sfp.c lib/errno.c \
		  lib/errlist.c lib/perror.c lib/dumpcp0r.c lib/debuglevel.c \
		  lib/disasm.c lib/expandpath.c \
		  \
		  man/Makefile man/arc.5 man/milo.man.m4 \
		  \
		  include/stand/console.h include/stand/malloc.h \
		  include/arc/arc.h include/arc/errno.h include/arc/stdlib.h \
		  include/arc/types.h include/arc/arcio.h \
                  include/stand/identify.h include/stand/disasm.h \
		  include/stand/mkargs.h include/stand/ctype.h \
		  include/stand/loader.h include/stand/debugger.h \
		  include/stand/debug.h include/stand/stdio.h \
		  include/stand/libio.h include/stand/pathnames.h \
		  include/stand/behave.h include/stand/machspec.h \
		  \
		  lib/libstand.a

PROGS		= milo pandora vmlinux hello

all:		dummy subdirs $(PROGS)

ifeq (.depend,$(wildcard .depend))
include .depend
endif

disk:		$(PROGS)
		mwrite -n $(PROGS) a:

dist:		dummy $(DISTFILES)
		-rm -rf milo-$(VERSION)
		mkdir milo-$(VERSION)
		tar cf - $(DISTFILES) | (cd milo-$(VERSION) && tar xf -)
		$(MAKE) -C milo-$(VERSION)/lib lite
		tar cvvf milo-$(VERSION).tar milo-$(VERSION)
		gzip -f9 milo-$(VERSION).tar
		rm -rf milo-$(VERSION)

subdirs:	$(SUBDIRS)
		set -e;for i in $(SUBDIRS); do $(MAKE) -C $$i;done

pandora:	milo
		ln -fs milo pandora

milo:		milo.o crt0.o launch.o identify.o acer_pica_61.o a.out.o elf.o \
		debugger.o parser.o commands.o olv_70010.o dskst_tyne.o \
		subdirs
		$(LD) $(LDFLAGS) -e _entry -o milo \
			crt0.o milo.o identify.o acer_pica_61.o launch.o \
			a.out.o debugger.o parser.o commands.o olv_70010.o \
			dskst_tyne.o -Llib -lstand
		: $(STRIP) milo

vmlinux:	vmlinux.o subdirs
		$(LD) -N -Ttext $(KERNELBASE) -o vmlinux vmlinux.o \
			-Llib -lstand

hello:		hello.o crt0.o subdirs
		$(LD) $(LDFLAGS) -e _entry -o hello crt0.o hello.o \
			-Llib -lstand
		: $(STRIP) hello

crt0.o:		crt0.S
		$(CC) $(CFLAGS) -c crt0.S

clean:		clean-local clean-recursive

clean-local:
		-rm -f *.[os] a.out
		-rm -rf milo-$(VERSION)

clean-recursive:
		@set -e;for i in $(SUBDIRS); do (cd $$i && $(MAKE) clean);done

distclean:	distclean-local distclean-recursive

distclean-local:	clean-local
		-rm -f .depend .tmpdepend *.bak *~ pandora milo vmlinux hello
		find include \( -name \*.bak -o -name \*~ -o -name \*.orig \
			-o -name \*.rej \) -print | xargs rm -f
		-rm -f milo-$(VERSION).tar.gz \#*\#

distclean-recursive:
		@set -e;for i in $(SUBDIRS); do (cd $$i && $(MAKE) distclean);done

depend:		depend-local depend-recursive

depend-local:
		set -e;for i in *.c; do echo -n "./";$(CPP) $(CFLAGS) -M $$i; \
			done > .tmpdepend
		mv .tmpdepend .depend

depend-recursive:
		@set -e;for i in $(SUBDIRS); do (cd $$i && $(MAKE) depend);done

ifeq (.depend,$(wildcard .depend))
dummy:

else
.depend:
	@echo "Generating dependencies..."
	@echo
	$(MAKE) depend
	@echo
	@echo "Successful. Try re-making (ignore the error that follows)"
	@echo
	exit 1

dummy:	.depend

endif
