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 == 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"; }
140 # No match on vendor, try the family
144 } elsif ($family >= 3) {
145 $arch = "i${family}86";
148 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
153 } elsif ($mach eq "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") {
165 } elsif ($vendor eq "GenuineIntel") {
168 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
178 if (Get("CPU_ARCH") eq "unknown") {
179 Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
182 ### Compiler and its Options ###
184 # C flags: tell the compiler we're speaking C99, and disable common symbols
185 Set("CLANG" => "-std=gnu99 -fno-common");
188 Set("COPT" => '-O2');
189 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
190 Append("COPT", '-march=$(CPU_ARCH)');
193 # C optimizations for highly exposed code
194 Set("COPT2" => '-O3');
197 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
198 Set("CWARNS_OFF" => '');
206 # Extra flags for compiling and linking shared libraries
207 Set("CSHARED" => '-fPIC');
208 if (IsSet("CONFIG_LOCAL")) {
209 Set("SONAME_PREFIX" => "lib/");
211 Set("SONAME_PREFIX" => "");
213 if (IsSet("CONFIG_DARWIN")) {
214 Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
216 Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
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");
236 Warn "Don't know anything about this GCC version, using default switches.\n";
239 if (IsSet("CONFIG_DEBUG")) {
241 Set("DEBUG_ASSERTS");
242 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
243 Set("CDEBUG" => "-ggdb");
245 # If building a release version:
246 Append("COPT" => "-fomit-frame-pointer");
247 Append("LOPT" => "-s");
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
260 ### Writing C headers with configuration ###
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";
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;
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";
289 ConfigHeader("autoconf.h", [
290 # Symbols with "_" anywhere in their name are exported