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 # (c) 2023 Milan Rusek <milan.rusek@economia.cz>
9 package UCW::Configure::C;
15 Test("OS", "Checking on which OS we run", sub {
18 Fail "Unable to determine OS type" if $? || $os eq "";
22 if (Get("OS") eq "Linux") {
24 } elsif (Get("OS") eq "Darwin") {
27 Fail "Don't know how to run on this operating system.";
33 Test("CC", "Checking for C compiler", sub { return "gcc"; });
36 Test("GCCVER", "Checking for GCC version", sub {
38 my $ver = `$gcc --version | sed '2,\$d; s/^\\(.* \\)*\\([0-9]*\\.[0-9]*\\).*/\\2/'`;
40 Fail "Unable to determine GCC version" if $? || $ver eq "";
43 my ($gccmaj, $gccmin) = split(/\./, Get("GCCVER"));
44 my $gccver = 1000*$gccmaj + $gccmin;
45 $gccver >= 3000 or Fail "GCC older than 3.0 doesn't support C99 well enough.";
49 Test("ARCH", "Checking for machine architecture", sub {
51 # We have to ask GCC for the target architecture, because it may
52 # differ from what uname tells us. This can happen even if we are
53 # not cross-compiling, for example on Linux with amd64 kernel, but
57 my $mach = `$gcc -dumpmachine 2>/dev/null`;
58 if (!$? && $mach ne "") {
62 Fail "Unable to determine machine type" if $? || $mach eq "";
65 if ($mach =~ /^i[0-9]86$/) {
67 } elsif ($mach =~ /^(x86[_-]|amd)64$/) {
69 } elsif ($mach eq 'powerpc') {
71 } elsif ($mach eq 'aarch64') {
74 return "unknown ($mach)";
78 my $arch = Get("ARCH");
79 if ($arch eq 'i386') {
81 UnSet("CPU_64BIT_POINTERS");
82 Set("CPU_LITTLE_ENDIAN");
83 UnSet("CPU_BIG_ENDIAN");
84 Set("CPU_ALLOW_UNALIGNED");
85 Set("CPU_STRUCT_ALIGN" => 4);
86 } elsif ($arch eq "amd64") {
88 Set("CPU_64BIT_POINTERS");
89 Set("CPU_LITTLE_ENDIAN");
90 UnSet("CPU_BIG_ENDIAN");
91 Set("CPU_ALLOW_UNALIGNED");
92 Set("CPU_STRUCT_ALIGN" => 8);
93 } elsif ($arch eq "powerpc") {
95 UnSet("CPU_64BIT_POINTERS");
96 UnSet("CPU_LITTLE_ENDIAN");
97 Set("CPU_BIG_ENDIAN");
98 UnSet("CPU_ALLOW_UNALIGNED");
99 Set("CPU_STRUCT_ALIGN" => 4);
100 } elsif ($arch eq "armhf") {
102 UnSet("CPU_64BIT_POINTERS");
103 Set("CPU_LITTLE_ENDIAN");
104 UnSet("CPU_BIG_ENDIAN");
105 UnSet("CPU_ALLOW_UNALIGNED");
106 Set("CPU_STRUCT_ALIGN" => 4);
107 } elsif ($arch eq "aarch64") {
109 Set("CPU_64BIT_POINTERS");
110 Set("CPU_LITTLE_ENDIAN");
111 UnSet("CPU_BIG_ENDIAN");
112 UnSet("CPU_ALLOW_UNALIGNED");
113 Set("CPU_STRUCT_ALIGN" => 8);
114 } elsif (!Get("CPU_LITTLE_ENDIAN") && !Get("CPU_BIG_ENDIAN")) {
115 Fail "Architecture not recognized, please set CPU_xxx variables manually.";
118 ### Compiler and its Options ###
120 # C flags: tell the compiler we're speaking C99, and disable common symbols
121 Set("CLANG" => "-std=gnu99 -fno-common");
124 Set("COPT" => '-O2');
125 if ($arch =~ /^(i386|amd64)$/ && Get("CONFIG_EXACT_CPU")) {
126 if ($gccver >= 4002) {
127 Append('COPT', '-march=native');
129 Warn "CONFIG_EXACT_CPU not supported with old GCC, ignoring.\n";
133 # C optimizations for highly exposed code
134 Set("COPT2" => '-O3');
137 Set("CWARNS" => '-Wall -W -Wno-parentheses -Wstrict-prototypes -Wmissing-prototypes');
138 Set("CWARNS_OFF" => '');
146 # Extra flags for compiling and linking shared libraries
147 Set("CSHARED" => '-fPIC');
148 Append("LOPT" => "-Wl,--rpath-link,run/lib -Lrun/lib");
149 if (!IsSet("CONFIG_LOCAL")) {
150 # Beware that in non-local builds the INSTALL_LIB_DIR must exist in
151 # standard search paths for shared libraries.
152 Set("SO_LINK_PATH" => '');
155 # In local builds, we need to link binaries with custom --rpath.
156 # GCC seems to fail when this directory does not exist.
157 Set("SO_LINK_PATH" => "-Wl,--rpath," . Get("INSTALL_LIB_DIR"));
159 my $libdir = Get("INSTALL_LIB_DIR");
160 if (IsSet("CONFIG_SHARED")) {
161 `mkdir -p $libdir`; Fail("Cannot create $libdir") if $?;
165 if (IsSet("CONFIG_DARWIN")) {
166 Set("LSHARED" => '-dynamiclib -install_name $(@F)$(SONAME_SUFFIX) -undefined dynamic_lookup');
168 Set("LSHARED" => '-shared -Wl,-soname,$(@F)$(SONAME_SUFFIX)');
171 # Extra switches depending on GCC version:
172 if ($gccver == 3000) {
173 Append("COPT" => "-fstrict-aliasing");
174 } elsif ($gccver == 3003) {
175 Append("CWARNS" => "-Wundef -Wredundant-decls");
176 Append("COPT" => "-finline-limit=20000 --param max-inline-insns-auto=1000");
177 } elsif ($gccver == 3004) {
178 Append("CWARNS" => "-Wundef -Wredundant-decls");
179 Append("COPT" => "-finline-limit=2000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
180 } elsif ($gccver >= 4000) {
181 Append("CWARNS" => "-Wundef -Wredundant-decls -Wno-pointer-sign -Wdisabled-optimization -Wno-missing-field-initializers");
182 Append("CWARNS_OFF" => "-Wno-pointer-sign");
183 Append("COPT" => "-finline-limit=5000 --param large-function-insns=5000 --param inline-unit-growth=200 --param large-function-growth=400");
185 Warn "Don't know anything about this GCC version, using default switches.\n";
188 if (IsSet("CONFIG_DEBUG")) {
190 Set("DEBUG_ASSERTS");
191 Set("DEBUG_DIE_BY_ABORT") if Get("CONFIG_DEBUG") > 1;
192 Set("CDEBUG" => "-ggdb");
194 # If building a release version:
195 Append("COPT" => "-fomit-frame-pointer");
196 Append("LOPT" => "-s");
199 # Link-time optimization (experimental)
200 # This is currently very inefficient, because we do not attempt to disable
201 # optimizations when compiling individual modules. Therefore, we optimize
202 # each shared library module twice: when compiling and when linking.
203 # Doing it properly would require hacking makefiles.
204 if (IsSet("CONFIG_LTO")) {
205 Append("LOPT", "-flto");
206 Append("COPT", "-flto");
209 if (IsSet("CONFIG_DARWIN")) {
210 # gcc-4.0 on Darwin doesn't set this in the gnu99 mode
211 Append("CLANG" => "-fnested-functions");
212 # Directory hierarchy of the fink project
213 Append("LIBS" => "-L/sw/lib");
214 Append("COPT" => "-I/sw/include");
217 ### Compiling test programs ###
219 sub TestCompile($$) {
220 my ($testname, $source) = @_;
221 my $dir = "conftest-$testname";
222 `rm -rf $dir && mkdir $dir`; $? and Fail "Cannot initialize $dir";
224 open SRC, ">$dir/conftest.c";
229 map { defined($_) ? $_ : "" }
231 Get("CC"), Get("CLANG"), Get("COPT"), Get("CEXTRA"), Get("LIBS"),
232 "conftest.c", "-o", "conftest",
233 ">conftest.log", "2>&1"
238 `rm -rf $dir` unless Get("KEEP_CONFTEST");
243 ### Writing C headers with configuration ###
245 sub ConfigHeader($$) {
246 my ($hdr, $rules) = @_;
247 Log "Generating $hdr ... ";
248 open X, ">obj/$hdr" or Fail $!;
249 print X "/* Generated automatically by $0, please don't touch manually. */\n";
251 sub match_rules($$) {
252 my ($rules, $name) = @_;
253 for (my $i=0; $i < scalar @$rules; $i++) {
254 my ($r, $v) = ($rules->[$i], $rules->[$i+1]);
255 return $v if $name =~ $r;
260 foreach my $x (sort keys %UCW::Configure::vars) {
261 next unless match_rules($rules, $x);
262 my $v = $UCW::Configure::vars{$x};
263 # Try to add quotes if necessary
264 $v = '"' . $v . '"' unless ($v =~ /^"/ || $v =~ /^\d*$/);
265 print X "#define $x $v\n";
272 ConfigHeader("autoconf.h", [
273 # Symbols with "_" anywhere in their name are exported