#! /bin/sh
# Generated automatically from mgnuc.in by configure.
#---------------------------------------------------------------------------#
# Copyright (C) 1995-1997 The 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.
#---------------------------------------------------------------------------#
#
# IMPORTANT: the manpage is produced automatically from this help
# message, so if you change the help message, don't forget to check
# that the manpage still looks OK.
Help="\
Name:	mgnuc - Mercury front-end to GNU C
Usage:	mgnuc [<options>] [-- <gcc options>] files...
Options:
	-v, --verbose
		Echo gcc command before executing it.
	--no-ansi
		Don't pass \`-ansi' to gcc.
	--no-check
		Don't enable any of gcc's warnings.
	-s <grade>, --grade <grade>
	--asm-labels
	--gcc-non-local-gotos
	--gcc-global-registers
	-p, --profiling
	--profile-calls
	--profile-time
	--debug
	--use-trail
	--args {simple, compact}
	--pic-reg
	--inline-alloc
	--split-c-files
		See the documentation in the \"Invocation\" section
		of the Mercury User's Guide.
		
Description:
	This runs gcc with the appropriate options for compiling Mercury
	programs.
	Normally it invokes gcc in ANSI mode with almost all warnings enabled,
	but this can be changed using the \`--no-ansi' or \`--no-check'
	options.
Environment variables:
	MERCURY_C_INCL_DIR, MERCURY_DEFAULT_GRADE.
"

# *************************************************************************
# *** IMPORTANT NOTE: any changes to this file may also require similar ***
# *** changes to compiler/mercury_compile.m                             ***
# *************************************************************************

FULLARCH=m68k-unknown-linux-gnu
NONSHARED_LIB_DIR=${MERCURY_NONSHARED_LIB_DIR=/usr/lib/nonshared}
C_INCL_DIR=${MERCURY_C_INCL_DIR=/usr/lib/mercury/inc}
DEFAULT_GRADE=${MERCURY_DEFAULT_GRADE=asm_jump.gc}
CC=${MERCURY_C_COMPILER="gcc"}
CFLAGS_FOR_REGS=""
CFLAGS_FOR_GOTOS="-fno-defer-pop"
AS=as

case "$CC" in
    *gcc*)
	ANSI_OPTS="-ansi"
	CHECK_OPTS="
	      -Wall -Wwrite-strings -Wpointer-arith -Wcast-qual -Wtraditional
	      -Wshadow -Wstrict-prototypes -Wmissing-prototypes -Wno-unused"

# Note: we do not enable the following gcc warnings:
# -Wredundant-decls	causes too many complaints in system header files
# -Wconversion		really only intended to help people using \`unprotoize'
# -Waggregate-return	not useful, IMHO

# -Wcast-align 		causes redundant warnings in memory.c
# -pedantic		causes unsuppressable warnings about LVALUE_CAST()
# -Wnested-externs	causes unsuppressable warnings about callentry()
# -Wid-clash-31 		causes warnings about entry_mercury__xxx ...
# -Wenum-clash 		is for C++ only
# -Wunused		causes various spurious warnings

	OPT_OPTS="-O2 -fomit-frame-pointer"
	COMPILER=gcc
	;;
    *lcc*)
	ANSI_OPTS=
	CHECK_OPTS=
	OPT_OPTS=""
	COMPILER=lcc
	;;
    *)
	ANSI_OPTS=
	CHECK_OPTS=
	OPT_OPTS="-O"
	COMPILER=unknown
	;;
esac

AS_OPTS=""
SPLIT_OPTS=""
INLINE_ALLOC_OPTS=""

verbose=false
assemble=false

# include the file `init_grade_options.sh-subr'

#---------------------------------------------------------------------------#
# Copyright (C) 1997 The 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.
#---------------------------------------------------------------------------#
#
# parse_grade_options.sh-subr:
#	An `sh' subroutine for initializing grade-related options.
#	Used by the `ml' and `mgnuc' scripts.
#
#	The code here should be inserted before a script's option-parsing
#	loop.  The invoking script must define a DEFAULT_GRADE option.
#
#---------------------------------------------------------------------------#

asm_labels=true
non_local_gotos=true
global_regs=true
gc_method=conservative
profile_time=false
profile_calls=false
use_trail=false
args_method=compact
debug=false

case $# in
	0) set - "--grade $DEFAULT_GRADE" ;;
	0) set - "--grade $DEFAULT_GRADE" "$@" ;;
