#! /bin/sh
# Generated automatically from ml.in by configure.
#---------------------------------------------------------------------------#
# Copyright (C) 1995 University of Melbourne.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#---------------------------------------------------------------------------#
#
# ML - Mercury Linker.
#
# Invokes GCC with the appropriate options to link in the Mercury library.
#
# Usage: ml [<ml options>] [<gcc options] files...
# Options:
# 	-v, --verbose
#		Echo gcc command line before executing it
# 	--no-demangle
#		Don't pipe the output of the linker the Mercury demangler
# 	-g, --no-strip
#		Do not strip debugging information
#	-s <grade>, --grade <grade>
#		Specify which grade of the Mercury library to link with.
#		(The default grade is determined by `configure'.)
#
# Environment variables: MERCURY_C_LIB_DIR, MERCURY_DEFAULT_GRADE

FULLARCH=i486-unknown-linux
NONSHARED_LIB_DIR=${MERCURY_NONSHARED_LIB_DIR=/usr/lib/nonshared}
DEFAULT_LIBDIR=/usr/lib/mercury/lib
LIBDIR=${MERCURY_C_LIB_DIR=$DEFAULT_LIBDIR}
DEFAULT_GRADE=${MERCURY_DEFAULT_GRADE=asm_fast.gc}
# DEMANGLER=${MERCURY_DEMANGLER=/usr/lib/mercury/bin/i486-unknown-linux/mdemangle}
DEMANGLER=${MERCURY_DEMANGLER=mdemangle}
CC=${MERCURY_C_COMPILER="gcc"}
MKFIFO=${MERCURY_MKFIFO="/usr/bin/mkfifo"}

verbose=false
case $FULLARCH in
	*-win95|*-winnt|*-win32)
		# `gcc -s' is broken in gnu-win32
		strip=false
		;;
	*)
		strip=true
		;;
esac
demangle=true
GRADE=$DEFAULT_GRADE

while true; do
    case "$1" in
	-v|--verbose)
		verbose=true
		shift ;;
	--demangle)
		demangle=true
		shift ;;
	--no-demangle)
		demangle=false
		shift ;;
	-s|--grade)
		shift
		GRADE="$1"
		shift ;;
	--strip)
		strip=true
		shift ;;
	-g|--no-strip)
		strip=false
		shift ;;
	--no-libs)
		MERCURY_LIBS=
		shift ;;
	--)
		shift
		break ;;
	-static|-shared)
		break ;;
	-s*)
		GRADE="` expr $1 : '-s\(.*\)' `"
		shift ;;
	*)
		break ;;
    esac
done

case "$GRADE" in
	*.gc.prof*)
		LIBGC="-lgc_prof"
		;;
	*.gc*)
		LIBGC="-lgc"
		;;
	*)
		LIBGC=
		;;
esac

case $strip in
	true)	STRIP_OPTS="-s" ;;
	false)  STRIP_OPTS="" ;;
esac

LIBS=${MERCURY_LIBS="-lmercury -lmer $LIBGC -lm"}

case $FULLARCH in
	*-solaris*)
		LIBDIR_OPTS="
		-R$DEFAULT_LIBDIR/$FULLARCH -L$LIBDIR/$FULLARCH
		-R$DEFAULT_LIBDIR/$GRADE/$FULLARCH -L$LIBDIR/$GRADE/$FULLARCH
		"
		;;
	alpha-dec-osf*)
		LIBDIR_OPTS="
	-Wl,-rpath,$DEFAULT_LIBDIR/$FULLARCH:$DEFAULT_LIBDIR/$GRADE/$FULLARCH
		-L$LIBDIR/$FULLARCH -L$LIBDIR/$GRADE/$FULLARCH
		"
		;;
	*-sgi-irix5*)
		LIBDIR_OPTS="
	-Wl,-rpath,$DEFAULT_LIBDIR/$FULLARCH:$DEFAULT_LIBDIR/$GRADE/$FULLARCH
		-L$LIBDIR/$FULLARCH -L$LIBDIR/$GRADE/$FULLARCH
		"
		case "$GRADE" in
			*fast*|*jump*)
				ARCH_OPTS=-non_shared
LIBRARY_PATH="$NONSHARED_LIB_DIR:/usr/lib/nonshared:$LIBRARY_PATH"
				export LIBRARY_PATH
				;;
		esac
		;;
	*)
		LIBDIR_OPTS="
		-L$LIBDIR/$FULLARCH
		-L$LIBDIR/$GRADE/$FULLARCH
		"
		;;
esac

case "$MKFIFO" in
    none) demangle=false ;;
esac

case $verbose in
    true)
	case $demangle in
	    false)
		echo $CC $STRIP_OPTS $ARCH_OPTS "$@" $LIBDIR_OPTS $LIBS
		;;
	    true)
		echo $CC $STRIP_OPTS $ARCH_OPTS "$@" $LIBDIR_OPTS $LIBS "|"
		echo "$DEMANGLER"
		;;
	esac
	;;
esac

case $demangle in
    true)
	# we would like to just run $CC and pipe the result into $DEMANGLER,
	# but `$CC | $DEMANGLER' would return the wrong exit status, so
	# we need to use a named pipe; if the system doesn't have named
	# pipes, then we don't use the demangler

		# create the pipe, making sure we remove it if interrupted
	PIPE=/tmp/ml.$$
	trap 'rm -f $PIPE; exit 1' 1 2 3 13 15 
	$MKFIFO $PIPE
		# execute the demangler in the background, with stdin 
		# coming from the pipe and with stdout redirected to stderr
	exec $DEMANGLER 1>&2 < $PIPE &
		# redirect our stdout and stderr into the pipe
	exec >$PIPE 2>&1
		# now we can remove the pipe; since is an open file, it will
		# stay around until $CC and $DEMANGLER exit
	rm -f $PIPE
		# finally execute $CC; stdout & stderr will
		# go via the pipe to $DEMANGLER and then to stderr
	;;
esac
case $# in
	0) exec $CC $STRIP_OPTS $ARCH_OPTS $LIBDIR_OPTS $LIBS ;;
	*) exec $CC $STRIP_OPTS $ARCH_OPTS "$@" $LIBDIR_OPTS $LIBS ;;
esac
