#
# 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.

VERSION		= 0.21

CC		= mips-linux-gcc
AS		= mips-linux-as -mcpu=r4000 -mips3
AR		= mips-linux-ar
LD		= mips-linux-ld
MAKE		= make
#
# Don't strip executables - there is a bug in mips-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		= mips-linux-strip
STRIP		= :

#
# 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 for full debug output. You have been warned!
#
DEBUG		= #-DDEBUG=1

CFLAGS		= -I./include -I/usr/mips-linux/include -O2 -mips3 -mcpu=r4000 \
		  -D__KERNEL__ -DLOADADDR=$(LOADADDR) -DKERNELBASE=$(KERNELBASE) \
		  -DVERSION=$(VERSION) $(DEBUG)

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

SUBDIRS		= lib

DISTFILES	= README TODO COPYING ChangeLog Makefile milo hello vmlinux \
		  milo.c identify.c acer_pica_61.c hello.c vmlinux.c crt0.S \
		  launch.S a.out.c elf.c debug.c parser.c commands.c \
		  \
		  lib/Makefile lib/__main.c lib/cachectl.S lib/console.c \
		  lib/ctype.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/fflush.c \
		  lib/fopen.c lib/fclose.c lib/__sflags.c lib/__sfp.c lib/errno.c \
		  lib/errlist.c lib/perror.c \
		  \
		  man/man8/milo.8 \
		  \
		  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/stand/identify.h \
		  include/stand/mkargs.h include/stand/ctype.h \
		  include/stand/loader.h include/stand/debug.h \
		  include/stand/stdio.h include/stand/libio.h \
		  \
		  lib/libstand.a

PROGS		= milo 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

milo:		milo.o crt0.o launch.o identify.o acer_pica_61.o a.out.o elf.o \
		debug.o parser.o commands.o subdirs
		$(LD) $(LDFLAGS) -e __entry -o milo \
			crt0.o milo.o identify.o acer_pica_61.o launch.o \
			a.out.o debug.o parser.o commands.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 *~ *.orig *.rej milo vmlinux hello
		find include \( -name \*.bak -o -name \*~ \) -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
