#!/bin/sh
# Configuration script for GNU CHILL
#   Copyright (C) 1994 Free Software Foundation, Inc.

#This file is part of GNU CC.

#GNU CC is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2, or (at your option)
#any later version.

#GNU CC is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with GNU CC; see the file COPYING.  If not, write to
#the Free Software Foundation, 59 Temple Place - Suite 330,
#Boston, MA 02111-1307, USA.  */

#
# Shell script to create proper links to machine-dependent files in
# preparation for compiling gcc.
#
# Options: --srcdir=DIR		specifies directory where sources are.
# 	   --host=HOST		specifies host configuration.
#	   --target=TARGET	specifies target configuration.
#	   --build=TARGET	specifies configuration of machine you are
#				using to compile GCC.
#	   --prefix=DIR		specifies directory to install in.
#	   --local-prefix=DIR	specifies directory to put local ./include in.
#	   --exec-prefix=DIR	specifies directory to install executables in.
#	   --with-gnu-ld	arrange to work with GNU ld.
#	   --with-gnu-as	arrange to work with GAS.
#	   --with-stabs		arrange to use stabs instead of host debug format.
#	   --with-elf		arrange to use elf instead of host debug format.
#	   --nfp		assume system has no FPU.
#
# If configure succeeds, it leaves its status in config.status.
# If configure fails after disturbing the status quo, 
# 	config.status is removed.
#

progname=$0
# Configure the runtime and regression-test directories
SUBDIRS="runtime utils"
SUBDIRS="$SUBDIRS testsuite/compile"
SUBDIRS="$SUBDIRS testsuite/execute"
SUBDIRS="$SUBDIRS testsuite/execute/telebras"
SUBDIRS="$SUBDIRS testsuite/noncompile"
SUBDIRS="$SUBDIRS testsuite/examples"
SUBDIRS="$SUBDIRS testsuite/execute/oe"
SUBDIRS="$SUBDIRS testsuite/compile/elektra"
SUBDIRS="$SUBDIRS testsuite/compile/votrics"

# Default --srcdir to the directory where the script is found, 
# if a directory was specified.
# The second sed call is to convert `.//configure' to `./configure'.
srcdir=`echo $0 | sed 's|//|/|' | sed 's|/[^/]*$||'`
if [ x$srcdir = x$0 ]
then
srcdir=
fi

host=

# Default prefix to /usr/local.
prefix=/usr/local

# local_prefix specifies where to find the directory /usr/local/include
# We don't use $(prefix) for this
# because we always want GCC to search /usr/local/include
# even if GCC is installed somewhere other than /usr/local.
# Think THREE TIMES before specifying any other value for this!
# DO NOT make this use $prefix!
local_prefix=/usr/local
# CYGNUS LOCAL: for our purposes, this must be prefix.  This is apparently
# only done for the benefit of glibc, and we don't use glibc.
local_prefix='$(prefix)'
# Default is to let the Makefile set exec_prefix from $(prefix)
exec_prefix='$(prefix)'

# CYGNUS LOCAL.  Default to nothing.
program_transform_name=
program_transform_set=
site=

remove=rm
hard_link=ln
symbolic_link='ln -s'
copy=cp

# Record all the arguments, to write them in config.status.
arguments=$*

#for Test
#remove="echo rm"
#hard_link="echo ln"
#symbolic_link="echo ln -s"

target=
host=
build=

