#! /bin/sh
# scripts/mgnuc.  Generated from mgnuc.in by configure.
#---------------------------------------------------------------------------#
# Copyright (C) 1995-2003 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/compile_target_code.m                         ***
# *************************************************************************

FULLARCH=i686-pc-linux-gnu
NONSHARED_LIB_DIR=${MERCURY_NONSHARED_LIB_DIR=/usr/lib/nonshared}

DEFAULT_GRADE=${MERCURY_DEFAULT_GRADE=reg.gc}
CC=${MERCURY_C_COMPILER="cc"}
CFLAGS_FOR_REGS="-fno-builtin -fno-omit-frame-pointer"
CFLAGS_FOR_GOTOS="-fno-defer-pop -fno-function-cse -fno-gcse"
CFLAGS_FOR_THREADS="-DMR_THREAD_SAFE -DLINUX_THREADS 			-D_THREAD_SAFE -D_REENTRANT"
AS="cc -c -x assembler-with-cpp"

case "$CC" in
    *gcc*)
	ANSI_OPTS="-ansi"
	CHECK_OPTS="
	      -Wall -Wwrite-strings
	      -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
# -Wpointer-arith	causes too many complaints in system header files
#			(fixed in gcc 3.0, though, so at some point
#			we should re-enable this)
# -Wtraditional		we don't care about compatibility with pre-ANSI C
# -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 ...
#              		we don't care about compatibility with C compilers
#			that have short fixed limits on identifier length
# -Wenum-clash 		is for C++ only
# -Wunused		causes various spurious warnings

	OPT_OPTS="-O2 -fomit-frame-pointer"
	DEBUG_OPT="-g"
	COMPILER=gcc
	;;
    *lcc*)
	ANSI_OPTS=
	CHECK_OPTS="-w"     # turn off all warnings due to spurious warnings.
	OPT_OPTS=""
	DEBUG_OPT="-g"
	COMPILER=lcc
	;;
    *cl* | *CL*)
	ANSI_OPTS=""
	CHECK_OPTS=""
	OPT_OPTS=""
	DEBUG_OPT="/Zi"
	COMPILER=cl
	;;
    *)
	ANSI_OPTS=
	CHECK_OPTS=
	OPT_OPTS="-O"
	DEBUG_OPT="-g"
	COMPILER=unknown
	;;
esac

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

verbose=false
assemble=false
c_debug=false
c_optimize=true
use_activation_counts=false
preserve_tail_recursion=true
mercury_config_dir=${MERCURY_STDLIB_DIR-/usr/lib/mercury}
mercury_config_dir=${MERCURY_CONFIG_DIR=$mercury_config_dir}
mercury_stdlib_dir=${MERCURY_STDLIB_DIR=/usr/lib/mercury}

# include the file `init_grade_options.sh-subr'

#---------------------------------------------------------------------------#
# Copyright (C) 1997-2003 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>
	--target {il, c, java, asm}
	--il
	--asm-labels
	--gcc-non-local-gotos
	--gcc-global-registers
	-H, --high-level-code
	--parallel
	--gc {boehm, mps, accurate, none}
	-p, --profiling
	--profile-calls
	--profile-time
	--profile-memory
	--profile-deep
	--record-term-sizes-as-words
	--record-term-sizes-as-cells
	--use-trail
	--reserve-tag
	--use-minimal-model
	--pic-reg
	--no-stack-trace
	--debug
	--decl-debug
		See the documentation in the \"Invocation\" section
		of the Mercury User's Guide."

# --gcc-nested-functions is not yet documented because it is not yet stable
# --high-level-data is not yet documented because it is not yet implemented
# --high-level is not yet documented because --high-level-data is
#		not yet implemented

target=c
highlevel_code=false
highlevel_data=false
gcc_nested_functions=false
asm_labels=true
non_local_gotos=true
global_regs=true
thread_safe=false
gc_method=boehm
profile_time=false
profile_calls=false
profile_memory=false
profile_deep=false
record_term_sizes_as_words=false
record_term_sizes_as_cells=false
use_trail=false
reserve_tag=false
use_minimal_model=false
pic_reg=false
stack_trace=false
require_tracing=false
decl_debug=false

