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