esac

#---------------------------------------------------------------------------#

while : ; do
    case "$1" in
	-h|--help|"-?")
		echo "$Help"
		exit 0
		;;

	-v|--verbose)
		verbose=true
		;;
	-v-|--no-verbose)
		verbose=false
		;;

	--assemble)
		assemble=true
		;;

	--no-ansi)
		ANSI_OPTS=
		;;

	--no-check)
		CHECK_OPTS=
		;;

	--split-c-files)
		SPLIT_OPTS=-DSPLIT_C_FILES
		;;
	--no-split-c-files)
		SPLIT_OPTS=
		;;

	--inline-alloc)
		INLINE_ALLOC_OPTS="-DINLINE_ALLOC -DSILENT"
		;;
	--no-inline-alloc)
		INLINE_ALLOC_OPTS=""
		;;

	--no-c-optimize)
		OPT_OPTS=""
		;;

	# include the file `parse_grade_options.sh-subr'
	
#---------------------------------------------------------------------------#
# Copyright (C) 1997 The 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.
#---------------------------------------------------------------------------#
#
# parse_grade_options.sh-subr:
#	An `sh' subroutine for parsing grade-related options.
#	Used by the `ml' and `mgnuc' scripts.
#
#	The code here should be inserted in the case statement in a scripts
#	option-parsing loop.
#
#---------------------------------------------------------------------------#

	--asm-labels)
		asm_labels=true ;;
	--no-asm-labels)
		asm_labels=false ;;

	--gcc-non-local-gotos)
		non_local_gotos=true ;;
	--no-gcc-non-local-gotos)
		non_local_gotos=false ;;

	--gcc-global-registers)
		global_regs=true ;;
	--no-gcc-global-registers)
		global_regs=false ;;

	--debug)
		debug=true ;;
	--no-debug)
		debug=false ;;

	--gc)
		shift
		case "$1" in
			accurate|conservative|none)
				gc_method=$1 ;;
			*)
				echo "$0: invalid gc method \`$1'" 1>&2
				exit 1
				;;
		esac
		;;

	-p|--profiling)
		profile_time=true
		profile_calls=true
		;;
	-p-|--no-profiling)
		profile_time=false
		profile_calls=false
		;;
	--profile-time)
		profile_time=true ;;
	--no-profile-time)
		profile_time=false ;;
	--profile-calls)
		profile_calls=true ;;
	--no-profile-calls)
		profile_calls=false ;;

	--use-trail)
		use_trail=true ;;
	--no-use-trail)
		use_trail=false ;;

	--args)
		shift
		case "$1" in
			simple|compact)
				args_method=$1;;
			*)
				echo "$0: invalid arg method \`$1'" 1>&2
				exit 1
				;;
		esac
		;;

	--pic-reg)
		pic_reg=true ;;
	--no-pic-reg)
		pic_reg=false ;;

	-s|--grade)
		shift
		grade="$1";

		# Convert a grade to a set of options.
		#
		# IMPORTANT: any changes to the handling of grades here
		# may also require changes to
		#	runtime/mercury_grade.h
		#	compiler/handle_options.m
		#	scripts/ml.in

		case "$grade" in
			*.debug)
				debug=true
				grade="` expr $grade : '\(.*\).debug' `"
				;;
			*)	debug=false
				;;
		esac

		case "$grade" in
			*.sa)
				args_method=simple
				grade="` expr $grade : '\(.*\).sa' `"
				;;
			*)	args_method=compact
				;;
		esac

		case "$grade" in
			*.tr)	use_trail=true
				grade="` expr $grade : '\(.*\).tr' `"
				;;
			*)	use_trail=false
				;;
		esac

		case "$grade" in
			*.prof)
				profile_time=true
				profile_calls=true
				grade="` expr $grade : '\(.*\).prof' `"
				;;
			*.proftime)	
				profile_time=true
				profile_calls=false
				grade="` expr $grade : '\(.*\).proftime' `"
				;;
			*.profcalls)	
				profile_time=false
				profile_calls=true
				grade="` expr $grade : '\(.*\).profcalls' `"
				;;
			*)
				profile_time=false
				profile_calls=false
				;;
		esac

		case "$grade" in
			*.agc)	gc_method=accurate
				grade="` expr $grade : '\(.*\).agc' `"
				;;
			*.gc)	gc_method=conservative
				grade="` expr $grade : '\(.*\).gc' `"
				;;
			*)	gc_method=none
				;;
		esac

		case "$grade" in
			asm_fast)
				global_regs=true
				non_local_gotos=true
				asm_labels=true
				;;
			fast)
				global_regs=true
				non_local_gotos=true
				asm_labels=false
				;;
			reg)
				global_regs=true
				non_local_gotos=false
				asm_labels=false
				;;
			asm_jump)
				global_regs=false
				non_local_gotos=true
				asm_labels=true
				;;
			jump)
				global_regs=false
				asm_labels=false
				non_local_gotos=true
				;;
			none)
				global_regs=false
				asm_labels=false
				non_local_gotos=false
				;;
			debug)
				debug=true
				global_regs=false
				asm_labels=false
				non_local_gotos=false
				;;
			*)
				echo "$0: invalid grade \`$grade'" 1>&2;
				exit 1
				;;
		esac
		;;
	-s*)
		grade="` expr $1 : '-s\(.*\)' `"
		# just insert it as `--grade $grade' and then reparse it
		case $# in
			0) set - "x --grade $grade" ;;
			0) set - "x --grade $grade" "$@" ;;
		esac
		;;

	--)
		shift
		break
		;;
	*)
		break
		;;
    esac
    shift