case $# in
	0) set - --grade "$DEFAULT_GRADE" ;;
	*) 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.
		Use the full contents of system headers,
		rather than the ANSI subset.
	--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.
	--mercury-standard-library-directory <directory>
	--mercury-stdlib-dir <directory>
		The directory containing the installed Mercury standard
		library.
	--no-mercury-standard-library-directory, --no-mercury-stdlib-dir
		Don't use an installed Mercury standard library.

$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_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__ -D_GNU_SOURCE"
		;;

	--no-check)
		CHECK_OPTS=
		;;

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

	--inline-alloc)
		INLINE_ALLOC_OPTS="-DMR_INLINE_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 ;;

	--use-activation-counts)
		use_activation_counts=true ;;
	--no-use-activation-counts)
		use_activation_counts=false ;;

	--preserve-tail-recursion)
		preserve_tail_recursion=true ;;
	--no-preserve-tail-recursion)
		preserve_tail_recursion=false ;;

	--mercury-standard-library-directory|--mercury-stdlib-dir)
		mercury_stdlib_dir="$2"
		mercury_config_dir="$2"
		shift
		;;
	--no-mercury-standard-library-directory|--no-mercury-stdlib-dir)
		unset mercury_stdlib_dir
		unset mercury_config_dir
		;;

	--mercury-config-directory|--mercury-config-dir)
		mercury_config_dir="$2"
		shift
		;;
	# We don't allow `MERCURY_CONFIG_DIR' to be unset
	# without `MERCURY_STDLIB_DIR' also being unset.

	# include the file `parse_grade_options.sh-subr'
	