for arg in $*;
do
  case $next_arg in
  --srcdir)
    srcdir=$arg
    next_arg=
    ;;
  --host)
    host=$arg
    next_arg=
    ;;
  --target)
    target=$arg
    next_arg=
    ;;
  --build)
    build=$arg
    next_arg=
    ;;
  --prefix)
    prefix=$arg
    next_arg=
    ;;
  --local-prefix)
    local_prefix=$arg
    next_arg=
    ;;
  --exec-prefix)
    exec_prefix=$arg
    next_arg=
    ;;
  --program-transform-name) # CYGNUS LOCAL
    # Double any backslashes or dollar signs in the argument.
    if [ -n "${arg}" ] ; then
      program_transform_name="${program_transform_name} -e `echo ${arg} | sed -e 's/\\\\/\\\\\\\\/g' -e 's/\\\$/$$/g'`"
    fi
    program_transform_set=yes
    next_arg=
    ;;    
  --program-prefix) # CYGNUS LOCAL
    if [ -n "${arg}" ]; then
      program_transform_name="${program_transform_name} -e s,^,`echo ${arg} | sed -e 's/\\\\/\\\\\\\\/g' -e 's/\\\$/$$/g'`,"
    fi
    program_transform_set=yes
    next_arg=
    ;;
  --program-suffix) # CYGNUS LOCAL
    if [ -n "${arg}" ]; then
      program_transform_name="${program_transform_name} -e s,\$\$,`echo ${arg} | sed -e 's/\\\\/\\\\\\\\/g' -e 's/\\\$/$$/g'`,"
    fi
    program_transform_set=yes
    next_arg=
    ;;
  --site) # CYGNUS LOCAL
    site=${arg}
    next_arg=
    ;;
  --x-*)
    next_arg=
    ;;
  *)
    case $arg in
     -srcdir | --srcdir | --srcdi | --srcd | --src | --sr | --s)
	next_arg=--srcdir
	;;
     -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=* | --s=*)
	srcdir=`echo $arg | sed 's/-*s[a-z]*=//'`
	;;
     -host | --host | --hos | --ho | --h)
	next_arg=--host
	;;
     -host=* | --host=* | --hos=* | --ho=* | --h=*)
	host=`echo $arg | sed 's/-*h[a-z]*=//'`
	;; 
     -target | --target | --targe | --targ | --tar | --ta | --t)
	next_arg=--target
	;;
     -target=* | --target=* | --targe=* | --targ=* | --tar=* | --ta=* | --t=*)
	target=`echo $arg | sed 's/-*t[a-z]*=//'`
	;; 
     -build | --build | --buil | --bui | --bu | --b)
	next_arg=--build
	;;
     -build=* | --build=* | --buil=* | --bui=* | --bu=* | --b=*)
	build=`echo $arg | sed 's/-*b[a-z]*=//'`
	;; 
     -prefix | --prefix | --prefi | --pref | --pre | --pr | --p)
	next_arg=--prefix
	;;
     -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
	prefix=`echo $arg | sed 's/-*p[a-z]*=//'`
	;;
     -local-prefix | --local-prefix | --local-prefi | --local-pref | --local-pre \
	| --local-pr | --local-p | --local- | --local | --loc | --lo | --l)
	next_arg=--local-prefix
	;;
     -local-prefix=* | --local-prefix=* | --local-prefi=* | --local-pref=* \
	| --local-pre=* | --local-pr=* | --local-p=* | --local-=* | --local=* \
	| --loc=* | --lo=* | --l=*)
	local_prefix=`echo $arg | sed 's/-*l[-a-z]*=//'`
	;;
     -exec-prefix | --exec-prefix | --exec-prefi | --exec-pref | --exec-pre \
	| --exec-pr | --exec-p | --exec- | --exec | --exe | --ex | --e)
	next_arg=--exec-prefix
	;;
     -exec-prefix=* | --exec-prefix=* | --exec-prefi=* | --exec-pref=* \
	| --exec-pre=* | --exec-pr=* | --exec-p=* | --exec-=* | --exec=* \
	| --exe=* | --ex=* | --e=*)
	exec_prefix=`echo $arg | sed 's/-*e[-a-z]*=//'`
	;;
     -program-transform-name | --program-transform-name \
	| --program-transform-nam | --program-transform-na \
	| --program-transform-n | --program-transform- | --program-transform \
	| --program-transfor | --program-transfo | --program-transf \
	| --program-trans | --program-tran | --program-tra \
	| --program-tr | --program-t)
	next_arg=--program-transform-name
	# CYGNUS LOCAL
	;;
     -program-transform-name=* | --program-transform-name=* \
	| --program-transform-nam=* | --program-transform-na=* \
	| --program-transform-n=* | --program-transform-=* \
	| --program-transform=* | --program-transfor=* | --program-transfo=* \
	| --program-transf=* | --program-trans=* | --program-tran=* \
	| --program-tra=* | --program-tr=* | --program-t=*)
	# CYGNUS LOCAL
	arg=`echo ${arg} | sed -e 's/^[-a-z_]*=//'`
	# Double any \ or $ in the argument.
	if [ -n "${arg}" ] ; then
	  program_transform_name="${program_transform_name} -e `echo ${arg} | sed -e 's/\\\\/\\\\\\\\/g' -e 's/\\\$/$$/g'`"
	fi
	program_transform_set=yes
	;;
     -program-prefix | --program-prefix | --program-prefi \
	| --program-pref | --program-pre | --program-pr \
	| --program-p)
	next_arg=--program-prefix
	# CYGNUS LOCAL
	;;
     -program-prefix=* | --program-prefix=* | --program-prefi=* \
	| --program-pref=* | --program-pre=* | --program-pr=* \
	| --program-p=*)
	# CYGNUS LOCAL
	arg=`echo ${arg} | sed -e 's/^[-a-z_]*=//'`
	if [ -n "${arg}" ]; then
	  program_transform_name="${program_transform_name} -e s,^,`echo ${arg} | sed -e 's/\\\\/\\\\\\\\/g' -e 's/\\\$/$$/g'`,"
	fi
	program_transform_set=yes
	;;
     -program-suffix | --program-suffix | --program-suffi \
	| --program-suff | --program-suf | --program-su \
	| --program-s)
	next_arg=--program-suffix
	# CYGNUS LOCAL
	;;
     -program-suffix=* | --program-suffix=* | --program-suffi=* \
	| --program-suff=* | --program-suf=* | --program-su=* \
	| --program-s=*)
	# CYGNUS LOCAL
	arg=`echo ${arg} | sed -e 's/^[-a-z_]*=//'`
	if [ -n "${arg}" ]; then
	  program_transform_name="${program_transform_name} -e s,\$\$,`echo ${arg} | sed -e 's/\\\\/\\\\\\\\/g' -e 's/\\\$/$$/g'`,"
	fi
	program_transform_set=yes
	;;
     -site | --site | --sit) # CYGNUS LOCAL
	next_arg=--site
	;;
     -site=* | --site=* | --sit=* | --si=*) # CYGNUS LOCAL
	site=`echo ${arg} | sed 's/^[-a-z]*=//'`
	;;
     -with-gnu-ld | --with-gnu-ld | --with-gnu-l)
	gnu_ld=yes
	;;
     -gas | --gas | --ga | --g | -with-gnu-as | --with-gnu-as | -with-gnu-a)
        gas=yes
	;;
     -nfp | --nfp | --nf | --n)
	nfp=yes
	;;
     -with-stabs | -with-stab | -with-sta | -with-st | -with-s \
	| --with-stabs | --with-stab | --with-sta | --with-st | --with-s \
	| -stabs | -stab | -sta | -st  \
	| --stabs | --stab | --sta | --st)
	stabs=yes
	;;
     -with-elf | -with-el | -with-se \
	| --with-elf | --with-el | --with-e \
	| -elf | -el | -e \
	|--elf | --el | --e)
	elf=yes
	;;
     -with-* | --with-*) ;; #ignored
     -without-* | --without-*) ;; #ignored
     -enable-* | --enable-*) ;; #ignored
     -x | --x) ;; # ignored
     -x-*=* | --x-*=*) ;; # ignored
     -x-* | --x-*)
	next_arg=--x-ignored # ignored
	;;
     --he*) ;; # ignored for now (--help)
     --vers*) ;; # ignored for now (--version)
     -v | -verb* | --verb*) ;; # ignored for now (--verbose)
     --program-*) ;; #ignored (--program-prefix, --program-suffix)
     --c*) ;; #ignored (--cache-file)
     --q*) ;; #ignored (--quiet)
     --si*) ;; #ignored (--silent)
     -*)
	echo "Invalid option \`$arg'" 1>&2
	exit 1
	;;
     *)
