CFILES=\
	byteio.c      elf64.c	    ipl.c	  offset.c      gzip.c \
	diskpart.c    ext2.c	    lib.c	  pdc_bootio.c  vsprintf.c \
	elf32.c	      fileio.c	    load.c	  pdc_cons.c	pdc_misc.c

OFILES=$(CFILES:.c=.o)

INCLUDES=bootloader.h common.h load.h

# cc-cross-prefix
# Usage: CROSS_COMPILE := $(call cc-cross-prefix, m68k-linux-gnu- m68k-linux-)
# Return first prefix where a prefix$(CC) is found in PATH.
# If no $(CC) found in PATH with listed prefixes return nothing
cc-cross-prefix =  \
	$(word 1, $(foreach c,$(1),                                   \
		$(shell set -e;                                       \
		if (which $(strip $(c))gcc) > /dev/null 2>&1 ; then   \
			echo $(c);                                    \
		fi)))

# cross compilation
CC_ARCHES = hppa hppa1.1
CC_SUFFIXES = linux linux-gnu unknown-linux-gnu
CROSS_COMPILE := $(call cc-cross-prefix, \
	$(foreach a,$(CC_ARCHES), \
	$(foreach s,$(CC_SUFFIXES),$(a)-$(s)-)))

CC	= ${CROSS_COMPILE}gcc
AR	= ar
LD	= ${CROSS_COMPILE}ld

ifneq ("$(wildcard /etc/debian_version)","")
BLDINFO := $(shell echo https://parisc.docs.kernel.org - `dpkg-parsechangelog -l../debian/changelog -SDate`)
else
BLDINFO := $(shell echo https://parisc.docs.kernel.org - `LC_TIME=C date`)
endif

# Source sharing with palo
VPATH=../lib:.

AFLAGS	= -I../lib
CFLAGS	= -DIPL_LOADER -I. -I../lib -I../include -O2 -mdisable-fpregs -Wall -fno-delete-null-pointer-checks -fno-builtin
LDFLAGS  = -N --section-start .init=0x60000 -e '$$START$$'

all:	iplelf

iplelf:	crt0.o ipl.a
	echo "const char bld_info[] = \"$(BLDINFO)\";" > build.c
	$(CC) $(CFLAGS) -c -o build.o build.c
	-rm -f build.c
	$(LD) $(LDFLAGS) -o iplelf crt0.o ipl.a build.o `$(CC) -print-libgcc-file-name`

ipl.a:	$(OFILES)
	$(AR) rv ipl.a $?

clean:
	rm -f iplelf *.o *.a

distclean: clean


.S.o:
	$(CC) -D__ASSEMBLY__ $(AFLAGS) -traditional -c -o $*.o $<

byteio.o \
ext2.o \
fileio.o \
lib.o \
offset.o \
pdc_bootio.o \
pdc_cons.o \
vsprintf.o :		bootloader.h

ipl.o :		bootloader.h load.h

diskpart.o :		common.h

elf32.o \
elf64.o \
load.o \
som.o :		common.h load.h

