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>
8 package UCW::Configure::C;
14 Test("OS", "Checking on which OS we run", sub {
17 Fail "Unable to determine OS type" if $? || $os eq "";
21 if (Get("OS") eq "Linux") {
23 } elsif (Get("OS") eq "Darwin") {
26 Fail "Don't know how to run on this operating system.";
32 Test("CC", "Checking for C compiler", sub { return "gcc"; });
35 Test("GCCVER", "Checking for GCC version", sub {
37 my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
39 Fail "Unable to determine GCC version" if $? || $ver eq "";
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.";
48 Test("ARCH", "Checking for machine architecture", sub {
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
56 my $mach = `$gcc -dumpmachine 2>/dev/null`;
57 if (!$? && $mach ne "") {
61 Fail "Unable to determine machine type" if $? || $mach eq "";
64 if ($mach =~ /^i[0-9]86$/) {
66 } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
68 } elsif ($mach eq 'powerpc') {
71 return "unknown ($mach)";
75 my $arch = Get("ARCH");
76 if ($arch eq '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") {
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") {
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") {
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.";
108 ### Compiler and its Options ###
110 # C flags: tell the compiler we're speaking C99, and disable common symbols
111 Set("CLANG" => "-std=gnu99 -fno-common");
114 Set("COPT" => '-O2');
115 if ($arch =~ /^(i386|amd64)$/ && Get("CONFIG_EXACT_CPU")) {
116 if ($gccver >= 4002) {
117 Append('COPT', '-march=native');
119 Warn "CONFIG_EXACT_CPU not supported with old GCC, ignoring.\n";
123 # C optimizations for highly exposed code
124 Set("COPT2" => '-O3');
127 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes');
128 Set("CWARNS_OFF" => '');
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" => '');
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"));
149 my $libdir = Get("INSTALL_LIB_DIR");
150 if (IsSet("CONFIG_SHARED")) {
151 `mkdir -p $libdir`; Fail("Cannot create $libdir") if $?;
155 if (IsSet("CONFIG_DARWIN")) {
156 Set("LSHARED" => '-dynamiclib -install_name $(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
158 Set("LSHARED" => '-shared -Wl,-soname,$(@F)$(SONAME_SUFFIX)');
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");
175 Warn "Don't know anything about this GCC version, using default switches.\n";
178 if (IsSet("CONFIG_DEBUG")) {
180 Set("DEBUG_ASSERTS");
181 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
182 Set("CDEBUG" => "-ggdb");
184 # If building a release version:
185 Append("COPT" => "-fomit-frame-pointer");
186 Append("LOPT" => "-s");
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");
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");
207 ### Compiling test programs ###
209 sub TestCompile($$) {
210 my ($testname, $source) = @_;
211 my $dir = "conftest-$testname";
212 `rm -rf $dir && mkdir $dir`; $? and Fail "Cannot initialize $dir";
214 open SRC, ">$dir/conftest.c";
219 map { defined($_) ? $_ : "" }
221 Get("CC"), Get("CLANG"), Get("COPT"), Get("CEXTRA"), Get("LIBS"),
222 "conftest.c", "-o", "conftest",
223 ">conftest.log", "2>&1"
228 `rm -rf $dir` unless Get("KEEP_CONFTEST");
233 ### Writing C headers with configuration ###
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";
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;
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";
262 ConfigHeader("autoconf.h", [
263 # Symbols with "_" anywhere in their name are exported