#! /bin/sh

SPEC_DIR=${SPEC_DIR:-/usr/local/mipsel-linux/lib/snow-specs}

if [ $# != 1 ]; then
  echo "Usage: mksnow <lib>"
  exit 1
fi

LIBRARY_NAME=`basename $1 '.a'`

SPEC_FILE=${SPEC_DIR}/${LIBRARY_NAME}.spec
. $SPEC_FILE

LDSCRIPT=${LIBRARY_NAME}-ldscript
SFILE=${LIBRARY_NAME}_syms.s
OFILE=${LIBRARY_NAME}_syms.o
SSOFILE=${LIBRARY_NAME}.${LIBRARY_MAJORVERSION}.sso

snow-make-ldscript --startaddr=$LIBRARY_ADDRESS > $LDSCRIPT
mipsel-linux-ld -T $LDSCRIPT /usr/local/mipsel-linux/lib/crti.o \
  /usr/local/mipsel-linux/lib/snow/crtbegin.o --whole-archive $LIBRARY_ARCHIVES \
  --no-whole-archive \
  /usr/local/mipsel-linux/lib/snow/crtend.o /usr/local/mipsel-linux/lib/crtn.o \
  -L/usr/local/mipsel-linux/lib/snow $LIBRARY_DEPEND_PATHS \
  -o $SSOFILE
CMD="mipsel-linux-objdump --syms $SSOFILE | \
  snow-makesyms --libname $LIBRARY_NAME \
  --majorversion 1 $LIBRARY_STUB_OPTIONS"
for LIB in $LIBRARY_DEPENDS; do
  CMD="$CMD --library $LIB"
done
eval $CMD > $SFILE
mipsel-linux-as $SFILE -o $OFILE
mipsel-linux-ar rc ${LIBRARY_NAME}.sa $OFILE
mipsel-linux-ranlib ${LIBRARY_NAME}.sa

#
# Cleanup.
#
rm $LDSCRIPT
rm $SFILE
rm $OFILE
