~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to BUILD/check-cpu

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# Check cpu of current machine and find the
 
4
# best compiler optimization flags for gcc
 
5
# Will return result in:
 
6
# cpu_arg        : Type of CPU
 
7
# check_cpu_args : Arguments for GCC compiler settings
 
8
#
 
9
 
 
10
check_cpu () {
 
11
  CPUINFO=/proc/cpuinfo
 
12
  if test -n "$TEST_CPUINFO" ; then
 
13
    CPUINFO=$TEST_CPUINFO
 
14
  fi
 
15
  if test -r "$CPUINFO" -a "$CPUINFO" != " " ; then
 
16
    # on Linux (and others?) we can get detailed CPU information out of /proc
 
17
    cpuinfo="cat $CPUINFO"
 
18
 
 
19
    # detect CPU family
 
20
    cpu_family=`$cpuinfo | grep 'family' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -1`
 
21
    if test -z "$cpu_family" ; then
 
22
      cpu_family=`$cpuinfo | grep 'cpu' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -1`
 
23
    fi
 
24
 
 
25
    # detect CPU vendor and model
 
26
    cpu_vendor=`$cpuinfo | grep 'vendor_id' | cut -d ':' -f 2 | cut -d ' ' -f 2 | head -1`
 
27
    model_name=`$cpuinfo | grep 'model name' | cut -d ':' -f 2 | head -1`
 
28
    if test -z "$model_name" ; then
 
29
      model_name=`$cpuinfo | grep 'cpu model' | cut -d ':' -f 2 | head -1`
 
30
    fi
 
31
 
 
32
    # fallback: get CPU model from uname output
 
33
    if test -z "$model_name" ; then
 
34
      model_name=`uname -m`
 
35
    fi
 
36
 
 
37
    # parse CPU flags
 
38
    for flag in `$cpuinfo | grep '^flags' | sed -e 's/^flags.*: //' -e 's/[^a-zA-Z0-9_ ]/_/g'`; do 
 
39
      eval cpu_flag_$flag=yes
 
40
    done
 
41
  else
 
42
    # Fallback when there is no /proc/cpuinfo
 
43
    CPUINFO=" "
 
44
    case "`uname -s`" in
 
45
      FreeBSD|OpenBSD)
 
46
        cpu_family=`uname -m`;
 
47
        model_name=`sysctl -n hw.model`
 
48
        ;;
 
49
      Darwin)
 
50
        cpu_family=`uname -p`
 
51
        model_name=`machine`
 
52
        ;;
 
53
      *)
 
54
        cpu_family=`uname -m`;
 
55
        model_name=`uname -p`;
 
56
        ;;
 
57
    esac
 
58
  fi
 
59
 
 
60
  # detect CPU shortname as used by gcc options 
 
61
  # this list is not complete, feel free to add further entries
 
62
  cpu_arg=""
 
63
  case "$cpu_family--$model_name" in
 
64
    # DEC Alpha
 
65
    Alpha*EV6*)
 
66
      cpu_arg="ev6";
 
67
      ;;
 
68
    # Intel ia32
 
69
    *Intel*Core*|*X[eE][oO][nN]*)
 
70
      # a Xeon is just another pentium4 ...
 
71
      # ... unless it has the "lm" (long-mode) flag set, 
 
72
      # in that case it's a Xeon with EM64T support
 
73
      # If SSE3 support exists it is a Core2 Duo or newer
 
74
      # So is Intel Core.
 
75
      if [ -z "$cpu_flag_lm" ]; then
 
76
        cpu_arg="pentium4"
 
77
      else
 
78
        cpu_arg="nocona"
 
79
      fi
 
80
      if test -z "$cpu_flag_ssse3" ; then
 
81
        core2="no"
 
82
      else
 
83
        core2="yes"
 
84
      fi
 
85
      ;;
 
86
    *Pentium*4*Mobile*)
 
87
      cpu_arg="pentium4m"
 
88
      ;;
 
89
    *Pentium*4*)
 
90
      cpu_arg="pentium4"
 
91
      ;;
 
92
    *Pentium*III*Mobile*)
 
93
      cpu_arg="pentium3m"
 
94
      ;;
 
95
    *Pentium*III*)
 
96
      cpu_arg="pentium3"
 
97
      ;;
 
98
    *Pentium*M*pro*)
 
99
      cpu_arg="pentium-m"
 
100
    ;;
 
101
    *Celeron\(R\)*\ M*)
 
102
      cpu_arg="pentium-m"
 
103
      ;;
 
104
    *Celeron*Coppermine*)
 
105
      cpu_arg="pentium3"
 
106
      ;;
 
107
    *Celeron\(R\)*)
 
108
      cpu_arg="pentium4"
 
109
      ;;
 
110
    *Celeron*)
 
111
      cpu_arg="pentium2"
 
112
      ;;
 
