]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/utilities.pm
4042439661da40df767b42339b388b83eb8ab7a7
[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         chomp(my $data = `$executable`);
166         my $output = "";
167         while ($data =~ /-L(\S+)/)
168         {
169                 my $libpath = $1;
170                 if (!exists $already_added{$libpath})
171                 {
172                         print "Adding extra library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
173                         $already_added{$libpath} = 1;
174                 }
175                 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath " unless defined $main::opt_disablerpath;
176                 $data =~ s/-L(\S+)//;
177         }
178         return $output;
179 }
180
181 sub extend_pkg_path()
182 {
183         if (!exists $ENV{PKG_CONFIG_PATH})
184         {
185                 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
186         }
187         else
188         {
189                 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
190         }
191 }
192
193 sub pkgconfig_get_include_dirs($$$;$)
194 {
195         my ($packagename, $headername, $defaults, $module) = @_;
196
197         extend_pkg_path();
198
199         print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
200
201         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
202         my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
203         my $foo = "";
204         if ((!defined $v) || ($v eq ""))
205         {
206                 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
207                 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
208                 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
209                 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
210                 chomp($find);
211                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
212                 {
213                         print "(\e[1;32mFound via search\e[0m) ";
214                         $foo = "-I$1";
215                 }
216                 else
217                 {
218                         $foo = " ";
219                         undef $v;
220                 }
221                 $ret = "$foo";
222         }
223         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
224         {
225                 $ret = "$foo " . $defaults;
226         }
227         chomp($ret);
228         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
229         {
230                 my $key = "default_includedir_$packagename";
231                 if (exists $main::config{$key})
232                 {
233                         $ret = $main::config{$key};
234                 }
235                 else
236                 {
237                         $headername =~ s/^\///;
238                         promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
239                         $packagename =~ tr/a-z/A-Z/;
240                         if (defined $v)
241                         {
242                                 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
243                         }
244                         else
245                         {
246                                 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
247                         }
248                         $main::config{$key} =~ s/^\s+//g;
249                         $ret = $main::config{$key};
250                         return $ret;
251                 }
252         }
253         else
254         {
255                 chomp($v);
256                 my $key = "default_includedir_$packagename";
257                 $packagename =~ tr/a-z/A-Z/;
258                 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
259                 $main::config{$key} =~ s/^\s+//g;
260                 $ret = $main::config{$key};
261                 print "\e[1;32m$ret\e[0m (version $v)\n";
262         }
263         $ret =~ s/^\s+//g;
264         return $ret;
265 }
266
267 sub pkgconfig_check_version($$;$)
268 {
269         my ($packagename, $version, $module) = @_;
270
271         extend_pkg_path();
272
273         print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
274
275         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
276         if (defined $v)
277         {
278                 chomp($v);
279         }
280         if ((defined $v) && ($v ne ""))
281         {
282                 if (!system "pkg-config --atleast-version $version $packagename")
283                 {
284                         print "\e[1;32mYes (version $v)\e[0m\n";
285                         return 1;
286                 }
287                 else
288                 {
289                         print "\e[1;32mNo (version $v)\e[0m\n";
290                         return 0;
291                 }
292         }
293         # If we didnt find it, we  cant definitively say its too old.
294         # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
295         # the missing library later on.
296         print "\e[1;32mNo (not found)\e[0m\n";
297         return 1;
298 }
299
300 sub pkgconfig_get_lib_dirs($$$;$)
301 {
302         my ($packagename, $libname, $defaults, $module) = @_;
303
304         extend_pkg_path();
305
306         print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
307
308         my $v = `pkg-config --modversion $packagename 2>/dev/null`;
309         my $ret = `pkg-config --libs $packagename 2>/dev/null`;
310
311         my $foo = "";
312         if ((!defined $v) || ($v eq ""))
313         {
314                 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
315                 $foo = `$locbin "$libname" | head -n 1`;
316                 $foo =~ /(.+)\Q$libname\E/;
317                 my $find = $1;
318                 chomp($find);
319                 if ((defined $find) && ($find ne "") && ($find ne $packagename))
320                 {
321                         print "(\e[1;32mFound via search\e[0m) ";
322                         $foo = "-L$1";
323                 }
324                 else
325                 {
326                         $foo = " ";
327                         undef $v;
328                 }
329                 $ret = "$foo";
330         }
331
332         if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
333         {
334                 $ret = "$foo " . $defaults;
335         }
336         chomp($ret);
337         if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
338         {
339                 my $key = "default_libdir_$packagename";
340                 if (exists $main::config{$key})
341                 {
342                         $ret = $main::config{$key};
343                 }
344                 else
345                 {
346                         $libname =~ s/^\///;
347                         promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
348                         $main::config{$key} = "-L$main::config{$key}" . " $defaults";
349                         $main::config{$key} =~ s/^\s+//g;
350                         $ret = $main::config{$key};
351                         return $ret;
352                 }
353         }
354         else
355         {
356                 chomp($v);
357                 print "\e[1;32m$ret\e[0m (version $v)\n";
358                 my $key = "default_libdir_$packagename";
359                 $main::config{$key} = $ret;
360                 $main::config{$key} =~ s/^\s+//g;
361                 $ret =~ s/^\s+//g;
362         }
363         $ret =~ s/^\s+//g;
364         return $ret;
365 }
366
367 # Translate a $CompileFlags etc line and parse out function calls
368 # to functions within these modules at configure time.
369 sub translate_functions($$)
370 {
371         my ($line,$module) = @_;
372
373         eval
374         {
375                 $module =~ /modules*\/(.+?)$/;
376                 $module = $1;
377
378                 if ($line =~ /ifuname\(\!"(\w+)"\)/)
379                 {
380                         my $uname = $1;
381                         if ($uname eq $^O)
382                         {
383                                 $line = "";
384                                 return "";
385                         }
386
387                         $line =~ s/ifuname\(\!"(.+?)"\)//;
388                 }
389
390                 if ($line =~ /ifuname\("(\w+)"\)/)
391                 {
392                         my $uname = $1;
393                         if ($uname ne $^O)
394                         {
395                                 $line = "";
396                                 return "";
397                         }
398
399                         $line =~ s/ifuname\("(.+?)"\)//;
400                 }
401
402                 if ($line =~ /if\("(\w+)"\)/)
403                 {
404                         if (defined $main::config{$1})
405                         {
406                                 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
407                                 {
408                                         $line = "";
409                                         return "";
410                                 }
411                         }
412
413                         $line =~ s/if\("(.+?)"\)//;
414                 }
415                 if ($line =~ /if\(\!"(\w+)"\)/)
416                 {
417                         if (!exists $main::config{$1})
418                         {
419                                 $line = "";
420                                 return "";
421                         }
422                         else
423                         {
424                                 if (defined $1)
425                                 {
426                                         if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
427                                         {
428                                                 $line = "";
429                                                 return "";
430                                         }
431                                 }
432                         }
433
434                         $line =~ s/if\(\!"(.+?)"\)//;
435                 }
436                 while ($line =~ /exec\("(.+?)"\)/)
437                 {
438                         print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
439                         my $replace = `$1`;
440                         die $replace if ($replace =~ /Configuration failed/);
441                         chomp($replace);
442                         $line =~ s/exec\("(.+?)"\)/$replace/;
443                 }
444                 while ($line =~ /execruntime\("(.+?)"\)/)
445                 {
446                         $line =~ s/execruntime\("(.+?)"\)/`$1`/;
447                 }
448                 while ($line =~ /eval\("(.+?)"\)/)
449                 {
450                         print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
451                         my $tmpfile;
452                         do
453                         {
454                                 $tmpfile = tmpnam();
455                         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
456                         print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
457                         print TF $1;
458                         close TF;
459                         my $replace = `perl $tmpfile`;
460                         chomp($replace);
461                         $line =~ s/eval\("(.+?)"\)/$replace/;
462                 }
463                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
464                 {
465                         my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
466                         $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
467                 }
468                 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
469                 {
470                         if (pkgconfig_check_version($1, $2, $module) != 1)
471                         {
472                                 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
473                         }
474                         # This doesnt actually get replaced with anything
475                         $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
476                 }
477                 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
478                 {
479                         my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
480                         $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
481                 }
482                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
483                 {
484                         my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
485                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
486                 }
487                 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
488                 {
489                         my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
490                         $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
491                 }
492                 while ($line =~ /rpath\("(.+?)"\)/)
493                 {
494                         my $replace = make_rpath($1,$module);
495                         $line =~ s/rpath\("(.+?)"\)/$replace/;
496                 }
497         };
498         if ($@)
499         {
500                 my $err = $@;
501                 #$err =~ s/at .+? line \d+.*//g;
502                 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
503                 print "\nMake sure you have pkg-config installed\n";
504                 print "\nIn the case of gnutls configuration errors on debian,\n";
505                 print "Ubuntu, etc, you should ensure that you have installed\n";
506                 print "gnutls-bin as well as gnutls-dev and gnutls.\n";
507                 exit;
508         }
509         else
510         {
511                 return $line;
512         }
513 }
514
515 1;
516