#!/bin/sh
#
#  Usage:  build <archdir> <toolprefix> <dialect> <dest> <strip> <object>...
#

# Base of architecture-dependent directory
ARCHDIR=$1

# Prefix of architecture-dependent tools
TOOLPREFIX=$2

# Prefix of dialect {arm,armhf,armv7a,thumb2, ....}
DIALECT=$3

# Pathname of library file to generate
DEST=$4

# "strip" or "nostrip"
STRIP=$5

shift 5

if [ -r ${ARCHDIR}/lib/ld-2.25.so ] ; then
    DYNLINKER="ld-2.25.so"
elif [ -r ${ARCHDIR}/lib/ld-linux.so.3 ] ; then
    DYNLINKER="ld-linux.so.3"
else
    echo "Dynamic linker not found; cannot build libcrypt.so.1" >&2
    exit 1
fi
    
case $DIALECT in
    "arm")
	DIALECT_OPTION=" "
	;;
    "armv7a")
	DIALECT_OPTION="-tarmv7a"
	;;
    "armhf")
	DIALECT_OPTION="-tarmhf"
	;;
    "armv8")
       DIALECT_OPTION=" "
       ;;
    "thumb2")
	DIALECT_OPTION="-tthumb2"
	;;
    *)
	DIALECT_OPTION=" "
	;;
esac

case $TOOLPREFIX in
*mips*)
	${TOOLPREFIX}gcc ${DIALECT_OPTION} -o ${DEST} \
	    -mabi=32 -shared -static-libgcc -Wl,-O1 \
	    -Wl,-z,defs -Wl,-dynamic-linker=/lib/${DYNLINKER} \
	    -Wl,--version-script=libcrypt.map -Wl,-soname=libcrypt.so.1 \
	    -Wl,-z,relro \
	    -T libcrypt.so.lds \
	    abi-note.o -Wl,--whole-archive $* -Wl,--no-whole-archive interp.o \
	    ${ARCHDIR}/lib/libc.so.6 \
	    ${ARCHDIR}/usr/lib/libc_nonshared.a
	;;

*powerpc*)
	${TOOLPREFIX}gcc ${DIALECT_OPTION} -o ${DEST} \
	    -shared -static-libgcc -Wl,-O1 \
	    -msoft-float -Wl,-z,defs -Wl,-dynamic-linker=/lib/${DYNLINKER} \
	    -Wl,--version-script=libcrypt.map -Wl,-soname=libcrypt.so.1 \
	    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both \
	    -T libcrypt.so.lds \
	    abi-note.o -Wl,--whole-archive $* -Wl,--no-whole-archive interp.o \
	    ${ARCHDIR}/lib/libc.so.6 \
	    ${ARCHDIR}/usr/lib/libc_nonshared.a
	;;

*arm* | *aarch64*)
	${TOOLPREFIX}gcc ${DIALECT_OPTION} -o ${DEST} \
	    -shared -static-libgcc -Wl,-O1 \
	    -Wl,-z,defs -Wl,-dynamic-linker=/lib/${DYNLINKER} \
	    -Wl,--version-script=libcrypt.map -Wl,-soname=libcrypt.so.1 \
	    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both \
	    -T libcrypt.so.lds \
	    abi-note.o -Wl,--whole-archive $* -Wl,--no-whole-archive \
	    ${ARCHDIR}/lib/libc.so.6 \
	    ${ARCHDIR}/usr/lib/libc_nonshared.a
	;;

*i686*)
	${TOOLPREFIX}gcc ${DIALECT_OPTION} -o ${DEST} \
	    -shared -static-libgcc -Wl,-O1 \
	    -Wl,-z,defs -Wl,-dynamic-linker=/lib/${DYNLINKER} \
	    -Wl,--version-script=libcrypt.map -Wl,-soname=libcrypt.so.1 \
	    -Wl,-z,combreloc -Wl,-z,relro -Wl,--hash-style=both \
	    -T libcrypt.so.lds \
	    abi-note.o -Wl,--whole-archive $* -Wl,--no-whole-archive interp.o \
	    ${ARCHDIR}/lib/libc.so.6 \
	    ${ARCHDIR}/usr/lib/libc_nonshared.a
	;;
esac	

if [ "$STRIP" = "strip" ] ; then
    ${TOOLPREFIX}strip -R .note -R .comment ${DEST}
fi
