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