#---------------------------------------------------------------------------#
# Copyright (C) 1997-2004 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.
#
#---------------------------------------------------------------------------#

	--target)
		shift
		case "$1" in
			asm)
				target=asm ;;
			c|C)
				target=c ;;
			il|IL)
				target=il ;;
			java|Java)
				target=java ;;
			*)
				echo "$0: invalid target \`$1'" 1>&2
				exit 1
				;;
		esac
		;;

	--il|--IL|--il-only|--IL-only)
		target=il ;;

	--java|--Java|--java-only|--Java-only)
		target=java ;;

	--high-level-code|-H)
		highlevel_code=true ;;
	--no-high-level-code|-H-)
		highlevel_code=false ;;

	--high-level-data)
		highlevel_data=true ;;
	--no-high-level-data)
		highlevel_data=false ;;

	--gcc-nested-functions)
		gcc_nested_functions=true ;;
	--no-gcc-nested-functions)
		gcc_nested_functions=false ;;

	--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|boehm|mps|none|automatic)
				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=false
		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 ;;

	--record-term-sizes-as-words)
		record_term_sizes_as_words=true ;;
	--no-record-term-sizes-as-words)
		record_term_sizes_as_words=false ;;
	--record-term-sizes-as-cells)
		record_term_sizes_as_cells=true ;;
	--no-record-term-sizes-as-cells)
		record_term_sizes_as_cells=false ;;

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

	--reserve-tag)
		reserve_tag=true ;;
	--no-reserve-tag)
		reserve_tag=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 ;;

	--decl-debug)
		decl_debug=true ;;
	--no-decl-debug)
		decl_debug=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.

		target=c
		highlevel_code=false
		gcc_nested_functions=false
		highlevel_data=false
		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
		record_term_sizes_as_words=false
		record_term_sizes_as_cells=false
		use_trail=false
		reserve_tag=false
		use_minimal_model=false
		pic_reg=false
		stack_trace=false
		require_tracing=false
		decl_debug=false

		grade_pieces=`echo $grade | tr '.' ' '`
		for grade_piece in $grade_pieces
		do
			case "$grade_piece" in
				il)
					target=il
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=true
					gcc_nested_functions=false
					highlevel_data=true
					;;
				ilc)
					target=il
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=true
					gcc_nested_functions=false
					highlevel_data=false
					;;
				java)
					target=java
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=true
					gcc_nested_functions=false
					highlevel_data=true
					;;
				hl)
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=true
					gcc_nested_functions=false
					highlevel_data=true
					;;
				hlc)
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=true
					gcc_nested_functions=false
					highlevel_data=false
					;;
				hl_nest)
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=true
					gcc_nested_functions=true
					highlevel_data=true
					;;
				hlc_nest)
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=true
					gcc_nested_functions=true
					highlevel_data=false
					;;
				asm_fast)
					target=c
					asm_labels=true
					non_local_gotos=true
					global_regs=true
					highlevel_code=false
					gcc_nested_functions=false
					highlevel_data=false
					;;
				asm_jump)
					target=c
					asm_labels=true
					non_local_gotos=true
					global_regs=false
					highlevel_code=false
					gcc_nested_functions=false
					highlevel_data=false
					;;
				fast)
					target=c
					asm_labels=false
					non_local_gotos=true
					global_regs=true
					highlevel_code=false
					gcc_nested_functions=false
					highlevel_data=false
					;;
				jump)
					target=c
					asm_labels=false
					non_local_gotos=true
					global_regs=false
					highlevel_code=false
					gcc_nested_functions=false
					highlevel_data=false
					;;
				reg)
					target=c
					asm_labels=false
					non_local_gotos=false
					global_regs=true
					highlevel_code=false
					gcc_nested_functions=false
					highlevel_data=false
					;;
				none)
					target=c
					asm_labels=false
					non_local_gotos=false
					global_regs=false
					highlevel_code=false
					gcc_nested_functions=false
					highlevel_data=false
					;;

				par)
					thread_safe=true
					;;

				agc)
					gc_method=accurate
					;;
				mps)
					gc_method=mps
					;;
				gc)
					gc_method=boehm
					;;
				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=false
					profile_calls=false
					profile_memory=false
					profile_deep=true
					;;

				tsw)
					record_term_sizes_as_words=true
					record_term_sizes_as_cells=false
					;;

				tsc)
					record_term_sizes_as_words=false
					record_term_sizes_as_cells=true
					;;

				tr)
					use_trail=true
					;;

				rt)
					reserve_tag=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
					;;
				decldebug)
					stack_trace=true
					require_tracing=true
					decl_debug=true
					;;

				*)
					echo "$0: unknown grade component" \
						"\`$grade_piece'" 1>&2
					exit 1
					;;
			esac
		done
		;;

	-s*)
		grade="` expr $1 : '-s\(.*\)' `"
		# just insert it as `--grade $grade' and then reparse it
		shift
		case $# in
			0) set - x --grade "$grade" ;;
			*) set - x --grade "$grade" "$@" ;;
		esac
		;;


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

# include the file `final_grade_options.sh-subr'

#---------------------------------------------------------------------------#
# Copyright (C) 1998-2002, 2004 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

#
# --decl-debug implies --debug
#
case $decl_debug in true)
	require_tracing=true
	stack_trace=true
	;;
esac

#
# --target asm, IL or Java implies --high-level-code
#
case $target in asm|il|java)
	highlevel_code=true ;;
esac

#
# --target IL or Java implies --high-level-data
#
case $target in il|java)
	highlevel_data=true ;;
esac

#
# --target IL or Java implies --gc automatic
#
case $target in il|java)
	gc_method=automatic ;;
esac

#
# --high-level-code disables the use of low-level gcc extensions
#
case $highlevel_code in true)
	non_local_gotos=false
	asm_labels=false
	global_regs=false
	;;
esac

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

# compute the canonical grade name from the options settings
#

# include the file `canonical_grade.sh-subr'

