]> mj.ucw.cz Git - libucw.git/blob - lib/autoconf.cfg
Added automatic configuration for Prescott, Nocona, Pentium-M and amd64 CPU's.
[libucw.git] / lib / autoconf.cfg
1 # Automatic configuration of the UCW Library
2 # (c) 2005 Martin Mares <mj@ucw.cz>
3
4 ### OS ###
5
6 Test("OS", "Checking on which OS we run", sub {
7         my $os = `uname`;
8         chomp $os;
9         Fail "Unable to determine OS type" if $? || $os eq "";
10         return $os;
11 });
12
13 if (Get("OS") eq "Linux") {
14         Set("CONFIG_LINUX");
15 } else {
16         Fail "Don't know how to run on this operating system.";
17 }
18
19 ### Compiler ###
20
21 # Default compiler
22 Test("CC", "Checking for C compiler", sub { return "gcc"; });
23
24 # GCC version
25 Test("GCCVER", "Checking for GCC version", sub {
26         my $gcc = Get("CC");
27         my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)\\?\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
28         chomp $ver;
29         Fail "Unable to determine GCC version" if $? || $ver eq "";
30         return $ver;
31 });
32 my ($gccmaj, $gccmin) = split(/\./, Get("GCCVER"));
33 my $gccver = 1000*$gccmaj + $gccmin;
34 $gccver >= 3000 or Fail "GCC older than 3.0 doesn't support C99 well enough.";
35
36 ### CPU ###
37
38 Test("ARCH", "Checking for machine architecture", sub {
39         my $mach = `uname -m`;
40         chomp $mach;
41         Fail "Unable to determine machine type" if $? || $mach eq "";
42         if ($mach =~ /^i[0-9]86$/) {
43                 return "i386";
44         } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
45                 return "amd64";
46         } else {
47                 return "unknown";
48         }
49 });
50
51 sub parse_cpuinfo() {
52         IsSet("CONFIG_EXACT_CPU") && IsSet("CONFIG_LINUX") && open X, "/proc/cpuinfo" || undef;
53         my %pc = ();
54         while (<X>) {
55                 chomp;
56                 /^$/ && last;
57                 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
58         }
59         close X;
60
61         my $vendor = $pc{'vendor_id'} || "";
62         my $family = $pc{'cpu family'} || 0;
63         my $model  = $pc{'model'} || 0;
64         return ($vendor, $family, $model);
65 }
66
67 Test("CPU_ARCH", "Checking for CPU architecture", sub {
68         my $mach = Get("ARCH");
69         if ($mach eq "i386") {
70                 Set("CPU_I386");
71                 UnSet("CPU_64BIT_POINTERS");
72                 Set("CPU_LITTLE_ENDIAN");
73                 UnSet("CPU_BIG_ENDIAN");
74                 Set("CPU_ALLOW_UNALIGNED");
75                 Set("CPU_STRUCT_ALIGN" => 4);
76                 my $arch = "";
77                 if (my ($vendor, $family, $model) = parse_cpuinfo()) {
78                         # Try to understand CPU vendor, family and model [inspired by MPlayer's configure script]
79                         if ($vendor eq "AuthenticAMD") {
80                                 if ($family >= 6) {
81                                         if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
82                                         elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
83                                         else { $arch = "athlon"; }
84                                 }
85                         } elsif ($vendor eq "GenuineIntel") {
86                                 if ($family >= 15 && $gccver >= 3003) {
87                                         if ($model >= 4) { $arch = "nocona"; }
88                                         elsif ($model >= 3) { $arch = "prescott"; }
89                                         else { $arch = "pentium4"; }
90                                 } elsif ($family == 6 && $gccver >= 3003) {
91                                         if (($model == 9 || $model == 13) && $gccver >= 3004) { $arch = "pentium-m"; }
92                                         elsif ($model >= 7) { $arch = "pentium3"; }
93                                         elsif ($model >= 3) { $arch = "pentium2"; }
94                                 }
95                         }
96
97                         # No match on vendor, try the family
98                         if ($arch eq "") {
99                                 if ($family >= 6) {
100                                         $arch = "i686";
101                                 } elsif ($family >= 3) {
102                                         $arch = "i${family}86";
103                                 }
104                         }
105                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
106                 }
107                 return $arch ? $arch : "i386";
108         } elsif ($mach eq "amd64") {
109                 Set("CPU_AMD64");
110                 Set("CPU_64BIT_POINTERS");
111                 Set("CPU_LITTLE_ENDIAN");
112                 UnSet("CPU_BIG_ENDIAN");
113                 Set("CPU_ALLOW_UNALIGNED");
114                 Set("CPU_STRUCT_ALIGN" => 8);
115                 if (my ($vendor, $family, $model) = parse_cpuinfo()) {
116                         # In x86-64 world, the detection is somewhat easier so far...
117                         if ($vendor eq "AuthenticAMD") {
118                                 $arch = "athlon64";
119                         } elsif ($vendor eq "GenuineIntel") {
120                                 $arch = "nocona";
121                         }
122                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
123                 }
124                 return $arch ? $arch : "x86-64";
125         } else {
126                 return undef;
127         }
128 });
129
130 if (!IsSet("CPU_ARCH")) { Warn "CPU type not recognized, using defaults, keep fingers crossed."; }
131
132 ### Compiler and its Options ###
133
134 # C flags: tell the compiler we're speaking C99
135 Set("CLANG" => "-std=gnu99");
136
137 # C optimizations
138 Set("COPT" => '-O2');
139 Append("COPT", '-march=$(CPU_ARCH)') if IsSet("CPU_ARCH");
140
141 # C optimizations for highly exposed code
142 Set("COPT2" => '-O3');
143
144 # Warnings
145 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
146 Set("CWARNS_OFF" => '');
147
148 # Linker flags
149 Set("LOPT" => "");
150
151 # Extra libraries
152 Set("LIBS" => "");
153
154 # Extra flags for compiling and linking shared libraries
155 Set("CSHARED" => '-fPIC');
156 Set("LSHARED" => '-shared -Wl,-soname,lib/$(@F)');
157
158 # Extra switches depending on GCC version:
159 if ($gccver == 3000) {
160         Append("COPT" => "-fstrict-aliasing");
161 } elsif ($gccver == 3003) {
162         Append("CWARNS" => "-Wundef -Wredundant-decls");
163         Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
164 } elsif ($gccver == 3004) {
165         Append("CWARNS" => "-Wundef -Wredundant-decls");
166         Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
167 } elsif ($gccver == 4000 || $gccver == 4001) {
168         Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
169         Append("CWARNS_OFF" => "-Wno-pointer-sign");
170         Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
171 } else {
172         Warn "Don't know anything about this GCC version, using default switches.\n";
173 }
174
175 if (IsSet("CONFIG_DEBUG")) {
176         # If debugging:
177         Set("DEBUG_ASSERTS");
178         Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
179         Set("CDEBUG" => "-ggdb");
180 } else {
181         # If building a release version:
182         Append("COPT" => "-fomit-frame-pointer");
183         Append("LOPT" => "-s");
184 }
185
186 # Decide how will lib/partmap.c work
187 Set("PARTMAP_IS_MMAP") if IsSet("CPU_64BIT_POINTERS");
188
189 # If debugging memory allocations:
190 #LIBS+=-lefence
191 #CDEBUG+=-DDEBUG_DMALLOC
192 #LIBS+=-ldmalloc
193
194 # Return success
195 1;