1 # Automatic configuration of the UCW Library
2 # (c) 2005--2007 Martin Mares <mj@ucw.cz>
3 # (c) 2006 Robert Spalek <robert@ucw.cz>
7 Test("OS", "Checking on which OS we run", sub {
10 Fail "Unable to determine OS type" if $? || $os eq "";
14 if (Get("OS") eq "Linux") {
16 } elsif (Get("OS") eq "Darwin") {
19 Fail "Don't know how to run on this operating system.";
25 Test("CC", "Checking for C compiler", sub { return "gcc"; });
28 Test("GCCVER", "Checking for GCC version", sub {
30 my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
32 Fail "Unable to determine GCC version" if $? || $ver eq "";
35 my ($gccmaj, $gccmin) = split(/\./, Get("GCCVER"));
36 my $gccver = 1000*$gccmaj + $gccmin;
37 $gccver >= 3000 or Fail "GCC older than 3.0 doesn't support C99 well enough.";
41 Test("ARCH", "Checking for machine architecture", sub {
42 my $mach = `uname -m`;
44 Fail "Unable to determine machine type" if $? || $mach eq "";
45 if ($mach =~ /^i[0-9]86$/) {
47 } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
54 sub parse_cpuinfo_linux() {
55 open X, "/proc/cpuinfo" || undef;
60 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
63 return ($pc{'vendor_id'},
68 sub parse_cpuinfo_darwin() {
69 @cpu = (`sysctl -n machdep.cpu.vendor`,
70 `sysctl -n machdep.cpu.family`,
71 `sysctl -n machdep.cpu.model`);
78 if (IsSet("CONFIG_LINUX")) {
79 @cpu = parse_cpuinfo_linux();
80 } elsif (IsSet("CONFIG_DARWIN")) {
81 @cpu = parse_cpuinfo_darwin();
83 $cpu[0] = "" if !defined $cpu[0];
84 $cpu[1] = 0 if !defined $cpu[1];
85 $cpu[2] = 0 if !defined $cpu[2];
89 Test("CPU_ARCH", "Checking for CPU architecture", sub {
90 my $mach = Get("ARCH");
92 if ($mach eq "i386") {
94 UnSet("CPU_64BIT_POINTERS");
95 Set("CPU_LITTLE_ENDIAN");
96 UnSet("CPU_BIG_ENDIAN");
97 Set("CPU_ALLOW_UNALIGNED");
98 Set("CPU_STRUCT_ALIGN" => 4);
99 if (IsSet("CONFIG_EXACT_CPU")) {
100 my ($vendor, $family, $model) = parse_cpuinfo();
101 # Try to understand CPU vendor, family and model [inspired by MPlayer's configure script]
102 if ($vendor eq "AuthenticAMD") {
104 if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
105 elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
106 else { $arch = "athlon"; }
108 } elsif ($vendor eq "GenuineIntel") {
109 if ($family >= 15 && $gccver >= 3003) {
110 if ($model >= 4) { $arch = "nocona"; }
111 elsif ($model >= 3) { $arch = "prescott"; }
112 else { $arch = "pentium4"; }
113 } elsif ($family == 6 && $gccver >= 3003) {
114 if ($model == 15) { $arch = "prescott"; }
115 elsif (($model == 9 || $model == 13) && $gccver >= 3004) { $arch = "pentium-m"; }
116 elsif ($model >= 7) { $arch = "pentium3"; }
117 elsif ($model >= 3) { $arch = "pentium2"; }
121 # No match on vendor, try the family
125 } elsif ($family >= 3) {
126 $arch = "i${family}86";
129 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
134 } elsif ($mach eq "amd64") {
136 Set("CPU_64BIT_POINTERS");
137 Set("CPU_LITTLE_ENDIAN");
138 UnSet("CPU_BIG_ENDIAN");
139 Set("CPU_ALLOW_UNALIGNED");
140 Set("CPU_STRUCT_ALIGN" => 8);
141 if (IsSet("CONFIG_EXACT_CPU")) {
142 # In x86-64 world, the detection is somewhat easier so far...
143 my ($vendor, $family, $model) = parse_cpuinfo();
144 if ($vendor eq "AuthenticAMD") {
146 } elsif ($vendor eq "GenuineIntel") {
149 Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
159 if (Get("CPU_ARCH") eq "unknown") {
160 Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
163 ### Compiler and its Options ###
165 # C flags: tell the compiler we're speaking C99, and disable common symbols
166 Set("CLANG" => "-std=gnu99 -fno-common");
169 Set("COPT" => '-O2');
170 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
171 Append("COPT", '-march=$(CPU_ARCH)');
174 # C optimizations for highly exposed code
175 Set("COPT2" => '-O3');
178 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
179 Set("CWARNS_OFF" => '');
187 # Extra flags for compiling and linking shared libraries
188 Set("CSHARED" => '-fPIC');
189 if (IsSet("CONFIG_DARWIN")) {
190 Set("LSHARED" => '-dynamiclib -install_name lib/$(@F) -undefined dynamic_lookup');
192 Set("LSHARED" => '-shared -Wl,-soname,lib/$(@F)');
195 # Extra switches depending on GCC version:
196 if ($gccver == 3000) {
197 Append("COPT" => "-fstrict-aliasing");
198 } elsif ($gccver == 3003) {
199 Append("CWARNS" => "-Wundef -Wredundant-decls");
200 Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
201 } elsif ($gccver == 3004) {
202 Append("CWARNS" => "-Wundef -Wredundant-decls");
203 Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
204 } elsif ($gccver == 4000 || $gccver == 4001) {
205 Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
206 Append("CWARNS_OFF" => "-Wno-pointer-sign");
207 Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
208 } elsif ($gccver == 4002) {
209 Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
210 Append("CWARNS_OFF" => "-Wno-pointer-sign");
211 Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400 -fgnu89-inline");
213 Warn "Don't know anything about this GCC version, using default switches.\n";
216 if (IsSet("CONFIG_DEBUG")) {
218 Set("DEBUG_ASSERTS");
219 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
220 Set("CDEBUG" => "-ggdb");
222 # If building a release version:
223 Append("COPT" => "-fomit-frame-pointer");
224 Append("LOPT" => "-s");
227 if (IsSet("CONFIG_DARWIN")) {
228 # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
229 Append("CLANG" => "-fnested-functions");
230 # Directory hierarchy of the fink project
231 Append("LIBS" => "-L/sw/lib");
232 Append("COPT" => "-I/sw/include");
233 # Fill in some constants not found in the system header files
234 Set("SOL_TCP" => 6); # missing in /usr/include/netinet/tcp.h
237 # Determine page size
238 Test("CPU_PAGE_SIZE", "Determining page size", sub {
240 if (IsSet("CONFIG_DARWIN")) {
241 $p = `sysctl -n hw.pagesize`;
242 defined $p or Fail "sysctl hw.pagesize failed";
243 } elsif (IsSet("CONFIG_LINUX")) {
244 $p = `getconf PAGE_SIZE`;
245 defined $p or Fail "getconf PAGE_SIZE failed";
251 if (IsSet("CONFIG_LARGE_FILES") && IsSet("CONFIG_LINUX")) {
252 # Use 64-bit versions of file functions
256 # Decide how will lib/partmap.c work
257 Set("PARTMAP_IS_MMAP") if IsSet("CPU_64BIT_POINTERS");
259 # Option for lib/mempool.c
262 # Guess optimal bit width of the radix-sorter
263 if (Get("CPU_ARCH") eq "default" || Get("CPU_ARCH") =~ /^i[345]86$/) {
264 # This should be safe everywhere
265 Set("CONFIG_UCW_RADIX_SORTER_BITS" => 10);
267 # Use this on modern CPU's
268 Set("CONFIG_UCW_RADIX_SORTER_BITS" => 12);
271 # If debugging memory allocations:
273 #CDEBUG+=-DDEBUG_DMALLOC