#---------------------------------------------------------------------------#
# Copyright (C) 2000-2003 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.
#---------------------------------------------------------------------------#
#
# canonical_grade.sh-subr:
#	An `sh' subroutine for computing a canonical grade string based on
#	the values of grade-related options.
#	It is used by the `ml', `c2init' and `canonical_grade' scripts.
#
#	The code here should be inserted after init_grade_options.sh-subr,
#	parse_grade_options.sh-subr and final_grade_options.sh-subr, which
#	together define a set of shell variables giving the values of the
#	various grade options.
#
#	Canonical_grade.sh-subr defines the variable GRADE, which will contain
#	the canonical string for the grade implied by the option values.
#
#	If the option values are inconsistent, this script fragment will
#	print an error message and exit with failure.
#
# IMPORTANT: any changes to the handling of grades here may also require
# changes to all the files indicated by runtime/mercury_grade.h.

case $non_local_gotos,$global_regs in
	true,true) 	GRADE="fast" ;;
	true,false)  	GRADE="jump" ;;
	false,true)  	GRADE="reg" ;;
	false,false)   	GRADE="none" ;;
esac

case $asm_labels in
	true)		GRADE="asm_$GRADE" ;;
	false)		;;
esac

case $highlevel_code,$highlevel_data,$GRADE,$target in
	true,true,none,*)		
		case $target in
			c|asm)	GRADE="hl" ;;
			il)	GRADE="il" ;;
			java)	GRADE="java" ;;
			*)
				progname=`basename $0`
				echo "$progname: unknown target: $target"
				exit 1
				;;
		esac
		;;
	true,false,none,*)	
		case $target in
			c|asm)	GRADE="hlc" ;;
			il)	GRADE="ilc" ;;
			*)
				progname=`basename $0`
				echo "$progname: unsupported target: $target"
				exit 1
				;;
		esac
		;;
	false,false,*,*)
		# GRADE was set above
		;;
	false,true,*,*)
		progname=`basename $0`
		echo "$progname: error: \`--high-level-data' requires \`--high-level-code'" 1>&2
		exit 1
		;;
	*)
		echo "$progname: error: \`--high-level-code' is incompatible with the use of" 1>&2
		echo "$progname: error: low-level gcc extensions implied by grade \`$GRADE'" 1>&2
		exit 1
		;;
esac

case $gcc_nested_functions,$highlevel_code in
	true,true)	GRADE="${GRADE}_nest" ;;
	*)		;;
esac
	
case $thread_safe in
	true)	GRADE="$GRADE.par" ;;
	false)	;;
esac

case $gc_method in
	conservative)	GRADE="$GRADE.gc" ;;	# deprecated; alias for boehm
	boehm)		GRADE="$GRADE.gc" ;;
	mps)		GRADE="$GRADE.mps" ;;
	accurate)	GRADE="$GRADE.agc" ;;
esac

case $profile_time,$profile_calls,$profile_memory,$profile_deep in
	true,true,false,false)	GRADE="$GRADE.prof" ;;
	true,false,false,false)	GRADE="$GRADE.proftime" ;;
	false,true,false,false)	GRADE="$GRADE.profcalls" ;;
	true,true,true,false)	GRADE="$GRADE.profall" ;;
	false,true,true,false)	GRADE="$GRADE.memprof" ;;
	false,false,false,true)	GRADE="$GRADE.profdeep" ;;
	false,false,false,false)	;;
	*)			progname=`basename $0`
				echo "$progname: error: invalid combination of profiling options." 1>&2
				exit 1
				;;
esac

case $use_trail in
	true)		GRADE="$GRADE.tr" ;;
	false)		;;
esac

case $reserve_tag in
	true)		GRADE="$GRADE.rt" ;;
	false)		;;
esac

case $use_minimal_model in
	true)	GRADE="$GRADE.mm" ;;
	false)	;;
esac

# We ignore the value of $picreg in computing the name of the grade
# that we will use as a directory name in the Mercury linker.
# case $picreg in
# 	true)	GRADE="$GRADE.picreg" ;;
# 	false)	;;
# esac

