acinclude/ax_gcc_archflag.m4
changeset 5029 5d7d48157be7
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/acinclude/ax_gcc_archflag.m4	Tue Jan 18 22:40:45 2011 -0800
     1.3 @@ -0,0 +1,215 @@
     1.4 +# ===========================================================================
     1.5 +#      http://www.gnu.org/software/autoconf-archive/ax_gcc_archflag.html
     1.6 +# ===========================================================================
     1.7 +#
     1.8 +# SYNOPSIS
     1.9 +#
    1.10 +#   AX_GCC_ARCHFLAG([PORTABLE?], [ACTION-SUCCESS], [ACTION-FAILURE])
    1.11 +#
    1.12 +# DESCRIPTION
    1.13 +#
    1.14 +#   This macro tries to guess the "native" arch corresponding to the target
    1.15 +#   architecture for use with gcc's -march=arch or -mtune=arch flags. If
    1.16 +#   found, the cache variable $ax_cv_gcc_archflag is set to this flag and
    1.17 +#   ACTION-SUCCESS is executed; otherwise $ax_cv_gcc_archflag is is set to
    1.18 +#   "unknown" and ACTION-FAILURE is executed. The default ACTION-SUCCESS is
    1.19 +#   to add $ax_cv_gcc_archflag to the end of $CFLAGS.
    1.20 +#
    1.21 +#   PORTABLE? should be either [yes] (default) or [no]. In the former case,
    1.22 +#   the flag is set to -mtune (or equivalent) so that the architecture is
    1.23 +#   only used for tuning, but the instruction set used is still portable. In
    1.24 +#   the latter case, the flag is set to -march (or equivalent) so that
    1.25 +#   architecture-specific instructions are enabled.
    1.26 +#
    1.27 +#   The user can specify --with-gcc-arch=<arch> in order to override the
    1.28 +#   macro's choice of architecture, or --without-gcc-arch to disable this.
    1.29 +#
    1.30 +#   When cross-compiling, or if $CC is not gcc, then ACTION-FAILURE is
    1.31 +#   called unless the user specified --with-gcc-arch manually.
    1.32 +#
    1.33 +#   Requires macros: AX_CHECK_COMPILER_FLAGS, AX_GCC_X86_CPUID
    1.34 +#
    1.35 +#   (The main emphasis here is on recent CPUs, on the principle that doing
    1.36 +#   high-performance computing on old hardware is uncommon.)
    1.37 +#
    1.38 +# LICENSE
    1.39 +#
    1.40 +#   Copyright (c) 2008 Steven G. Johnson <stevenj@alum.mit.edu>
    1.41 +#   Copyright (c) 2008 Matteo Frigo
    1.42 +#
    1.43 +#   This program is free software: you can redistribute it and/or modify it
    1.44 +#   under the terms of the GNU General Public License as published by the
    1.45 +#   Free Software Foundation, either version 3 of the License, or (at your
    1.46 +#   option) any later version.
    1.47 +#
    1.48 +#   This program is distributed in the hope that it will be useful, but
    1.49 +#   WITHOUT ANY WARRANTY; without even the implied warranty of
    1.50 +#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
    1.51 +#   Public License for more details.
    1.52 +#
    1.53 +#   You should have received a copy of the GNU General Public License along
    1.54 +#   with this program. If not, see <http://www.gnu.org/licenses/>.
    1.55 +#
    1.56 +#   As a special exception, the respective Autoconf Macro's copyright owner
    1.57 +#   gives unlimited permission to copy, distribute and modify the configure
    1.58 +#   scripts that are the output of Autoconf when processing the Macro. You
    1.59 +#   need not follow the terms of the GNU General Public License when using
    1.60 +#   or distributing such scripts, even though portions of the text of the
    1.61 +#   Macro appear in them. The GNU General Public License (GPL) does govern
    1.62 +#   all other use of the material that constitutes the Autoconf Macro.
    1.63 +#
    1.64 +#   This special exception to the GPL applies to versions of the Autoconf
    1.65 +#   Macro released by the Autoconf Archive. When you make and distribute a
    1.66 +#   modified version of the Autoconf Macro, you may extend this special
    1.67 +#   exception to the GPL to apply to your modified version as well.
    1.68 +
    1.69 +#serial 8
    1.70 +
    1.71 +AC_DEFUN([AX_GCC_ARCHFLAG],
    1.72 +[AC_REQUIRE([AC_PROG_CC])
    1.73 +AC_REQUIRE([AC_CANONICAL_HOST])
    1.74 +
    1.75 +AC_ARG_WITH(gcc-arch, [AS_HELP_STRING([--with-gcc-arch=<arch>], [use architecture <arch> for gcc -march/-mtune, instead of guessing])],
    1.76 +	ax_gcc_arch=$withval, ax_gcc_arch=yes)
    1.77 +
    1.78 +AC_MSG_CHECKING([for gcc architecture flag])
    1.79 +AC_MSG_RESULT([])
    1.80 +AC_CACHE_VAL(ax_cv_gcc_archflag,
    1.81 +[
    1.82 +ax_cv_gcc_archflag="unknown"
    1.83 +
    1.84 +if test "$GCC" = yes; then
    1.85 +
    1.86 +if test "x$ax_gcc_arch" = xyes; then
    1.87 +ax_gcc_arch=""
    1.88 +if test "$cross_compiling" = no; then
    1.89 +case $host_cpu in
    1.90 +  i[[3456]]86*|x86_64*) # use cpuid codes, in part from x86info-1.7 by D. Jones
    1.91 +     AX_GCC_X86_CPUID(0)
    1.92 +     AX_GCC_X86_CPUID(1)
    1.93 +     case $ax_cv_gcc_x86_cpuid_0 in
    1.94 +       *:756e6547:*:*) # Intel
    1.95 +          case $ax_cv_gcc_x86_cpuid_1 in
    1.96 +	    *5[[48]]?:*:*:*) ax_gcc_arch="pentium-mmx pentium" ;;
    1.97 +	    *5??:*:*:*) ax_gcc_arch=pentium ;;
    1.98 +	    *6[[3456]]?:*:*:*) ax_gcc_arch="pentium2 pentiumpro" ;;
    1.99 +	    *6a?:*[[01]]:*:*) ax_gcc_arch="pentium2 pentiumpro" ;;
   1.100 +	    *6a?:*[[234]]:*:*) ax_gcc_arch="pentium3 pentiumpro" ;;
   1.101 +	    *6[[9d]]?:*:*:*) ax_gcc_arch="pentium-m pentium3 pentiumpro" ;;
   1.102 +	    *6[[78b]]?:*:*:*) ax_gcc_arch="pentium3 pentiumpro" ;;
   1.103 +	    *6??:*:*:*) ax_gcc_arch=pentiumpro ;;
   1.104 +            *f3[[347]]:*:*:*|*f4[1347]:*:*:*)
   1.105 +		case $host_cpu in
   1.106 +                  x86_64*) ax_gcc_arch="nocona pentium4 pentiumpro" ;;
   1.107 +                  *) ax_gcc_arch="prescott pentium4 pentiumpro" ;;
   1.108 +                esac ;;
   1.109 +            *f??:*:*:*) ax_gcc_arch="pentium4 pentiumpro";;
   1.110 +          esac ;;
   1.111 +       *:68747541:*:*) # AMD
   1.112 +          case $ax_cv_gcc_x86_cpuid_1 in
   1.113 +	    *5[[67]]?:*:*:*) ax_gcc_arch=k6 ;;
   1.114 +	    *5[[8d]]?:*:*:*) ax_gcc_arch="k6-2 k6" ;;
   1.115 +	    *5[[9]]?:*:*:*) ax_gcc_arch="k6-3 k6" ;;
   1.116 +	    *60?:*:*:*) ax_gcc_arch=k7 ;;
   1.117 +	    *6[[12]]?:*:*:*) ax_gcc_arch="athlon k7" ;;
   1.118 +	    *6[[34]]?:*:*:*) ax_gcc_arch="athlon-tbird k7" ;;
   1.119 +	    *67?:*:*:*) ax_gcc_arch="athlon-4 athlon k7" ;;
   1.120 +	    *6[[68a]]?:*:*:*)
   1.121 +	       AX_GCC_X86_CPUID(0x80000006) # L2 cache size
   1.122 +	       case $ax_cv_gcc_x86_cpuid_0x80000006 in
   1.123 +                 *:*:*[[1-9a-f]]??????:*) # (L2 = ecx >> 16) >= 256
   1.124 +			ax_gcc_arch="athlon-xp athlon-4 athlon k7" ;;
   1.125 +                 *) ax_gcc_arch="athlon-4 athlon k7" ;;
   1.126 +	       esac ;;
   1.127 +	    *f[[4cef8b]]?:*:*:*) ax_gcc_arch="athlon64 k8" ;;
   1.128 +	    *f5?:*:*:*) ax_gcc_arch="opteron k8" ;;
   1.129 +	    *f7?:*:*:*) ax_gcc_arch="athlon-fx opteron k8" ;;
   1.130 +	    *f??:*:*:*) ax_gcc_arch="k8" ;;
   1.131 +          esac ;;
   1.132 +	*:746e6543:*:*) # IDT
   1.133 +	   case $ax_cv_gcc_x86_cpuid_1 in
   1.134 +	     *54?:*:*:*) ax_gcc_arch=winchip-c6 ;;
   1.135 +	     *58?:*:*:*) ax_gcc_arch=winchip2 ;;
   1.136 +	     *6[[78]]?:*:*:*) ax_gcc_arch=c3 ;;
   1.137 +	     *69?:*:*:*) ax_gcc_arch="c3-2 c3" ;;
   1.138 +	   esac ;;
   1.139 +     esac
   1.140 +     if test x"$ax_gcc_arch" = x; then # fallback
   1.141 +	case $host_cpu in
   1.142 +	  i586*) ax_gcc_arch=pentium ;;
   1.143 +	  i686*) ax_gcc_arch=pentiumpro ;;
   1.144 +        esac
   1.145 +     fi
   1.146 +     ;;
   1.147 +
   1.148 +  sparc*)
   1.149 +     AC_PATH_PROG([PRTDIAG], [prtdiag], [prtdiag], [$PATH:/usr/platform/`uname -i`/sbin/:/usr/platform/`uname -m`/sbin/])
   1.150 +     cputype=`(((grep cpu /proc/cpuinfo | cut -d: -f2) ; ($PRTDIAG -v |grep -i sparc) ; grep -i cpu /var/run/dmesg.boot ) | head -n 1) 2> /dev/null`
   1.151 +     cputype=`echo "$cputype" | tr -d ' -' |tr $as_cr_LETTERS $as_cr_letters`
   1.152 +     case $cputype in
   1.153 +         *ultrasparciv*) ax_gcc_arch="ultrasparc4 ultrasparc3 ultrasparc v9" ;;
   1.154 +         *ultrasparciii*) ax_gcc_arch="ultrasparc3 ultrasparc v9" ;;
   1.155 +         *ultrasparc*) ax_gcc_arch="ultrasparc v9" ;;
   1.156 +         *supersparc*|*tms390z5[[05]]*) ax_gcc_arch="supersparc v8" ;;
   1.157 +         *hypersparc*|*rt62[[056]]*) ax_gcc_arch="hypersparc v8" ;;
   1.158 +         *cypress*) ax_gcc_arch=cypress ;;
   1.159 +     esac ;;
   1.160 +
   1.161 +  alphaev5) ax_gcc_arch=ev5 ;;
   1.162 +  alphaev56) ax_gcc_arch=ev56 ;;
   1.163 +  alphapca56) ax_gcc_arch="pca56 ev56" ;;
   1.164 +  alphapca57) ax_gcc_arch="pca57 pca56 ev56" ;;
   1.165 +  alphaev6) ax_gcc_arch=ev6 ;;
   1.166 +  alphaev67) ax_gcc_arch=ev67 ;;
   1.167 +  alphaev68) ax_gcc_arch="ev68 ev67" ;;
   1.168 +  alphaev69) ax_gcc_arch="ev69 ev68 ev67" ;;
   1.169 +  alphaev7) ax_gcc_arch="ev7 ev69 ev68 ev67" ;;
   1.170 +  alphaev79) ax_gcc_arch="ev79 ev7 ev69 ev68 ev67" ;;
   1.171 +
   1.172 +  powerpc*)
   1.173 +     cputype=`((grep cpu /proc/cpuinfo | head -n 1 | cut -d: -f2 | cut -d, -f1 | sed 's/ //g') ; /usr/bin/machine ; /bin/machine; grep CPU /var/run/dmesg.boot | head -n 1 | cut -d" " -f2) 2> /dev/null`
   1.174 +     cputype=`echo $cputype | sed -e 's/ppc//g;s/ *//g'`
   1.175 +     case $cputype in
   1.176 +       *750*) ax_gcc_arch="750 G3" ;;
   1.177 +       *740[[0-9]]*) ax_gcc_arch="$cputype 7400 G4" ;;
   1.178 +       *74[[4-5]][[0-9]]*) ax_gcc_arch="$cputype 7450 G4" ;;
   1.179 +       *74[[0-9]][[0-9]]*) ax_gcc_arch="$cputype G4" ;;
   1.180 +       *970*) ax_gcc_arch="970 G5 power4";;
   1.181 +       *POWER4*|*power4*|*gq*) ax_gcc_arch="power4 970";;
   1.182 +       *POWER5*|*power5*|*gr*|*gs*) ax_gcc_arch="power5 power4 970";;
   1.183 +       603ev|8240) ax_gcc_arch="$cputype 603e 603";;
   1.184 +       *) ax_gcc_arch=$cputype ;;
   1.185 +     esac
   1.186 +     ax_gcc_arch="$ax_gcc_arch powerpc"
   1.187 +     ;;
   1.188 +esac
   1.189 +fi # not cross-compiling
   1.190 +fi # guess arch
   1.191 +
   1.192 +if test "x$ax_gcc_arch" != x -a "x$ax_gcc_arch" != xno; then
   1.193 +for arch in $ax_gcc_arch; do
   1.194 +  if test "x[]m4_default([$1],yes)" = xyes; then # if we require portable code
   1.195 +    flags="-mtune=$arch"
   1.196 +    # -mcpu=$arch and m$arch generate nonportable code on every arch except
   1.197 +    # x86.  And some other arches (e.g. Alpha) don't accept -mtune.  Grrr.
   1.198 +    case $host_cpu in i*86|x86_64*) flags="$flags -mcpu=$arch -m$arch";; esac
   1.199 +  else
   1.200 +    flags="-march=$arch -mcpu=$arch -m$arch"
   1.201 +  fi
   1.202 +  for flag in $flags; do
   1.203 +    AX_CHECK_COMPILER_FLAGS($flag, [ax_cv_gcc_archflag=$flag; break])
   1.204 +  done
   1.205 +  test "x$ax_cv_gcc_archflag" = xunknown || break
   1.206 +done
   1.207 +fi
   1.208 +
   1.209 +fi # $GCC=yes
   1.210 +])
   1.211 +AC_MSG_CHECKING([for gcc architecture flag])
   1.212 +AC_MSG_RESULT($ax_cv_gcc_archflag)
   1.213 +if test "x$ax_cv_gcc_archflag" = xunknown; then
   1.214 +  m4_default([$3],:)
   1.215 +else
   1.216 +  m4_default([$2], [CFLAGS="$CFLAGS $ax_cv_gcc_archflag"])
   1.217 +fi
   1.218 +])