]> mj.ucw.cz Git - libucw.git/blob - ucw/autoconf.cfg
ucw docs: Hash routines
[libucw.git] / ucw / autoconf.cfg
1 # Automatic configuration of the UCW Library
2 # (c) 2005--2008 Martin Mares <mj@ucw.cz>
3 # (c) 2006 Robert Spalek <robert@ucw.cz>
4
5 ### Installation paths ###
6
7 Log "Determining installation prefix ... ";
8 if (IsSet("CONFIG_LOCAL")) {
9         Log "local build\n";
10         Set("INSTALL_PREFIX", "");
11         Set("INSTALL_USR_PREFIX", "");
12         Set("INSTALL_VAR_PREFIX", "");
13 } else {
14         Set("PREFIX", "/usr/local") unless IsSet("PREFIX");
15         my $ipx = Get("PREFIX");
16         $ipx =~ s{/$}{};
17         Set("INSTALL_PREFIX", "$ipx/");
18         my $upx = ($ipx eq "" ? "/usr/" : "$ipx/");
19         Set("INSTALL_USR_PREFIX", $upx);
20         $upx =~ s{^/usr\b}{/var};
21         Set("INSTALL_VAR_PREFIX", $upx);
22         Log Get("PREFIX") . "\n";
23 }
24
25 Set("INSTALL_CONFIG_DIR", '$(INSTALL_PREFIX)$(CONFIG_DIR)');
26 Set("INSTALL_BIN_DIR", '$(INSTALL_USR_PREFIX)bin');
27 Set("INSTALL_SBIN_DIR", '$(INSTALL_USR_PREFIX)sbin');
28 Set("INSTALL_LIB_DIR", '$(INSTALL_USR_PREFIX)lib');
29 Set("INSTALL_INCLUDE_DIR", '$(INSTALL_USR_PREFIX)include');
30 Set("INSTALL_PKGCONFIG_DIR", '$(INSTALL_USR_PREFIX)lib/pkgconfig');
31 Set("INSTALL_SHARE_DIR", '$(INSTALL_USR_PREFIX)share');
32 Set("INSTALL_MAN_DIR", '$(INSTALL_USR_PREFIX)share/man');
33 Set("INSTALL_LOG_DIR", '$(INSTALL_VAR_PREFIX)log');
34 Set("INSTALL_STATE_DIR", '$(INSTALL_VAR_PREFIX)lib');
35 Set("INSTALL_RUN_DIR", '$(INSTALL_VAR_PREFIX)run');
36
37 ### OS ###
38
39 Test("OS", "Checking on which OS we run", sub {
40         my $os = `uname`;
41         chomp $os;
42         Fail "Unable to determine OS type" if $? || $os eq "";
43         return $os;
44 });
45
46 if (Get("OS") eq "Linux") {
47         Set("CONFIG_LINUX");
48 } elsif (Get("OS") eq "Darwin") {
49         Set("CONFIG_DARWIN");
50 } else {
51         Fail "Don't know how to run on this operating system.";
52 }
53
54 ### Compiler ###
55
56 # Default compiler
57 Test("CC", "Checking for C compiler", sub { return "gcc"; });
58
59 # GCC version
60 Test("GCCVER", "Checking for GCC version", sub {
61         my $gcc = Get("CC");
62         my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
63         chomp $ver;
64         Fail "Unable to determine GCC version" if $? || $ver eq "";
65         return $ver;
66 });
67 my ($gccmaj, $gccmin) = split(/\./, Get("GCCVER"));
68 my $gccver = 1000*$gccmaj + $gccmin;
69 $gccver >= 3000 or Fail "GCC older than 3.0 doesn't support C99 well enough.";
70
71 ### CPU ###
72
73 Test("ARCH", "Checking for machine architecture", sub {
74         my $mach = `uname -m`;
75         chomp $mach;
76         Fail "Unable to determine machine type" if $? || $mach eq "";
77         if ($mach =~ /^i[0-9]86$/) {
78                 return "i386";
79         } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
80                 return "amd64";
81         } else {
82                 return "unknown";
83         }
84 });
85
86 sub parse_cpuinfo_linux() {
87         open X, "/proc/cpuinfo" || undef;
88         my %pc = ();
89         while (<X>) {
90                 chomp;
91                 /^$/ && last;
92                 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
93         }
94         close X;
95         return ($pc{'vendor_id'},
96                 $pc{'cpu family'},
97                 $pc{'model'});
98 }
99
100 sub parse_cpuinfo_darwin() {
101         @cpu = (`sysctl -n machdep.cpu.vendor`,
102                 `sysctl -n machdep.cpu.family`,
103                 `sysctl -n machdep.cpu.model`);
104         chomp @cpu;
105         return @cpu;
106 }
107
108 sub parse_cpuinfo() {
109         my @cpu;
110         if (IsSet("CONFIG_LINUX")) {
111                 @cpu = parse_cpuinfo_linux();
112         } elsif (IsSet("CONFIG_DARWIN")) {
113                 @cpu = parse_cpuinfo_darwin();
114         }
115         $cpu[0] = "" if !defined $cpu[0];
116         $cpu[1] = 0 if !defined $cpu[1];
117         $cpu[2] = 0 if !defined $cpu[2];
118         return @cpu;
119 }
120
121 Test("CPU_ARCH", "Checking for CPU architecture", sub {
122         my $mach = Get("ARCH");
123         my $arch = "";
124         if ($mach eq "i386") {
125                 Set("CPU_I386");
126                 UnSet("CPU_64BIT_POINTERS");
127                 Set("CPU_LITTLE_ENDIAN");
128                 UnSet("CPU_BIG_ENDIAN");
129                 Set("CPU_ALLOW_UNALIGNED");
130                 Set("CPU_STRUCT_ALIGN" => 4);
131                 if (IsSet("CONFIG_EXACT_CPU")) {
132                         my ($vendor, $family, $model) = parse_cpuinfo();
133                         # Try to understand CPU vendor, family and model [inspired by MPlayer's configure script]
134                         if ($vendor eq "AuthenticAMD") {
135                                 if ($family >= 6) {
136                                         if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
137                                         elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
138                                         else { $arch = "athlon"; }
139                                 }
140                         } elsif ($vendor eq "GenuineIntel") {
141                                 if ($family >= 15 && $gccver >= 3003) {
142                                         if ($model >= 4) { $arch = "nocona"; }
143                                         elsif ($model >= 3) { $arch = "prescott"; }
144                                         else { $arch = "pentium4"; }
145                                 } elsif ($family == 6 && $gccver >= 3003) {
146                                         if ($model == 15) { $arch = "prescott"; }
147                                         elsif (($model == 9 || $model == 13) && $gccver >= 3004) { $arch = "pentium-m"; }
148                                         elsif ($model >= 7) { $arch = "pentium3"; }
149                                         elsif ($model >= 3) { $arch = "pentium2"; }
150                                 }
151                         }
152
153                         # No match on vendor, try the family
154                         if ($arch eq "") {
155                                 if ($family >= 6) {
156                                         $arch = "i686";
157                                 } elsif ($family >= 3) {
158                                         $arch = "i${family}86";
159                                 }
160                         }
161                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
162                         return $arch;
163                 } else {
164                         return "default";
165                 }
166         } elsif ($mach eq "amd64") {
167                 Set("CPU_AMD64");
168                 Set("CPU_64BIT_POINTERS");
169                 Set("CPU_LITTLE_ENDIAN");
170                 UnSet("CPU_BIG_ENDIAN");
171                 Set("CPU_ALLOW_UNALIGNED");
172                 Set("CPU_STRUCT_ALIGN" => 8);
173                 if (IsSet("CONFIG_EXACT_CPU")) {
174                         # In x86-64 world, the detection is somewhat easier so far...
175                         my ($vendor, $family, $model) = parse_cpuinfo();
176                         if ($vendor eq "AuthenticAMD") {
177                                 $arch = "athlon64";
178                         } elsif ($vendor eq "GenuineIntel") {
179                                 $arch = "nocona";
180                         }
181                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
182                         return $arch;
183                 } else {
184                         return "default";
185                 }
186         } else {
187                 return "unknown";
188         }
189 });
190
191 if (Get("CPU_ARCH") eq "unknown") {
192         Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
193 }
194
195 ### Compiler and its Options ###
196
197 # C flags: tell the compiler we're speaking C99, and disable common symbols
198 Set("CLANG" => "-std=gnu99 -fno-common");
199
200 # C optimizations
201 Set("COPT" => '-O2');
202 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
203         Append("COPT", '-march=$(CPU_ARCH)');
204 }
205
206 # C optimizations for highly exposed code
207 Set("COPT2" => '-O3');
208
209 # Warnings
210 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
211 Set("CWARNS_OFF" => '');
212
213 # Linker flags
214 Set("LOPT" => "");
215
216 # Extra libraries
217 Set("LIBS" => "");
218
219 # Extra flags for compiling and linking shared libraries
220 Set("CSHARED" => '-fPIC');
221 if (IsSet("CONFIG_LOCAL")) {
222         Set("SONAME_PREFIX" => "lib/");
223 } else {
224         Set("SONAME_PREFIX" => "");
225 }
226 if (IsSet("CONFIG_DARWIN")) {
227         Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
228 } else {
229         Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
230 }
231
232 # Extra switches depending on GCC version:
233 if ($gccver == 3000) {
234         Append("COPT" => "-fstrict-aliasing");
235 } elsif ($gccver == 3003) {
236         Append("CWARNS" => "-Wundef -Wredundant-decls");
237         Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
238 } elsif ($gccver == 3004) {
239         Append("CWARNS" => "-Wundef -Wredundant-decls");
240         Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
241 } elsif ($gccver >= 4000) {
242         Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
243         Append("CWARNS_OFF" => "-Wno-pointer-sign");
244         Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
245         if ($gccver >= 4002) {
246                 Append("COPT" => "-fgnu89-inline");
247         }
248 } else {
249         Warn "Don't know anything about this GCC version, using default switches.\n";
250 }
251
252 if (IsSet("CONFIG_DEBUG")) {
253         # If debugging:
254         Set("DEBUG_ASSERTS");
255         Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
256         Set("CDEBUG" => "-ggdb");
257 } else {
258         # If building a release version:
259         Append("COPT" => "-fomit-frame-pointer");
260         Append("LOPT" => "-s");
261 }
262
263 if (IsSet("CONFIG_DARWIN")) {
264         # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
265         Append("CLANG" => "-fnested-functions");
266         # Directory hierarchy of the fink project
267         Append("LIBS" => "-L/sw/lib");
268         Append("COPT" => "-I/sw/include");
269         # Fill in some constants not found in the system header files
270         Set("SOL_TCP" => 6);            # missing in /usr/include/netinet/tcp.h
271 }
272
273 # Determine page size
274 Test("CPU_PAGE_SIZE", "Determining page size", sub {
275         my $p;
276         if (IsSet("CONFIG_DARWIN")) {
277                 $p = `sysctl -n hw.pagesize`;
278                 defined $p or Fail "sysctl hw.pagesize failed";
279         } elsif (IsSet("CONFIG_LINUX")) {
280                 $p = `getconf PAGE_SIZE`;
281                 defined $p or Fail "getconf PAGE_SIZE failed";
282         }
283         chomp $p;
284         return $p;
285 });
286
287 if (IsSet("CONFIG_LARGE_FILES") && IsSet("CONFIG_LINUX")) {
288         # Use 64-bit versions of file functions
289         Set("CONFIG_LFS");
290 }
291
292 # Decide how will ucw/partmap.c work
293 Set("PARTMAP_IS_MMAP") if IsSet("CPU_64BIT_POINTERS");
294
295 # Option for ucw/mempool.c
296 Set("POOL_IS_MMAP");
297
298 # Guess optimal bit width of the radix-sorter
299 if (Get("CPU_ARCH") eq "default" || Get("CPU_ARCH") =~ /^i[345]86$/) {
300         # This should be safe everywhere
301         Set("CONFIG_UCW_RADIX_SORTER_BITS" => 10);
302 } else {
303         # Use this on modern CPU's
304         Set("CONFIG_UCW_RADIX_SORTER_BITS" => 12);
305 }
306
307 # If debugging memory allocations:
308 #LIBS+=-lefence
309
310 # Remember PKG_CONFIG_PATH used for building, so that it will be propagated to
311 # pkg-config's run locally in the makefiles.
312 Set("PKG_CONFIG_PATH", $ENV{"PKG_CONFIG_PATH"}) if defined $ENV{"PKG_CONFIG_PATH"};
313
314 # Return success
315 1;