]> mj.ucw.cz Git - leo.git/blob - perl/UCW/Configure/C.pm
Updated UCW::Configure to the version from current LibUCW
[leo.git] / perl / UCW / Configure / C.pm
1 # UCW Library configuration system: OS and C compiler
2 # (c) 2005--2012 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 package UCW::Configure::C;
9 use UCW::Configure;
10
11 use strict;
12 use warnings;
13
14 Test("OS", "Checking on which OS we run", sub {
15         my $os = `uname`;
16         chomp $os;
17         Fail "Unable to determine OS type" if $? || $os eq "";
18         return $os;
19 });
20
21 if (Get("OS") eq "Linux") {
22         Set("CONFIG_LINUX");
23 } elsif (Get("OS") eq "Darwin") {
24         Set("CONFIG_DARWIN");
25 } else {
26         Fail "Don't know how to run on this operating system.";
27 }
28
29 ### Compiler ###
30
31 # Default compiler
32 Test("CC", "Checking for C compiler", sub { return "gcc"; });
33
34 # GCC version
35 Test("GCCVER", "Checking for GCC version", sub {
36         my $gcc = Get("CC");
37         my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
38         chomp $ver;
39         Fail "Unable to determine GCC version" if $? || $ver eq "";
40         return $ver;
41 });
42 my ($gccmaj, $gccmin) = split(/\./, Get("GCCVER"));
43 my $gccver = 1000*$gccmaj + $gccmin;
44 $gccver >= 3000 or Fail "GCC older than 3.0 doesn't support C99 well enough.";
45
46 ### CPU ###
47
48 Test("ARCH", "Checking for machine architecture", sub {
49         #
50         # We have to ask GCC for the target architecture, because it may
51         # differ from what uname tells us. This can happen even if we are
52         # not cross-compiling, for example on Linux with amd64 kernel, but
53         # i386 userspace.
54         #
55         my $gcc = Get("CC");
56         my $mach = `$gcc -dumpmachine 2>/dev/null`;
57         if (!$? && $mach ne "") {
58                 $mach =~ s/-.*//;
59         } else {
60                 $mach = `uname -m`;
61                 Fail "Unable to determine machine type" if $? || $mach eq "";
62         }
63         chomp $mach;
64         if ($mach =~ /^i[0-9]86$/) {
65                 return "i386";
66         } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
67                 return "amd64";
68         } elsif ($mach eq 'powerpc') {
69                 return $mach;
70         } else {
71                 return "unknown ($mach)";
72         }
73 });
74
75 my $arch = Get("ARCH");
76 if ($arch eq 'i386') {
77         Set("CPU_I386");
78         UnSet("CPU_64BIT_POINTERS");
79         Set("CPU_LITTLE_ENDIAN");
80         UnSet("CPU_BIG_ENDIAN");
81         Set("CPU_ALLOW_UNALIGNED");
82         Set("CPU_STRUCT_ALIGN" => 4);
83 } elsif ($arch eq "amd64") {
84         Set("CPU_AMD64");
85         Set("CPU_64BIT_POINTERS");
86         Set("CPU_LITTLE_ENDIAN");
87         UnSet("CPU_BIG_ENDIAN");
88         Set("CPU_ALLOW_UNALIGNED");
89         Set("CPU_STRUCT_ALIGN" => 8);
90 } elsif ($arch eq "powerpc") {
91         Set("CPU_AMD64");
92         UnSet("CPU_64BIT_POINTERS");
93         UnSet("CPU_LITTLE_ENDIAN");
94         Set("CPU_BIG_ENDIAN");
95         UnSet("CPU_ALLOW_UNALIGNED");
96         Set("CPU_STRUCT_ALIGN" => 4);
97 } elsif ($arch eq "armhf") {
98         Set("CPU_ARM");
99         UnSet("CPU_64BIT_POINTERS");
100         Set("CPU_LITTLE_ENDIAN");
101         UnSet("CPU_BIG_ENDIAN");
102         UnSet("CPU_ALLOW_UNALIGNED");
103         Set("CPU_STRUCT_ALIGN" => 4);
104 } elsif (!Get("CPU_LITTLE_ENDIAN") && !Get("CPU_BIG_ENDIAN")) {
105         Fail "Architecture not recognized, please set CPU_xxx variables manually.";
106 }
107
108 ### Compiler and its Options ###
109
110 # C flags: tell the compiler we're speaking C99, and disable common symbols
111 Set("CLANG" => "-std=gnu99 -fno-common");
112
113 # C optimizations
114 Set("COPT" => '-O2');
115 if ($arch =~ /^(i386|amd64)$/ && Get("CONFIG_EXACT_CPU")) {
116         if ($gccver >= 4002) {
117                 Append('COPT', '-march=native');
118         } else {
119                 Warn "CONFIG_EXACT_CPU not supported with old GCC, ignoring.\n";
120         }
121 }
122
123 # C optimizations for highly exposed code
124 Set("COPT2" => '-O3');
125
126 # Warnings
127 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes');
128 Set("CWARNS_OFF" => '');
129
130 # Linker flags
131 Set("LOPT" => "");
132
133 # Extra libraries
134 Set("LIBS" => "");
135
136 # Extra flags for compiling and linking shared libraries
137 Set("CSHARED" => '-fPIC');
138 Append("LOPT" => "-Wl,--rpath-link,run/lib -Lrun/lib");
139 if (!IsSet("CONFIG_LOCAL")) {
140         # Beware that in non-local builds the INSTALL_LIB_DIR must exist in
141         # standard search paths for shared libraries.
142         Set("SO_LINK_PATH" => '');
143 }
144 else {
145         # In local builds, we need to link binaries with custom --rpath.
146         # GCC seems to fail when this directory does not exist.
147         Set("SO_LINK_PATH" => "-Wl,--rpath," . Get("INSTALL_LIB_DIR"));
148         AtWrite {
149                 my $libdir = Get("INSTALL_LIB_DIR");
150                 if (IsSet("CONFIG_SHARED")) {
151                         `mkdir -p $libdir`; Fail("Cannot create $libdir") if $?;
152                 }
153         };
154 }
155 if (IsSet("CONFIG_DARWIN")) {
156         Set("LSHARED" => '-dynamiclib -install_name $(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
157 } else {
158         Set("LSHARED" => '-shared -Wl,-soname,$(@F)$(SONAME_SUFFIX)');
159 }
160
161 # Extra switches depending on GCC version:
162 if ($gccver == 3000) {
163         Append("COPT" => "-fstrict-aliasing");
164 } elsif ($gccver == 3003) {
165         Append("CWARNS" => "-Wundef -Wredundant-decls");
166         Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
167 } elsif ($gccver == 3004) {
168         Append("CWARNS" => "-Wundef -Wredundant-decls");
169         Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
170 } elsif ($gccver >= 4000) {
171         Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
172         Append("CWARNS_OFF" => "-Wno-pointer-sign");
173         Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
174 } else {
175         Warn "Don't know anything about this GCC version, using default switches.\n";
176 }
177
178 if (IsSet("CONFIG_DEBUG")) {
179         # If debugging:
180         Set("DEBUG_ASSERTS");
181         Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
182         Set("CDEBUG" => "-ggdb");
183 } else {
184         # If building a release version:
185         Append("COPT" => "-fomit-frame-pointer");
186         Append("LOPT" => "-s");
187 }
188
189 # Link-time optimization (experimental)
190 # This is currently very inefficient, because we do not attempt to disable
191 # optimizations when compiling individual modules. Therefore, we optimize
192 # each shared library module twice: when compiling and when linking.
193 # Doing it properly would require hacking makefiles.
194 if (IsSet("CONFIG_LTO")) {
195         Append("LOPT", "-flto");
196         Append("COPT", "-flto");
197 }
198
199 if (IsSet("CONFIG_DARWIN")) {
200         # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
201         Append("CLANG" => "-fnested-functions");
202         # Directory hierarchy of the fink project
203         Append("LIBS" => "-L/sw/lib");
204         Append("COPT" => "-I/sw/include");
205 }
206
207 ### Compiling test programs ###
208
209 sub TestCompile($$) {
210         my ($testname, $source) = @_;
211         my $dir = "conftest-$testname";
212         `rm -rf $dir && mkdir $dir`; $? and Fail "Cannot initialize $dir";
213
214         open SRC, ">$dir/conftest.c";
215         print SRC $source;
216         close SRC;
217
218         my $cmd = join(" ",
219                 map { defined($_) ? $_ : "" }
220                         "cd $dir &&",
221                         Get("CC"), Get("CLANG"), Get("COPT"), Get("CEXTRA"), Get("LIBS"),
222                         "conftest.c", "-o", "conftest",
223                         ">conftest.log", "2>&1"
224                 );
225         `$cmd`;
226         my $result = !$?;
227
228         `rm -rf $dir` unless Get("KEEP_CONFTEST");
229
230         return $result;
231 }
232
233 ### Writing C headers with configuration ###
234
235 sub ConfigHeader($$) {
236         my ($hdr, $rules) = @_;
237         Log "Generating $hdr ... ";
238         open X, ">obj/$hdr" or Fail $!;
239         print X "/* Generated automatically by $0, please don't touch manually. */\n";
240
241         sub match_rules($$) {
242                 my ($rules, $name) = @_;
243                 for (my $i=0; $i < scalar @$rules; $i++) {
244                         my ($r, $v) = ($rules->[$i], $rules->[$i+1]);
245                         return $v if $name =~ $r;
246                 }
247                 return 0;
248         }
249
250         foreach my $x (sort keys %UCW::Configure::vars) {
251                 next unless match_rules($rules, $x);
252                 my $v = $UCW::Configure::vars{$x};
253                 # Try to add quotes if necessary
254                 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
255                 print X "#define $x $v\n";
256         }
257         close X;
258         Log "done\n";
259 }
260
261 AtWrite {
262         ConfigHeader("autoconf.h", [
263                 # Symbols with "_" anywhere in their name are exported
264                 "_" => 1
265         ]);
266 };
267
268 # Return success
269 1;