#
# Makefile
#   Copyright (C) 1990, 1991, 1992, 1993 ,1994 Free Software Foundation
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
#

#
# Makefile for newlib/libc/stub/hppa. This is the board support code
# for the HP-PRO hppa embedded boards. <rob@cygnus.com>
#

DESTDIR =
VPATH = @srcdir@
srcdir = @srcdir@
objdir = .
srcroot = $(srcdir)/../..
objroot = $(objdir)/../..

prefix = @prefix
exec_prefix = @exec_prefix@

bindir = @bindir@
libdir = @libdir@
tooldir = $(exec_prefix)/$(target_alias)

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

SHELL = /bin/sh

CC = @CC@

AS = @AS@
AR = @AR@
LD = @LD@
RANLIB = @RANLIB@

#
# As the boot process only deals with LIF volumes, here's the src
# where we borrow existing code modules and where we put our
# boot image if we can bvuild one.
#
LIF_SRC = /usr/tftpdir/srcboot.lif
LIF_DEST = /usr/tftpdir/romboot.lif

HPDEFS = -D_INCLUDE_POSIX_SOURCE -D_INCLUDE_XOPEN_SOURCE \
         -D_INCLUDE_HPUX_SOURCE -DPCXT 

LIBS_FOR_TARGET = -lc

#
# The original flags used by HP for the boot ROMS were
# CFLAGS_FOR_TARGET = -c -Aa +O3 +OS -Wc,-DA1.0 -Wc,-DS1.0 +ESfic +ESsfc ${HPDEFS}
CFLAGS_FOR_TARGET = -c -Wd,-a -g
# the original value for -R was 0
LDFLAGS_FOR_TARGET =  -v -t -N -R 10000 -a archive
INCLUDES = -I${srcdir}

BSP_OBJS= pa_stub.o debugger.o iva_table.o io.o
STUBCRT0=crt0.o
STUBLIB=hppa.o
LDSCRIPT=hppa.ld
SHARED_DIR=
SHARED_OBJS=
# SHARED_OBJS=$(SHARED_DIR)

# Host specific makefile fragment comes in here.
@host_makefile_frag@

all: boot

# this target is only for testing
test: ${STUBCRT0} $(STUBLIB) test.o $(BSP_OBJS)
#	${LD} $(LDFLAGS_FOR_TARGET) ${STUBCRT0} test.o $(BSP_OBJS) -o test $(LIBS_FOR_TARGET)
	rootme=`pwd` ; \
	${LD} $(LDFLAGS_FOR_TARGET) ${STUBCRT0} test.o pa_stub.o debugger.o iva_table.o io.o -o test $(LIBS_FOR_TARGET)

$(STUBLIB): $(BSP_OBJS)
	${LD} -r $(BSP_OBJS) $(SHARED_OBJS) -o $(STUBLIB)

checksum: $(srcdir)/checksum.c
	$(CC) -g $(srcdir)/checksum.c -o checksum

# this is the target from HP's Makefile, it may be bogus
hpstub: $(OBJS)
	$(LD) $(LDFLAGS_FOR_TARGET) -e main -o $@ $(LIBRARY)/crt1.o \
	$(OBJS) -lc -m >stub.map

install:
	$(INSTALL_PROGRAM) $(STUBLIB) $(DESTDIR)$(tooldir)/lib/$(MULTISUBDIR)$(STUBLIB)
	$(INSTALL_PROGRAM) $(SHARED_DIR)/$(STUBCRT0) $(DESTDIR)$(tooldir)/lib/$(MULTISUBDIR)$(STUBCRT0)
	$(INSTALL_PROGRAM) $(srcdir)/$(LDSCRIPT) $(DESTDIR)$(tooldir)/lib/$(LDSCRIPT)

.c.o:
	$(CC) $(CFLAGS_FOR_TARGET) $(INCLUDES) -c $<

.c.s:
	$(CC) $(CFLAGS_FOR_TARGET) -S $(INCLUDES) $<
#	$(CC) $(CFLAGS_FOR_TARGET) $(NEWLIB_CFLAGS) $(INCLUDES) -c $<

.s.o:
	$(CC) $(CFLAGS_FOR_TARGET) $(INCLUDES) -c $<
