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