#! /bin/sh
# Generated automatically from c2init.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.
#---------------------------------------------------------------------------#

# C2INIT - Convert *.c to *_init.c
#
# This script outputs an appropriate init.c, given the .c files.
# Type `c2init --help' for usage message.
#

# If you change these, you will also need to change Mmake.common.in,
# scripts/ml.in, tools/bootcheck, tools/binary, tools/binary_step and
# tools/linear.
RT_LIB_NAME=mer_rt
STD_LIB_NAME=mer_std
TRACE_LIB_NAME=mer_trace
BROWSER_LIB_NAME=mer_browser

MERCURY_MOD_LIB_DIR=${MERCURY_MOD_LIB_DIR=/usr/lib/mercury/modules}
MKINIT=${MERCURY_MKINIT=mkinit}

# maximum number of calls to put in a single function
maxcalls=40
defentry=mercury__main_2_0
init_opt=""
trace_opt=""
library_opt=""
extra_inits_opt=""
aditi_opt=""
extra_init_dirs=""

# 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

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

# 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:	c2init - Create Mercury initialization file.
Usage:	c2init [options] *.c *.init ...
Options: 
	-a, --aditi
		Generate a function to upload Aditi-RL data to a database.
	-c <n>, --max-calls <n>
		Break up the initialization into groups of at most <n> function
		calls.  (Default value of <n> is 40.)
	-i, --include-initialization-code
		Always include code that calls the initialization functions
		of the various modules. With this option, the debugger can use
		information from any modules that were compiled with execution
		tracing to print (partial) stack traces, and to print the
		values of variables in ancestors of the current call, even
		in grades in which this not normally possible.
	-t, --trace
		Enable execution tracing in the generated executable.
		Implies -i.
	-l, --library
		Don't generate a \`main()' function.
		Instead, generate a function
			mercury_main(int argc, char **argv);
		(declared in \"init.h\") that can be called from C code.
		(A more fine-grained interface is also available;
		see \"init.h\" for details.)
	-I <directory>, --init-file-directory <directory>
		Include <directory> in the list of directories searched to
		locate \`.init' files.
	-w <label>, --entry-point <label>
		Set entry point to <label>.
		(Default value is \`mercury__main_2_0'.)
	-x, --extra-inits
		Search \`.c' files for extra initialization functions.
		(This may be necessary if the C files contain
		hand-coded C code with \`INIT' comments, rather than
		containing only C code that was automatically generated
		by the Mercury compiler.)

$grade_usage

Environment variables:
	MERCURY_MOD_LIB_DIR, MERCURY_MOD_LIB_MODS, MERCURY_MKINIT.
"

while true; do
	case "$1" in
	-a|--aditi)
		aditi_opt="-a"; shift;;	
	-a-|--no-aditi)
		aditi_opt=""; shift;;	
	-c|--max-calls)
		maxcalls="$2"; shift;;

	-i|--include-initialization-code)
		init_opt="-i";;

	-t|--trace)
		trace_opt="-t";;

	-l|--library)
		library_opt="-l";;

	-l-|--no-library)
		library_opt="";;

	-I|--init-file-directory)
		extra_init_dirs="$extra_init_dirs -I $2"; shift;;

	-w|--entry-point)
		defentry="$2"; shift;;

	-x|--extra-inits)
		extra_inits_opt="-x";;

	-x-|--no-extra-inits)
		extra_inits_opt="";;

	-h|--help|"-?")
		echo "$Help"
		exit 0;;

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


	--)
		break;;

	-*)
		echo "`basename $0`: invalid option \`$1'" 1>&2;
		echo "Try \`$0 --help' for help." 1>&2;
		exit 1;;

	*)
		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

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

case $require_tracing in
	true)
		trace_opt="-t" ;;
esac

case $stack_trace in
	true)
		init_opt="-i" ;;
esac

case "$trace_opt" in
	-t)
		init_opt="-i"
		MERCURY_MOD_LIB_MODS=${MERCURY_MOD_LIB_MODS="\
				$MERCURY_MOD_LIB_DIR/$RT_LIB_NAME.init \
				$MERCURY_MOD_LIB_DIR/$STD_LIB_NAME.init \
				$MERCURY_MOD_LIB_DIR/$BROWSER_LIB_NAME.init \
			"}
		;;
	*)
		MERCURY_MOD_LIB_MODS=${MERCURY_MOD_LIB_MODS="\
				$MERCURY_MOD_LIB_DIR/$RT_LIB_NAME.init \
				$MERCURY_MOD_LIB_DIR/$STD_LIB_NAME.init \
			"}
		;;
esac

case $# in
	0) exec $MKINIT $aditi_opt -c"$maxcalls" $init_opt $trace_opt \
		$library_opt -w"$defentry" $extra_inits_opt \
		$extra_init_dirs $EXTRA_INIT_FILES $MERCURY_MOD_LIB_MODS
	   ;;
	*) exec $MKINIT $aditi_opt -c"$maxcalls" $init_opt $trace_opt \
		$library_opt -w"$defentry" $extra_inits_opt \
		$extra_init_dirs "$@" $EXTRA_INIT_FILES $MERCURY_MOD_LIB_MODS
	   ;;
esac
