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 (!Get("CPU_LITTLE_ENDIAN") && !Get("CPU_BIG_ENDIAN")) {
98 Fail "Architecture not recognized, please set CPU_xxx variables manually.";
101 ### Compiler and its Options ###
103 # C flags: tell the compiler we're speaking C99, and disable common symbols
104 Set("CLANG" => "-std=gnu99 -fno-common");
107 Set("COPT" => '-O2');
108 if ($arch =~ /^(i386|amd64)$/ && Get("CONFIG_EXACT_CPU")) {
109 if ($gccver >= 4002) {
110 Append('COPT', '-march=native');
112 Warn "CONFIG_EXACT_CPU not supported with old GCC, ignoring.\n";
116 # C optimizations for highly exposed code
117 Set("COPT2" => '-O3');
120 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes');
121 Set("CWARNS_OFF" => '');
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" => '');
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"));
142 my $libdir = Get("INSTALL_LIB_DIR");
143 if (IsSet("CONFIG_SHARED")) {
144 `mkdir -p $libdir`; Fail("Cannot create $libdir") if $?;
148 if (IsSet("CONFIG_DARWIN")) {
149 Set("LSHARED" => '-dynamiclib -install_name $(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
151 Set("LSHARED" => '-shared -Wl,-soname,$(@F)$(SONAME_SUFFIX)');
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");
168 Warn "Don't know anything about this GCC version, using default switches.\n";
171 if (IsSet("CONFIG_DEBUG")) {
173 Set("DEBUG_ASSERTS");
174 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
175 Set("CDEBUG" => "-ggdb");
177 # If building a release version:
178 Append("COPT" => "-fomit-frame-pointer");
179 Append("LOPT" => "-s");
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");
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");
200 ### Compiling test programs ###
202 sub TestCompile($$) {
203 my ($testname, $source) = @_;
204 my $dir = "conftest-$testname";
205 `rm -rf $dir && mkdir $dir`; $? and Fail "Cannot initialize $dir";
207 open SRC, ">$dir/conftest.c";
212 map { defined($_) ? $_ : "" }
214 Get("CC"), Get("CLANG"), Get("COPT"), Get("CEXTRA"), Get("LIBS"),
215 "conftest.c", "-o", "conftest",
216 ">conftest.log", "2>&1"
221 `rm -rf $dir` unless Get("KEEP_CONFTEST");
226 ### Writing C headers with configuration ###
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";
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;
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";
255 ConfigHeader("autoconf.h", [
256 # Symbols with "_" anywhere in their name are exported