]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/utilities.pm
Fix checking whether kqueue is available.
[user/henk/code/inspircd.git] / make / utilities.pm
1 #
2 # InspIRCd -- Internet Relay Chat Daemon
3 #
4 #   Copyright (C) 2010 Daniel De Graaf <danieldg@inspircd.org>
5 #   Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7 #   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8 #
9 # This file is part of InspIRCd.  InspIRCd is free software: you can
10 # redistribute it and/or modify it under the terms of the GNU General Public
11 # License as published by the Free Software Foundation, version 2.
12 #
13 # This program is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16 # details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21
22
23 package make::utilities;
24
25 require 5.8.0;
26
27 use strict;
28 use warnings FATAL => qw(all);
29
30 use Exporter 'import';
31 use POSIX;
32 use Getopt::Long;
33 use Fcntl;
34 our @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
35
36 # Parse the output of a *_config program,
37 # such as pcre_config, take out the -L
38 # directive and return an rpath for it.
39
40 # \e[1;32msrc/Makefile\e[0m
41
42 my %already_added = ();
43 my $if_skip_lines = 0;
44
45 sub promptstring($$$$$)
46 {
47         my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
48         my $var;
49         if (!$main::interactive)
50         {
51                 my $opt_commandlineswitch;
52                 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
53                 if (defined $opt_commandlineswitch)
54                 {
55                         print "\e[1;32m$opt_commandlineswitch\e[0m\n";
56                         $var = $opt_commandlineswitch;
57                 }
58                 else
59                 {
60                         die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
61                 }
62         }
63         else
64         {
65                 print "\nPlease enter the $prompt?\n";
66                 print "[\e[1;32m$default\e[0m] -> ";
67                 chomp($var = <STDIN>);
68         }
69         if ($var eq "")
70         {
71                 $var = $default;
72         }
73         $main::config{$configitem} = $var;
74 }
75
76 sub make_rpath($;$)
77 {
78         my ($executable, $module) = @_;
79         return "" if defined $ENV{DISABLE_RPATH};
80         chomp(my $data = `$executable`);
81         my $output = "";
82         while ($data =~ /-L(\S+)/)
83         {
84                 my $libpath = $1;
85                 if (!exists $already_added{$libpath})
86                 {
87                         print "Adding runtime library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
88                         $already_added{$libpath} = 1;
89                 }
90                 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath ";
91                 $data =~ s/-L(\S+)//;
92         }
93         return $output;
94 }
95
96 sub extend_pkg_path()
97 {
98         return if defined $ENV{DISABLE_EXTEND_PKG_PATH};
99         if (!exists $ENV{PKG_CONFIG_PATH})
100         {
101                 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
102         }
103         else
104         {
105                 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
106         }
107 }
108
109 sub pkgconfig_get_include_dirs($$$;$)
110 {
111         my ($packagename, $headername, $defaults, $module) = @_;
112
113         my $key = "default_includedir_$packagename";
114         if (exists $main::config{$key})
115         {
116                 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
117                 my $ret = $main::config{$key};
118                 print "\e[1;32m$ret\e[0m (cached)\n";
119                 return $ret;
120         }
121
122         extend_pkg_path();
123
124         print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
125
126         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
127         my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
128         my $foo = "";
129         if ((!defined $v) || ($v eq ""))
130         {
131                 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
132                 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
133                 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
134                 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
135                 chomp($find);
136                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
137                 {
138                         print "(\e[1;32mFound via search\e[0m) ";
139                         $foo = "-I$1";
140                 }
141                 else
142                 {
143                         $foo = " ";
144                         undef $v;
145                 }
146                 $ret = "$foo";
147         }
148         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
149         {
150                 $ret = "$foo " . $defaults;
151         }
152         chomp($ret);
153         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
154         {
155                 my $key = "default_includedir_$packagename";
156                 if (exists $main::config{$key})
157                 {
158                         $ret = $main::config{$key};
159                 }
160                 else
161                 {
162                         $headername =~ s/^\///;
163                         promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
164                         $packagename =~ tr/a-z/A-Z/;
165                         if (defined $v)
166                         {
167                                 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
168                         }
169                         else
170                         {
171                                 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
172                         }
173                         $main::config{$key} =~ s/^\s+//g;
174                         $ret = $main::config{$key};
175                         return $ret;
176                 }
177         }
178         else
179         {
180                 chomp($v);
181                 my $key = "default_includedir_$packagename";
182                 $packagename =~ tr/a-z/A-Z/;
183                 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
184                 $main::config{$key} =~ s/^\s+//g;
185                 $ret = $main::config{$key};
186                 print "\e[1;32m$ret\e[0m (version $v)\n";
187         }
188         $ret =~ s/^\s+//g;
189         return $ret;
190 }
191
192 sub pkgconfig_check_version($$;$)
193 {
194         my ($packagename, $version, $module) = @_;
195
196         extend_pkg_path();
197
198         print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
199
200         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
201         if (defined $v)
202         {
203                 chomp($v);
204         }
205         if ((defined $v) && ($v ne ""))
206         {
207                 if (!system "pkg-config --atleast-version $version $packagename")
208                 {
209                         print "\e[1;32mYes (version $v)\e[0m\n";
210                         return 1;
211                 }
212                 else
213                 {
214                         print "\e[1;32mNo (version $v)\e[0m\n";
215                         return 0;
216                 }
217         }
218         # If we didnt find it, we  cant definitively say its too old.
219         # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
220         # the missing library later on.
221         print "\e[1;32mNo (not found)\e[0m\n";
222         return 1;
223 }
224
225 sub pkgconfig_get_lib_dirs($$$;$)
226 {
227         my ($packagename, $libname, $defaults, $module) = @_;
228
229         my $key = "default_libdir_$packagename";
230         if (exists $main::config{$key})
231         {
232                 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
233                 my $ret = $main::config{$key};
234                 print "\e[1;32m$ret\e[0m (cached)\n";
235                 return $ret;
236         }
237
238         extend_pkg_path();
239
240         print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
241
242         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
243         my $ret = `pkg-config --libs $packagename 2>/dev/null`;
244
245         my $foo = "";
246         if ((!defined $v) || ($v eq ""))
247         {
248                 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
249                 $foo = `$locbin "$libname" | head -n 1`;
250                 $foo =~ /(.+)\Q$libname\E/;
251                 my $find = $1;
252                 chomp($find);
253                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
254                 {
255                         print "(\e[1;32mFound via search\e[0m) ";
256                         $foo = "-L$1";
257                 }
258                 else
259                 {
260                         $foo = " ";
261                         undef $v;
262                 }
263                 $ret = "$foo";
264         }
265
266         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
267         {
268                 $ret = "$foo " . $defaults;
269         }
270         chomp($ret);
271         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
272         {
273                 my $key = "default_libdir_$packagename";
274                 if (exists $main::config{$key})
275                 {
276                         $ret = $main::config{$key};
277                 }
278                 else
279                 {
280                         $libname =~ s/^\///;
281                         promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
282                         $main::config{$key} = "-L$main::config{$key}" . " $defaults";
283                         $main::config{$key} =~ s/^\s+//g;
284                         $ret = $main::config{$key};
285                         return $ret;
286                 }
287         }
288         else
289         {
290                 chomp($v);
291                 print "\e[1;32m$ret\e[0m (version $v)\n";
292                 my $key = "default_libdir_$packagename";
293                 $main::config{$key} = $ret;
294                 $main::config{$key} =~ s/^\s+//g;
295                 $ret =~ s/^\s+//g;
296         }
297         $ret =~ s/^\s+//g;
298         return $ret;
299 }
300
301 # Translate a $CompileFlags etc line and parse out function calls
302 # to functions within these modules at configure time.
303 sub translate_functions($$)
304 {
305         my ($line,$module) = @_;
306
307         eval
308         {
309                 $module =~ /modules*\/(.+?)$/;
310                 $module = $1;
311
312                 # This is only a cursory check, just designed to catch casual accidental use of backticks.
313                 # There are pleanty of ways around it, but its not supposed to be for security, just checking
314                 # that people are using the new configuration api as theyre supposed to and not just using
315                 # backticks instead of eval(), being as eval has accountability. People wanting to get around
316                 # the accountability will do so anyway.
317                 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
318                 {
319                         die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
320                 }
321
322                 if ($line =~ /if(gt|lt)\("(.+?)","(.+?)"\)/) {
323                         chomp(my $result = `$2 2>/dev/null`);
324                         if (($1 eq 'gt' && $result le $3) || ($1 eq 'lt' && $result ge $3)) {
325                                 $line = substr $line, 0, $-[0];
326                         } else {
327                                 $line =~ s/if$1\("$2","$3"\)//;
328                         }
329                 }
330
331                 if ($line =~ /ifuname\(\!"(\w+)"\)/)
332                 {
333                         my $uname = $1;
334                         if ($uname eq $^O)
335                         {
336                                 $line = "";
337                                 return "";
338                         }
339
340                         $line =~ s/ifuname\(\!"(.+?)"\)//;
341                 }
342
343                 if ($line =~ /ifuname\("(\w+)"\)/)
344                 {
345                         my $uname = $1;
346                         if ($uname ne $^O)
347                         {
348                                 $line = "";
349                                 return "";
350                         }
351
352                         $line =~ s/ifuname\("(.+?)"\)//;
353                 }
354
355                 if ($line =~ /if\("(\w+)"\)/)
356                 {
357                         if (defined $main::config{$1})
358                         {
359                                 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
360                                 {
361                                         $line = "";
362                                         return "";
363                                 }
364                         }
365
366                         $line =~ s/if\("(.+?)"\)//;
367                 }
368                 if ($line =~ /if\(\!"(\w+)"\)/)
369                 {
370                         if (!exists $main::config{$1})
371                         {
372                                 $line = "";
373                                 return "";
374                         }
375                         else
376                         {
377                                 if (defined $1)
378                                 {
379                                         if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
380                                         {
381                                                 $line = "";
382                                                 return "";
383                                         }
384                                 }
385                         }
386
387                         $line =~ s/if\(\!"(.+?)"\)//;
388                 }
389                 while ($line =~ /exec\("(.+?)"\)/)
390                 {
391                         print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
392                         my $replace = `$1`;
393                         die $replace if ($replace =~ /Configuration failed/);
394                         chomp($replace);
395                         $line =~ s/exec\("(.+?)"\)/$replace/;
396                 }
397                 while ($line =~ /execruntime\("(.+?)"\)/)
398                 {
399                         $line =~ s/execruntime\("(.+?)"\)/`$1`/;
400                 }
401                 while ($line =~ /eval\("(.+?)"\)/)
402                 {
403                         print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
404                         my $tmpfile;
405                         do
406                         {
407                                 $tmpfile = tmpnam();
408                         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
409                         print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
410                         print TF $1;
411                         close TF;
412                         my $replace = `perl $tmpfile`;
413                         chomp($replace);
414                         unlink($tmpfile);
415                         $line =~ s/eval\("(.+?)"\)/$replace/;
416                 }
417                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
418                 {
419                         my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
420                         $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
421                 }
422                 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
423                 {
424                         if (pkgconfig_check_version($1, $2, $module) != 1)
425                         {
426                                 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
427                         }
428                         # This doesnt actually get replaced with anything
429                         $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
430                 }
431                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
432                 {
433                         my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
434                         $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
435                 }
436                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
437                 {
438                         my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
439                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
440                 }
441                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
442                 {
443                         my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
444                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
445                 }
446                 while ($line =~ /rpath\("(.+?)"\)/)
447                 {
448                         my $replace = make_rpath($1,$module);
449                         $line =~ s/rpath\("(.+?)"\)/$replace/;
450                 }
451         };
452         if ($@)
453         {
454                 my $err = $@;
455                 #$err =~ s/at .+? line \d+.*//g;
456                 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
457                 print "\nMake sure you have pkg-config installed\n";
458                 print "\nIn the case of gnutls configuration errors on debian,\n";
459                 print "Ubuntu, etc, you should ensure that you have installed\n";
460                 print "gnutls-bin as well as libgnutls-dev and libgnutls.\n";
461                 exit;
462         }
463         else
464         {
465                 return $line;
466         }
467 }
468
469 1;
470