Cleanup MIPS preconfigure script
diff mbox

Message ID 6b5ce132-2a69-4ee9-aaa5-d07792e72b50@BAMAIL02.ba.imgtec.org
State New
Headers show

Commit Message

Steve Ellcey Sept. 4, 2014, 8:58 p.m. UTC
I would like to simplify the MIPS preconfigure script.  Right now it looks
at the GCC flags and tries to determine what it should set 'machine' to based
on those flags (and os_config).  I would like to run the compiler and check
what macros are set and use that to set the machine variable instead.

This seems safer since right now a platform that generated the n64 ABI by
default (as an example) might not work correctly because the '-mabi=64' flag
would not need to appear in $CFLAGS to generate n64 code.

Other platforms like arm, m68k, powerpc, and tile are already using this
technique of calling the compiler and checking macros and the macros I am
checking on MIPS (_MIPS_SIM, _MIPS_ISA, and __mips16) are already used in
various glibc source files.

Note that I removed the reference to kern64 since it no longer exists
and I removed the addition of $machine from the end of the 'machine='
line for mips32 as there did not seem to be any reason for it.

Tested with mips-mti-linux-gnu and mips-linux-gnu and mips64-linux-gnu
builds.

OK to checkin?

Steve Ellcey
sellcey@mips.com


2014-09-04  Steve Ellcey  <sellcey@mips.com>

	* sysdeps/mips/preconfigure: Modify ABI tests.

Comments

Maciej W. Rozycki Sept. 4, 2014, 10:29 p.m. UTC | #1
On Thu, 4 Sep 2014, Steve Ellcey  wrote:

> diff --git a/sysdeps/mips/preconfigure b/sysdeps/mips/preconfigure
> index b215eb2..df958d6 100644
> --- a/sysdeps/mips/preconfigure
> +++ b/sysdeps/mips/preconfigure
> @@ -1,34 +1,25 @@
> -case "$machine" in
> -mips64*)	base_machine=mips64
> -		case "$CC $CFLAGS $CPPFLAGS " in
> -		*" -mabi=n32 "*) mips_cc_abi=n32 ;;
> -		*" -mabi=64 "*|*" -mabi=n64 "*) mips_cc_abi=64 ;;
> -		*" -mabi=32 "*|*" -mabi=o32 "*) mips_cc_abi=32 ;;
> -		*) mips_cc_abi=default ;;
> -		esac
> -		case $config_os in
> -		*abin32*) mips_config_abi=n32 ;;
> -		*abi64*|*abin64*) mips_config_abi=64 ;;
> -		*abi32*|*abio32*) mips_config_abi=32 ;;
> -		*) mips_config_abi=$mips_cc_abi ;;
> -		esac
> -		case $mips_config_abi in
> -		default) machine=mips/mips64/n32 mips_config_abi=n32 ;;
> -		n32) machine=mips/mips64/n32 ;;
> -		64) machine=mips/mips64/n64 ;;
> -		32) machine=mips/mips32/kern64 ;;
> -		esac
> -		machine=$machine/$config_machine
> -		if test $mips_config_abi != $mips_cc_abi; then
> -		  # This won't make it to config.make, but we want to
> -		  # set this in case configure tests depend on it.
> -		  CPPFLAGS="$CPPFLAGS -mabi=$mips_config_abi"
> -		fi
> -		;;
> -mips*)		base_machine=mips
> -		case "$CC $CFLAGS $CPPFLAGS " in
> -		*" -mips16 "*) machine=mips/mips32/mips16/$machine ;;
> -		*) machine=mips/mips32/$machine ;;
> -		esac
> -		;;
> -esac
> +
> +abiflag=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define _MIPS_SIM \(.*\)/\1/p'`
> +isaflag=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define _MIPS_ISA \(.*\)/\1/p'`
> +mips16flag=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define __mips16 \(.*\)/\1/p'`
> +
> +if  test "$isaflag" = "_MIPS_ISA_MIPS64"; then
> +	base_machine=mips64
> +	if test "$abiflag" = "_ABIO32" ; then
> +		machine=mips/mips32
> +	elif test "$abiflag" = "_ABIN32" ; then
> +		machine=mips/mips64/n32
> +	elif test "$abiflag" = "_ABI64" ; then
> +		machine=mips/mips64/n64
> +	else
> +		as_fn_error $? "Unable to determine ABI." "$LINENO" 5
> +	fi
> +else
> +	base_machine=mips
> +	if test "$mips16flag" = "1" ; then
> +		machine=mips/mips32/mips16
> +	else
> +		machine=mips/mips32
> +	fi
> +fi
> +machine=$machine/$config_machine

 Hmm, that's quite a change in the interpretation of host triplets, but 
