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