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>
8 package UCW::Configure::C;
14 Test("OS", "Checking on which OS we run", sub {
17 Fail "Unable to determine OS type" if $? || $os eq "";
21 if (Get("OS") eq "Linux") {
23 } elsif (Get("OS") eq "Darwin") {
26 Fail "Don't know how to run on this operating system.";
32 Test("CC", "Checking for C compiler", sub { return "gcc"; });
35 Test("GCCVER", "Checking for GCC version", sub {
37 my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
39 Fail "Unable to determine GCC version" if $? || $ver eq "";
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.";
48 Test("ARCH", "Checking for machine architecture", sub {
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
56 my $mach = `$gcc -dumpmachine 2>/dev/null`;
57 if (!$? && $mach ne "") {
61 Fail "Unable to determine machine type" if $? || $mach eq "";
64 if ($mach =~ /^i[0-9]86$/) {
66 } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
69 return "unknown ($mach)";
73 sub parse_cpuinfo_linux() {
74 open X, "/proc/cpuinfo" || undef;
79 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
82 return ($pc{'vendor_id'},
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`);
97 if (IsSet("CONFIG_LINUX")) {
98 @cpu = parse_cpuinfo_linux();
99 } elsif (IsSet("CONFIG_DARWIN")) {
100 @cpu = parse_cpuinfo_darwin();
102 $cpu[0] = "" if !defined $cpu[0];
103 $cpu[1] = 0 if !defined $cpu[1];
104 $cpu[2] = 0 if !defined $cpu[2];
108 Test("CPU_ARCH", "Checking for CPU architecture", sub {
109 my $mach = Get("ARCH");
111 if ($mach eq "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") {
123 if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
124 elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
125 else { $arch = "athlon"; }
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"; }
141 # No match on vendor, try the family
145 } elsif ($family >= 3) {
146 $arch = "i${family}86";
149 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
154 } elsif ($mach eq "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") {
166 } elsif ($vendor eq "GenuineIntel") {
169 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
179 if (Get("CPU_ARCH") eq "unknown") {
180 Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
183 ### Compiler and its Options ###
185 # C flags: tell the compiler we're speaking C99, and disable common symbols
186 Set("CLANG" => "-std=gnu99 -fno-common");
189 Set("COPT" => '-O2');
190 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
191 Append("COPT", '-march=$(CPU_ARCH)');
194 # C optimizations for highly exposed code
195 Set("COPT2" => '-O3');
198 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
199 Set("CWARNS_OFF" => '');
207 # Extra flags for compiling and linking shared libraries
208 Set("CSHARED" => '-fPIC');
209 if (IsSet("CONFIG_LOCAL")) {
210 Set("SONAME_PREFIX" => "lib/");
212 Set("SONAME_PREFIX" => "");
214 if (IsSet("CONFIG_DARWIN")) {
215 Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
217 Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
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");
237 Warn "Don't know anything about this GCC version, using default switches.\n";
240 if (IsSet("CONFIG_DEBUG")) {
242 Set("DEBUG_ASSERTS");
243 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
244 Set("CDEBUG" => "-ggdb");
246 # If building a release version:
247 Append("COPT" => "-fomit-frame-pointer");
248 Append("LOPT" => "-s");
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 if(IsGiven("O_DIRECT") && IsSet("O_DIRECT") && (Get("O_DIRECT") != 0))
261 Fail("Darwin does not support direct IO");
263 Set("O_DIRECT" => 0);
266 ### Writing C headers with configuration ###
268 sub ConfigHeader($$) {
269 my ($hdr, $rules) = @_;
270 Log "Generating $hdr ... ";
271 open X, ">obj/$hdr" or Fail $!;
272 print X "/* Generated automatically by $0, please don't touch manually. */\n";
274 sub match_rules($$) {
275 my ($rules, $name) = @_;
276 for (my $i=0; $i < scalar @$rules; $i++) {
277 my ($r, $v) = ($rules->[$i], $rules->[$i+1]);
278 return $v if $name =~ $r;
283 foreach my $x (sort keys %UCW::Configure::vars) {
284 next unless match_rules($rules, $x);
285 my $v = $UCW::Configure::vars{$x};
286 # Try to add quotes if necessary
287 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
288 print X "#define $x $v\n";
295 ConfigHeader("autoconf.h", [
296 # Symbols with "_" anywhere in their name are exported