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$/) {
69 return "unknown ($mach)";
73 my $arch = Get("ARCH");
74 if ($arch eq 'i386') {
76 UnSet("CPU_64BIT_POINTERS");
77 Set("CPU_LITTLE_ENDIAN");
78 UnSet("CPU_BIG_ENDIAN");
79 Set("CPU_ALLOW_UNALIGNED");
80 Set("CPU_STRUCT_ALIGN" => 4);
81 } elsif ($arch eq "amd64") {
83 Set("CPU_64BIT_POINTERS");
84 Set("CPU_LITTLE_ENDIAN");
85 UnSet("CPU_BIG_ENDIAN");
86 Set("CPU_ALLOW_UNALIGNED");
87 Set("CPU_STRUCT_ALIGN" => 8);
88 } elsif (!Get("CPU_LITTLE_ENDIAN") && !Get("CPU_BIG_ENDIAN")) {
89 Fail "Architecture not recognized, please set CPU_xxx variables manually.";
92 ### Compiler and its Options ###
94 # C flags: tell the compiler we're speaking C99, and disable common symbols
95 Set("CLANG" => "-std=gnu99 -fno-common");
99 if ($arch =~ /^(i386|amd64)$/ && Get("CONFIG_EXACT_CPU")) {
100 if ($gccver >= 4002) {
101 Append('COPT', '-march=native');
103 Warn "CONFIG_EXACT_CPU not supported with old GCC, ignoring.\n";
107 # C optimizations for highly exposed code
108 Set("COPT2" => '-O3');
111 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes');
112 Set("CWARNS_OFF" => '');
120 # Extra flags for compiling and linking shared libraries
121 Set("CSHARED" => '-fPIC');
122 if (IsSet("CONFIG_LOCAL")) {
123 Set("SONAME_PREFIX" => "lib/");
124 Append("LOPT" => "-Wl,--rpath-link -Wl,run");
126 Set("SONAME_PREFIX" => "");
127 Append("LOPT" => "-Wl,--rpath-link -Wl,run/lib");
129 if (IsSet("CONFIG_DARWIN")) {
130 Set("LSHARED" => '-dynamiclib -install_name $(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
132 Set("LSHARED" => '-shared -Wl,-soname,$(SONAME_PREFIX)$(@F)$(SONAME_SUFFIX)');
135 # Extra switches depending on GCC version:
136 if ($gccver == 3000) {
137 Append("COPT" => "-fstrict-aliasing");
138 } elsif ($gccver == 3003) {
139 Append("CWARNS" => "-Wundef -Wredundant-decls");
140 Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
141 } elsif ($gccver == 3004) {
142 Append("CWARNS" => "-Wundef -Wredundant-decls");
143 Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
144 } elsif ($gccver >= 4000) {
145 Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
146 Append("CWARNS_OFF" => "-Wno-pointer-sign");
147 Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
149 Warn "Don't know anything about this GCC version, using default switches.\n";
152 if (IsSet("CONFIG_DEBUG")) {
154 Set("DEBUG_ASSERTS");
155 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
156 Set("CDEBUG" => "-ggdb");
158 # If building a release version:
159 Append("COPT" => "-fomit-frame-pointer");
160 Append("LOPT" => "-s");
163 if (IsSet("CONFIG_DARWIN")) {
164 # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
165 Append("CLANG" => "-fnested-functions");
166 # Directory hierarchy of the fink project
167 Append("LIBS" => "-L/sw/lib");
168 Append("COPT" => "-I/sw/include");
171 ### Compiling test programs ###
173 sub TestCompile($$) {
174 my ($testname, $source) = @_;
175 my $dir = "conftest-$testname";
176 `rm -rf $dir && mkdir $dir`; $? and Fail "Cannot initialize $dir";
178 open SRC, ">$dir/conftest.c";
183 map { defined($_) ? $_ : "" }
185 Get("CC"), Get("CLANG"), Get("COPT"), Get("CEXTRA"), Get("LIBS"),
186 "conftest.c", "-o", "conftest",
187 ">conftest.log", "2>&1"
192 `rm -rf $dir` unless Get("KEEP_CONFTEST");
197 ### Writing C headers with configuration ###
199 sub ConfigHeader($$) {
200 my ($hdr, $rules) = @_;
201 Log "Generating $hdr ... ";
202 open X, ">obj/$hdr" or Fail $!;
203 print X "/* Generated automatically by $0, please don't touch manually. */\n";
205 sub match_rules($$) {
206 my ($rules, $name) = @_;
207 for (my $i=0; $i < scalar @$rules; $i++) {
208 my ($r, $v) = ($rules->[$i], $rules->[$i+1]);
209 return $v if $name =~ $r;
214 foreach my $x (sort keys %UCW::Configure::vars) {
215 next unless match_rules($rules, $x);
216 my $v = $UCW::Configure::vars{$x};
217 # Try to add quotes if necessary
218 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
219 print X "#define $x $v\n";
226 ConfigHeader("autoconf.h", [
227 # Symbols with "_" anywhere in their name are exported