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