]> mj.ucw.cz Git - libucw.git/blob - ucw/autoconf.cfg
Libucw supports installation
[libucw.git] / ucw / autoconf.cfg
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>
5
6 ### OS ###
7
8 Test("OS", "Checking on which OS we run", sub {
9         my $os = `uname`;
10         chomp $os;
11         Fail "Unable to determine OS type" if $? || $os eq "";
12         return $os;
13 });
14
15 if (Get("OS") eq "Linux") {
16         Set("CONFIG_LINUX");
17 } elsif (Get("OS") eq "Darwin") {
18         Set("CONFIG_DARWIN");
19 } else {
20         Fail "Don't know how to run on this operating system.";
21 }
22
23 ### Compiler ###
24
25 # Default compiler
26 Test("CC", "Checking for C compiler", sub { return "gcc"; });
27
28 # GCC version
29 Test("GCCVER", "Checking for GCC version", sub {
30         my $gcc = Get("CC");
31         my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
32         chomp $ver;
33         Fail "Unable to determine GCC version" if $? || $ver eq "";
34         return $ver;
35 });
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.";
39
40 ### CPU ###
41
42 Test("ARCH", "Checking for machine architecture", sub {
43         my $mach = `uname -m`;
44         chomp $mach;
45         Fail "Unable to determine machine type" if $? || $mach eq "";
46         if ($mach =~ /^i[0-9]86$/) {
47                 return "i386";
48         } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
49                 return "amd64";
50         } else {
51                 return "unknown";
52         }
53 });
54
55 sub parse_cpuinfo_linux() {
56         open X, "/proc/cpuinfo" || undef;
57         my %pc = ();
58         while (<X>) {
59                 chomp;
60                 /^$/ && last;
61                 /^([^\t]+)\t+:\s*(.*)$/ and $pc{$1}=$2;
62         }
63         close X;
64         return ($pc{'vendor_id'},
65                 $pc{'cpu family'},
66                 $pc{'model'});
67 }
68
69 sub parse_cpuinfo_darwin() {
70         @cpu = (`sysctl -n machdep.cpu.vendor`,
71                 `sysctl -n machdep.cpu.family`,
72                 `sysctl -n machdep.cpu.model`);
73         chomp @cpu;
74         return @cpu;
75 }
76
77 sub parse_cpuinfo() {
78         my @cpu;
79         if (IsSet("CONFIG_LINUX")) {
80                 @cpu = parse_cpuinfo_linux();
81         } elsif (IsSet("CONFIG_DARWIN")) {
82                 @cpu = parse_cpuinfo_darwin();
83         }
84         $cpu[0] = "" if !defined $cpu[0];
85         $cpu[1] = 0 if !defined $cpu[1];
86         $cpu[2] = 0 if !defined $cpu[2];
87         return @cpu;
88 }
89
90 Test("CPU_ARCH", "Checking for CPU architecture", sub {
91         my $mach = Get("ARCH");
92         my $arch = "";
93         if ($mach eq "i386") {
94                 Set("CPU_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") {
104                                 if ($family >= 6) {
105                                         if ($model >= 31 && $gccver >= 3004) { $arch = "athlon64"; }
106                                         elsif ($model >= 6 && $gccver >= 3003) { $arch = "athlon-xp"; }
107                                         else { $arch = "athlon"; }
108                                 }
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"; }
119                                 }
120                         }
121
122                         # No match on vendor, try the family
123                         if ($arch eq "") {
124                                 if ($family >= 6) {
125                                         $arch = "i686";
126                                 } elsif ($family >= 3) {
127                                         $arch = "i${family}86";
128                                 }
129                         }
130                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
131                         return $arch;
132                 } else {
133                         return "default";
134                 }
135         } elsif ($mach eq "amd64") {
136                 Set("CPU_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") {
146                                 $arch = "athlon64";
147                         } elsif ($vendor eq "GenuineIntel") {
148                                 $arch = "nocona";
149                         }
150                         Log (($arch ne "") ? "(using /proc/cpuinfo) " : "(don't understand /proc/cpuinfo) ");
151                         return $arch;
152                 } else {
153                         return "default";
154                 }
155         } else {
156                 return "unknown";
157         }
158 });
159
160 if (Get("CPU_ARCH") eq "unknown") {
161         Warn "CPU architecture not recognized, using defaults, keep fingers crossed.\n";
162 }
163
164 ### Compiler and its Options ###
165
166 # C flags: tell the compiler we're speaking C99, and disable common symbols
167 Set("CLANG" => "-std=gnu99 -fno-common");
168
169 # C optimizations
170 Set("COPT" => '-O2');
171 if (Get("CPU_ARCH") ne "unknown" && Get("CPU_ARCH") ne "default") {
172         Append("COPT", '-march=$(CPU_ARCH)');
173 }
174
175 # C optimizations for highly exposed code
176 Set("COPT2" => '-O3');
177
178 # Warnings
179 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes -Winline');
180 Set("CWARNS_OFF" => '');
181
182 # Linker flags
183 Set("LOPT" => "");
184
185 # Extra libraries
186 Set("LIBS" => "");
187
188 # Extra flags for compiling and linking shared libraries
189 Set("CSHARED" => '-fPIC');
190 if (IsSet("CONFIG_LOCAL")) {
191         Set("SONAME_PREFIX" => "lib/");
192 } else {
193         Set("SONAME_PREFIX" => "");
194 }
195 if (IsSet("CONFIG_DARWIN")) {
196         Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
197 } else {
198         Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
199 }
200
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");
216         }
217 } else {
218         Warn "Don't know anything about this GCC version, using default switches.\n";
219 }
220
221 if (IsSet("CONFIG_DEBUG")) {
222         # If debugging:
223         Set("DEBUG_ASSERTS");
224         Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
225         Set("CDEBUG" => "-ggdb");
226 } else {
227         # If building a release version:
228         Append("COPT" => "-fomit-frame-pointer");
229         Append("LOPT" => "-s");
230 }
231
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
240 }
241
242 # Return success
243 1;