I'm leaning towards finding it acceptable, I think we have sufficient 
means in GCC nowadays to control the default ABI so that we don't have to 
rely on suffixes in the triplets.  I wonder if a sanity check wouldn't be 
good to have though, such as rejecting mips64* with the compiler set to 
the 32-bit ABI to trap accidental silly use.

 Your change has a flaw though, you can't rely on _MIPS_ISA being set 
exactly to _MIPS_ISA_MIPS64 on determining if you want a 64-bit or a 
32-bit configuration, there are other 64-bit ISAs, starting from 
_MIPS_ISA_MIPS3, e.g. I have an n64 MIPS III compiler.  I think you can 
just skip this check altogether, GCC will have set the ABI and the ISA 
consistently already and you can merge the two legs of this conditional 
into one.  You may just sanity-check that a 64-bit ABI is not used 
together with the MIPS16 option as we have no 64-bit MIPS16 PIC support.

 Thanks for thinking of cleaning this piece up.

  Maciej
Steve Ellcey Sept. 4, 2014, 10:52 p.m. UTC | #2
On Thu, 2014-09-04 at 23:29 +0100, Maciej W. Rozycki wrote:

>  Hmm, that's quite a change in the interpretation of host triplets, but 
> I'm leaning towards finding it acceptable, I think we have sufficient 
> means in GCC nowadays to control the default ABI so that we don't have to 
> rely on suffixes in the triplets.  I wonder if a sanity check wouldn't be 
> good to have though, such as rejecting mips64* with the compiler set to 
> the 32-bit ABI to trap accidental silly use.

Someone might want to use a 32 bit ABI on mips64 though, how about a
warning instead of an error for that case?

>  Your change has a flaw though, you can't rely on _MIPS_ISA being set 
> exactly to _MIPS_ISA_MIPS64 on determining if you want a 64-bit or a 
> 32-bit configuration, there are other 64-bit ISAs, starting from 
> _MIPS_ISA_MIPS3, e.g. I have an n64 MIPS III compiler.  I think you can 
> just skip this check altogether, GCC will have set the ABI and the ISA 
> consistently already and you can merge the two legs of this conditional 
> into one.  You may just sanity-check that a 64-bit ABI is not used 
> together with the MIPS16 option as we have no 64-bit MIPS16 PIC support.

I like the idea of not using _MIPS_ISA.  The other difference between
the 32 and 64 branches is setting 'base_machine' and it doesn't look to
me that we use base_machine anywhere.  The only use of it anywhere in
glibc that I see is in the x86_64 preconfigure script.  I agree that
using mips16 with n32 or n64 should be an error.  I will update the
patch.

Steve Ellcey
Maciej W. Rozycki Sept. 4, 2014, 11:23 p.m. UTC | #3
On Thu, 4 Sep 2014, Steve Ellcey wrote:

> >  Hmm, that's quite a change in the interpretation of host triplets, but 
> > I'm leaning towards finding it acceptable, I think we have sufficient 
> > means in GCC nowadays to control the default ABI so that we don't have to 
> > rely on suffixes in the triplets.  I wonder if a sanity check wouldn't be 
> > good to have though, such as rejecting mips64* with the compiler set to 
> > the 32-bit ABI to trap accidental silly use.
> 
> Someone might want to use a 32 bit ABI on mips64 though, how about a
> warning instead of an error for that case?

 Hmm, I never thought of such use and I keep considering it weird, but 
