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';
34 our @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
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.
40 # \e[1;32msrc/Makefile\e[0m
42 my %already_added = ();
43 my $if_skip_lines = 0;
45 sub promptstring($$$$$)
47 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
49 if (!$main::interactive)
51 my $opt_commandlineswitch;
52 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
53 if (defined $opt_commandlineswitch)
55 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
56 $var = $opt_commandlineswitch;
60 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
65 print "\nPlease enter the $prompt?\n";
66 print "[\e[1;32m$default\e[0m] -> ";
67 chomp($var = <STDIN>);
73 $main::config{$configitem} = $var;
78 my ($executable, $module) = @_;
79 chomp(my $data = `$executable`);
81 while ($data =~ /-L(\S+)/)
84 if (!exists $already_added{$libpath})
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;
89 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath " unless defined $main::opt_disablerpath;
97 if (!exists $ENV{PKG_CONFIG_PATH})
99 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
103 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
107 sub pkgconfig_get_include_dirs($$$;$)
109 my ($packagename, $headername, $defaults, $module) = @_;
111 my $key = "default_includedir_$packagename";
112 if (exists $main::config{$key})
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";
122 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
124 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
125 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
127 if ((!defined $v) || ($v eq ""))
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 : '';
134 if ((defined $find) && ($find ne "") && ($find ne $packagename))
136 print "(\e[1;32mFound via search\e[0m) ";
146 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
148 $ret = "$foo " . $defaults;
151 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
153 my $key = "default_includedir_$packagename";
154 if (exists $main::config{$key})
156 $ret = $main::config{$key};
160 $headername =~ s/^\///;
161 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
162 $packagename =~ tr/a-z/A-Z/;
165 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
169 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
171 $main::config{$key} =~ s/^\s+//g;
172 $ret = $main::config{$key};
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";
190 sub pkgconfig_check_version($$;$)
192 my ($packagename, $version, $module) = @_;
196 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
198 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
203 if ((defined $v) && ($v ne ""))
205 if (!system "pkg-config --atleast-version $version $packagename")
207 print "\e[1;32mYes (version $v)\e[0m\n";
212 print "\e[1;32mNo (version $v)\e[0m\n";
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";
223 sub pkgconfig_get_lib_dirs($$$;$)
225 my ($packagename, $libname, $defaults, $module) = @_;
227 my $key = "default_libdir_$packagename";
228 if (exists $main::config{$key})
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";
238 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
240 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
241 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
244 if ((!defined $v) || ($v eq ""))
246 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
247 $foo = `$locbin "$libname" | head -n 1`;
248 $foo =~ /(.+)\Q$libname\E/;
251 if ((defined $find) && ($find ne "") && ($find ne $packagename))
253 print "(\e[1;32mFound via search\e[0m) ";
264 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
266 $ret = "$foo " . $defaults;
269 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
271 my $key = "default_libdir_$packagename";
272 if (exists $main::config{$key})
274 $ret = $main::config{$key};
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};
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;
299 # Translate a $CompileFlags etc line and parse out function calls
300 # to functions within these modules at configure time.
301 sub translate_functions($$)
303 my ($line,$module) = @_;
307 $module =~ /modules*\/(.+?)$/;
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\(.+?`.+?\)/))
317 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
320 if ($line =~ /ifuname\(\!"(\w+)"\)/)
329 $line =~ s/ifuname\(\!"(.+?)"\)//;
332 if ($line =~ /ifuname\("(\w+)"\)/)
341 $line =~ s/ifuname\("(.+?)"\)//;
344 if ($line =~ /if\("(\w+)"\)/)
346 if (defined $main::config{$1})
348 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
355 $line =~ s/if\("(.+?)"\)//;
357 if ($line =~ /if\(\!"(\w+)"\)/)
359 if (!exists $main::config{$1})
368 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
376 $line =~ s/if\(\!"(.+?)"\)//;
378 while ($line =~ /exec\("(.+?)"\)/)
380 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
382 die $replace if ($replace =~ /Configuration failed/);
384 $line =~ s/exec\("(.+?)"\)/$replace/;
386 while ($line =~ /execruntime\("(.+?)"\)/)
388 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
390 while ($line =~ /eval\("(.+?)"\)/)
392 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
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";
401 my $replace = `perl $tmpfile`;
403 $line =~ s/eval\("(.+?)"\)/$replace/;
405 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
407 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
408 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
410 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
412 if (pkgconfig_check_version($1, $2, $module) != 1)
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.";
416 # This doesnt actually get replaced with anything
417 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
419 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
421 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
422 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
424 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
426 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
427 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
429 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
431 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
432 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
434 while ($line =~ /rpath\("(.+?)"\)/)
436 my $replace = make_rpath($1,$module);
437 $replace = "" if ($^O =~ /darwin/i);
438 $line =~ s/rpath\("(.+?)"\)/$replace/;
444 #$err =~ s/at .+? line \d+.*//g;
445 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
446 print "\nMake sure you have pkg-config installed\n";
447 print "\nIn the case of gnutls configuration errors on debian,\n";
448 print "Ubuntu, etc, you should ensure that you have installed\n";
449 print "gnutls-bin as well as gnutls-dev and gnutls.\n";