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(get_version 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 my %already_added = ();
45 return %version if %version;
47 # Attempt to retrieve version information from src/version.sh
48 chomp(my $vf = `sh src/version.sh 2>/dev/null`);
49 if ($vf =~ /^InspIRCd-([0-9]+)\.([0-9]+)\.([0-9]+)(?:\+(\w+))?$/) {
50 %version = ( MAJOR => $1, MINOR => $2, PATCH => $3, LABEL => $4 );
53 # Attempt to retrieve missing version information from Git
54 chomp(my $gr = `git describe --tags 2>/dev/null`);
55 if ($gr =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-\d+-(\w+))?$/) {
56 $version{MAJOR} = $1 unless defined $version{MAJOR};
57 $version{MINOR} = $2 unless defined $version{MINOR};
58 $version{PATCH} = $3 unless defined $version{PATCH};
59 $version{LABEL} = $4 if defined $4;
62 # The user is using a stable release which does not have
64 $version{LABEL} = 'release' unless defined $version{LABEL};
66 # If any of these fields are missing then the user has deleted the
67 # version file and is not running from Git. Fill in the fields with
68 # dummy data so we don't get into trouble with undef values later.
69 $version{MAJOR} = '0' unless defined $version{MAJOR};
70 $version{MINOR} = '0' unless defined $version{MINOR};
71 $version{PATCH} = '0' unless defined $version{PATCH};
76 sub module_installed($) {
82 sub prompt_bool($$$) {
83 my ($interactive, $question, $default) = @_;
84 my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n');
85 return $answer =~ /y/i;
89 my ($interactive, $question, $default) = @_;
90 my ($answer, $create) = (undef, 'y');
92 $answer = rel2abs(prompt_string($interactive, $question, $default));
93 $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y');
95 mkpath($answer, 0, 0750);
98 unless (defined $mkpath) {
99 print "Error: unable to create $answer!\n\n";
106 sub prompt_string($$$) {
107 my ($interactive, $question, $default) = @_;
108 return $default unless $interactive;
109 print $question, "\n";
110 print "[\e[1;32m$default\e[0m] => ";
111 chomp(my $answer = <STDIN>);
113 return $answer ? $answer : $default;
119 $count = `sysctl -n hw.ncpu`;
120 } elsif ($^O eq 'darwin') {
121 $count = `sysctl -n hw.activecpu`;
122 } elsif ($^O eq 'linux') {
123 $count = `getconf _NPROCESSORS_ONLN`;
124 } elsif ($^O eq 'solaris') {
125 $count = `psrinfo -p`;
131 sub promptstring($$$$$)
133 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
135 if (!$main::interactive)
137 my $opt_commandlineswitch;
138 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
139 if (defined $opt_commandlineswitch)
141 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
142 $var = $opt_commandlineswitch;
146 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
151 print "\nPlease enter the $prompt?\n";
152 print "[\e[1;32m$default\e[0m] -> ";
153 chomp($var = <STDIN>);
159 $main::config{$configitem} = $var;
164 my ($executable, $module) = @_;
165 chomp(my $data = `$executable`);
167 while ($data =~ /-L(\S+)/)
170 if (!exists $already_added{$libpath})
172 print "Adding extra library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
173 $already_added{$libpath} = 1;
175 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath " unless defined $main::opt_disablerpath;
176 $data =~ s/-L(\S+)//;
181 sub extend_pkg_path()
183 if (!exists $ENV{PKG_CONFIG_PATH})
185 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
189 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
193 sub pkgconfig_get_include_dirs($$$;$)
195 my ($packagename, $headername, $defaults, $module) = @_;
199 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
201 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
202 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
204 if ((!defined $v) || ($v eq ""))
206 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
207 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
208 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
209 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
211 if ((defined $find) && ($find ne "") && ($find ne $packagename))
213 print "(\e[1;32mFound via search\e[0m) ";
223 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
225 $ret = "$foo " . $defaults;
228 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
230 my $key = "default_includedir_$packagename";
231 if (exists $main::config{$key})
233 $ret = $main::config{$key};
237 $headername =~ s/^\///;
238 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
239 $packagename =~ tr/a-z/A-Z/;
242 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
246 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
248 $main::config{$key} =~ s/^\s+//g;
249 $ret = $main::config{$key};
256 my $key = "default_includedir_$packagename";
257 $packagename =~ tr/a-z/A-Z/;
258 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
259 $main::config{$key} =~ s/^\s+//g;
260 $ret = $main::config{$key};
261 print "\e[1;32m$ret\e[0m (version $v)\n";
267 sub pkgconfig_check_version($$;$)
269 my ($packagename, $version, $module) = @_;
273 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
275 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
280 if ((defined $v) && ($v ne ""))
282 if (!system "pkg-config --atleast-version $version $packagename")
284 print "\e[1;32mYes (version $v)\e[0m\n";
289 print "\e[1;32mNo (version $v)\e[0m\n";
293 # If we didnt find it, we cant definitively say its too old.
294 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
295 # the missing library later on.
296 print "\e[1;32mNo (not found)\e[0m\n";
300 sub pkgconfig_get_lib_dirs($$$;$)
302 my ($packagename, $libname, $defaults, $module) = @_;
306 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
308 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
309 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
312 if ((!defined $v) || ($v eq ""))
314 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
315 $foo = `$locbin "$libname" | head -n 1`;
316 $foo =~ /(.+)\Q$libname\E/;
319 if ((defined $find) && ($find ne "") && ($find ne $packagename))
321 print "(\e[1;32mFound via search\e[0m) ";
332 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
334 $ret = "$foo " . $defaults;
337 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
339 my $key = "default_libdir_$packagename";
340 if (exists $main::config{$key})
342 $ret = $main::config{$key};
347 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
348 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
349 $main::config{$key} =~ s/^\s+//g;
350 $ret = $main::config{$key};
357 print "\e[1;32m$ret\e[0m (version $v)\n";
358 my $key = "default_libdir_$packagename";
359 $main::config{$key} = $ret;
360 $main::config{$key} =~ s/^\s+//g;
367 # Translate a $CompileFlags etc line and parse out function calls
368 # to functions within these modules at configure time.
369 sub translate_functions($$)
371 my ($line,$module) = @_;
375 $module =~ /modules*\/(.+?)$/;
378 if ($line =~ /ifuname\(\!"(\w+)"\)/)
387 $line =~ s/ifuname\(\!"(.+?)"\)//;
390 if ($line =~ /ifuname\("(\w+)"\)/)
399 $line =~ s/ifuname\("(.+?)"\)//;
402 if ($line =~ /if\("(\w+)"\)/)
404 if (defined $main::config{$1})
406 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
413 $line =~ s/if\("(.+?)"\)//;
415 if ($line =~ /if\(\!"(\w+)"\)/)
417 if (!exists $main::config{$1})
426 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
434 $line =~ s/if\(\!"(.+?)"\)//;
436 while ($line =~ /exec\("(.+?)"\)/)
438 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
440 die $replace if ($replace =~ /Configuration failed/);
442 $line =~ s/exec\("(.+?)"\)/$replace/;
444 while ($line =~ /execruntime\("(.+?)"\)/)
446 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
448 while ($line =~ /eval\("(.+?)"\)/)
450 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
455 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
456 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
459 my $replace = `perl $tmpfile`;
461 $line =~ s/eval\("(.+?)"\)/$replace/;
463 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
465 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
466 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
468 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
470 if (pkgconfig_check_version($1, $2, $module) != 1)
472 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
474 # This doesnt actually get replaced with anything
475 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
477 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
479 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
480 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
482 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
484 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
485 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
487 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
489 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
490 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
492 while ($line =~ /rpath\("(.+?)"\)/)
494 my $replace = make_rpath($1,$module);
495 $line =~ s/rpath\("(.+?)"\)/$replace/;
501 #$err =~ s/at .+? line \d+.*//g;
502 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
503 print "\nMake sure you have pkg-config installed\n";
504 print "\nIn the case of gnutls configuration errors on debian,\n";
505 print "Ubuntu, etc, you should ensure that you have installed\n";
506 print "gnutls-bin as well as gnutls-dev and gnutls.\n";