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