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