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