113
    *Turion*)
 
114
      cpu_arg="athlon64"
 
115
      ;;
 
116
    *Athlon*64*)
 
117
      cpu_arg="athlon64"
 
118
      ;;
 
119
    *Athlon*)
 
120
      cpu_arg="athlon"
 
121
      ;;
 
122
    *Opteron*)
 
123
      cpu_arg="opteron"
 
124
      ;;
 
125
    # MacOSX / Intel  
 
126
    *i386*i486*)
 
127
      cpu_arg="pentium-m"
 
128
      ;;
 
129
    *i386*)
 
130
      cpu_arg="i386"
 
131
      ;;
 
132
    #Core 2 Duo  
 
133
    *Intel*Core\(TM\)2*)
 
134
      cpu_arg="nocona"
 
135
      ;;
 
136
    # Intel ia64
 
137
    *Itanium*)
 
138
      cpu_arg="itanium"
 
139
      ;;
 
140
    # Solaris Sparc
 
141
    *sparc*sun4u*)
 
142
      cpu_arg="sparc"
 
143
      ;;
 
144
    # Power PC
 
145
    *ppc*)
 
146
      cpu_arg="powerpc"
 
147
      ;;
 
148
    *powerpc*)
 
149
      cpu_arg="powerpc"
 
150
      ;;
 
151
    # unknown
 
152
    *)
 
153
      cpu_arg=""
 
154
      ;;
 
155
  esac
 
156
 
 
157
  if test -z "$cpu_arg" ; then
 
158
    if test "$CPUINFO" != " " ; then
 
159
      # fallback to uname if necessary
 
160
      TEST_CPUINFO=" "
 
161
      check_cpu_cflags=""
 
162
      check_cpu
 
163
      return
 
164
    fi
 
165
    echo "BUILD/check-cpu: Oops, could not find out what kind of cpu this machine is using." >&2
 
166
    check_cpu_cflags=""
 
167
    return
 
168
  fi
 
169
 
 
170
  # different compiler versions have different option names
 
171
  # for CPU specific command line options
 
172
  if test -z "$CC" ; then
 
173
    cc="gcc";
 
174
  else
 
175
    cc=$CC
 
176
  fi
 
177
 
 
178
  cc_ver=`$cc --version | sed 1q`
 
179
  cc_verno=`echo $cc_ver | sed -e 's/^.*gcc/gcc/g; s/[^0-9. ]//g;        s/^ *//g; s/ .*//g'`
 
180
  set -- `echo $cc_verno | tr '.' ' '`
 
181
  cc_major=$1
 
182
  cc_minor=$2
 
183
  cc_patch=$3
 
184
  cc_comp=`expr $cc_major '*' 100 '+' $cc_minor`
 
185
  
 
186
  case "$cc_ver--$cc_verno" in
 
187
    *GCC*)
 
188
        # different gcc backends (and versions) have different CPU flags
 
189
        case `gcc -dumpmachine` in
 
190
          i?86-* | x86_64-*)
 
191
            if test "$cc_comp" -lt 304 ; then
 
192
              check_cpu_cflags="-mcpu=${cpu_arg}"
 
193
            elif test "$cc_comp" -ge 402 ; then
 
194
              check_cpu_cflags="-mtune=native"
 
195
            else
 
196
              check_cpu_cflags="-mtune=${cpu_arg}"
 
197
            fi
 
198
            ;;
 
199
          ppc-*)
 
200
              check_cpu_cflags="-mcpu=${cpu_arg} -mtune=${cpu_arg}"
 
201
            ;;
 
202
          *)
 
203
            check_cpu_cflags=""
 
204
            return
 
205
            ;;
 
206
        esac
 
207
      ;;
 
208
    2.95.*)
 
209
      # GCC 2.95 doesn't expose its name in --version output
 
210
      check_cpu_cflags="-m${cpu_arg}"
 
211
      ;;
 
212
    *)
 
213
      check_cpu_cflags=""
 
214
      return
 
215
      ;;
 
216
  esac
 
217
 
 
218
  # now we check whether the compiler really understands the cpu type
 
219
  touch __test.c
 
220
 
 
221
  if test "x$core2" = "xyes" ; then
 
222
    cpu_arg="core2"
 
223
  fi
 
224
  while [ "$cpu_arg" ] ; do
 
225
    printf "testing $cpu_arg ... " >&2
 
226
          
 
227
    # compile check
 
228
    eval "$cc -c $check_cpu_cflags __test.c" 2>/dev/null
 
229
    if test "x$?" = "x0" ; then
 
230
      echo ok >&2
 
231
      break;
 
232
    fi
 
233
 
 
234
    echo failed >&2
 
235
    check_cpu_cflags=""
 
236
    break;
 
237
  done
 
238
  rm __test.*
 
239
}
 
240
 
 
241
check_cpu