# Allow configure HOST TARGET
	if [ x$host = x ]
	then
		host=$target
	fi
	target=$arg
	;;
    esac
  esac
done

# Find the source files, if location was not specified.
if [ x$srcdir = x ]
then
	srcdirdefaulted=1
	srcdir=.
	if [ ! -r tree.c ]
	then
		srcdir=..
	fi
fi

if [ ! -r ${srcdir}/grant.c ]
then
	if [ x$srcdirdefaulted = x ]
	then
	  echo "$progname: Can't find CHILL frontend sources in \`${srcdir}'" 1>&2
	else
	  echo "$progname: Can't find CHILL frontend sources in \`.' or \`..'" 1>&2
	fi
	exit 1
fi

# Make sure that scripts are executable
[ -w ${srcdir} -a -f ${srcdir}/regression.sh   ] && \
  chmod +x ${srcdir}/regression.sh 
[ -w ${srcdir} -a -f ${srcdir}/regression.prpt ] && \
  chmod +x ${srcdir}/regression.prpt
[ -w ${srcdir} -a -f ${srcdir}/regression.awk3 ] && \
  chmod +x ${srcdir}/regression.awk3

if [ -r ${srcdir}/config.status ] && [ x$srcdir != x. ]
then
	echo "$progname: \`configure' has been run in \`${srcdir}'" 1>&2
	exit 1
