#!/bin/sh
# Configuration script for GNU GPC
#   Copyright (C) 1988, 1990, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.

#
# Shell script to create proper links to machine-dependent files in
# preparation for compiling gpc after you have compiled gcc.
#
Usage="configure [--srcdir=DIR] [--pvsdir=DIR] [--gas] [--nfp] [--gccdir=DIR] [--gccsrc=DIR]"
#
# GPC is always built with the same HOST and TARGET for which GCC was built,
#     so it is not necessary to specify those. 
#	--srcdir = GPC specific sources (default . && ..)
#	--gccsrc = Location of GCC Source files (default . && ..)
#	--gccdir = Location of GCC Object files (default $gccsrc)
#	--pvsdir = Location of Pascal Validation Suite (no default)
#	--prefix = Installation prefix
#	--gas	 = use gas
#	--nfp	 = ????
#
# If configure succeeds, it leaves its status in gpc.status.
# If configure fails after disturbing the status quo, 
# 	gpc.status is removed.
#

progname=$0

srcdir=`echo $progname | sed 's|/[^/]*$||'`
if [ x$srcdir = x$progname ]
then
srcdir=
fi

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

# If user does not give --prefix get the prefixes used with
# the gcc we are loaded with.
#
prefix=

# Assume gcc objects are in this directory
gccdir=.
# And their sources are here, too
gccsrc=.

pvs=0
for arg in $*;
do
  case $arg in
   -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=*)
	prefix=`echo $arg | sed 's/[+-]*p[a-z]*=//'`
	echo "If you use --prefix, the path may differ from installed gcc path"
	;;
   -srcdir=* | --srcdir=* | --srcdi=* | --srcd=* | --src=* | --sr=* | --s=*)
	srcdir=`echo $arg | sed 's/[+-]*s[a-z]*=//'`
	;;
   -pvsdir=* | --pvsdir=* | --pvsdi=* | --pvsd=* | --pvs=* | --pv=*)
	pvsdir=`echo $arg | sed 's/[+-]*p[a-z]*=//'`
	pvs=1
	;;
   -gccdir=* | --gccdir=* | --gccdi=* | --gccd=*)
	gccdir=`echo $arg | sed 's/[+-]*gccd[a-z]*=//'`
	;;
   -gccsrc=* | --gccsrc=* | --gccsr=* | --gccs=*)
	gccsrc=`echo $arg | sed 's/[+-]*gccs[a-z]*=//'`
	;;
   -gas | --gas | --ga | --g)
        gas=yes
	;;
   -nfp | --nfp | --nf | --n)
	nfp=yes
	;;
   *)
	echo Illegal parameter
	echo "Usage: $progname $Usage"
	;;	
  esac
done

# Complain if objects are not found
if [ ! -f $gccdir/tree.o ]
then
	echo "No GCC objects found"
	echo "Usage: $progname $Usage"
	if [ -r gpc.status ]
	then
		cat gpc.status
	fi
	exit 1
fi

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

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

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

if [ ! -r ${gccsrc}/tree.h ]
then
	if [ x$gccsrcdirdefaulted = x ]
	then
	  echo "$progname: Can't find GCC compiler sources in \`${gccsrc}'." 1>&2
	else
	  echo "$progname: Can't find GCC compiler sources in \`.' or \`..'." 1>&2
	fi
	exit 1
fi

TARGET_REPLACE=

if [ -z "$prefix" ]; then
	if [ ! -f ${gccdir}/xgcc ]; then
		echo "Can't run ${gccdir}/{gccdir}/xgcc to find out prefix"
		exit 1
	fi
	temp=`${gccdir}/xgcc -print-libgcc-file-name`
	if [ x$temp = x ]; then
		echo "Can't find out prefix by running ${gccdir}/xgcc. Install gcc first"
		echo "Or use the --prefix switch"
		exit 1
	fi
	version=`${gccdir}/xgcc -dumpversion`
	prefix=`echo $temp | sed 's+\(.*\)/lib/gcc-lib/.*+\1+'`
	if [ x$prefix = xlibgcc.a ]; then
		echo "You need to install GCC before configuring GPC."
		echo "Otherwise I can not find out the correct prefix."
		echo
		echo "You do not need to put this GCC version in public use,"
		echo "but libgcc.a has to be where you have configured it to be"
		echo "when you compiled GCC, so that gccdir/xgcc says where it is."
		exit 1
	fi
	echo "Using prefix $prefix"

	target=`echo $temp | sed 's+'${prefix}'/lib/gcc-lib/\(.*\)/'${version}'/libgcc.a+\1+'`
	echo "Using target $target"

	TARGET_REPLACE="s@^target[ ]*=.*@target = ${target}@"
fi

# Install a makefile, and make it set VPATH
# 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
.)
	cp Makefile.in Makefile.temp
	;;
*)
	echo "VPATH = .:${srcdir}:${gccsrc}" > x
	cat x ${srcdir}/Makefile.in | sed "s@^srcdir = \.@srcdir = ${srcdir}@" > Makefile.temp
	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.temp > Makefile.tem
else
	cp Makefile.temp Makefile.tem
fi

# Remove all formfeeds. Some makes get confused by them.
# Modify the path where to find GCC binaries
cat Makefile.tem | \
 sed	-e "s@^gccdir = \$(gccsrc)@gccdir = ${gccdir}@" \
	-e "s@@@"					\
	-e "s@^prefix[ 	]*=.*@prefix = $prefix@" 	\
	-e "s@^gccsrc = \.@gccsrc = ${gccsrc}@" > Makefile.tem1

if [ -n "$TARGET_REPLACE" ]; then
	cat Makefile.tem1 | sed "$TARGET_REPLACE" > Makefile.tem2
	mv Makefile.tem2 Makefile.tem1
fi

# Delete the intermediate files
rm Makefile.temp Makefile.tem

# Check if cross-compiling
CROSS=
if [ -f $gccdir/config.status ]; then
	CROSS=`grep "and target" $gccdir/config.status`
fi

# Define macro CROSS_COMPILE in compilation if this is a cross-compiler.
if [ x$CROSS = x ]
then
	mv Makefile.tem1 Makefile
else
	echo "CROSS=-DCROSS_COMPILE" > Makefile
	cat Makefile.tem1 >> Makefile
	rm Makefile.tem1
fi

if [ -f ${srcdir}/config/${host_make_var_file} ]
then
	echo "Created \`Makefile' using \`${host_make_var_file}'."
else
	echo "Created \`Makefile'."
fi

# Hmm, find out where the new c-compiler is for the run time system
# (Just to compile it with it, no special reason)
p=`pwd`
cd $gccdir
cdir=`pwd`
cd $p

# Copy run time system Makefile.in
if [ ! -d rts ]; then mkdir rts; fi
cat $srcdir/rts/Makefile.in | \
	sed -e "s@^CC =.*@CC = $cdir/xgcc@" > rts/Makefile

echo "Created run time system Makefile"

# Remove links to the required directories
rm -f srcdir gccdir gccsrc

# Link the source gpc directory for run time system makefile
ln -s $srcdir srcdir
ln -s $gccsrc gccsrc
ln -s $gccdir gccdir
echo "Linked srcdir -> $srcdir and gccdir -> $gccsrc"

# Document current setup
if [ x$CROSS = x ]; then
 echo "# Compiling $srcdir with $gccdir (objects) and $gccsrc (sources)" | tee gpc.status
else
 echo "# Cross-compiling $srcdir with $gccdir (objects) and $gccsrc (sources)" | tee gpc.status
fi

echo "# Configured with the following command:" >> gpc.status
echo "$0 $*" >> gpc.status

exit 0
