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