2 # InspIRCd -- Internet Relay Chat Daemon
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>
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.
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
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/>.
23 package make::utilities;
28 use warnings FATAL => qw(all);
30 use Exporter 'import';
35 our @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
37 # Parse the output of a *_config program,
38 # such as pcre_config, take out the -L
39 # directive and return an rpath for it.
41 # \e[1;32msrc/Makefile\e[0m
43 my %already_added = ();
44 my $if_skip_lines = 0;
46 sub promptstring($$$$$)
48 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
50 if (!$main::interactive)
52 my $opt_commandlineswitch;
53 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
54 if (defined $opt_commandlineswitch)
56 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
57 $var = $opt_commandlineswitch;
61 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
66 print "\nPlease enter the $prompt?\n";
67 print "[\e[1;32m$default\e[0m] -> ";
68 chomp($var = <STDIN>);
74 $main::config{$configitem} = $var;
79 my ($executable, $module) = @_;
80 return "" if defined $ENV{DISABLE_RPATH};
81 chomp(my $data = `$executable`);
83 while ($data =~ /-L(\S+)/)
86 if (!exists $already_added{$libpath})
88 print "Adding runtime library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
89 $already_added{$libpath} = 1;
91 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath ";
99 return if defined $ENV{DISABLE_EXTEND_PKG_PATH};
100 if (!exists $ENV{PKG_CONFIG_PATH})
102 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
106 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
110 sub pkgconfig_get_include_dirs($$$;$)
112 my ($packagename, $headername, $defaults, $module) = @_;
114 my $key = "default_includedir_$packagename";
115 if (exists $main::config{$key})
117 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
118 my $ret = $main::config{$key};
119 print "\e[1;32m$ret\e[0m (cached)\n";
125 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
127 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
128 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
130 if ((!defined $v) || ($v eq ""))
132 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
133 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
134 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
135 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
137 if ((defined $find) && ($find ne "") && ($find ne $packagename))
139 print "(\e[1;32mFound via search\e[0m) ";
149 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
151 $ret = "$foo " . $defaults;
154 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
156 my $key = "default_includedir_$packagename";
157 if (exists $main::config{$key})
159 $ret = $main::config{$key};
163 $headername =~ s/^\///;
164 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
165 $packagename =~ tr/a-z/A-Z/;
168 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
172 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
174 $main::config{$key} =~ s/^\s+//g;
175 $ret = $main::config{$key};
182 my $key = "default_includedir_$packagename";
183 $packagename =~ tr/a-z/A-Z/;
184 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
185 $main::config{$key} =~ s/^\s+//g;
186 $ret = $main::config{$key};
187 print "\e[1;32m$ret\e[0m (version $v)\n";
193 sub pkgconfig_check_version($$;$)
195 my ($packagename, $version, $module) = @_;
199 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
201 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
206 if ((defined $v) && ($v ne ""))
208 if (!system "pkg-config --atleast-version $version $packagename")
210 print "\e[1;32mYes (version $v)\e[0m\n";
215 print "\e[1;32mNo (version $v)\e[0m\n";
219 # If we didnt find it, we cant definitively say its too old.
220 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
221 # the missing library later on.
222 print "\e[1;32mNo (not found)\e[0m\n";
226 sub pkgconfig_get_lib_dirs($$$;$)
228 my ($packagename, $libname, $defaults, $module) = @_;
230 my $key = "default_libdir_$packagename";
231 if (exists $main::config{$key})
233 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
234 my $ret = $main::config{$key};
235 print "\e[1;32m$ret\e[0m (cached)\n";
241 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
243 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
244 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
247 if ((!defined $v) || ($v eq ""))
249 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
250 $foo = `$locbin "$libname" | head -n 1`;
251 $foo =~ /(.+)\Q$libname\E/;
254 if ((defined $find) && ($find ne "") && ($find ne $packagename))
256 print "(\e[1;32mFound via search\e[0m) ";
267 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
269 $ret = "$foo " . $defaults;
272 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
274 my $key = "default_libdir_$packagename";
275 if (exists $main::config{$key})
277 $ret = $main::config{$key};
282 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
283 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
284 $main::config{$key} =~ s/^\s+//g;
285 $ret = $main::config{$key};
292 print "\e[1;32m$ret\e[0m (version $v)\n";
293 my $key = "default_libdir_$packagename";
294 $main::config{$key} = $ret;
295 $main::config{$key} =~ s/^\s+//g;
302 # Translate a $CompileFlags etc line and parse out function calls
303 # to functions within these modules at configure time.
304 sub translate_functions($$)
306 my ($line,$module) = @_;
310 $module =~ /modules*\/(.+?)$/;
313 # This is only a cursory check, just designed to catch casual accidental use of backticks.
314 # There are pleanty of ways around it, but its not supposed to be for security, just checking
315 # that people are using the new configuration api as theyre supposed to and not just using
316 # backticks instead of eval(), being as eval has accountability. People wanting to get around
317 # the accountability will do so anyway.
318 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
320 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
323 if ($line =~ /if(gt|lt)\("(.+?)","(.+?)"\)/) {
324 chomp(my $result = `$2 2>/dev/null`);
325 if (($1 eq 'gt' && $result le $3) || ($1 eq 'lt' && $result ge $3)) {
326 $line = substr $line, 0, $-[0];
328 $line =~ s/if$1\("$2","$3"\)//;
332 if ($line =~ /ifuname\(\!"(\w+)"\)/)
341 $line =~ s/ifuname\(\!"(.+?)"\)//;
344 if ($line =~ /ifuname\("(\w+)"\)/)
353 $line =~ s/ifuname\("(.+?)"\)//;
356 if ($line =~ /if\("(\w+)"\)/)
358 if (defined $main::config{$1})
360 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
367 $line =~ s/if\("(.+?)"\)//;
369 if ($line =~ /if\(\!"(\w+)"\)/)
371 if (!exists $main::config{$1})
380 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
388 $line =~ s/if\(\!"(.+?)"\)//;
390 while ($line =~ /exec\("(.+?)"\)/)
392 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
394 die $replace if ($replace =~ /Configuration failed/);
396 $line =~ s/exec\("(.+?)"\)/$replace/;
398 while ($line =~ /execruntime\("(.+?)"\)/)
400 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
402 while ($line =~ /eval\("(.+?)"\)/)
404 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
408 $tmpfile = File::Temp::tmpnam();
409 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
410 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
413 my $replace = `perl $tmpfile`;
416 $line =~ s/eval\("(.+?)"\)/$replace/;
418 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
420 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
421 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
423 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
425 if (pkgconfig_check_version($1, $2, $module) != 1)
427 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
429 # This doesnt actually get replaced with anything
430 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
432 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
434 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
435 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
437 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
439 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
440 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
442 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
444 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
445 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
447 while ($line =~ /rpath\("(.+?)"\)/)
449 my $replace = make_rpath($1,$module);
450 $line =~ s/rpath\("(.+?)"\)/$replace/;
456 #$err =~ s/at .+? line \d+.*//g;
457 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
458 print "\nMake sure you have pkg-config installed\n";
459 print "\nIn the case of gnutls configuration errors on debian,\n";
460 print "Ubuntu, etc, you should ensure that you have installed\n";
461 print "gnutls-bin as well as libgnutls-dev and libgnutls.\n";