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