| 1 | #!/bin/sh |
| 2 | # Copyright (c) 2005, 2010, Oracle and/or its affiliates. |
| 3 | # |
| 4 | # This program is free software; you can redistribute it and/or modify |
| 5 | # it under the terms of the GNU General Public License as published by |
| 6 | # the Free Software Foundation; version 2 of the License. |
| 7 | # |
| 8 | # This program is distributed in the hope that it will be useful, |
| 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | # GNU General Public License for more details. |
| 12 | # |
| 13 | # You should have received a copy of the GNU General Public License |
| 14 | # along with this program; if not, write to the Free Software |
| 15 | # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335 USA |
| 16 | |
| 17 | # |
| 18 | # Check cpu of current machine and find the |
| 19 | # best compiler optimization flags for gcc |
| 20 | # Will return result in: |
| 21 | # cpu_arg : Type of CPU |
| 22 | # low_cpu_arg : Type of CPU used up until GCC v3.3 |
| 23 | # check_cpu_args : Arguments for GCC compiler settings |
| 24 | # |
| 25 | |
| 26 | check_compiler_cpu_flags () { |
| 27 | # different compiler versions have different option names |
| 28 | # for CPU specific command line options |
| 29 | if test -z "$CC" ; then |
| 30 | cc="gcc"; |
| 31 | else |
| 32 | cc=$CC |
| 33 | fi |
| 34 | |
| 35 | # check if compiler is gcc and dump its version |
| 36 | cc_verno=`$cc -dumpversion 2>/dev/null` |
| 37 | if test "x$?" = "x0" ; then |
| 38 | set -- `echo $cc_verno | tr '.' ' '` |
| 39 | cc_ver="GCC" |
| 40 | cc_major=$1 |
| 41 | cc_minor=$2 |
| 42 | cc_patch=$3 |
| 43 | if test -z "$cc_minor"; then |
| 44 | cc_minor="0"; |
| 45 | fi |
| 46 | if test -z "$cc_patch"; then |
| 47 | cc_minor="0"; |
| 48 | fi |
| 49 | cc_comp=`expr $cc_major '*' 100 '+' $cc_minor` |
| 50 | fi |
| 51 | |
| 52 | case "$cc_ver--$cc_verno" in |
| 53 | *GCC*) |
| 54 | # different gcc backends (and versions) have different CPU flags |
| 55 | case `gcc -dumpmachine` in |
| 56 | i?86-* | x86_64-*) |
| 57 | if test "$cc_comp" -lt 304 ; then |
| 58 | check_cpu_cflags="-mcpu=${low_cpu_arg}" |
| 59 | elif test "$cc_comp" -ge 402 ; then |
| 60 | check_cpu_cflags="-mtune=native" |
| 61 | else |
| 62 | check_cpu_cflags="-mtune=${cpu_arg}" |
| 63 | fi |
| 64 | ;; |
| 65 | ppc-*) |
| 66 | check_cpu_cflags="-mcpu=${cpu_arg} -mtune=${cpu_arg}" |
| 67 | ;; |
| 68 | *) |
| 69 | check_cpu_cflags="" |
| 70 | return |
| 71 | ;; |
| 72 | esac |
| 73 | ;; |
| 74 | 2.95.*) |
| 75 | # GCC 2.95 doesn't expose its name in --version output |
| 76 | check_cpu_cflags="-m${cpu_arg}" |
| 77 | ;; |
| 78 | *) |
| 79 | check_cpu_cflags="" |
| 80 | return |
| 81 | ;; |
| 82 | esac |
| 83 | |
| 84 | # now we check whether the compiler really understands the cpu type |
| 85 | touch __test.c |
| 86 | |
| 87 | while [ "$cpu_arg" ] ; do |
| 88 | printf "testing $cpu_arg ... " >&2 |
| 89 | |
| 90 | # compile check |
| 91 | eval "$cc -c $check_cpu_cflags __test.c" 2>/dev/null |
| 92 | if test "x$?" = "x0" ; then |
| 93 | echo ok >&2 |
| 94 | break; |
| 95 | fi |
| 96 | |
| 97 | echo failed >&2 |
| 98 | check_cpu_cflags="" |
| 99 | break; |
| 100 | done |
| 101 | rm __test.* |
| 102 | return 0 |
| 103 | } |
| 104 | |
| 105 | check_cpu () { |
| 106 | CPUINFO=/proc/cpuinfo |
| 107 | if test -n "$TEST_CPUINFO" ; then |
| 108 | CPUINFO=$TEST_CPUINFO |
| 109 | fi |
| 110 | if test -r "$CPUINFO" -a "$CPUINFO" != " " ; then |
| 111 | # on Linux (and others?) we can get detailed CPU information out of /proc |
| 112 | cpuinfo="cat $CPUINFO" |
| 113 | |
| 114 | # detect CPU architecture |
| 115 | cpu_arch=`$cpuinfo | grep 'arch' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -1` |
| 116 | |
| 117 | # detect CPU family |
| 118 | cpu_family=`$cpuinfo | grep 'family' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -1` |
| 119 | if test -z "$cpu_family" ; then |
| 120 | cpu_family=`$cpuinfo | grep 'cpu' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -1` |
| 121 | fi |
| 122 | |
| 123 | # detect CPU vendor and model |
| 124 | cpu_vendor=`$cpuinfo | grep 'vendor_id' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -1` |
| 125 | model_name=`$cpuinfo | grep 'model name' | cut -d ':' -f 2 | head -1` |
| 126 | if test -z "$model_name" ; then |
| 127 | model_name=`$cpuinfo | grep 'cpu model' | cut -d ':' -f 2 | head -1` |
| 128 | fi |
| 129 | |
| 130 | # fallback: get CPU model from uname output |
| 131 | if test -z "$model_name" ; then |
| 132 | model_name=`uname -m` |
| 133 | fi |
| 134 | |
| 135 | # parse CPU flags |
| 136 | for flag in `$cpuinfo | grep '^flags' | sed -e 's/^flags.*: //' -e 's/[^a-zA-Z0-9_ ]/_/g'`; do |
| 137 | eval cpu_flag_$flag=yes |
| 138 | done |
| 139 | else |
| 140 | # Fallback when there is no /proc/cpuinfo |
| 141 | CPUINFO=" " |
| 142 | case "`uname -s`" in |
| 143 | FreeBSD|OpenBSD) |
| 144 | cpu_family=`uname -m`; |
| 145 | model_name=`sysctl -n hw.model` |
| 146 | ;; |
| 147 | Darwin) |
| 148 | cpu_family=`sysctl -n machdep.cpu.vendor` |
| 149 | model_name=`sysctl -n machdep.cpu.brand_string` |
| 150 | if [ -z "$cpu_family" -o -z "$model_name" ] |
| 151 | then |
| 152 | cpu_family=`uname -p` |
| 153 | model_name=`machine` |
| 154 | fi |
| 155 | ;; |
| 156 | *) |
| 157 | cpu_family=`uname -p`; |
| 158 | model_name=`uname -m`; |
| 159 | ;; |
| 160 | esac |
| 161 | fi |
| 162 | |
| 163 | # detect CPU shortname as used by gcc options |
| 164 | # this list is not complete, feel free to add further entries |
| 165 | cpu_arg="" |
| 166 | low_cpu_arg="" |
| 167 | case "$cpu_vendor--$cpu_family--$model_name--$spu_arch" in |
| 168 | # DEC Alpha |
| 169 | *Alpha*EV6*) |
| 170 | cpu_arg="ev6"; |
| 171 | ;; |
| 172 | #Core 2 Duo |
| 173 | *Intel*Core\(TM\)2*) |
| 174 | cpu_arg="nocona" |
| 175 | core2="yes" |
| 176 | ;; |
| 177 | # Intel ia32 |
| 178 | *Intel*Core*|*X[eE][oO][nN]*) |
| 179 | # a Xeon is just another pentium4 ... |
| 180 | # ... unless it has the "lm" (long-mode) flag set, |
| 181 | # in that case it's a Xeon with EM64T support |
| 182 | # If SSE3 support exists it is a Core2 Duo or newer |
| 183 | # So is Intel Core. |
| 184 | if [ -z "$cpu_flag_lm" ]; then |
| 185 | cpu_arg="pentium4" |
| 186 | else |
| 187 | cpu_arg="nocona" |
| 188 | fi |
| 189 | if test -z "$cpu_flag_ssse3" ; then |
| 190 | core2="no" |
| 191 | else |
| 192 | core2="yes" |
| 193 | fi |
| 194 | ;; |
| 195 | *Pentium*4*Mobile*) |
| 196 | cpu_arg="pentium4m" |
| 197 | ;; |
| 198 | *Pentium\(R\)*\ M*) |
| 199 | cpu_arg="pentium-m" |
| 200 | low_cpu_arg="pentium3" |
| 201 | ;; |
| 202 | *Pentium\(R\)*\ D*) |
| 203 | cpu_arg="prescott" |
| 204 | ;; |
| 205 | *Pentium*4*) |
| 206 | cpu_arg="pentium4" |
| 207 | ;; |
| 208 | *Pentium*III*Mobile*) |
| 209 | cpu_arg="pentium3m" |
| 210 | ;; |
| 211 | *Pentium*III*) |
| 212 | cpu_arg="pentium3" |
| 213 | ;; |
| 214 | *Pentium*M*pro*) |
| 215 | cpu_arg="pentium-m" |
| 216 | ;; |
| 217 | *Celeron\(R\)*\ M*) |
| 218 | cpu_arg="pentium-m" |
| 219 | ;; |
| 220 | *Celeron*Coppermine*) |
| 221 | cpu_arg="pentium3" |
| 222 | ;; |
| 223 | *Celeron\(R\)*) |
| 224 | cpu_arg="pentium4" |
| 225 | ;; |
| 226 | *Celeron*) |
| 227 | cpu_arg="pentium2" |
| 228 | ;; |
| 229 | *Atom*) |
| 230 | cpu_arg="prescott" |
| 231 | ;; |
| 232 | *GenuineIntel*) |
| 233 | cpu_arg="pentium" |
| 234 | ;; |
| 235 | *Turion*) |
| 236 | cpu_arg="athlon64" |
| 237 | ;; |
| 238 | *Athlon*64*) |
| 239 | cpu_arg="athlon64" |
| 240 | ;; |
| 241 | *Athlon*) |
| 242 | cpu_arg="athlon" |
| 243 | ;; |
| 244 | *AMD-K7*) |
| 245 | cpu_arg="athlon" |
| 246 | ;; |
| 247 | *Athlon*XP\ *) |
| 248 | cpu_arg="athlon-xp" |
| 249 | ;; |
| 250 | *AMD*Sempron\(tm\)*) |
| 251 | cpu_arg="athlon-mp" |
| 252 | ;; |
| 253 | *AMD*Athlon\(tm\)\ 64*) |
| 254 | cpu_arg="k8" |
| 255 | ;; |
| 256 | *Opteron*) |
| 257 | cpu_arg="opteron" |
| 258 | ;; |
| 259 | *Phenom*) |
| 260 | cpu_arg="k8" |
| 261 | ;; |
| 262 | *AuthenticAMD*) |
| 263 | cpu_arg="k6" |
| 264 | ;; |
| 265 | *HygonGenuine*) |
| 266 | cpu_arg="k8" |
| 267 | ;; |
| 268 | *VIA\ *) |
| 269 | cpu_arg="i686" |
| 270 | ;; |
| 271 | # MacOSX / Intel |
| 272 | *i386*i486*) |
| 273 | cpu_arg="pentium-m" |
| 274 | ;; |
| 275 | *i386*) |
| 276 | cpu_arg="i386" |
| 277 | ;; |
| 278 | # Intel ia64 |
| 279 | *Itanium*) |
| 280 | cpu_arg="itanium" |
| 281 | ;; |
| 282 | *IA-64*) |
| 283 | cpu_arg="itanium" |
| 284 | ;; |
| 285 | # Solaris Sparc |
| 286 | *sparc*sun4[uv]*) |
| 287 | cpu_arg="sparc" |
| 288 | ;; |
| 289 | # Power PC |
| 290 | *ppc*) |
| 291 | cpu_arg="powerpc" |
| 292 | ;; |
| 293 | *powerpc*) |
| 294 | cpu_arg="powerpc" |
| 295 | ;; |
| 296 | # unknown |
| 297 | *) |
| 298 | cpu_arg="" |
| 299 | ;; |
| 300 | esac |
| 301 | |
| 302 | if test "x$low_cpu_arg" = "x" ; then |
| 303 | low_cpu_arg="$cpu_arg" |
| 304 | fi |
| 305 | |
| 306 | if test -z "$cpu_arg" ; then |
| 307 | if test "$CPUINFO" != " " ; then |
| 308 | # fallback to uname if necessary |
| 309 | TEST_CPUINFO=" " |
| 310 | check_cpu_cflags="" |
| 311 | check_cpu |
| 312 | return |
| 313 | fi |
| 314 | echo "BUILD/check-cpu: Oops, could not find out what kind of cpu this machine is using." >&2 |
| 315 | check_cpu_cflags="" |
| 316 | return |
| 317 | fi |
| 318 | |
| 319 | if test "x$compiler" = "x" ; then |
| 320 | check_compiler_cpu_flags |
| 321 | fi |
| 322 | |
| 323 | if test "x$core2" = "xyes" ; then |
| 324 | cpu_arg="core2" |
| 325 | fi |
| 326 | |
| 327 | return 0 |
| 328 | } |
| 329 | |
| 330 | check_cpu |