done

#
# convert mgnuc options into gcc options
#
# IMPORTANT: any changes here will require similar changes to
# compiler/mercury_compile.m.
#

case $asm_labels in
	true)		ASM_OPTS="-DUSE_ASM_LABELS" ;;
	false)		ASM_OPTS="" ;;
esac

case $non_local_gotos in
	true)		GOTO_OPTS="-DUSE_GCC_NONLOCAL_GOTOS" ;;
	false)		GOTO_OPTS="" ;;
esac

case $global_regs in
	true)		REG_OPTS="-DUSE_GCC_GLOBAL_REGISTERS" ;;
	false)		REG_OPTS="" ;;
esac

case $debug in
	true)		DEBUG_OPTS="-g" ;;
	false)		DEBUG_OPTS="$OPT_OPTS -DSPEED" ;;
esac

case $gc_method in
	accurate)	GC_OPTS="-DNATIVE_GC" ;;
	conservative)	GC_OPTS="-DCONSERVATIVE_GC" ;;
	none)		GC_OPTS="" ;;
esac

case $profile_time in
	true)		PROF_TIME_OPTS="-DPROFILE_TIME" ;;
	false)		PROF_TIME_OPTS="-DNO_SIGNALS" ;;
esac

case $profile_calls in
	true)		PROF_CALLS_OPTS="-DPROFILE_CALLS" ;;
	false)		PROF_CALLS_OPTS="" ;;
esac

case $use_trail in
	true)		TRAIL_OPTS="-DMR_USE_TRAIL" ;;
	false)		TRAIL_OPTS="" ;;
esac

case $args_method in
	simple)		ARG_OPTS="" ;;
	compact)	ARG_OPTS="-DCOMPACT_ARGS" ;;
esac

case $pic_reg in
	true)		PICREG_OPTS="-DPIC_REG" ;;
	false)		PICREG_OPTS="" ;;
esac


# check that special grades are only used with gcc
case $COMPILER in
	gcc)	;;
	*)	case "$GRADE_OPTS" in
			*USE_GCC*)
		echo "$0: For compilers other than GNU C, the only" 1>&2
		echo "$0: grades allowed are \`debug' and \`none'" 1>&2
					;;
		esac
esac

GRADE_OPTS="$DEBUG_OPTS $ASM_OPTS $GOTO_OPTS $REG_OPTS"

# if we're using global registers, add CFLAGS_FOR_REGS
case $global_regs in
	true)	GRADE_OPTS="$GRADE_OPTS $CFLAGS_FOR_REGS" ;;
	false)	;;
esac

# if we're using non-local gotos, add CFLAGS_FOR_GOTOS
case $non_local_gotos in
	true)	GRADE_OPTS="$GRADE_OPTS $CFLAGS_FOR_GOTOS" ;;
	false)	;;
esac

#
# Special case hacks for particular architectures
# Any code here needs to be duplicated in ../configure.in.
#

ARCH_OPTS=""
case "$FULLARCH" in
	mips-sgi-irix5.*)
		# nonlocal gotos don't work with PIC, which is the
		# default for Irix 5, so if nonlocal gotos are enabled
		# we need to disable the use of shared libraries.
		case $non_local_gotos in
			true)
