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