#	$(AS) $(CFLAGS_FOR_TARGET) $(INCLUDES) -c $<

.SUFFIXES: .S .c .o .s .lif
#
# lifinit --
#	-vnnn 	Sets volume size to nnn bytes. 
#		nnn must be a multiple of 256.
#	-d    	Sets directory size to nnn file entries.
#		nnn must be a multiple of 8.
#	-nsss   Sets the volume name to be sss.
#	-Knnn	Forces the directory start location to be the
#               nearest multiple of nnn x 1024 bytes.
#	-snnn	set the initial system load (ISL) start address to
#               nnn in the volume label
#	-lnnn	Specifies the length in bytes of the ISL code.
#	-ennn	Set the ISL entry point to nnn bytes from the
#               beginning of the ISL
#
# lifcp --
#	-r	Forces RAW mode copying.
#	-T	Sets the file type of the directory entry.
#		ASCII is 1
#		BIN is -23951
#		BINARY is -2
#	-Knnn	forces each file copied in to begin on a nnn x
#               1024-byte boundary from the beginning of the volume
#	Note: the word echoed below MUST be 10 characters exactly.
#
# somxtract infile outfile --
#	converts a series 800 file *only* into a raw image
#
LIF_VOL = this_lif
# 65532
boot: test checksum
#	 FIXME: This fills up the hard disk unless "-R" is used
	@rm -f ${LIF_VOL} ${LIF_DEST}
	@ipl_entry=`nm -t d $< | grep "T .START." | sed -e 's/T .START.//' -e 's/^0*//'` ; \
	echo "Entry point is at $${ipl_entry}" ; \
	if test x"$${ipl_entry}" != x  ; then \
	ipl_entry=`expr $${ipl_entry} - 65536` ; \
	echo "Entry point offset is $${ipl_entry}" ; \
	cp $< $<.raw ; \
	rm -f $<.dis ; \
	objdump -d $< > $<.dis ; \
	somxtract $< $<.raw ; \
	ipl_size=`checksum -s $<.raw $<.image  | sed -e 's/ is the.*//'` ; \
	echo "IPL_SIZE is $${ipl_size}" ; \
	    lifinit -v64000000 -d64 -n"CYGNUS    " -K2 -s4096 -l$${ipl_size} -e$${ipl_entry} ${LIF_DEST} ;\
	    else \
		echo "ERROR: \$$START\$$ symbol not found" ; \
	    fi
	@chmod a+rw $<.image
	@lifcp -r -K2 $<.image ${LIF_DEST}:TEST
	@rm -f $<.image $<.dis
	@lifls -l ${LIF_DEST}
	@objdump -d $< > $<.dis

.S.o:
	rm -f $*.s
	${CPP} $< > $*.s
	$(CC) $(CFLAGS_FOR_TARGET) $(INCLUDES) -c $*.s
#	$(CC) $(CFLAGS_FOR_TARGET) $(NEWLIB_CFLAGS) $(INCLUDES) -c $<

.c.list:
	$(CC) $(CFLAGS) $*.c -S
	$(AS) $(AFLAGS) $*.s -Wa,-l > $*.list
	rm $*.s

.s.list:
	$(AS) $(AFLAGS) $*.s -Wa,-l > $*.list

doc:	

clean mostlyclean:
	rm -f *~ *.map *.list stub romboot.lif a.out *.raw *.image
	rm -f *.o $(STUBLIB) $(STUBCRT0) checksum *.dis test

distclean maintainer-clean realclean: clean
	rm -f Makefile config.cache config.log config.status
	
pa_stub.o:	$(srcdir)/pa_stub.c
debugger.o:	$(srcdir)/debugger.s
iva_table.o:	$(srcdir)/iva_table.s
io.o:		$(srcdir)/io.c
crt0.o:		$(srcdir)/crt0.s
test.o:		$(srcdir)/test.c
term_in.o:      $(srcdir)/term_in.s
term_out.o:     $(srcdir)/term_out.s
term_init.o:    $(srcdir)/term_init.s

.PHONY: info install-info clean-info
info:
install-info:
clean-info:

Makefile: Makefile.in config.status @host_makefile_frag_path@
	$(SHELL) config.status

config.status: configure
	$(SHELL) config.status --recheck
