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