LIBRARY_PATH="$NONSHARED_LIB_DIR:/usr/lib/nonshared:$LIBRARY_PATH"
				export LIBRARY_PATH
				AS_OPTS="-non_shared"
			;;
		esac
		;;
	i?86-*)
		# the use of stack_pointer in the ASM_JUMP macro
		# defined in runtime/goto.h causes lots of warnings
		# about using possibly uninitialized variables;
		# there's no easy way to supress them except by
		# disabling the warning.
		CHECK_OPTS="$CHECK_OPTS -Wno-uninitialized"
		;;
esac

#
# Kludge for gcc-2.6.3 bug on mips: if you use gcc global registers and -O2,
# gcc 2.6.3 gets an internal error compiling library/int.c and
# compiler/bytecode.c.  As a work-around, we compile those file with -O1.
# Similarly library/bag.c needs to be compiled with -O0.
# Similarly for gcc-2.6.3 on alpha with compiler/modules.c.
#
case $COMPILER in gcc)
	case "$FULLARCH" in
		mips*)
			case "$*" in
			*" int.c "*|*" int.dir/int_"*".c "*)
				case "`$CC --version 2>/dev/null`" in 2.6.*)
					ARCH_OPTS="$ARCH_OPTS -O1" ;;
				esac;;
			*" bytecode.c "*|*" bytecode.dir/bytecode_"*".c "*)
				case "`$CC --version 2>/dev/null`" in 2.6.*)
					ARCH_OPTS="$ARCH_OPTS -O1" ;;
				esac;;
			*" bag.c "*)
				case "`$CC --version 2>/dev/null`" in 2.6.*)
					ARCH_OPTS="$ARCH_OPTS -O0" ;;
				esac;;
			esac ;;
		alpha*)
			case "$*" in
			*" modules.c "*|*" modules.dir/modules_"*".c "*)
				case "`$CC --version 2>/dev/null`" in 2.6.*)
					ARCH_OPTS="$ARCH_OPTS -O1" ;;
				esac
			esac ;;
		i?86*)
			case "$*" in
			*" call_gen.c "*|*" modules.dir/call_gen"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O0" ;;
			esac ;;
	esac ;;
esac

#
# On sparc-sun-solaris2, we need to use -fPIC rather than -fpic if we're
# using grade `none', because otherwise the Mercury standard library
# overflows the fixed limit on the number of "small pic" references.
#
case "$FULLARCH" in sparc-sun-solaris2*)
	case "$grade" in none)
		case "$*" in *-fpic*)
			echo "mgnuc: using -fPIC rather than -fpic"
			OVERRIDE_OPTS="$OVERRIDE_OPTS -fPIC" ;;
		esac ;;
	esac ;;
esac

case $assemble in true)
	case $verbose in true)
		echo $AS $AS_OPTS "$@" ;;
	esac
	exec $AS $AS_OPTS "$@" ;;
esac

case $verbose in true)
	echo $CC -I$C_INCL_DIR $ANSI_OPTS $CHECK_OPTS \
		$GRADE_OPTS $GC_OPTS $PROF_TIME_OPTS $PROF_CALLS_OPTS \
		$INLINE_ALLOC_OPTS $TRAIL_OPTS $SPLIT_OPTS \
		$PICREG_OPTS $ARCH_OPTS $ARG_OPTS "$@" $OVERRIDE_OPTS ;;
esac
case $# in
	0) exec $CC -I$C_INCL_DIR $ANSI_OPTS $CHECK_OPTS \
		$GRADE_OPTS $GC_OPTS $PROF_TIME_OPTS $PROF_CALLS_OPTS \
		$INLINE_ALLOC_OPTS $TRAIL_OPTS $SPLIT_OPTS \
		$PICREG_OPTS $ARCH_OPTS $OVERRIDE_OPTS ;;
	*) exec $CC -I$C_INCL_DIR $ANSI_OPTS $CHECK_OPTS \
		$GRADE_OPTS $GC_OPTS $PROF_TIME_OPTS $PROF_CALLS_OPTS \
		$INLINE_ALLOC_OPTS $TRAIL_OPTS $SPLIT_OPTS \
		$PICREG_OPTS $ARCH_OPTS $ARG_OPTS "$@" $OVERRIDE_OPTS ;;
esac
