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 return "" if defined $ENV{DISABLE_RPATH};
166 chomp(my $data = `$executable`);
168 while ($data =~ /-L(\S+)/)
171 if (!exists $already_added{$libpath})
173 print "Adding runtime library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
174 $already_added{$libpath} = 1;
176 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath ";
177 $data =~ s/-L(\S+)//;
182 sub extend_pkg_path()
184 if (!exists $ENV{PKG_CONFIG_PATH})
186 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
190 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
194 sub pkgconfig_get_include_dirs($$$;$)
196 my ($packagename, $headername, $defaults, $module) = @_;
200 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
202 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
203 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
205 if ((!defined $v) || ($v eq ""))
207 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
208 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
209 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
210 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
212 if ((defined $find) && ($find ne "") && ($find ne $packagename))
214 print "(\e[1;32mFound via search\e[0m) ";
224 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
226 $ret = "$foo " . $defaults;
229 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
231 my $key = "default_includedir_$packagename";
232 if (exists $main::config{$key})
234 $ret = $main::config{$key};
238 $headername =~ s/^\///;
239 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
240 $packagename =~ tr/a-z/A-Z/;
243 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
247 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
249 $main::config{$key} =~ s/^\s+//g;
250 $ret = $main::config{$key};
257 my $key = "default_includedir_$packagename";
258 $packagename =~ tr/a-z/A-Z/;
259 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
260 $main::config{$key} =~ s/^\s+//g;
261 $ret = $main::config{$key};
262 print "\e[1;32m$ret\e[0m (version $v)\n";
268 sub pkgconfig_check_version($$;$)
270 my ($packagename, $version, $module) = @_;
274 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
276 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
281 if ((defined $v) && ($v ne ""))
283 if (!system "pkg-config --atleast-version $version $packagename")
285 print "\e[1;32mYes (version $v)\e[0m\n";
290 print "\e[1;32mNo (version $v)\e[0m\n";
294 # If we didnt find it, we cant definitively say its too old.
295 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
296 # the missing library later on.
297 print "\e[1;32mNo (not found)\e[0m\n";
301 sub pkgconfig_get_lib_dirs($$$;$)
303 my ($packagename, $libname, $defaults, $module) = @_;
307 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
309 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
310 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
313 if ((!defined $v) || ($v eq ""))
315 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
316 $foo = `$locbin "$libname" | head -n 1`;
317 $foo =~ /(.+)\Q$libname\E/;
320 if ((defined $find) && ($find ne "") && ($find ne $packagename))
322 print "(\e[1;32mFound via search\e[0m) ";
333 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
335 $ret = "$foo " . $defaults;
338 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
340 my $key = "default_libdir_$packagename";
341 if (exists $main::config{$key})
343 $ret = $main::config{$key};
348 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
349 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
350 $main::config{$key} =~ s/^\s+//g;
351 $ret = $main::config{$key};
358 print "\e[1;32m$ret\e[0m (version $v)\n";
359 my $key = "default_libdir_$packagename";
360 $main::config{$key} = $ret;
361 $main::config{$key} =~ s/^\s+//g;
368 # Translate a $CompileFlags etc line and parse out function calls
369 # to functions within these modules at configure time.
370 sub translate_functions($$)
372 my ($line,$module) = @_;
376 $module =~ /modules*\/(.+?)$/;
379 if ($line =~ /ifuname\(\!"(\w+)"\)/)
388 $line =~ s/ifuname\(\!"(.+?)"\)//;
391 if ($line =~ /ifuname\("(\w+)"\)/)
400 $line =~ s/ifuname\("(.+?)"\)//;
403 if ($line =~ /if\("(\w+)"\)/)
405 if (defined $main::config{$1})
407 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
414 $line =~ s/if\("(.+?)"\)//;
416 if ($line =~ /if\(\!"(\w+)"\)/)
418 if (!exists $main::config{$1})
427 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
435 $line =~ s/if\(\!"(.+?)"\)//;
437 while ($line =~ /exec\("(.+?)"\)/)
439 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
441 die $replace if ($replace =~ /Configuration failed/);
443 $line =~ s/exec\("(.+?)"\)/$replace/;
445 while ($line =~ /execruntime\("(.+?)"\)/)
447 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
449 while ($line =~ /eval\("(.+?)"\)/)
451 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
456 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
457 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
460 my $replace = `perl $tmpfile`;
463 $line =~ s/eval\("(.+?)"\)/$replace/;
465 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
467 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
468 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
470 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
472 if (pkgconfig_check_version($1, $2, $module) != 1)
474 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
476 # This doesnt actually get replaced with anything
477 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
479 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
481 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
482 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
484 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
486 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
487 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
489 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
491 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
492 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
494 while ($line =~ /rpath\("(.+?)"\)/)
496 my $replace = make_rpath($1,$module);
497 $line =~ s/rpath\("(.+?)"\)/$replace/;
503 #$err =~ s/at .+? line \d+.*//g;
504 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
505 print "\nMake sure you have pkg-config installed\n";
506 print "\nIn the case of gnutls configuration errors on debian,\n";
507 print "Ubuntu, etc, you should ensure that you have installed\n";
508 print "gnutls-bin as well as libgnutls-dev and libgnutls.\n";