#!/bin/sh 

# ABLE compiler wrapper

# Find the able library prefix
if test "x${ABLE_LIB}" = "x"; then
    # The ABLE_LIB isn't set, try the default install path
    ABLE_LIB=/opt/simtec/able
    if [ ! -d "${ABLE_LIB}" ]; then
	# try using the $0 and assuming the library root is above it
	ABLE_LIB=$(dirname $(dirname $(readlink -f "$0")))
	if [ ! -d "${ABLE_LIB}" ]; then
	    echo >&2 ABLE Library path is not valid.
	    exit 2
	fi
    fi
fi
if [ ! -f "${ABLE_LIB}/able-settings.sh" ]; then
    echo >&2 ABLE Library is not correctly installed.
    exit 2
fi

# aquire settings
. ${ABLE_LIB}/able-settings.sh

# determine which compiler script called as
case $0 in
    *cc)
	COMPILER=gcc
	;;
    *++)
	COMPILER=g++
	;;
    *)
        echo >&2 Compiler script must be called as -gcc or -g++
	exit 3
esac

# Get compiler include paths
ABLE_GCCINC=$(dirname `${ABLE_INTERNAL_CROSS}${COMPILER} --print-lib`)/include

# Determine our compile mode
determine_compile_mode () {
    COMPILE_MODE=compile_link
    while test "x$1" != "x"; do
	case "$1" in
	    -c|-E|-M|-MM)
		COMPILE_MODE=compile
		;;
	    --)
		break
		;;
	    *)
		;;
	esac
	shift
    done
}
determine_compile_mode "$@"

# Called to simply compile or create dependencies, no linking
if [ "${COMPILE_MODE}" = "compile" ]; then
   exec ${ABLE_INTERNAL_CROSS}${COMPILER} ${ABLE_INTERNAL_CROSS_SWITCHES} -nostdinc -I. -I${ABLE_LIB}/include -I${ABLE_GCCINC} "$@"
fi

# ensure a suitable compiler support library is selected
case ${ABLE_COMPILER_TYPE} in
    gcc)
	ABLE_LIB_LIBCC="-lgcc-able"
    ;;
    *)
	ABLE_LIB_LIBCC=""
esac

# Select C lib
if [ ! -r "${ABLE_LIB}/lib/libc.a" ]; then
    echo >&2 ABLE C library archive cannot be found.
    exit 2
else
    ABLE_LIB_LIBC="-lc"
fi

# select ld script
if [ ! -r "${ABLE_LIB}/lib/ld.script" ]; then
    echo >&2 ABLE Library link script cannot be found.
    exit 2
else
    ABLE_LIB_LDSCRIPT="${ABLE_LIB}/lib/ld.script"
fi

# select crt0
if [ ! -r "${ABLE_LIB}/lib/crt0.o" ]; then
    echo >&2 ABLE crt0 start object cannot be found.
    exit 2
else
    ABLE_LIB_CRT0="${ABLE_LIB}/lib/crt0.o"
fi

# Obtain output name
determine_output_filename () {
    CC_OUTPUT_NAME="a.out"
    while test "x$1" != "x"; do
	case "$1" in
	    -o*)
		REST=${1##-o}
		shift
		if test "x$REST" = "x"; then
		    CC_OUTPUT_NAME="$1"
		    shift
		else
		    CC_OUTPUT_NAME="$REST"
		fi
		;;
	    --output=*)
		REST=${1##--output=}
		shift
		CC_OUTPUT_NAME="$REST"
		;;
	    *)
	        shift
		;;
	esac
    done
}
determine_output_filename "$@"

# The big enchilada, compile and link all in one go

${ABLE_INTERNAL_CROSS}${COMPILER} ${ABLE_INTERNAL_CROSS_SWITCHES} -nostdinc -I. -I${ABLE_LIB}/include -I${ABLE_GCCINC} -nostdlib -L${ABLE_LIB}/lib -Xlinker --library-path=${ABLE_LIB}/lib -T ${ABLE_LIB_LDSCRIPT} -Xlinker --start-group "$@" ${ABLE_LIB_CRT0} ${ABLE_LIB_LIBC} ${ABLE_LIB_LIBCC} -Xlinker --end-group 

if [ $? -eq 0 -a -e "${CC_OUTPUT_NAME}" ]; then
    # Successful compile & link, try the object copy to get our binary
    TMPFILE=$(mktemp)
    mv ${CC_OUTPUT_NAME} ${TMPFILE}
    ${ABLE_INTERNAL_CROSS}objcopy -O binary -R __ident ${TMPFILE} ${CC_OUTPUT_NAME}
    rm ${TMPFILE}
fi
