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