#! /bin/sh
# Generated automatically from mgnuc.in by configure.
#---------------------------------------------------------------------------#
# Copyright (C) 1995-1999 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.
		
# *************************************************************************
# *** IMPORTANT NOTE: any changes to this file may also require similar ***
# *** changes to compiler/mercury_compile.m                             ***
# *************************************************************************

FULLARCH=i686-pc-linux-gnu
NONSHARED_LIB_DIR=${MERCURY_NONSHARED_LIB_DIR=/usr/lib/nonshared}
MERC_C_INCL_DIR=${MERCURY_C_INCL_DIR=/usr/lib/mercury/inc}
MERC_ALL_C_INCL_DIRS=${MERCURY_ALL_C_INCL_DIRS=-I$MERC_C_INCL_DIR}
LOCAL_C_INCL_DIR=${MERCURY_LOCAL_C_INCL_DIR=/usr/local/include}
ALL_LOCAL_C_INCL_DIRS=${MERCURY_ALL_LOCAL_C_INCL_DIRS=-I$LOCAL_C_INCL_DIR}

DEFAULT_GRADE=${MERCURY_DEFAULT_GRADE=asm_fast.gc}
CC=${MERCURY_C_COMPILER="gcc"}
CFLAGS_FOR_REGS="-fno-builtin -fno-omit-frame-pointer"
CFLAGS_FOR_GOTOS="-fno-defer-pop -fno-function-cse -fno-gcse"
AS="gcc -c -x assembler-with-cpp"

case "$CC" in
    *gcc*)
	ANSI_OPTS="-ansi"
	CHECK_OPTS="
	      -Wall -Wwrite-strings -Wpointer-arith -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-qual		causes LOTS of redundant warnings
# -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="-w"     # turn off all warnings due to spurious warnings.
	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
c_debug=false
c_optimize=true

# include the file `init_grade_options.sh-subr'

#---------------------------------------------------------------------------#
# Copyright (C) 1997-1999 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.
#---------------------------------------------------------------------------#
#
# init_grade_options.sh-subr:
#	An `sh' subroutine for initializing grade-related options.
#	Used by the `ml', `mgnuc' and `c2init' scripts.
#
#	The code here should be inserted before a script's option-parsing
#	loop.  The invoking script must define a DEFAULT_GRADE option.
#
# IMPORTANT: any changes to the handling of grades here may also require
# changes to all the files indicated by runtime/mercury_grade.h.
#
# This file must initialize all the shell variables used by
# parse_grade_options.sh-subr, and must list all the options processed in
# parse_grade_options.sh-subr.
#
#---------------------------------------------------------------------------#

grade_usage="\
Grade options:
	-s <grade>, --grade <grade>
	--asm-labels
	--gcc-non-local-gotos
	--gcc-global-registers
	--parallel
	--gc {conservative, accurate, none}
	-p, --profiling
	--profile-calls
	--profile-time
	--profile-memory
	--use-trail
	--use-minimal-model
	--pic-reg
	--no-stack-trace
	--debug
		See the documentation in the \"Invocation\" section
		of the Mercury User's Guide."

# --profile-deep is not yet documented because it is not yet implemented

asm_labels=true
non_local_gotos=true
global_regs=true
thread_safe=false
gc_method=conservative
profile_time=false
profile_calls=false
profile_memory=false
profile_deep=false
use_trail=false
use_minimal_model=false
pic_reg=false
stack_trace=false
require_tracing=false

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

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

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.
	--low-level-debug
		Enable low-level debugging of the C code. Useful only
		for Mercury system developers.
	-g, --c-debug
		Generate object files that can debugged with C debuggers
		such as gdb.
	--no-c-optimize
		Disable optimization by the C compiler.
	--inline-alloc
		Inline calls to the memory allocator.
	--split-c-files
		This option must be specified if the argument C files were
		generated by the Mercury compiler with --split-c-files.

$grade_usage
		
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_ALL_C_INCL_DIRS, MERCURY_DEFAULT_GRADE.
"

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=
		DEFINE_OPTS="$DEFINE_OPTS -D__EXTENSIONS__"
		;;

	--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=""
		;;

	-g|--c-debug)
		c_debug=true
		;;
	-g-|--no-c-debug)
		c_debug=false
		;;

	--c-optimize)
		c_optimize=true
		;;
	--no-c-optimize)
		c_optimize=false
		;;

	--low-level-debug)
		low_level_debug=true ;;
	--no-low-level-debug)
		low_level_debug=false ;;

	# include the file `parse_grade_options.sh-subr'
	
