#! /usr/local/bin/gmake -f
GCC_LIB_ELEMENT_DIR=/usr/local/mipsel-linux/lib/snow
LIBC_LIB_ELEMENT_DIR=/usr/local/mipsel-linux/lib/snow
INSTALL_DIR=${SNOW_DIR:-/usr/local/mipsel-linux/snow}

# GCC_LIB_ELEMENT_DIR inserted by autoconf.  It points to where the 
# -msoft-float -mno-abicalls crtbegin.o/crtend.o are installed.

# LIBC_LIB_ELEMENT_DIR inserted by autoconf.  It points to where the
# -msoft-float -mno-abicalls crti/crtn are installed.

LIBRARY_SPEC = lib-spec

all::

include $(LIBRARY_SPEC)

LIBRARY_SCRIPT = $(LIBRARY_NAME)-ldscript
LIBRARY_IMAGE = $(LIBRARY_NAME).$(LIBRARY_MAJORVERSION).sso
LIBRARY_STUB = $(LIBRARY_NAME).sa

all:: $(LIBRARY_IMAGE) $(LIBRARY_STUB)

clean::
	-rm $(LIBRARY_SCRIPT) $(LIBRARY_IMAGE) $(LIBRARY_STUB) $(LIBRARY_NAME)_syms.s $(LIBRARY_NAME)_syms.o

LD = mipsel-linux-ld
AS = mipsel-linux-as
AR = mipsel-linux-ar
RANLIB = mipsel-linux-ranlib
OBJDUMP = mipsel-linux-objdump

$(LIBRARY_SCRIPT):
	snow-make-ldscript --startaddr=$(LIBRARY_ADDRESS) >$@

$(LIBRARY_IMAGE): $(LIBRARY_SCRIPT) $(LIBRARY_ARCHIVES)
	$(LD) -T $(LIBRARY_SCRIPT) $(LIBC_LIB_ELEMENT_DIR)/crti.o \
	  $(GCC_LIB_ELEMENT_DIR)/crtbegin.o --whole-archive $(LIBRARY_ARCHIVES) \
	  --no-whole-archive \
	  $(GCC_LIB_ELEMENT_DIR)/crtend.o $(LIBC_LIB_ELEMENT_DIR)/crtn.o \
	  -L$(INSTALL_DIR) $(LIBRARY_DEPEND_PATHS) \
	  -o $@

$(LIBRARY_NAME)_syms.s: $(LIBRARY_IMAGE)
	$(OBJDUMP) --syms $(LIBRARY_IMAGE) | \
	  snow-makesyms --libname $(LIBRARY_NAME) \
	  $(LIBRARY_STUB_OPTIONS) \
	  $(LIBRARY_DEPENDS:%=--library %) \
	  --majorversion $(LIBRARY_MAJORVERSION) >$@

$(LIBRARY_NAME)_syms.o: $(LIBRARY_NAME)_syms.s
	$(AS) $(LIBRARY_NAME)_syms.s -o $@

$(LIBRARY_STUB): $(LIBRARY_NAME)_syms.o $(LIBRARY_STUB_ADD)
	$(AR) rc $(LIBRARY_STUB) $^ 
	$(RANLIB) $(LIBRARY_STUB)

install: $(LIBRARY_IMAGE) $(LIBRARY_STUB)
	cp $(LIBRARY_IMAGE) $(INSTALL_DIR)
	cp $(LIBRARY_STUB) $(INSTALL_DIR)
	ln -fs $(LIBRARY_STUB) $(INSTALL_DIR)/$(LIBRARY_NAME).a