fi

host_xmake_file=
host_truncate_target=

# Complain if an arg is missing
if [ x$build = x ]
then
	# If host was specified, always use it for build also to avoid
	# confusion.  If someone wants a cross compiler where build != host,
	# then they must specify build explicitly.  Since this case is
	# extremely rare, it does not matter that it is slightly inconvenient.
	if [ x$host != x ]
	then
		build=$host
	
	# This way of testing the result of a command substitution is
	# defined by Posix.2 (section 3.9.1) as well as traditional shells.
	elif build=`${srcdir}/../config.guess`
	then
		echo "This appears to be a ${build} system." 1>&2

	elif [ x$target != x ]
	then
		echo 'Config.guess failed to determine the host type.  Defaulting to target.'
		build=$target
	else
		echo 'Config.guess failed to determine the host type.  You need to specify one.' 1>&2
		echo "\
Usage: `basename $progname` [--host=HOST] [--build=BUILD]
       [--prefix=DIR] [--gxx-include-dir=DIR] [--local-pref=DIR] [--exec-pref=DIR]
       [--with-gnu-as] [--with-gnu-ld] [--with-stabs] [--with-elf] [--nfp] TARGET" 1>&2
	echo "Where HOST, TARGET and BUILD are three-part configuration names " 1>&2
		if [ -r config.status ]
		then
			tail +2 config.status 1>&2
		fi
		exit 1
	fi
fi

# If $host was not specified, use $build.
if [ x$host = x ]
then
	host=$build
fi

# If $target was not specified, use $host.
if [ x$target = x ]
then
	target=$host
fi

# Validate the specs, and canonicalize them.
canon_build=`/bin/sh $srcdir/../config.sub $build` || exit 1
canon_host=`/bin/sh $srcdir/../config.sub $host` || exit 1
canon_target=`/bin/sh $srcdir/../config.sub $target` || exit 1

rm -f config.bak
if [ -f config.status ]; then mv -f config.status config.bak; fi

#
# For the current directory and all of the designated SUBDIRS,
# do the rest of the script...
#
if [ ! -d testsuite ] ; then mkdir testsuite; fi
_SUBDIRS=
for d in $SUBDIRS; do
	[ -d $srcdir/$d ] && _SUBDIRS="$_SUBDIRS $d"
done

savesrcdir=$srcdir
STARTDIR=`pwd`

