#
# $Id: Makefile,v 1.9 2003/08/07 19:32:24 malekith Exp $
#

arch := $(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc64/ -e s/arm.*/arm/ -e s/sa110/arm/)
xobj = 	obj_common.o obj_reloc.o obj_load.o insmod.o logger.o \
	modstat.o sys_nim.o string.o vsprintf.o ctype.o alloc.o \
	main.o syscall.o start.o \
	obj_$(arch).o raid.o
obj = $(addprefix o/,$(xobj))

CPPFLAGS = -Iinsmod/h -DELF_MACHINE_H='"elf_$(arch).h"' \
	-DARCH='"$(arch)"'
DEBUG = -g
CFLAGS = -Wall $(DEBUG) -O2
LIBS = 
target = bsp
targets = $(target) $(target)-raid
comp = $(CC) $(CPPFLAGS) $(CFLAGS) -c
gccdir = $(shell gcc -v 2>&1 | grep from | sed -e 's/.*from //' -e 's|/specs||')

ifeq ($(arch),sparc64)
LIBS += /usr/lib/libc.a $(gccdir)/libgcc.a
endif

ifeq ($(arch),alpha)
LIBS += /usr/lib/libc.a
endif

all: $(target) $(target)-raid

$(target): o $(obj) o/raid-stub.o
	cp o/raid-stub.o o/raid.o
	ld $(DEBUG) -o $@ $(obj) $(LIBS)

$(target)-raid: o $(obj)
	make -C raidstart
	cp raidstart/raid.o o/raid.o
	ld $(DEBUG) -o $@ $(obj) $(LIBS)

o/raid.o: o
	echo dummy > o/raid.o

o:
	mkdir o

clean:
	rm -f $(targets) core 
	rm -rf o
	make -C raidstart clean

small:
	$(MAKE) clean
	$(MAKE) CFLAGS="$(CFLAGS) -Os" $(targets)
	strip -R .comment -R .note $(targets)
	@ls -l $(targets)

version = $(shell perl -n -e '/define VERSION "(.*)"/ and print "$$1\n"' main.c)

dist:
	rm -rf bsp-$(version)
	mkdir bsp-$(version)
	tar --exclude-from=.cvsignore --exclude=bsp-$(version) \
		--exclude=CVS -cf - . | tar -C bsp-$(version) -xf -
	tar zcf bsp-$(version).tar.gz bsp-$(version)
	rm -rf bsp-$(version)
	
o/%.o: %.c
	$(comp) $< -o $@

o/%.o: insmod/%.c
	$(comp) $< -o $@
