1 # Automatic configuration, OS & CPU detection part
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 Test("OS", "Checking on which OS we run", sub {
11 Fail "Unable to determine OS type" if $? || $os eq "";
15 if (Get("OS") eq "Linux") {
17 } elsif (Get("OS") eq "Darwin") {
20 Fail "Don't know how to run on this operating system.";
26 Test("CC", "Checking for C compiler", sub { return "gcc"; });
29 Test("GCCVER", "Checking for GCC version", sub {
31 my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
33 Fail "Unable to determine GCC version" if $? || $ver eq "";
36 my ($gccmaj, $gccmin) = split(/\./, Get("GCCVER"));
37 my $gccver = 1000*$gccmaj + $gccmin;
38 $gccver >= 3000 or Fail "GCC older than 3.0 doesn't support C99 well enough.";
42 Test("ARCH", "Checking for machine architecture", sub {
43 my $mach = `uname -m`;
45 Fail "Unable to determine machine type" if $? || $mach eq "";
46 if ($mach =~ /^i[0-9]86$/) {
48 } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
55 sub parse_cpuinfo_linux() {
56 open X, "/proc/cpuinfo" || undef;
61 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
64 return ($pc{'vendor_id'},
69 sub parse_cpuinfo_darwin() {
70 @cpu = (`sysctl -n machdep.cpu.vendor`,
71 `sysctl -n machdep.cpu.family`,
72 `sysctl -n machdep.cpu.model`);
79 if (IsSet("CONFIG_LINUX")) {
80 @cpu = parse_cpuinfo_linux();
81 } elsif (IsSet("CONFIG_DARWIN")) {
82 @cpu = parse_cpuinfo_darwin();
84 $cpu[0] = "" if !defined $cpu[0];
85 $cpu[1] = 0 if !defined $cpu[1];
86 $cpu[2] = 0 if !defined $cpu[2];
90 Test("CPU_ARCH", "Checking for CPU architecture", sub {
91 my $mach = Get("ARCH");
93 if ($mach eq "i386") {
95 UnSet("CPU_64BIT_POINTERS");
96 Set("CPU_LITTLE_ENDIAN");
97 UnSet("CPU_BIG_ENDIAN");
98 Set("CPU_ALLOW_UNALIGNED");
99 Set("CPU_STRUCT_ALIGN" => 4);
100 if (IsSet("CONFIG_EXACT_CPU")) {
101 my ($vendor, $family, $model) = parse_cpuinfo();
102 # Try to understand CPU vendor, family and model [inspired by MPlayer's configure script]
103 if ($vendor eq "AuthenticAMD") {
105 if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
106 elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
107 else { $arch = "athlon"; }
109 } elsif ($vendor eq "GenuineIntel") {
110 if ($family >= 15 && $gccver >= 3003) {
111 if ($model >= 4) { $arch = "nocona"; }
112 elsif ($model >= 3) { $arch = "prescott"; }
113 else { $arch = "pentium4"; }
114 } elsif ($family == 6 && $gccver >= 3003) {
115 if ($model == 15) { $arch = "prescott"; }
116 elsif (($model == 9 || $model == 13) && $gccver >= 3004) { $arch = "pentium-m"; }
117 elsif ($model >= 7) { $arch = "pentium3"; }
118 elsif ($model >= 3) { $arch = "pentium2"; }
122 # No match on vendor, try the family
126 } elsif ($family >= 3) {
127 $arch = "i${family}86";
130 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
135 } elsif ($mach eq "amd64") {
137 Set("CPU_64BIT_POINTERS");
138 Set("CPU_LITTLE_ENDIAN");
139 UnSet("CPU_BIG_ENDIAN");
140 Set("CPU_ALLOW_UNALIGNED");
141 Set("CPU_STRUCT_ALIGN" => 8);
142 if (IsSet("CONFIG_EXACT_CPU")) {
143 # In x86-64 world, the detection is somewhat easier so far...
144 my ($vendor, $family, $model) = parse_cpuinfo();
145 if ($vendor eq "AuthenticAMD") {
147 } elsif ($vendor eq "GenuineIntel") {
150 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
160 if (Get("CPU_ARCH") eq "unknown") {
161 Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
164 ### Compiler and its Options ###
166 # C flags: tell the compiler we're speaking C99, and disable common symbols
167 Set("CLANG" => "-std=gnu99 -fno-common");
170 Set("COPT" => '-O2');
171 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
172 Append("COPT", '-march=$(CPU_ARCH)');
175 # C optimizations for highly exposed code
176 Set("COPT2" => '-O3');
179 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
180 Set("CWARNS_OFF" => '');
188 # Extra flags for compiling and linking shared libraries
189 Set("CSHARED" => '-fPIC');
190 if (IsSet("CONFIG_LOCAL")) {
191 Set("SONAME_PREFIX" => "lib/");
193 Set("SONAME_PREFIX" => "");
195 if (IsSet("CONFIG_DARWIN")) {
196 Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
198 Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
201 # Extra switches depending on GCC version:
202 if ($gccver == 3000) {
203 Append("COPT" => "-fstrict-aliasing");
204 } elsif ($gccver == 3003) {
205 Append("CWARNS" => "-Wundef -Wredundant-decls");
206 Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
207 } elsif ($gccver == 3004) {
208 Append("CWARNS" => "-Wundef -Wredundant-decls");
209 Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
210 } elsif ($gccver >= 4000) {
211 Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
212 Append("CWARNS_OFF" => "-Wno-pointer-sign");
213 Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
214 if ($gccver >= 4002) {
215 Append("COPT" => "-fgnu89-inline");
218 Warn "Don't know anything about this GCC version, using default switches.\n";
221 if (IsSet("CONFIG_DEBUG")) {
223 Set("DEBUG_ASSERTS");
224 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
225 Set("CDEBUG" => "-ggdb");
227 # If building a release version:
228 Append("COPT" => "-fomit-frame-pointer");
229 Append("LOPT" => "-s");
232 if (IsSet("CONFIG_DARWIN")) {
233 # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
234 Append("CLANG" => "-fnested-functions");
235 # Directory hierarchy of the fink project
236 Append("LIBS" => "-L/sw/lib");
237 Append("COPT" => "-I/sw/include");
238 # Fill in some constants not found in the system header files
239 Set("SOL_TCP" => 6); # missing in /usr/include/netinet/tcp.h