case $stack_trace,$require_tracing,$decl_debug in
	true,true,true)		GRADE="$GRADE.decldebug" ;;
	true,true,false)	GRADE="$GRADE.debug" ;;
	false,true,false)	GRADE="$GRADE.trace" ;;
	true,false,false)	GRADE="$GRADE.strce" ;;
	false,false,false)	;;
	*)			progname=`basename $0`
				echo "$progname: error: invalid combination of debugging options." 1>&2
				exit 1
				;;
esac

case $record_term_sizes_as_words,$record_term_sizes_as_cells in
	true,false)		GRADE="$GRADE.tsw" ;;
	false,true)		GRADE="$GRADE.tsc" ;;
	false,false)		;;
	*)			progname=`basename $0`
				echo "$progname: error: invalid combination of term size profiling options." 1>&2
				exit 1
				;;
esac

case "$mercury_stdlib_dir" in
    "")	MERC_ALL_C_INCL_DIRS=
    	;;
    *) 
	# The option setting code above guarantees that if
	# `--mercury-stdlib-dir' is set, `--mercury-config-dir'
	# is also set.
    	MERC_ALL_C_INCL_DIRS="-I$mercury_config_dir/conf -I$mercury_stdlib_dir/inc -I$mercury_stdlib_dir/lib/$GRADE/$FULLARCH/inc"
	;;
esac

# add /usr/local/include to the default search path, if needed
ALL_LOCAL_C_INCL_DIRS=${MERCURY_ALL_LOCAL_C_INCL_DIRS=}

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

case $c_debug in
	true)		C_DEBUG_OPTS="$DEBUG_OPT" ;;
	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 $highlevel_code in
	true)		HLC_OPTS="-DMR_HIGHLEVEL_CODE" ;;
	false)		HLC_OPTS="" ;;
esac

case $highlevel_data in
	true)		HLD_OPTS="-DMR_HIGHLEVEL_DATA" ;;
	false)		HLD_OPTS="" ;;
esac

case $gcc_nested_functions in
	true)		NEST_OPTS="-DMR_USE_GCC_NESTED_FUNCTIONS" ;;
	false)		NEST_OPTS="" ;;
esac

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

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

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

case $thread_safe in
	true)		THREAD_OPTS="$CFLAGS_FOR_THREADS"
			case $FULLARCH in *linux*) 
				# Don't use -ansi under Linux or we get parse
				# errors at sigset_t in the pthreads headers.
				# This doesn't seem to be necessary for recent
				# versions of Linux/glibc (e.g. glibc 2.1.2),
				# but I've left it in so we can remain
				# compatible with older versions.
				ANSI_OPTS=""
			esac ;;
	false)		THREAD_OPTS="" ;;
esac

# Set the correct flags if we're to use the MS Visual C runtime.
use_msvcrt=no
if test $use_msvcrt = "yes"; then
	case $thread_safe in
    # /MD states that we will use the MSVC runtime,
    # the boehm_gc collector assumes that the collector
    # has been built as a DLL if we are using the MSVC runtime
    # so we need to define GC_NOT_DLL when the collector isn't
    # built as a DLL.
		true)	MSVCRT_OPTS="/MD" ;;
		false)	MSVCRT_OPTS="/DGC_NOT_DLL /MD" ;;
	esac
else
	MSVCRT_OPTS=""
fi

case $gc_method in
	accurate)	GC_OPTS="-DMR_NATIVE_GC" ;;
	boehm)		GC_OPTS="-DMR_CONSERVATIVE_GC -DMR_BOEHM_GC" ;;
	conservative)	GC_OPTS="-DMR_CONSERVATIVE_GC -DMR_BOEHM_GC" ;;
	mps)		GC_OPTS="-DMR_CONSERVATIVE_GC -DMR_MPS_GC" ;;
	none)		GC_OPTS="" ;;
esac

case $profile_time in
	true)		PROF_TIME_OPTS="-DMR_MPROF_PROFILE_TIME" ;;
	false)		PROF_TIME_OPTS="-DNO_SIGNALS" ;;
			# We pass -DNO_SIGNALS unless profiling is enabled.
			# See comments in runtime/mercury_prof_mem.h for why.
