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 {
49 my $mach = `uname -m`;
51 Fail "Unable to determine machine type" if $? || $mach eq "";
52 if ($mach =~ /^i[0-9]86$/) {
54 } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
61 sub parse_cpuinfo_linux() {
62 open X, "/proc/cpuinfo" || undef;
67 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
70 return ($pc{'vendor_id'},
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`);
85 if (IsSet("CONFIG_LINUX")) {
86 @cpu = parse_cpuinfo_linux();
87 } elsif (IsSet("CONFIG_DARWIN")) {
88 @cpu = parse_cpuinfo_darwin();
90 $cpu[0] = "" if !defined $cpu[0];
91 $cpu[1] = 0 if !defined $cpu[1];
92 $cpu[2] = 0 if !defined $cpu[2];
96 Test("CPU_ARCH", "Checking for CPU architecture", sub {
97 my $mach = Get("ARCH");
99 if ($mach eq "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") {
111 if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
112 elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
113 else { $arch = "athlon"; }
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"; }
128 # No match on vendor, try the family
132 } elsif ($family >= 3) {
133 $arch = "i${family}86";
136 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
141 } elsif ($mach eq "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") {
153 } elsif ($vendor eq "GenuineIntel") {
156 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
166 if (Get("CPU_ARCH") eq "unknown") {
167 Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
170 ### Compiler and its Options ###
172 # C flags: tell the compiler we're speaking C99, and disable common symbols
173 Set("CLANG" => "-std=gnu99 -fno-common");
176 Set("COPT" => '-O2');
177 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
178 Append("COPT", '-march=$(CPU_ARCH)');
181 # C optimizations for highly exposed code
182 Set("COPT2" => '-O3');
185 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
186 Set("CWARNS_OFF" => '');
194 # Extra flags for compiling and linking shared libraries
195 Set("CSHARED" => '-fPIC');
196 if (IsSet("CONFIG_LOCAL")) {
197 Set("SONAME_PREFIX" => "lib/");
199 Set("SONAME_PREFIX" => "");
201 if (IsSet("CONFIG_DARWIN")) {
202 Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
204 Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
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");
224 Warn "Don't know anything about this GCC version, using default switches.\n";
227 if (IsSet("CONFIG_DEBUG")) {
229 Set("DEBUG_ASSERTS");
230 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
231 Set("CDEBUG" => "-ggdb");
233 # If building a release version:
234 Append("COPT" => "-fomit-frame-pointer");
235 Append("LOPT" => "-s");
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
248 ### Writing C headers with configuration ###
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";
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;
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";
277 ConfigHeader("autoconf.h", [
278 # Symbols with "_" anywhere in their name are exported