.PHONY: default
default: all

COMMON = strrchr \
	bzero \
	strchr \
	strlen \
	strcmp \
	memset \
	write \
	bcd2int \
	ntoh \
	strtoul \
	ctype_ \
	exception


# Operating (hardware) environment

LIB += liboea
OEA = oea_exit \
	oea_msr \
	oea_write \
	oea_mtspr \
	oea_mtsr \
	oea_read \
	oea_nr_cpu \
	oea_cpu_nr \
	oea_abort \
	$(COMMON)

liboea.ba: $(OEA:%=%.bo)
	rm -f liboea.ba
	$(AR) r $@ $(OEA:%=%.bo)
	$(RANLIB) liboea.ba
liboea.la: $(OEA:%=%.lo)
	rm -f liboea.la
	$(AR) r $@ $(OEA:%=%.lo)
	$(RANLIB) liboea.la


# Virtual (user mode) environment

LIB += libvea
O += crt0
VEA = call \
	sbrk \
	cerror \
	errno \
	brk \
	$(COMMON)

libvea.ba: $(VEA:%=%.bo)
	rm -f libvea.ba
	$(AR) r $@ $(VEA:%=%.bo)
	$(RANLIB) libvea.ba
libvea.la: $(VEA:%=%.lo)
	rm -f libvea.la
	$(AR) r $@ $(VEA:%=%.lo)
	$(RANLIB) libvea.la


# OpenBoot (CHIRP) environment

LIB += libof
O += of1275_note_old of1275_note_virt of1275_note_real
OF = \
	of_write  \
	of_read  \
	of_exit \
	of1275 \
	$(COMMON)

libof.ba: $(OF:%=%.bo)
	rm -f libof.ba
	$(AR) r $@ $(OF:%=%.bo)
	$(RANLIB) libof.ba
libof.la: $(OF:%=%.lo)
	rm -f libof.la
	$(AR) r $@ $(OF:%=%.lo)
	$(RANLIB) libof.la


# Bug (moto mon) environment

LIB += libbug
O += bug
BUG = bug_write \
	bug_exit \
	bug_abort \
	$(COMMON)

libbug.ba: $(BUG:%=%.bo)
	rm -f libbug.ba
	$(AR) r $@ $(BUG:%=%.bo)
	$(RANLIB) libbug.ba
libbug.la: $(BUG:%=%.lo)
	rm -f libbug.la
	$(AR) r $@ $(BUG:%=%.lo)
	$(RANLIB) libbug.la


install:
	diff sbrk.S $(NETBSD)/lib/libc/arch/ppc/sys/sbrk.S
	diff brk.S $(NETBSD)/lib/libc/arch/ppc/sys/brk.S
	diff crt0.s $(NETBSD)/lib/csu/ppc/crt0.s
	diff SYS.h $(NETBSD)/lib/libc/arch/ppc/SYS.h
ALL = $(LIB:%=%.ba) $(LIB:%=%.la) $(O:%=%.lo) $(O:%=%.bo)

all: $(ALL)
.PHONEY: all

CONFIG_TARGET = powerpc-eabi
AS=$(CONFIG_TARGET)-as
LD=$(CONFIG_TARGET)-ld -g
CC=$(CONFIG_TARGET)-gcc -g
AR=$(CONFIG_TARGET)-ar
#RUN=$(CONFIG_TARGET)-run
RUN=$$HOME/build/psim/build/sim/ppc/run
RANLIB=$(CONFIG_TARGET)-ranlib
CFLAGS= -v

.SUFFIXES: # drop .a
.SUFFIXES: .S .s .c .bo .lo .be .le .ba .la

%.bo: %.s
	$(AS) -mbig -o $@ $<
%.bo: %.S
	$(CC) -mbig -E -I. $< > $@,
	$(AS) -mbig $@, -o $@,,
	rm $@,
	mv $@,, $@
%.bo: %.c $(INCLUDES)
	$(CC) -mbig -c -I. -o $@ $<
%.be: %.bo $(LIB:%=%.ba) $(CRT0:%=%.bo)
	$(LD) $(LDFLAGS) -oformat elf32-powerpc -o $@ $(CRT0:%=%.bo) $< $(LIB:%=%.ba)

%.lo: %.s
	$(AS) -mlittle -o $@ $<
%.lo: %.S
	$(CC) -mlittle -E -I. $< > $@,
	$(AS) -mlittle $@, -o $@,,
	rm $@,
	mv $@,, $@
%.lo: %.c $(INCLUDES)
	$(CC) -mlittle -I. -c -o $@ $<
%.le: %.lo $(LIB:%=%.la) $(CRT0:%=%.lo)
	$(LD) $(LDFLAGS) -oformat elf32-powerpcle -o $@ $(CRT0:%=%.lo) $< $(LIB:%=%.la)

tests test: all
	@dir=`pwd` ; \
	dir=`basename $$dir` ; \
	for t in x $(TESTS) ; \
	do \
	  if [ $$t = x ] ; \
	  then \
	    continue ; \
	  else \
	    echo "****" ; \
	    echo "**** Running $$dir/$$t" ; \
	    echo "****" ; \
	    ( set -xe ; \
	        $(RUN_PREFIX) \
		$(RUN) \
		$(RUN_FLAGS) \
		$$t \
		$(RUN_ARGS) \
	    ) || exit 1 ; \
	  fi \
	done

Makefile: Makefile.in ../Makefile.in
	cat Makefile.in ../Makefile.in > Makefile~
	mv Makefile~ Makefile


clean:
	rm -f $(ALL)
	rm -f *.o *.bo *.lo *.be
	rm -f *.a *.ba *.la *.le
	rm -f *, *~
	rm -f *.core

tidy:
	rm -f *.core
	rm -f *, *~
