]> mj.ucw.cz Git - libucw.git/blob - ucw/perl/UCW/Configure/C.pm
make Sherlock compilable on Darwin without hacks due to missing direct IO
[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 == 23) { $arch = "nocona"; }
134                                         elsif ($model == 15) { $arch = "prescott"; }
135                                         elsif (($model == 9 || $model == 13) && $gccver >= 3004) { $arch = "pentium-m"; }
136                                         elsif ($model >= 7) { $arch = "pentium3"; }
137                                         elsif ($model >= 3) { $arch = "pentium2"; }
138                                 }
139                         }
140
141                         # No match on vendor, try the family
142                         if ($arch eq "") {
143                                 if ($family >= 6) {
144                                         $arch = "i686";
145                                 } elsif ($family >= 3) {
146                                         $arch = "i${family}86";
147                                 }
148                         }
149                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
150                         return $arch;
151                 } else {
152                         return "default";
153                 }
154         } elsif ($mach eq "amd64") {
155                 Set("CPU_AMD64");
156                 Set("CPU_64BIT_POINTERS");
157                 Set("CPU_LITTLE_ENDIAN");
158                 UnSet("CPU_BIG_ENDIAN");
159                 Set("CPU_ALLOW_UNALIGNED");
160                 Set("CPU_STRUCT_ALIGN" => 8);
161                 if (IsSet("CONFIG_EXACT_CPU")) {
162                         # In x86-64 world, the detection is somewhat easier so far...
163                         my ($vendor, $family, $model) = parse_cpuinfo();
164                         if ($vendor eq "AuthenticAMD") {
165                                 $arch = "athlon64";
166                         } elsif ($vendor eq "GenuineIntel") {
167                                 $arch = "nocona";
168                         }
169                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
170                         return $arch;
171                 } else {
172                         return "default";
173                 }
174         } else {
175                 return "unknown";
176         }
177 });
178
179 if (Get("CPU_ARCH") eq "unknown") {
180         Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
181 }
182
183 ### Compiler and its Options ###
184
185 # C flags: tell the compiler we're speaking C99, and disable common symbols
186 Set("CLANG" => "-std=gnu99 -fno-common");
187
188 # C optimizations
189 Set("COPT" => '-O2');
190 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
191         Append("COPT", '-march=$(CPU_ARCH)');
192 }
193
194 # C optimizations for highly exposed code
195 Set("COPT2" => '-O3');
196
197 # Warnings
198 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
199 Set("CWARNS_OFF" => '');
200
201 # Linker flags
202 Set("LOPT" => "");
203
204 # Extra libraries
205 Set("LIBS" => "");
206
207 # Extra flags for compiling and linking shared libraries
208 Set("CSHARED" => '-fPIC');
209 if (IsSet("CONFIG_LOCAL")) {
210         Set("SONAME_PREFIX" => "lib/");
211 } else {
212         Set("SONAME_PREFIX" => "");
213 }
214 if (IsSet("CONFIG_DARWIN")) {
215         Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
216 } else {
217         Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
218 }
219
220 # Extra switches depending on GCC version:
221 if ($gccver == 3000) {
222         Append("COPT" => "-fstrict-aliasing");
223 } elsif ($gccver == 3003) {
224         Append("CWARNS" => "-Wundef -Wredundant-decls");
225         Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
226 } elsif ($gccver == 3004) {
227         Append("CWARNS" => "-Wundef -Wredundant-decls");
228         Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
229 } elsif ($gccver >= 4000) {
230         Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
231         Append("CWARNS_OFF" => "-Wno-pointer-sign");
232         Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
233         if ($gccver >= 4002) {
234                 Append("COPT" => "-fgnu89-inline");
235         }
236 } else {
237         Warn "Don't know anything about this GCC version, using default switches.\n";
238 }
239
240 if (IsSet("CONFIG_DEBUG")) {
241         # If debugging:
242         Set("DEBUG_ASSERTS");
243         Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
244         Set("CDEBUG" => "-ggdb");
245 } else {
246         # If building a release version:
247         Append("COPT" => "-fomit-frame-pointer");
248         Append("LOPT" => "-s");
249 }
250
251 if (IsSet("CONFIG_DARWIN")) {
252         # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
253         Append("CLANG" => "-fnested-functions");
254         # Directory hierarchy of the fink project
255         Append("LIBS" => "-L/sw/lib");
256         Append("COPT" => "-I/sw/include");
257         # Fill in some constants not found in the system header files
258         Set("SOL_TCP" => 6);            # missing in /usr/include/netinet/tcp.h
259         Set("O_DIRECT" => 0);           # Darwin doesn't support direct IO, so make Sherlock at least compilable
260 }
261
262 ### Writing C headers with configuration ###
263
264 sub ConfigHeader($$) {
265         my ($hdr, $rules) = @_;
266         Log "Generating $hdr ... ";
267         open X, ">obj/$hdr" or Fail $!;
268         print X "/* Generated automatically by $0, please don't touch manually. */\n";
269
270         sub match_rules($$) {
271                 my ($rules, $name) = @_;
272                 for (my $i=0; $i < scalar @$rules; $i++) {
273                         my ($r, $v) = ($rules->[$i], $rules->[$i+1]);
274                         return $v if $name =~ $r;
275                 }
276                 return 0;
277         }
278
279         foreach my $x (sort keys %UCW::Configure::vars) {
280                 next unless match_rules($rules, $x);
281                 my $v = $UCW::Configure::vars{$x};
282                 # Try to add quotes if necessary
283                 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
284                 print X "#define $x $v\n";
285         }
286         close X;
287         Log "done\n";
288 }
289
290 AtWrite {
291         ConfigHeader("autoconf.h", [
292                 # Symbols with "_" anywhere in their name are exported
293                 "_" => 1
294         ]);
295 };
296
297 # Return success
298 1;