#---------------------------------------------------------------------------#
# Copyright (C) 1997-1999 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', `mgnuc' and `c2init' scripts.
#
#	The code here should be inserted in the case statement in a script's
#	option-parsing loop.
#
# IMPORTANT: any changes to the handling of grades here may also require
# changes to all the files indicated by runtime/mercury_grade.h.
#
# This file should handle the setting of all the shell variables defined by
# init_grade_options.sh-subr.
#
#---------------------------------------------------------------------------#

	--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 ;;

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

	--parallel)
		thread_safe=true ;;

	-p|--profiling|--time-profiling)
		profile_time=true
		profile_calls=true
		profile_memory=false
		profile_deep=false
		;;
	--memory-profiling)
		profile_time=false
		profile_calls=true
		profile_memory=true
		profile_deep=false
		;;
	--deep-profiling)
		profile_time=true
		profile_calls=false
		profile_memory=false
		profile_deep=true
		;;
	-p-|--no-profiling)
		profile_time=false
		profile_calls=false
		profile_memory=false
		profile_deep=false
		;;
	--profile-time)
		profile_time=true ;;
	--no-profile-time)
		profile_time=false ;;
	--profile-calls)
		profile_calls=true ;;
	--no-profile-calls)
		profile_calls=false ;;
	--profile-memory)
		profile_memory=true ;;
	--no-profile-memory)
		profile_memory=false ;;
	--profile-deep)
		profile_deep=true ;;
	--no-profile-deep)
		profile_deep=false ;;

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

	--use-minimal-model)
		use_minimal_model=true ;;
	--no-use-minimal-model)
		use_minimal_model=false ;;

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

# The following more fine-grained options have been omitted
# since they are not very useful and would probably only confuse people.
#	--stack-trace)
#		stack_trace=true ;;
#	--no-stack-trace)
#		stack_trace=false ;;
#	--require-tracing)
#		require_tracing=true ;;
#	--no-require-tracing)
#		require_tracing=false ;;

	--debug)
		stack_trace=true
		require_tracing=true ;;
	--no-debug)
		stack_trace=false
		require_tracing=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 all the files indicated by
		# runtime/mercury_grade.h.

		asm_labels=false
		non_local_gotos=false
		global_regs=false
		thread_safe=false
		gc_method=none
		profile_time=false
		profile_calls=false
		profile_memory=false
		profile_deep=false
		use_trail=false
		use_minimal_model=false
		pic_reg=false
		stack_trace=false
		require_tracing=false

		grade_pieces=`echo $grade | tr '.' ' '`
		for grade_piece in $grade_pieces
		do
			case "$grade_piece" in
				asm_fast)
					asm_labels=true
					non_local_gotos=true
					global_regs=true
					;;
				asm_jump)
					asm_labels=true
					non_local_gotos=true
					global_regs=false
					;;
				fast)
					asm_labels=false
					non_local_gotos=true
					global_regs=true
					;;
				jump)
					asm_labels=false
					non_local_gotos=true
					global_regs=false
					;;
				reg)
					asm_labels=false
					non_local_gotos=false
					global_regs=true
					;;
				none)
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					;;

				par)
					thread_safe=true
					;;

				agc)
					gc_method=accurate
					;;
				gc)
					gc_method=conservative
					;;
				nogc)
					gc_method=none
					;;

				memprof)
					profile_time=false
					profile_calls=true
					profile_memory=true
					profile_deep=false
					;;
				prof)
					profile_time=true
					profile_calls=true
					profile_memory=false
					profile_deep=false
					;;
				proftime)	
					profile_time=true
					profile_calls=false
					profile_memory=false
					profile_deep=false
					;;
				profcalls)	
					profile_time=false
					profile_calls=true
					profile_memory=false
					profile_deep=false
					;;
				profall)
					profile_time=true
					profile_calls=true
					profile_memory=true
					profile_deep=false
					;;
				profdeep)
					profile_time=true
					profile_calls=false
					profile_memory=false
					profile_deep=true
					;;

				tr)
					use_trail=true
					;;

				mm)
					use_minimal_model=true
					;;

				picreg)
					pic_reg=true
					;;

# The following more fine-grained options have been omitted
# since they are not very useful and would probably only confuse people.
#				trace)
#					stack_trace=false
#					require_tracing=true
#
#				strce)
#					stack_trace=true
#					require_tracing=false

				debug)
					stack_trace=true
					require_tracing=true
					;;
			esac
		done
		;;

	-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

# include the file `final_grade_options.sh-subr'

#---------------------------------------------------------------------------#
# Copyright (C) 1998-1999 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.
#---------------------------------------------------------------------------#
#
# final_grade_options.sh-subr:
#	An `sh' subroutine for handling implications between grade-related
#	options. Used by the `ml', `mgnuc' and `c2init' scripts.
#
#	The code here should be inserted after a script's option-parsing
#	loop.
#
# IMPORTANT: any changes to the handling of grades here may also require
# changes to compiler/handle_options.m.
#
# This file should only use the shell variables initialized by
# init_grade_options.sh-subr.
#
#---------------------------------------------------------------------------#