esac

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

case $profile_memory in
	true)		PROF_MEMORY_OPTS="-DMR_MPROF_PROFILE_MEMORY" ;;
	false)		PROF_MEMORY_OPTS="" ;;
esac

case $use_activation_counts in
	true)		ACTIVATION_COUNT_OPT="-DMR_USE_ACTIVATION_COUNTS" ;;
	false)		ACTIVATION_COUNT_OPT="";;
esac

case $preserve_tail_recursion in
	true)		PRESERVE_TAIL_RECURSION_OPT="-DMR_DEEP_PROFILING_TAIL_RECURSION" ;;
	false)		PRESERVE_TAIL_RECURSION_OPT="";;
esac

case $profile_deep in
	true)		PROF_DEEP_OPTS="-DMR_DEEP_PROFILING $ACTIVATION_COUNT_OPT $PRESERVE_TAIL_RECURSION_OPT" ;;
	false)		PROF_DEEP_OPTS="" ;;
esac

case $record_term_sizes_as_words,$record_term_sizes_as_cells in
	true,true)	progname=`basename $0`
			echo "$progname: we can't record both cell and word sizes"
			exit 1 ;;
	true,false)	RECORD_TERM_SIZE_OPTS="-DMR_RECORD_TERM_SIZES" ;;
	false,true)	RECORD_TERM_SIZE_OPTS="-DMR_RECORD_TERM_SIZES -DMR_RECORD_TERM_SIZES_AS_CELLS" ;;
	false,false)	RECORD_TERM_SIZE_OPTS="" ;;
esac

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

case $reserve_tag in
	true)           RESERVE_TAG_OPTS="-DMR_RESERVE_TAG" ;;
	false)          RESERVE_TAG_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="-DMR_PIC_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

case $decl_debug in
	true)		DECL_DEBUG_OPTS="-DMR_DECL_DEBUG" ;;
	false)		DECL_DEBUG_OPTS="" ;;
esac

GCC_OPTS="$NEST_OPTS $ASM_OPTS $GOTO_OPTS $REG_OPTS"

# check that special grades are only used with gcc
case $COMPILER in
	gcc|unknown)
		;;
	*)	case "$GCC_OPTS" in
			*USE_GCC*)
		progname=`basename $0`
		echo "$progname: For compilers other than GNU C, the only" 1>&2
		echo "$progname: base grade allowed is \`none'" 1>&2
					;;
		esac
		;;
esac

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

# if we're using non-local gotos, add CFLAGS_FOR_GOTOS
case $non_local_gotos in
	true)	GCC_OPTS="$GCC_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.
		case "$COMPILER" in
	    		gcc)
				CHECK_OPTS="$CHECK_OPTS -Wno-uninitialized"
			;;
		esac
		;;
	*-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 gcc-2.7.0 on i386 with compiler/call_gen.c.
# Similarly for egcs-1.1 on sparc with library/lexer.c, library/string.c,
# and compiler/{deforest,dupelim,mercury_to_mercury,modules}.c.
# Similarly for egcs-1.1.2 on i386 with library/table_builtin.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" ;;
			*" table_builtin.c "*|*" modules.dir/table_builtin"*".c "*)
				ARCH_OPTS="$ARCH_OPTS -O0" ;;
			*" random.c "*|*" random.dir/random"*".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 \
		$HLC_OPTS $HLD_OPTS $GCC_OPTS $MSVCRT_OPTS $GC_OPTS $DEFINE_OPTS \
		$STACK_TRACE_OPTS $TRACE_OPTS $DECL_DEBUG_OPTS \
		$LLDEBUG_OPTS $C_DEBUG_OPTS \
		$PROF_TIME_OPTS $PROF_CALLS_OPTS $PROF_MEMORY_OPTS \
		$PROF_DEEP_OPTS $INLINE_ALLOC_OPTS $TRAIL_OPTS \
		$RECORD_TERM_SIZE_OPTS \
		$RESERVE_TAG_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
