#!/bin/sh
# Configuration script for GNU GAS
#   Copyright (C) 1988, 1990, 1991 Free Software Foundation, Inc.

#This file is not yet part of GNU GAS.

#GNU GAS 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 1, or (at your option)
#any later version.

#GNU GAS 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 GAS; see the file COPYING.  If not, write to
#the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

# $Id$

#
# Shell script to create proper links to machine-dependent files in
# preparation for compiling gas.
#
# Usage: configure [-srcdir=DIR] [-host=HOST] TARGET
#
# If configure succeeds, it leaves its status in config.status.
# If configure fails after disturbing the status quo, 
# 	config.status is removed.
#

progname=$0

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

host=
target=

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

for arg in $*;
do
  case $arg in
   -srcdir=* | +srcdir=* | +srcdi=* | +srcd=* | +src=* | +sr=* | +s=*)
	srcdir=`echo $arg | sed 's/[+-]s[a-z]*=//'`
	;;
   -host=* | +host=* | +hos=* | +ho=* | +h=*)
	host=`echo $arg | sed 's/[+-]h[a-z]*=//'`
	;; 
   *)
# Allow configure HOST TARGET
	if [ x$host = x ]
	then
		host=$target
	fi
	target=$arg
	;;
  esac
done

# Complain if an arg is missing
if [ x$target = x ]
then
	echo "Usage: $progname [+srcdir=DIR] [+host=HOST] TARGET"
	echo -n "Where HOST and TARGET are something like "
	echo "\`vax', \`sun3', \`encore', etc."
	if [ -r config.status ]
	then
		cat config.status
	fi
	exit 1
fi

# Default other arg
if [ x$host = x ]
then
	host=$target
fi

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

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

# Decode the host machine, then the target machine.
# For the host machine, we save the ho variable as host_ho;
# then we decode the target machine and forget everything else
# that came from the host machine.
for machine in $host $target; do
	tc=
	obj=

	host_header=
	obj_header=
	obj_source=
	te_header=
	tc_header=
	tc_source=

	make_var_file=

	case $machine in
	generic)
		;;
	i860)
		tc=i860
		obj=aout
		;;
	pmax | dec3100)
		ho=pmax
		;;
	sun386)
		ho=sun386
		;;
	sun4 | sun4-aout | sun-4 | sun4-os4 | sun-4-os4)
		ho=sun4
		tc=sparc
		obj=aout
		;;
	sun4-bout)
		tc=sparc
		obj=bout
		;;
	sun4-bfd-sunos)
		tc=sparc
		obj=bfd-sunos
		;;
	i960 | i960-coff)
		tc=i960
		obj=coff
		te=ic960
		;;
	i960-bout)
		tc=i960
		obj=bout
		;;
	i960-aout)
		tc=i960
		obj=aout
		;;
	sun3 | sun3-aout)
		ho=sun3
		tc=m68k
		obj=aout
		te=sun3
		;;
	a29k | a29k-aout)
		tc=a29k
		obj=aout
		;;
	a29k-coff)
		tc=a29k
		obj=coff
		;;
	i386)
		tc=i386
		obj=aout
		;;
	ns32k)
		tc=ns32k
		obj=aout
		;;
	vax)
		tc=vax
		obj=aout
		;;
	rs6000)
		;;
	esac
	if [ x$pass1done = x ]
	then
		if [ x$ho = x ]; then ho=$host; fi
		if [ x$ho_header = x ]; then ho_ho_header=ho-$ho.h
		else ho_ho_header=$ho_header
		fi
		if [ x$make_var_file = x ]
		then make_var_file=make-$ho; fi
		ho_make_var_file=$make_var_file
		pass1done=yes
	else
		host_make_var_file=$ho_make_var_file
		ho_header=$ho_ho_header
	fi
done


# Default the machine-specific variables that were not explicitly set.
if [ x$te = x ]
then te=generic; fi

if [ x$te_header = x ]
then te_header=te-$te.h; fi


if [ x$tc = x ]
then tc=generic; fi

if [ x$tc_header = x ]
then tc_header=tc-$tc.h; fi

if [ x$tc_source = x ]
then tc_source=tc-$tc.c; fi


if [ x$obj = x ]
then obj=generic; fi

if [ x$obj_header = x ]
then obj_header=obj-$obj.h; fi

if [ x$obj_source = x ]
then obj_source=obj-$obj.c; fi


if [ x$atof_source = x ]
then atof_source=atof-ieee.c; fi

# Set up the list of links to be made.
# $links is the list of link names, and $files is the list of names to link to.
files="$ho_header $te_header $tc_header $tc_source $obj_header $obj_source $atof_source"
links="host.h targ-env.h targ-cpu.h targ-cpu.c obj-format.h obj-format.c atof-targ.c"

# Make the links.
while [ -n "$files" ]
do
	# set file to car of files, files to cdr of files
	set $files; file=$1; shift; files=$*
	set $links; link=$1; shift; links=$*

	if [ ! -r ${srcdir}/config/$file ]
	then
		echo "$progname: cannot create a link \`$link'," 1>&2
		echo "since the file \`config/$file' does not exist." 1>&2
		exit 1
	fi

	$remove -f $link
	rm -f config.status
	# Make a symlink if possible, otherwise try a hard link
	$symbolic_link ${srcdir}/config/$file $link 2>/dev/null || $hard_link ${srcdir}/config/$file $link

	if [ ! -r $link ]
	then
		echo "$progname: unable to link \`$link' to \`${srcdir}/config/$file'." 1>&2
		exit 1
	fi
	echo "Linked \`$link' to \`${srcdir}/config/$file'."
done

# Build a Makefile

# Install a makefile, and make it set srcdir
# if necessary so that the sources are found.
# Also change its value of srcdir.
# Also create a .gdbinit file which runs the one in srcdir
# and tells GDB to look there for source files.
case $srcdir in
.)
	;;
*)
	echo "VPATH = ${srcdir}" > x
	cat x ${srcdir}/Makefile.in | sed "s@^srcdir = \.@srcdir = ${srcdir}@" > Makefile.in
	rm x
	echo "dir ." > .gdbinit
	echo "dir ${srcdir}" >> .gdbinit
	echo "source ${srcdir}/.gdbinit" >> .gdbinit
	;;
esac

# Conditionalize the makefile for this machine.
if [ -f ${srcdir}/config/${host_make_var_file} ]
then
	sed -e "/####/  r ${srcdir}/config/${host_make_var_file}" Makefile.in > Makefile.tem
else
	cp Makefile.in Makefile.tem
fi

# Remove all formfeeds, since some Makes get confused by them.
sed "s///" Makefile.tem > Makefile.tem1

# Delete the intermediate files
rm Makefile.tem
if [ x$srcdir != x. ]
then
	rm Makefile.in
fi


# actual Makefile starts here.

echo "host = $host" > Makefile
echo "target = $target" >> Makefile

# Define macro CROSS_ASSEMBLE in compilation if this is a cross-assembler.
if [ x$host = x$target ]
then
	sed "/^ALL=gas/s//ALL=bootstrap/" < Makefile.tem1 >> Makefile
else
	echo "CROSS=-DCROSS_ASSEMBLE" >> Makefile
	cat Makefile.tem1 >> Makefile
fi

rm Makefile.tem1

echo "Edited the makefile"

if [ x$host = x$target ]
then
	echo "Links are now set up for use with a $target." \
		| tee config.status
else
	echo "Links are now set up for host $host and target $target." \
		| tee config.status
fi

exit 0
