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 get_cpu_count 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;
94 $count = `sysctl -n hw.ncpu`;
95 } elsif ($^O eq 'darwin') {
96 $count = `sysctl -n hw.activecpu`;
97 } elsif ($^O eq 'linux') {
98 $count = `getconf _NPROCESSORS_ONLN`;
99 } elsif ($^O eq 'solaris') {
100 $count = `psrinfo -p`;
106 sub promptstring($$$$$)
108 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
110 if (!$main::interactive)
112 my $opt_commandlineswitch;
113 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
114 if (defined $opt_commandlineswitch)
116 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
117 $var = $opt_commandlineswitch;
121 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
126 print "\nPlease enter the $prompt?\n";
127 print "[\e[1;32m$default\e[0m] -> ";
128 chomp($var = <STDIN>);
134 $main::config{$configitem} = $var;
139 my ($executable, $module) = @_;
140 chomp(my $data = `$executable`);
142 while ($data =~ /-L(\S+)/)
145 if (!exists $already_added{$libpath})
147 print "Adding extra library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
148 $already_added{$libpath} = 1;
150 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath " unless defined $main::opt_disablerpath;
151 $data =~ s/-L(\S+)//;
156 sub extend_pkg_path()
158 if (!exists $ENV{PKG_CONFIG_PATH})
160 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
164 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
168 sub pkgconfig_get_include_dirs($$$;$)
170 my ($packagename, $headername, $defaults, $module) = @_;
174 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
176 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
177 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
179 if ((!defined $v) || ($v eq ""))
181 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
182 $foo = `locate "$headername" 2>/dev/null | head -n 1`;
183 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
185 if ((defined $find) && ($find ne "") && ($find ne $packagename))
187 print "(\e[1;32mFound via search\e[0m) ";
197 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
199 $ret = "$foo " . $defaults;
202 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
204 my $key = "default_includedir_$packagename";
205 if (exists $main::config{$key})
207 $ret = $main::config{$key};
211 $headername =~ s/^\///;
212 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
213 $packagename =~ tr/a-z/A-Z/;
216 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
220 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
222 $main::config{$key} =~ s/^\s+//g;
223 $ret = $main::config{$key};
230 my $key = "default_includedir_$packagename";
231 $packagename =~ tr/a-z/A-Z/;
232 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
233 $main::config{$key} =~ s/^\s+//g;
234 $ret = $main::config{$key};
235 print "\e[1;32m$ret\e[0m (version $v)\n";
241 sub pkgconfig_check_version($$;$)
243 my ($packagename, $version, $module) = @_;
247 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
249 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
254 if ((defined $v) && ($v ne ""))
256 if (!system "pkg-config --atleast-version $version $packagename")
258 print "\e[1;32mYes (version $v)\e[0m\n";
263 print "\e[1;32mNo (version $v)\e[0m\n";
267 # If we didnt find it, we cant definitively say its too old.
268 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
269 # the missing library later on.
270 print "\e[1;32mNo (not found)\e[0m\n";
274 sub pkgconfig_get_lib_dirs($$$;$)
276 my ($packagename, $libname, $defaults, $module) = @_;
280 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
282 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
283 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
286 if ((!defined $v) || ($v eq ""))
288 $foo = `locate "$libname" | head -n 1`;
289 $foo =~ /(.+)\Q$libname\E/;
292 if ((defined $find) && ($find ne "") && ($find ne $packagename))
294 print "(\e[1;32mFound via search\e[0m) ";
305 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
307 $ret = "$foo " . $defaults;
310 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
312 my $key = "default_libdir_$packagename";
313 if (exists $main::config{$key})
315 $ret = $main::config{$key};
320 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
321 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
322 $main::config{$key} =~ s/^\s+//g;
323 $ret = $main::config{$key};
330 print "\e[1;32m$ret\e[0m (version $v)\n";
331 my $key = "default_libdir_$packagename";
332 $main::config{$key} = $ret;
333 $main::config{$key} =~ s/^\s+//g;
340 # Translate a $CompileFlags etc line and parse out function calls
341 # to functions within these modules at configure time.
342 sub translate_functions($$)
344 my ($line,$module) = @_;
348 $module =~ /modules*\/(.+?)$/;
351 if ($line =~ /ifuname\(\!"(\w+)"\)/)
360 $line =~ s/ifuname\(\!"(.+?)"\)//;
363 if ($line =~ /ifuname\("(\w+)"\)/)
372 $line =~ s/ifuname\("(.+?)"\)//;
375 if ($line =~ /if\("(\w+)"\)/)
377 if (defined $main::config{$1})
379 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
386 $line =~ s/if\("(.+?)"\)//;
388 if ($line =~ /if\(\!"(\w+)"\)/)
390 if (!exists $main::config{$1})
399 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
407 $line =~ s/if\(\!"(.+?)"\)//;
409 while ($line =~ /exec\("(.+?)"\)/)
411 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
413 die $replace if ($replace =~ /Configuration failed/);
415 $line =~ s/exec\("(.+?)"\)/$replace/;
417 while ($line =~ /execruntime\("(.+?)"\)/)
419 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
421 while ($line =~ /eval\("(.+?)"\)/)
423 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
428 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
429 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
432 my $replace = `perl $tmpfile`;
434 $line =~ s/eval\("(.+?)"\)/$replace/;
436 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
438 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
439 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
441 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
443 if (pkgconfig_check_version($1, $2, $module) != 1)
445 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
447 # This doesnt actually get replaced with anything
448 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
450 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
452 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
453 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
455 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
457 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
458 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
460 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
462 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
463 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
465 while ($line =~ /rpath\("(.+?)"\)/)
467 my $replace = make_rpath($1,$module);
468 $replace = "" if ($^O =~ /darwin/i);
469 $line =~ s/rpath\("(.+?)"\)/$replace/;
475 #$err =~ s/at .+? line \d+.*//g;
476 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
477 print "\nMake sure you have pkg-config installed\n";
478 print "\nIn the case of gnutls configuration errors on debian,\n";
479 print "Ubuntu, etc, you should ensure that you have installed\n";
480 print "gnutls-bin as well as gnutls-dev and gnutls.\n";