]> mj.ucw.cz Git - libucw.git/blob - lib/autoconf.cfg
autoconf.cfg: recognize correctly quad-core models of Intel Xeon cpus
[libucw.git] / lib / autoconf.cfg
1 # Automatic configuration of the UCW Library
2 # (c) 2005--2007 Martin Mares <mj@ucw.cz>
3 # (c) 2006 Robert Spalek <robert@ucw.cz>
4
5 ### OS ###
6
7 Test("OS", "Checking on which OS we run", sub {
8         my $os = `uname`;
9         chomp $os;
10         Fail "Unable to determine OS type" if $? || $os eq "";
11         return $os;
12 });
13
14 if (Get("OS") eq "Linux") {
15         Set("CONFIG_LINUX");
16 } elsif (Get("OS") eq "Darwin") {
17         Set("CONFIG_DARWIN");
18 } else {
19         Fail "Don't know how to run on this operating system.";
20 }
21
22 ### Compiler ###
23
24 # Default compiler
25 Test("CC", "Checking for C compiler", sub { return "gcc"; });
26
27 # GCC version
28 Test("GCCVER", "Checking for GCC version", sub {
29         my $gcc = Get("CC");
30         my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
31         chomp $ver;
32         Fail "Unable to determine GCC version" if $? || $ver eq "";
33         return $ver;
34 });
35 my ($gccmaj, $gccmin) = split(/\./, Get("GCCVER"));
36 my $gccver = 1000*$gccmaj + $gccmin;
37 $gccver >= 3000 or Fail "GCC older than 3.0 doesn't support C99 well enough.";
38
39 ### CPU ###
40
41 Test("ARCH", "Checking for machine architecture", sub {
42         my $mach = `uname -m`;
43         chomp $mach;
44         Fail "Unable to determine machine type" if $? || $mach eq "";
45         if ($mach =~ /^i[0-9]86$/) {
46                 return "i386";
47         } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
48                 return "amd64";
49         } else {
50                 return "unknown";
51         }
52 });
53
54 sub parse_cpuinfo_linux() {
55         open X, "/proc/cpuinfo" || undef;
56         my %pc = ();
57         while (<X>) {
58                 chomp;
59                 /^$/ && last;
60                 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
61         }
62         close X;
63         return ($pc{'vendor_id'},
64                 $pc{'cpu family'},
65                 $pc{'model'});
66 }
67
68 sub parse_cpuinfo_darwin() {
69         @cpu = (`sysctl -n machdep.cpu.vendor`,
70                 `sysctl -n machdep.cpu.family`,
71                 `sysctl -n machdep.cpu.model`);
72         chomp @cpu;
73         return @cpu;
74 }
75
76 sub parse_cpuinfo() {
77         my @cpu;
78         if (IsSet("CONFIG_LINUX")) {
79                 @cpu = parse_cpuinfo_linux();
80         } elsif (IsSet("CONFIG_DARWIN")) {
81                 @cpu = parse_cpuinfo_darwin();
82         }
83         $cpu[0] = "" if !defined $cpu[0];
84         $cpu[1] = 0 if !defined $cpu[1];
85         $cpu[2] = 0 if !defined $cpu[2];
86         return @cpu;
87 }
88
89 Test("CPU_ARCH", "Checking for CPU architecture", sub {
90         my $mach = Get("ARCH");
91         my $arch = "";
92         if ($mach eq "i386") {
93                 Set("CPU_I386");
94                 UnSet("CPU_64BIT_POINTERS");
95                 Set("CPU_LITTLE_ENDIAN");
96                 UnSet("CPU_BIG_ENDIAN");
97                 Set("CPU_ALLOW_UNALIGNED");
98                 Set("CPU_STRUCT_ALIGN" => 4);
99                 if (IsSet("CONFIG_EXACT_CPU")) {
100                         my ($vendor, $family, $model) = parse_cpuinfo();
101                         # Try to understand CPU vendor, family and model [inspired by MPlayer's configure script]
102                         if ($vendor eq "AuthenticAMD") {
103                                 if ($family >= 6) {
104                                         if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
105                                         elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
106                                         else { $arch = "athlon"; }
107                                 }
108                         } elsif ($vendor eq "GenuineIntel") {
109                                 if ($family >= 15 && $gccver >= 3003) {
110                                         if ($model >= 4) { $arch = "nocona"; }
111                                         elsif ($model >= 3) { $arch = "prescott"; }
112                                         else { $arch = "pentium4"; }
113                                 } elsif ($family == 6 && $gccver >= 3003) {
114                                         if ($model == 23) { $arch = "nocona"; }
115                                         elsif ($model == 15) { $arch = "prescott"; }
116                                         elsif (($model == 9 || $model == 13) && $gccver >= 3004) { $arch = "pentium-m"; }
117                                         elsif ($model >= 7) { $arch = "pentium3"; }
118                                         elsif ($model >= 3) { $arch = "pentium2"; }
119                                 }
120                         }
121
122                         # No match on vendor, try the family
123                         if ($arch eq "") {
124                                 if ($family >= 6) {
125                                         $arch = "i686";
126                                 } elsif ($family >= 3) {
127                                         $arch = "i${family}86";
128                                 }
129                         }
130                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
131                         return $arch;
132                 } else {
133                         return "default";
134                 }
135         } elsif ($mach eq "amd64") {
136                 Set("CPU_AMD64");
137                 Set("CPU_64BIT_POINTERS");
138                 Set("CPU_LITTLE_ENDIAN");
139                 UnSet("CPU_BIG_ENDIAN");
140                 Set("CPU_ALLOW_UNALIGNED");
141                 Set("CPU_STRUCT_ALIGN" => 8);
142                 if (IsSet("CONFIG_EXACT_CPU")) {
143                         # In x86-64 world, the detection is somewhat easier so far...
144                         my ($vendor, $family, $model) = parse_cpuinfo();
145                         if ($vendor eq "AuthenticAMD") {
146                                 $arch = "athlon64";
147                         } elsif ($vendor eq "GenuineIntel") {
148                                 $arch = "nocona";
149                         }
150                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
151                         return $arch;
152                 } else {
153                         return "default";
154                 }
155         } else {
156                 return "unknown";
157         }
158 });
159
160 if (Get("CPU_ARCH") eq "unknown") {
161         Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
162 }
163
164 ### Compiler and its Options ###
165
166 # C flags: tell the compiler we're speaking C99, and disable common symbols
167 Set("CLANG" => "-std=gnu99 -fno-common");
168
169 # C optimizations
170 Set("COPT" => '-O2');
171 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
172         Append("COPT", '-march=$(CPU_ARCH)');
173 }
174
175 # C optimizations for highly exposed code
176 Set("COPT2" => '-O3');
177
178 # Warnings
179 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
180 Set("CWARNS_OFF" => '');
181
182 # Linker flags
183 Set("LOPT" => "");
184
185 # Extra libraries
186 Set("LIBS" => "");
187
188 # Extra flags for compiling and linking shared libraries
189 Set("CSHARED" => '-fPIC');
190 if (IsSet("CONFIG_DARWIN")) {
191         Set("LSHARED" => '-dynamiclib -install_name lib/$(@F) -undefined dynamic_lookup');
192 } else {
193         Set("LSHARED" => '-shared -Wl,-soname,lib/$(@F)');
194 }
195
196 # Extra switches depending on GCC version:
197 if ($gccver == 3000) {
198         Append("COPT" => "-fstrict-aliasing");
199 } elsif ($gccver == 3003) {
200         Append("CWARNS" => "-Wundef -Wredundant-decls");
201         Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
202 } elsif ($gccver == 3004) {
203         Append("CWARNS" => "-Wundef -Wredundant-decls");
204         Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
205 } elsif ($gccver == 4000 || $gccver == 4001) {
206         Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
207         Append("CWARNS_OFF" => "-Wno-pointer-sign");
208         Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
209 } elsif ($gccver == 4002) {
210         Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
211         Append("CWARNS_OFF" => "-Wno-pointer-sign");
212         Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400 -fgnu89-inline");
213 } else {
214         Warn "Don't know anything about this GCC version, using default switches.\n";
215 }
216
217 if (IsSet("CONFIG_DEBUG")) {
218         # If debugging:
219         Set("DEBUG_ASSERTS");
220         Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
221         Set("CDEBUG" => "-ggdb");
222 } else {
223         # If building a release version:
224         Append("COPT" => "-fomit-frame-pointer");
225         Append("LOPT" => "-s");
226 }
227
228 if (IsSet("CONFIG_DARWIN")) {
229         # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
230         Append("CLANG" => "-fnested-functions");
231         # Directory hierarchy of the fink project
232         Append("LIBS" => "-L/sw/lib");
233         Append("COPT" => "-I/sw/include");
234         # Fill in some constants not found in the system header files
235         Set("SOL_TCP" => 6);            # missing in /usr/include/netinet/tcp.h
236 }
237
238 # Determine page size
239 Test("CPU_PAGE_SIZE", "Determining page size", sub {
240         my $p;
241         if (IsSet("CONFIG_DARWIN")) {
242                 $p = `sysctl -n hw.pagesize`;
243                 defined $p or Fail "sysctl hw.pagesize failed";
244         } elsif (IsSet("CONFIG_LINUX")) {
245                 $p = `getconf PAGE_SIZE`;
246                 defined $p or Fail "getconf PAGE_SIZE failed";
247         }
248         chomp $p;
249         return $p;
250 });
251
252 if (IsSet("CONFIG_LARGE_FILES") && IsSet("CONFIG_LINUX")) {
253         # Use 64-bit versions of file functions
254         Set("CONFIG_LFS");
255 }
256
257 # Decide how will lib/partmap.c work
258 Set("PARTMAP_IS_MMAP") if IsSet("CPU_64BIT_POINTERS");
259
260 # Option for lib/mempool.c
261 Set("POOL_IS_MMAP");
262
263 # Guess optimal bit width of the radix-sorter
264 if (Get("CPU_ARCH") eq "default" || Get("CPU_ARCH") =~ /^i[345]86$/) {
265         # This should be safe everywhere
266         Set("CONFIG_UCW_RADIX_SORTER_BITS" => 10);
267 } else {
268         # Use this on modern CPU's
269         Set("CONFIG_UCW_RADIX_SORTER_BITS" => 12);
270 }
271
272 # If debugging memory allocations:
273 #LIBS+=-lefence
274 #CDEBUG+=-DDEBUG_DMALLOC
275 #LIBS+=-ldmalloc
276
277 # Return success
278 1;