for subdir in $_SUBDIRS
do
	tmake_file=
	host_xmake_file=
	oldsrcdir=$savesrcdir

	# ${invsubdir} is inverse of ${subdir), *with* trailing /, if needed.
	invsubdir=`echo ${subdir}/ | sed -e 's|\./||g' -e 's|[^/]*/|../|g'`

	# Re-adjust the path
	# Also create a .gdbinit file which runs the one in srcdir
	# and tells GDB to look there for source files.

	case $oldsrcdir in
	".") srcdir=. ;;
	/*) # absolute path
               	srcdir=${oldsrcdir}/${subdir}  ;;
        *) # otherwise relative
		srcdir=${invsubdir}${oldsrcdir}/${subdir} ;;
	esac

        if [ -r ${oldsrcdir}/${subdir}/.gdbinit -a ${oldsrcdir} != "." ] ; then
		cat > ${subdir}/.gdbinit <<EOF
dir .
dir ${srcdir}
source ${srcdir}/.gdbinit
EOF
	fi

	case $oldsrcdir in
	/*)	;;
	*)	oldsrcdir=${invsubdir}${oldsrcdir} ;;
	esac
	mainsrcdir=${oldsrcdir}/..
	test -d $subdir || mkdir $subdir
	cd $subdir
	#
	# Create Makefile.tem from Makefile.in.
	# Make it set VPATH if necessary so that the sources are found.
	# Also change its value of srcdir.
	rm -f Makefile.tem
	echo "VPATH = ${srcdir}" \
	  | cat - ${srcdir}/Makefile.in \
	  | sed "s@^srcdir = \.@srcdir = ${srcdir}@" > Makefile.tem

	# Conditionalize the makefile for this host machine.
	if [ -f ${mainsrcdir}/config/${host_xmake_file} ]
	then
		rm -f Makefile.xx
		sed -e "/####host/  r ${mainsrcdir}/config/${host_xmake_file}" Makefile.tem > Makefile.xx
		echo "Merged ${host_xmake_file}."
		rm -f Makefile.tem
		mv Makefile.xx Makefile.tem
	else
	# Say in the makefile that there is no host_xmake_file,
	# by using a name which (when interpreted relative to $srcdir/config)
	# will duplicate another dependency: $srcdir/Makefile.in.
		host_xmake_file=../Makefile.in
	fi
	
	# Define variables host_canonical, build_canonical, and target_canonical
	# because some Cygnus local changes in the Makefile depend on them.
	echo host_canonical = ${canon_host} > Makefile.xx
	echo target_canonical = ${canon_target} >> Makefile.xx
	echo build_canonical = ${canon_build} >> Makefile.xx
	cat Makefile.tem >> Makefile.xx
	mv Makefile.xx Makefile.tem
	
	# Conditionalize the makefile for this target machine.
	if [ -f ${mainsrcdir}/config/${tmake_file} ]
	then
		rm -f Makefile.xx
		sed -e "/####target/  r ${mainsrcdir}/config/${tmake_file}" Makefile.tem > Makefile.xx
		echo "Merged ${tmake_file}."
		rm -f Makefile.tem
		mv Makefile.xx Makefile.tem
	else
	# Say in the makefile that there is no tmake_file,
	# by using a name which (when interpreted relative to $srcdir/config)
	# will duplicate another dependency: $srcdir/Makefile.in.
		tmake_file=../Makefile.in
	fi
	
	# CYGNUS LOCAL
	# Conditionalize the makefile for this site.
	if [ -f ${mainsrcdir}/config/ms-${site} ]
	then
		rm -f Makefile.xx
		sed -e "/####site/  r ${mainsrcdir}/config/ms-${site}" Makefile.tem > Makefile.xx
		echo "Merged ms-${site}."
		rm -f Makefile.tem
		mv Makefile.xx Makefile.tem
	fi
	
	# CYGNUS LOCAL
	# If this is a cross compilation, and we have newlib in the build
	# tree, then define inhibit_libc in LIBGCC2_CFLAGS.  This will cause
	# __eprintf to be left out of libgcc.a, but that's OK because newlib
	# has its own version of assert.h.
	if [ x$host != x$target ]; then
	  sed -e 's/^\(LIBGCC2_CFLAGS[ 	]*=[ 	]*\)/\1-Dinhibit_libc /' Makefile.tem > Makefile.tem2
	  rm -f Makefile.tem
	  mv Makefile.tem2 Makefile.tem
	fi
	
	# Remove all formfeeds, since some Makes get confused by them.
	# Also arrange to give the variables `target', `host_xmake_file',
	# `tmake_file', `prefix', `local_prefix', `exec_prefix', `FIXINCLUDES'
	# and `INSTALL_HEADERS_DIR' values in the Makefile from the values
	# they have in this script.
	# CYGNUS LOCAL: FLOAT_H, CROSS_FLOAT_H, objdir
	rm -f Makefile.xx
	sed -e "s///" -e "s/^target=.*$/target=${target}/" \
	    -e "s|^xmake_file=.*$|xmake_file=${host_xmake_file}|" \
	    -e "s|^tmake_file=.*$|tmake_file=${tmake_file}|" \
	    -e "s|^version=.*$|version=${version}|" \
	    -e "s|^prefix[ 	]*=.*|prefix = $prefix|" \
	    -e "s|^local_prefix[ 	]*=.*|local_prefix = $local_prefix|" \
	    -e "s|^exec_prefix[ 	]*=.*|exec_prefix = $exec_prefix|" \
	    -e "s|^objdir[ 	]*=.*|objdir=`pwd`|" \
	    Makefile.tem > Makefile.xx
	rm -f Makefile.tem
	mv Makefile.xx Makefile.tem
	
	# Install Makefile for real, after making final changes.
	# Define macro CROSS_COMPILE in compilation if this is a cross-compiler.
	# Also use all.cross instead of all.internal, and add cross-make to Makefile.
	if [ x$canon_host = x$canon_target ]
	then
		rm -f Makefile
	  	if [ x$canon_host = x$canon_build ]
		then
			mv Makefile.tem Makefile
		else
	#		When building gcc with a cross-compiler, we need to fix a
	#		few things.
			echo "build= $build" > Makefile
			sed -e "/####build/  r ${mainsrcdir}/build-make" Makefile.tem >> Makefile
			rm -f Makefile.tem Makefile.xx
		fi
	else
		rm -f Makefile
		echo "CROSS=-DCROSS_COMPILE" > Makefile
		sed -e "/####cross/  r ${mainsrcdir}/cross-make" Makefile.tem >> Makefile
		rm -f Makefile.tem Makefile.xx
	fi
	
	echo "Created \`$subdir/Makefile'."
	
	if [ xx${vint} != xx ]
	then
		vintmsg=" (vint)"
	fi
	
	# Describe the chosen configuration in config.status.
	# Make that file a shellscript which will reestablish the same configuration.

	rm -f config.bak
	if [ -f config.status ]; then mv -f config.status config.bak; fi

	echo "#!/bin/sh
	# This directory was configured as follows:
cd $invsubdir; ${progname}" $arguments > config.new
	echo echo host=$canon_host target=$canon_target build=$canon_build >> config.new
	chmod a+x config.new

	# If we aren't executing the configure script in .
	if [ x$subdir != x. ]
	then
		if [ -f $srcdir/configure ]
		then
			echo "Running \`${CONFIG_SHELL-sh} $srcdir/configure $arguments\'"
			${CONFIG_SHELL-sh} $srcdir/configure $arguments
			echo "${srcdir}/configure" $arguments >> config.new
			echo echo host=$canon_host target=$canon_target build=$canon_build >> config.new
		fi
	fi

	if [ -f config.bak ] && cmp config.bak config.new >/dev/null 2>/dev/null;
	then
		mv -f config.bak config.status
		rm -f config.new
	else
		mv -f config.new config.status
		rm -f config.bak
	fi

	cd $STARTDIR
done   # end of current-dir SUBDIRS loop
	
srcdir=$savesrcdir

# Describe the chosen configuration in config.status.
# Make that file a shellscript which will reestablish the same configuration.
echo "#!/bin/sh
# This directory was configured as follows:
${progname}" $arguments > config.new
echo echo host=$canon_host target=$canon_target build=$canon_build >> config.new
chmod a+x config.new
if [ -f config.bak ] && cmp config.bak config.new >/dev/null 2>/dev/null;
then
	mv -f config.bak config.status
	rm -f config.new
else
	mv -f config.new config.status
	rm -f config.bak
fi

exit 0
