]> mj.ucw.cz Git - libucw.git/blob - ucw/perl/UCW/Configure/C.pm
b14759258ff9c83eb9a45311be989eb6482c25a9
[libucw.git] / ucw / 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 (!Get("CPU_LITTLE_ENDIAN") && !Get("CPU_BIG_ENDIAN")) {
98         Fail "Architecture not recognized, please set CPU_xxx variables manually.";
99 }
100
101 ### Compiler and its Options ###
102
103 # C flags: tell the compiler we're speaking C99, and disable common symbols
104 Set("CLANG" => "-std=gnu99 -fno-common");
105
106 # C optimizations
107 Set("COPT" => '-O2');
108 if ($arch =~ /^(i386|amd64)$/ && Get("CONFIG_EXACT_CPU")) {
109         if ($gccver >= 4002) {
110                 Append('COPT', '-march=native');
111         } else {
112                 Warn "CONFIG_EXACT_CPU not supported with old GCC, ignoring.\n";
113         }
114 }
115
116 # C optimizations for highly exposed code
117 Set("COPT2" => '-O3');
118
119 # Warnings
120 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes');
121 Set("CWARNS_OFF" => '');
122
123 # Linker flags
124 Set("LOPT" => "");
125
126 # Extra libraries
127 Set("LIBS" => "");
128
129 # Extra flags for compiling and linking shared libraries
130 Set("CSHARED" => '-fPIC');
131 Append("LOPT" => "-Wl,--rpath-link,run/lib -Lrun/lib");
132 if (!IsSet("CONFIG_LOCAL")) {
133         # Beware that in non-local builds the INSTALL_LIB_DIR must exist in
134         # standard search paths for shared libraries.
135         Set("SO_LINK_PATH" => '');
136 }
137 else {
138         # In local builds, we need to link binaries with custom --rpath.
139         # GCC seems to fail when this directory does not exist.
140         Set("SO_LINK_PATH" => "-Wl,--rpath," . Get("INSTALL_LIB_DIR"));
141         AtWrite {
142                 my $libdir = Get("INSTALL_LIB_DIR");
143                 if (IsSet("CONFIG_SHARED")) {
144                         `mkdir -p $libdir`; Fail("Cannot create $libdir") if $?;
145                 }
146         };
147 }
148 if (IsSet("CONFIG_DARWIN")) {
149         Set("LSHARED" => '-dynamiclib -install_name $(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
150 } else {
151         Set("LSHARED" => '-shared -Wl,-soname,$(@F)$(SONAME_SUFFIX)');
152 }
153
154 # Extra switches depending on GCC version:
155 if ($gccver == 3000) {
156         Append("COPT" => "-fstrict-aliasing");
157 } elsif ($gccver == 3003) {
158         Append("CWARNS" => "-Wundef -Wredundant-decls");
159         Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
160 } elsif ($gccver == 3004) {
161         Append("CWARNS" => "-Wundef -Wredundant-decls");
162         Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
163 } elsif ($gccver >= 4000) {
164         Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
165         Append("CWARNS_OFF" => "-Wno-pointer-sign");
166         Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
167 } else {
168         Warn "Don't know anything about this GCC version, using default switches.\n";
169 }
170
171 if (IsSet("CONFIG_DEBUG")) {
172         # If debugging:
173         Set("DEBUG_ASSERTS");
174         Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
175         Set("CDEBUG" => "-ggdb");
176 } else {
177         # If building a release version:
178         Append("COPT" => "-fomit-frame-pointer");
179         Append("LOPT" => "-s");
180 }
181
182 # Link-time optimization (experimental)
183 # This is currently very inefficient, because we do not attempt to disable
184 # optimizations when compiling individual modules. Therefore, we optimize
185 # each shared library module twice: when compiling and when linking.
186 # Doing it properly would require hacking makefiles.
187 if (IsSet("CONFIG_LTO")) {
188         Append("LOPT", "-flto");
189         Append("COPT", "-flto");
190 }
191
192 if (IsSet("CONFIG_DARWIN")) {
193         # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
194         Append("CLANG" => "-fnested-functions");
195         # Directory hierarchy of the fink project
196         Append("LIBS" => "-L/sw/lib");
197         Append("COPT" => "-I/sw/include");
198 }
199
200 ### Compiling test programs ###
201
202 sub TestCompile($$) {
203         my ($testname, $source) = @_;
204         my $dir = "conftest-$testname";
205         `rm -rf $dir && mkdir $dir`; $? and Fail "Cannot initialize $dir";
206
207         open SRC, ">$dir/conftest.c";
208         print SRC $source;
209         close SRC;
210
211         my $cmd = join(" ",
212                 map { defined($_) ? $_ : "" }
213                         "cd $dir &&",
214                         Get("CC"), Get("CLANG"), Get("COPT"), Get("CEXTRA"), Get("LIBS"),
215                         "conftest.c", "-o", "conftest",
216                         ">conftest.log", "2>&1"
217                 );
218         `$cmd`;
219         my $result = !$?;
220
221         `rm -rf $dir` unless Get("KEEP_CONFTEST");
222
223         return $result;
224 }
225
226 ### Writing C headers with configuration ###
227
228 sub ConfigHeader($$) {
229         my ($hdr, $rules) = @_;
230         Log "Generating $hdr ... ";
231         open X, ">obj/$hdr" or Fail $!;
232         print X "/* Generated automatically by $0, please don't touch manually. */\n";
233
234         sub match_rules($$) {
235                 my ($rules, $name) = @_;
236                 for (my $i=0; $i < scalar @$rules; $i++) {
237                         my ($r, $v) = ($rules->[$i], $rules->[$i+1]);
238                         return $v if $name =~ $r;
239                 }
240                 return 0;
241         }
242
243         foreach my $x (sort keys %UCW::Configure::vars) {
244                 next unless match_rules($rules, $x);
245                 my $v = $UCW::Configure::vars{$x};
246                 # Try to add quotes if necessary
247                 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
248                 print X "#define $x $v\n";
249         }
250         close X;
251         Log "done\n";
252 }
253
254 AtWrite {
255         ConfigHeader("autoconf.h", [
256                 # Symbols with "_" anywhere in their name are exported
257                 "_" => 1
258         ]);
259 };
260
261 # Return success
262 1;