having looked through the current script again we already permit e.g. 
`mips64-linux-gnuabi32' to select o32, so I take my concern back, no need 
for any checks here.

> >  Your change has a flaw though, you can't rely on _MIPS_ISA being set 
> > exactly to _MIPS_ISA_MIPS64 on determining if you want a 64-bit or a 
> > 32-bit configuration, there are other 64-bit ISAs, starting from 
> > _MIPS_ISA_MIPS3, e.g. I have an n64 MIPS III compiler.  I think you can 
> > just skip this check altogether, GCC will have set the ABI and the ISA 
> > consistently already and you can merge the two legs of this conditional 
> > into one.  You may just sanity-check that a 64-bit ABI is not used 
> > together with the MIPS16 option as we have no 64-bit MIPS16 PIC support.
> 
> I like the idea of not using _MIPS_ISA.  The other difference between
> the 32 and 64 branches is setting 'base_machine' and it doesn't look to
> me that we use base_machine anywhere.  The only use of it anywhere in
> glibc that I see is in the x86_64 preconfigure script.  I agree that
> using mips16 with n32 or n64 should be an error.  I will update the
> patch.

 Good.  Frankly (with the observation I made above) I think there should 
be no difference between configuring for `mips-linux-gnu' and 
`mips64-linux-gnuabi32'.

 I think the current arrangement has its historical reasons, there was a 
time when the Linux kernel had separate `mips' and `mips64' ports -- 
arch/mips and arch/mips64 as well as include/asm-mips and 
include/asm-mips64 for code and headers respectively (now asm headers live 
under arch/ too).  For glibc it was the latter that mattered as kernel 
headers are needed in our build process and had to be fetched from the 
correct place, with the usual default being /usr/src/linux/include/linux 
and /usr/src/linux/include/asm-<cpu>.  Hence the distinction and maybe the 
presence of `kern64'.  The `mips64' port supported all the three ABIs so 
any of them could have been selected to be built for.

 Of course all it does not matter anymore, the MIPS port of Linux was the 
first to merge its 32-bit and its 64-bit variant and other ports followed, 
e.g. `sparc' and `sparc64'.

  Maciej

Patch
diff mbox

diff --git a/sysdeps/mips/preconfigure b/sysdeps/mips/preconfigure
index b215eb2..df958d6 100644
--- a/sysdeps/mips/preconfigure
+++ b/sysdeps/mips/preconfigure
@@ -1,34 +1,25 @@ 
-case "$machine" in
-mips64*)	base_machine=mips64
-		case "$CC $CFLAGS $CPPFLAGS " in
-		*" -mabi=n32 "*) mips_cc_abi=n32 ;;
-		*" -mabi=64 "*|*" -mabi=n64 "*) mips_cc_abi=64 ;;
-		*" -mabi=32 "*|*" -mabi=o32 "*) mips_cc_abi=32 ;;
-		*) mips_cc_abi=default ;;
-		esac
-		case $config_os in
-		*abin32*) mips_config_abi=n32 ;;
-		*abi64*|*abin64*) mips_config_abi=64 ;;
-		*abi32*|*abio32*) mips_config_abi=32 ;;
-		*) mips_config_abi=$mips_cc_abi ;;
-		esac
-		case $mips_config_abi in
-		default) machine=mips/mips64/n32 mips_config_abi=n32 ;;
-		n32) machine=mips/mips64/n32 ;;
-		64) machine=mips/mips64/n64 ;;
-		32) machine=mips/mips32/kern64 ;;
-		esac
-		machine=$machine/$config_machine
-		if test $mips_config_abi != $mips_cc_abi; then
-		  # This won't make it to config.make, but we want to
-		  # set this in case configure tests depend on it.
-		  CPPFLAGS="$CPPFLAGS -mabi=$mips_config_abi"
-		fi
-		;;
-mips*)		base_machine=mips
-		case "$CC $CFLAGS $CPPFLAGS " in
-		*" -mips16 "*) machine=mips/mips32/mips16/$machine ;;
-		*) machine=mips/mips32/$machine ;;
-		esac
-		;;
-esac
+
+abiflag=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define _MIPS_SIM \(.*\)/\1/p'`
+isaflag=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define _MIPS_ISA \(.*\)/\1/p'`
+mips16flag=`$CC $CFLAGS $CPPFLAGS -E -dM -xc /dev/null | sed -n 's/^#define __mips16 \(.*\)/\1/p'`
+
+if  test "$isaflag" = "_MIPS_ISA_MIPS64"; then
+	base_machine=mips64
+	if test "$abiflag" = "_ABIO32" ; then
+		machine=mips/mips32
+	elif test "$abiflag" = "_ABIN32" ; then
+		machine=mips/mips64/n32
+	elif test "$abiflag" = "_ABI64" ; then
+		machine=mips/mips64/n64
+	else
+		as_fn_error $? "Unable to determine ABI." "$LINENO" 5
+	fi
+else
+	base_machine=mips
+	if test "$mips16flag" = "1" ; then
+		machine=mips/mips32/mips16
+	else
+		machine=mips/mips32
+	fi
+fi
+machine=$machine/$config_machine