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/>.
27 package make::utilities;
30 use warnings FATAL => qw(all);
32 use Exporter 'import';
35 use File::Spec::Functions qw(rel2abs);
39 our @EXPORT = qw(module_installed prompt_bool prompt_dir prompt_string make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
41 # Parse the output of a *_config program,
42 # such as pcre_config, take out the -L
43 # directive and return an rpath for it.
45 # \e[1;32msrc/Makefile\e[0m
47 my %already_added = ();
48 my $if_skip_lines = 0;
50 sub module_installed($)
57 sub prompt_bool($$$) {
58 my ($interactive, $question, $default) = @_;
59 my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n');
60 return $answer =~ /y/i;
64 my ($interactive, $question, $default) = @_;
65 my ($answer, $create) = (undef, 'y');
67 $answer = rel2abs(prompt_string($interactive, $question, $default));
68 $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y');
70 mkpath($answer, 0, 0750);
73 unless (defined $mkpath) {
74 print "Error: unable to create $answer!\n\n";
81 sub prompt_string($$$) {
82 my ($interactive, $question, $default) = @_;
83 return $default unless $interactive;
84 print $question, "\n";
85 print "[\e[1;32m$default\e[0m] => ";
86 chomp(my $answer = <STDIN>);
88 return $answer ? $answer : $default;
91 sub promptstring($$$$$)
93 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
95 if (!$main::interactive)
97 my $opt_commandlineswitch;
98 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
99 if (defined $opt_commandlineswitch)
101 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
102 $var = $opt_commandlineswitch;
106 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
111 print "\nPlease enter the $prompt?\n";
112 print "[\e[1;32m$default\e[0m] -> ";
113 chomp($var = <STDIN>);
119 $main::config{$configitem} = $var;
124 my ($executable, $module) = @_;
125 chomp(my $data = `$executable`);
127 while ($data =~ /-L(\S+)/)
130 if (!exists $already_added{$libpath})
132 print "Adding extra library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
133 $already_added{$libpath} = 1;
135 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath " unless defined $main::opt_disablerpath;
136 $data =~ s/-L(\S+)//;
141 sub extend_pkg_path()
143 if (!exists $ENV{PKG_CONFIG_PATH})
145 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
149 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
153 sub pkgconfig_get_include_dirs($$$;$)
155 my ($packagename, $headername, $defaults, $module) = @_;
159 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
161 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
162 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
164 if ((!defined $v) || ($v eq ""))
166 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
167 $foo = `locate "$headername" 2>/dev/null | head -n 1`;
168 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
170 if ((defined $find) && ($find ne "") && ($find ne $packagename))
172 print "(\e[1;32mFound via search\e[0m) ";
182 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
184 $ret = "$foo " . $defaults;
187 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
189 my $key = "default_includedir_$packagename";
190 if (exists $main::config{$key})
192 $ret = $main::config{$key};
196 $headername =~ s/^\///;
197 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
198 $packagename =~ tr/a-z/A-Z/;
201 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
205 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
207 $main::config{$key} =~ s/^\s+//g;
208 $ret = $main::config{$key};
215 my $key = "default_includedir_$packagename";
216 $packagename =~ tr/a-z/A-Z/;
217 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
218 $main::config{$key} =~ s/^\s+//g;
219 $ret = $main::config{$key};
220 print "\e[1;32m$ret\e[0m (version $v)\n";
226 sub pkgconfig_check_version($$;$)
228 my ($packagename, $version, $module) = @_;
232 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
234 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
239 if ((defined $v) && ($v ne ""))
241 if (!system "pkg-config --atleast-version $version $packagename")
243 print "\e[1;32mYes (version $v)\e[0m\n";
248 print "\e[1;32mNo (version $v)\e[0m\n";
252 # If we didnt find it, we cant definitively say its too old.
253 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
254 # the missing library later on.
255 print "\e[1;32mNo (not found)\e[0m\n";
259 sub pkgconfig_get_lib_dirs($$$;$)
261 my ($packagename, $libname, $defaults, $module) = @_;
265 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
267 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
268 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
271 if ((!defined $v) || ($v eq ""))
273 $foo = `locate "$libname" | head -n 1`;
274 $foo =~ /(.+)\Q$libname\E/;
277 if ((defined $find) && ($find ne "") && ($find ne $packagename))
279 print "(\e[1;32mFound via search\e[0m) ";
290 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
292 $ret = "$foo " . $defaults;
295 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
297 my $key = "default_libdir_$packagename";
298 if (exists $main::config{$key})
300 $ret = $main::config{$key};
305 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
306 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
307 $main::config{$key} =~ s/^\s+//g;
308 $ret = $main::config{$key};
315 print "\e[1;32m$ret\e[0m (version $v)\n";
316 my $key = "default_libdir_$packagename";
317 $main::config{$key} = $ret;
318 $main::config{$key} =~ s/^\s+//g;
325 # Translate a $CompileFlags etc line and parse out function calls
326 # to functions within these modules at configure time.
327 sub translate_functions($$)
329 my ($line,$module) = @_;
333 $module =~ /modules*\/(.+?)$/;
336 if ($line =~ /ifuname\(\!"(\w+)"\)/)
345 $line =~ s/ifuname\(\!"(.+?)"\)//;
348 if ($line =~ /ifuname\("(\w+)"\)/)
357 $line =~ s/ifuname\("(.+?)"\)//;
360 if ($line =~ /if\("(\w+)"\)/)
362 if (defined $main::config{$1})
364 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
371 $line =~ s/if\("(.+?)"\)//;
373 if ($line =~ /if\(\!"(\w+)"\)/)
375 if (!exists $main::config{$1})
384 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
392 $line =~ s/if\(\!"(.+?)"\)//;
394 while ($line =~ /exec\("(.+?)"\)/)
396 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
398 die $replace if ($replace =~ /Configuration failed/);
400 $line =~ s/exec\("(.+?)"\)/$replace/;
402 while ($line =~ /execruntime\("(.+?)"\)/)
404 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
406 while ($line =~ /eval\("(.+?)"\)/)
408 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
413 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
414 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
417 my $replace = `perl $tmpfile`;
419 $line =~ s/eval\("(.+?)"\)/$replace/;
421 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
423 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
424 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
426 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
428 if (pkgconfig_check_version($1, $2, $module) != 1)
430 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
432 # This doesnt actually get replaced with anything
433 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
435 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
437 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
438 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
440 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
442 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
443 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
445 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
447 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
448 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
450 while ($line =~ /rpath\("(.+?)"\)/)
452 my $replace = make_rpath($1,$module);
453 $replace = "" if ($^O =~ /darwin/i);
454 $line =~ s/rpath\("(.+?)"\)/$replace/;
460 #$err =~ s/at .+? line \d+.*//g;
461 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
462 print "\nMake sure you have pkg-config installed\n";
463 print "\nIn the case of gnutls configuration errors on debian,\n";
464 print "Ubuntu, etc, you should ensure that you have installed\n";
465 print "gnutls-bin as well as gnutls-dev and gnutls.\n";