]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/utilities.pm
Fix off by one in ping timeout.
[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 BEGIN {
24         require 5.8.0;
25 }
26
27 package make::utilities;
28
29 use strict;
30 use warnings FATAL => qw(all);
31
32 use Exporter 'import';
33 use Fcntl;
34 use File::Path;
35 use File::Spec::Functions qw(rel2abs);
36 use Getopt::Long;
37 use POSIX;
38
39 our @EXPORT = qw(get_version module_installed prompt_bool prompt_dir prompt_string get_cpu_count make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
40
41 my %already_added = ();
42 my %version = ();
43
44 sub get_version {
45         return %version if %version;
46
47         # Attempt to retrieve version information from src/version.sh
48         chomp(my $vf = `sh src/version.sh 2>/dev/null`);
49         if ($vf =~ /^InspIRCd-([0-9]+)\.([0-9]+)\.([0-9]+)(?:\+(\w+))?$/) {
50                 %version = ( MAJOR => $1, MINOR => $2, PATCH => $3, LABEL => $4 );
51         }
52
53         # Attempt to retrieve missing version information from Git
54         chomp(my $gr = `git describe --tags 2>/dev/null`);
55         if ($gr =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-\d+-(\w+))?$/) {
56                 $version{MAJOR} = $1 unless defined $version{MAJOR};
57                 $version{MINOR} = $2 unless defined $version{MINOR};
58                 $version{PATCH} = $3 unless defined $version{PATCH};
59                 $version{LABEL} = $4 if defined $4;
60         }
61
62         # The user is using a stable release which does not have
63         # a label attached.
64         $version{LABEL} = 'release' unless defined $version{LABEL};
65
66         # If any of these fields are missing then the user has deleted the
67         # version file and is not running from Git. Fill in the fields with
68         # dummy data so we don't get into trouble with undef values later.
69         $version{MAJOR} = '0' unless defined $version{MAJOR};
70         $version{MINOR} = '0' unless defined $version{MINOR};
71         $version{PATCH} = '0' unless defined $version{PATCH};
72
73         return %version;
74 }
75
76 sub module_installed($) {
77         my $module = shift;
78         eval("use $module;");
79         return !$@;
80 }
81
82 sub prompt_bool($$$) {
83         my ($interactive, $question, $default) = @_;
84         my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n');
85         return $answer =~ /y/i;
86 }
87
88 sub prompt_dir($$$) {
89         my ($interactive, $question, $default) = @_;
90         my ($answer, $create) = (undef, 'y');
91         do {
92                 $answer = rel2abs(prompt_string($interactive, $question, $default));
93                 $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y');
94                 my $mkpath = eval {
95                         mkpath($answer, 0, 0750);
96                         return 1;
97                 };
98                 unless (defined $mkpath) {
99                         print "Error: unable to create $answer!\n\n";
100                         $create = 0;
101                 }
102         } while (!$create);
103         return $answer;
104 }
105
106 sub prompt_string($$$) {
107         my ($interactive, $question, $default) = @_;
108         return $default unless $interactive;
109         print $question, "\n";
110         print "[\e[1;32m$default\e[0m] => ";
111         chomp(my $answer = <STDIN>);
112         print "\n";
113         return $answer ? $answer : $default;
114 }
115
116 sub get_cpu_count {
117         my $count = 1;
118         if ($^O =~ /bsd/) {
119                 $count = `sysctl -n hw.ncpu`;
120         } elsif ($^O eq 'darwin') {
121                 $count = `sysctl -n hw.activecpu`;
122         } elsif ($^O eq 'linux') {
123                 $count = `getconf _NPROCESSORS_ONLN`;
124         } elsif ($^O eq 'solaris') {
125                 $count = `psrinfo -p`;
126         }
127         chomp($count);
128         return $count;
129 }
130
131 sub promptstring($$$$$)
132 {
133         my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
134         my $var;
135         if (!$main::interactive)
136         {
137                 my $opt_commandlineswitch;
138                 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
139                 if (defined $opt_commandlineswitch)
140                 {
141                         print "\e[1;32m$opt_commandlineswitch\e[0m\n";
142                         $var = $opt_commandlineswitch;
143                 }
144                 else
145                 {
146                         die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
147                 }
148         }
149         else
150         {
151                 print "\nPlease enter the $prompt?\n";
152                 print "[\e[1;32m$default\e[0m] -> ";
153                 chomp($var = <STDIN>);
154         }
155         if ($var eq "")
156         {
157                 $var = $default;
158         }
159         $main::config{$configitem} = $var;
160 }
161
162 sub make_rpath($;$)
163 {
164         my ($executable, $module) = @_;
165         return "" if defined $ENV{DISABLE_RPATH};
166         chomp(my $data = `$executable`);
167         my $output = "";
168         while ($data =~ /-L(\S+)/)
169         {
170                 my $libpath = $1;
171                 if (!exists $already_added{$libpath})
172                 {
173                         print "Adding runtime library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
174                         $already_added{$libpath} = 1;
175                 }
176                 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath ";
177                 $data =~ s/-L(\S+)//;
178         }
179         return $output;
180 }
181
182 sub extend_pkg_path()
183 {
184         if (!exists $ENV{PKG_CONFIG_PATH})
185         {
186                 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
187         }
188         else
189         {
190                 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
191         }
192 }
193
194 sub pkgconfig_get_include_dirs($$$;$)
195 {
196         my ($packagename, $headername, $defaults, $module) = @_;
197
198         extend_pkg_path();
199
200         print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
201
202         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
203         my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
204         my $foo = "";
205         if ((!defined $v) || ($v eq ""))
206         {
207                 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
208                 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
209                 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
210                 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
211                 chomp($find);
212                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
213                 {
214                         print "(\e[1;32mFound via search\e[0m) ";
215                         $foo = "-I$1";
216                 }
217                 else
218                 {
219                         $foo = " ";
220                         undef $v;
221                 }
222                 $ret = "$foo";
223         }
224         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
225         {
226                 $ret = "$foo " . $defaults;
227         }
228         chomp($ret);
229         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
230         {
231                 my $key = "default_includedir_$packagename";
232                 if (exists $main::config{$key})
233                 {
234                         $ret = $main::config{$key};
235                 }
236                 else
237                 {
238                         $headername =~ s/^\///;
239                         promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
240                         $packagename =~ tr/a-z/A-Z/;
241                         if (defined $v)
242                         {
243                                 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
244                         }
245                         else
246                         {
247                                 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
248                         }
249                         $main::config{$key} =~ s/^\s+//g;
250                         $ret = $main::config{$key};
251                         return $ret;
252                 }
253         }
254         else
255         {
256                 chomp($v);
257                 my $key = "default_includedir_$packagename";
258                 $packagename =~ tr/a-z/A-Z/;
259                 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
260                 $main::config{$key} =~ s/^\s+//g;
261                 $ret = $main::config{$key};
262                 print "\e[1;32m$ret\e[0m (version $v)\n";
263         }
264         $ret =~ s/^\s+//g;
265         return $ret;
266 }
267
268 sub pkgconfig_check_version($$;$)
269 {
270         my ($packagename, $version, $module) = @_;
271
272         extend_pkg_path();
273
274         print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
275
276         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
277         if (defined $v)
278         {
279                 chomp($v);
280         }
281         if ((defined $v) && ($v ne ""))
282         {
283                 if (!system "pkg-config --atleast-version $version $packagename")
284                 {
285                         print "\e[1;32mYes (version $v)\e[0m\n";
286                         return 1;
287                 }
288                 else
289                 {
290                         print "\e[1;32mNo (version $v)\e[0m\n";
291                         return 0;
292                 }
293         }
294         # If we didnt find it, we  cant definitively say its too old.
295         # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
296         # the missing library later on.
297         print "\e[1;32mNo (not found)\e[0m\n";
298         return 1;
299 }
300
301 sub pkgconfig_get_lib_dirs($$$;$)
302 {
303         my ($packagename, $libname, $defaults, $module) = @_;
304
305         extend_pkg_path();
306
307         print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
308
309         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
310         my $ret = `pkg-config --libs $packagename 2>/dev/null`;
311
312         my $foo = "";
313         if ((!defined $v) || ($v eq ""))
314         {
315                 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
316                 $foo = `$locbin "$libname" | head -n 1`;
317                 $foo =~ /(.+)\Q$libname\E/;
318                 my $find = $1;
319                 chomp($find);
320                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
321                 {
322                         print "(\e[1;32mFound via search\e[0m) ";
323                         $foo = "-L$1";
324                 }
325                 else
326                 {
327                         $foo = " ";
328                         undef $v;
329                 }
330                 $ret = "$foo";
331         }
332
333         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
334         {
335                 $ret = "$foo " . $defaults;
336         }
337         chomp($ret);
338         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
339         {
340                 my $key = "default_libdir_$packagename";
341                 if (exists $main::config{$key})
342                 {
343                         $ret = $main::config{$key};
344                 }
345                 else
346                 {
347                         $libname =~ s/^\///;
348                         promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
349                         $main::config{$key} = "-L$main::config{$key}" . " $defaults";
350                         $main::config{$key} =~ s/^\s+//g;
351                         $ret = $main::config{$key};
352                         return $ret;
353                 }
354         }
355         else
356         {
357                 chomp($v);
358                 print "\e[1;32m$ret\e[0m (version $v)\n";
359                 my $key = "default_libdir_$packagename";
360                 $main::config{$key} = $ret;
361                 $main::config{$key} =~ s/^\s+//g;
362                 $ret =~ s/^\s+//g;
363         }
364         $ret =~ s/^\s+//g;
365         return $ret;
366 }
367
368 # Translate a $CompileFlags etc line and parse out function calls
369 # to functions within these modules at configure time.
370 sub translate_functions($$)
371 {
372         my ($line,$module) = @_;
373
374         eval
375         {
376                 $module =~ /modules*\/(.+?)$/;
377                 $module = $1;
378
379                 if ($line =~ /ifuname\(\!"(\w+)"\)/)
380                 {
381                         my $uname = $1;
382                         if ($uname eq $^O)
383                         {
384                                 $line = "";
385                                 return "";
386                         }
387
388                         $line =~ s/ifuname\(\!"(.+?)"\)//;
389                 }
390
391                 if ($line =~ /ifuname\("(\w+)"\)/)
392                 {
393                         my $uname = $1;
394                         if ($uname ne $^O)
395                         {
396                                 $line = "";
397                                 return "";
398                         }
399
400                         $line =~ s/ifuname\("(.+?)"\)//;
401                 }
402
403                 if ($line =~ /if\("(\w+)"\)/)
404                 {
405                         if (defined $main::config{$1})
406                         {
407                                 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
408                                 {
409                                         $line = "";
410                                         return "";
411                                 }
412                         }
413
414                         $line =~ s/if\("(.+?)"\)//;
415                 }
416                 if ($line =~ /if\(\!"(\w+)"\)/)
417                 {
418                         if (!exists $main::config{$1})
419                         {
420                                 $line = "";
421                                 return "";
422                         }
423                         else
424                         {
425                                 if (defined $1)
426                                 {
427                                         if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
428                                         {
429                                                 $line = "";
430                                                 return "";
431                                         }
432                                 }
433                         }
434
435                         $line =~ s/if\(\!"(.+?)"\)//;
436                 }
437                 while ($line =~ /exec\("(.+?)"\)/)
438                 {
439                         print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
440                         my $replace = `$1`;
441                         die $replace if ($replace =~ /Configuration failed/);
442                         chomp($replace);
443                         $line =~ s/exec\("(.+?)"\)/$replace/;
444                 }
445                 while ($line =~ /execruntime\("(.+?)"\)/)
446                 {
447                         $line =~ s/execruntime\("(.+?)"\)/`$1`/;
448                 }
449                 while ($line =~ /eval\("(.+?)"\)/)
450                 {
451                         print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
452                         my $tmpfile;
453                         do
454                         {
455                                 $tmpfile = tmpnam();
456                         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
457                         print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
458                         print TF $1;
459                         close TF;
460                         my $replace = `perl $tmpfile`;
461                         chomp($replace);
462                         unlink($tmpfile);
463                         $line =~ s/eval\("(.+?)"\)/$replace/;
464                 }
465                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
466                 {
467                         my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
468                         $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
469                 }
470                 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
471                 {
472                         if (pkgconfig_check_version($1, $2, $module) != 1)
473                         {
474                                 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
475                         }
476                         # This doesnt actually get replaced with anything
477                         $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
478                 }
479                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
480                 {
481                         my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
482                         $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
483                 }
484                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
485                 {
486                         my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
487                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
488                 }
489                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
490                 {
491                         my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
492                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
493                 }
494                 while ($line =~ /rpath\("(.+?)"\)/)
495                 {
496                         my $replace = make_rpath($1,$module);
497                         $line =~ s/rpath\("(.+?)"\)/$replace/;
498                 }
499         };
500         if ($@)
501         {
502                 my $err = $@;
503                 #$err =~ s/at .+? line \d+.*//g;
504                 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
505                 print "\nMake sure you have pkg-config installed\n";
506                 print "\nIn the case of gnutls configuration errors on debian,\n";
507                 print "Ubuntu, etc, you should ensure that you have installed\n";
508                 print "gnutls-bin as well as libgnutls-dev and libgnutls.\n";
509                 exit;
510         }
511         else
512         {
513                 return $line;
514         }
515 }
516
517 1;
518