#
# .tr grade is not compatible with .mm
#	(see comment in runtime/mercury_tabling.c for rationale)
#
case $use_trail,$use_minimal_model in
	true,true)
		echo "trailing and minimal model tabling are not compatible" 1>&2
		exit 1 ;;
esac

#
# .debug grade implies --use-trail in the absence of .mm
#	(see comment in compiler/handle_options.m for rationale)
#
case $stack_trace,$require_tracing,$use_minimal_model in
	true,true,false)
		use_trail=true ;;
esac

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

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

case $c_debug in
	true)		C_DEBUG_OPTS="-g" ;;
	false)		C_DEBUG_OPTS="" ;;
esac

case $c_optimize in
	true)		;;
	false)		OPT_OPTS="" ;;
esac

case $low_level_debug in
	true)		LLDEBUG_OPTS="-DMR_LOWLEVEL_DEBUG" ;;
	false)		LLDEBUG_OPTS="" ;;
esac

#
# convert grade mgnuc options into gcc options
#
# IMPORTANT: any changes here may require similar changes to all the files
# mentioned in runtime/mercury_grade.h.
#

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 $thread_safe in
	true)
	case $FULLARCH in
		*solaris*) THREAD_OPTS="-DMR_THREAD_SAFE -DSOLARIS_THREADS \
				-D_SOLARIS_PTHREADS -D_REENTRANT"
			;;

		*linux*) THREAD_OPTS="-DMR_THREAD_SAFE -DLINUX_THREADS \
				-D_THREAD_SAFE -D_REENTRANT"
				# Don't use -ansi under Linux or we get
				# parse errors at sigset_t in the pthreads
				# headers.
			ANSI_OPTS=""
			;;

		*)		THREAD_OPTS=""
			;;
	esac ;;
	false)		THREAD_OPTS="" ;;
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 $profile_memory in
	true)		PROF_MEMORY_OPTS="-DPROFILE_MEMORY" ;;
	false)		PROF_MEMORY_OPTS="" ;;
esac

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

case $use_minimal_model in
	true)		MINIMAL_MODEL_OPTS="-DMR_USE_MINIMAL_MODEL" ;;
	false)		MINIMAL_MODEL_OPTS="" ;;
esac

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

case $stack_trace in
	true)		STACK_TRACE_OPTS="-DMR_STACK_TRACE" ;;
	false)		STACK_TRACE_OPTS="" ;;
esac

case $require_tracing in
	true)		TRACE_OPTS="-DMR_REQUIRE_TRACING" ;;
	false)		TRACE_OPTS="" ;;
esac

GRADE_OPTS="$ASM_OPTS $GOTO_OPTS $REG_OPTS"

# 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: base grade allowed is \`none'" 1>&2
					;;
		esac
esac

# 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"
		;;
	*-solaris*|*-sunos*)
		# The solaris headers for pthreads are not ANSI :-(
		case $thread_safe in true)
			ANSI_OPTS=""
			;;
		esac
		;;
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.
# Similarly for egcs-1.1 on sparc with library/lexer.c, library/string.c,
# and compiler/{deforest,dupelim,mercury_to_mercury,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 ;;
		sparc*)
			case "$*" in
			*" lexer.c "*|*" modules.dir/lexer"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O1" ;;
			*" string.c "*|*" modules.dir/string"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O1" ;;
			*" deforest.c "*|*" modules.dir/deforest"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O1" ;;
			*" dupelim.c "*|*" modules.dir/dupelim"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O1" ;;
			*" dl.c "*|*" modules.dir/dl"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O1" ;;
			*" mercury_to_mercury.c "*|*" modules.dir/mercury_to_mercury"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O1" ;;
			*" modules.c "*|*" modules.dir/modules"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O1" ;;
			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


ALL_CC_OPTS="$MERC_ALL_C_INCL_DIRS $ANSI_OPTS $CHECK_OPTS $OPT_OPTS \
		$GRADE_OPTS $GC_OPTS $DEFINE_OPTS \
		$TRACE_OPTS $STACK_TRACE_OPTS $LLDEBUG_OPTS $C_DEBUG_OPTS \
		$PROF_TIME_OPTS $PROF_CALLS_OPTS $PROF_MEMORY_OPTS \
		$INLINE_ALLOC_OPTS $TRAIL_OPTS $MINIMAL_MODEL_OPTS \
		$SPLIT_OPTS $THREAD_OPTS $PICREG_OPTS $ARCH_OPTS $ARG_OPTS"

case $verbose in true)
	echo $CC $ALL_CC_OPTS "$@" $OVERRIDE_OPTS $ALL_LOCAL_C_INCL_DIRS;;
esac
case $# in
	0) exec $CC $ALL_CC_OPTS $OVERRIDE_OPTS ;;
	*) exec $CC $ALL_CC_OPTS "$@" $OVERRIDE_OPTS $ALL_LOCAL_C_INCL_DIRS;;
esac
