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';
38 our @EXPORT = qw(get_version module_installed get_cpu_count make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
40 my %already_added = ();
44 return %version if %version;
46 # Attempt to retrieve version information from src/version.sh
47 chomp(my $vf = `sh src/version.sh 2>/dev/null`);
48 if ($vf =~ /^InspIRCd-([0-9]+)\.([0-9]+)\.([0-9]+)(?:\+(\w+))?$/) {
49 %version = ( MAJOR => $1, MINOR => $2, PATCH => $3, LABEL => $4 );
52 # Attempt to retrieve missing version information from Git
53 chomp(my $gr = `git describe --tags 2>/dev/null`);
54 if ($gr =~ /^v([0-9]+)\.([0-9]+)\.([0-9]+)(?:-\d+-(\w+))?$/) {
55 $version{MAJOR} = $1 unless defined $version{MAJOR};
56 $version{MINOR} = $2 unless defined $version{MINOR};
57 $version{PATCH} = $3 unless defined $version{PATCH};
58 $version{LABEL} = $4 if defined $4;
61 # The user is using a stable release which does not have
63 $version{LABEL} = 'release' unless defined $version{LABEL};
65 # If any of these fields are missing then the user has deleted the
66 # version file and is not running from Git. Fill in the fields with
67 # dummy data so we don't get into trouble with undef values later.
68 $version{MAJOR} = '0' unless defined $version{MAJOR};
69 $version{MINOR} = '0' unless defined $version{MINOR};
70 $version{PATCH} = '0' unless defined $version{PATCH};
75 sub module_installed($) {
84 $count = `sysctl -n hw.ncpu`;
85 } elsif ($^O eq 'darwin') {
86 $count = `sysctl -n hw.activecpu`;
87 } elsif ($^O eq 'linux') {
88 $count = `getconf _NPROCESSORS_ONLN`;
89 } elsif ($^O eq 'solaris') {
90 $count = `psrinfo -p`;
96 sub promptstring($$$$$)
98 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
100 if (!$main::interactive)
102 my $opt_commandlineswitch;
103 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
104 if (defined $opt_commandlineswitch)
106 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
107 $var = $opt_commandlineswitch;
111 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
116 print "\nPlease enter the $prompt?\n";
117 print "[\e[1;32m$default\e[0m] -> ";
118 chomp($var = <STDIN>);
124 $main::config{$configitem} = $var;
129 my ($executable, $module) = @_;
130 return "" if defined $ENV{DISABLE_RPATH};
131 chomp(my $data = `$executable`);
133 while ($data =~ /-L(\S+)/)
136 if (!exists $already_added{$libpath})
138 print "Adding runtime library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
139 $already_added{$libpath} = 1;
141 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath ";
142 $data =~ s/-L(\S+)//;
147 sub extend_pkg_path()
149 if (!exists $ENV{PKG_CONFIG_PATH})
151 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
155 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
159 sub pkgconfig_get_include_dirs($$$;$)
161 my ($packagename, $headername, $defaults, $module) = @_;
165 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
167 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
168 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
170 if ((!defined $v) || ($v eq ""))
172 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
173 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
174 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
175 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
177 if ((defined $find) && ($find ne "") && ($find ne $packagename))
179 print "(\e[1;32mFound via search\e[0m) ";
189 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
191 $ret = "$foo " . $defaults;
194 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
196 my $key = "default_includedir_$packagename";
197 if (exists $main::config{$key})
199 $ret = $main::config{$key};
203 $headername =~ s/^\///;
204 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
205 $packagename =~ tr/a-z/A-Z/;
208 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
212 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
214 $main::config{$key} =~ s/^\s+//g;
215 $ret = $main::config{$key};
222 my $key = "default_includedir_$packagename";
223 $packagename =~ tr/a-z/A-Z/;
224 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
225 $main::config{$key} =~ s/^\s+//g;
226 $ret = $main::config{$key};
227 print "\e[1;32m$ret\e[0m (version $v)\n";
233 sub pkgconfig_check_version($$;$)
235 my ($packagename, $version, $module) = @_;
239 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
241 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
246 if ((defined $v) && ($v ne ""))
248 if (!system "pkg-config --atleast-version $version $packagename")
250 print "\e[1;32mYes (version $v)\e[0m\n";
255 print "\e[1;32mNo (version $v)\e[0m\n";
259 # If we didnt find it, we cant definitively say its too old.
260 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
261 # the missing library later on.
262 print "\e[1;32mNo (not found)\e[0m\n";
266 sub pkgconfig_get_lib_dirs($$$;$)
268 my ($packagename, $libname, $defaults, $module) = @_;
272 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
274 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
275 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
278 if ((!defined $v) || ($v eq ""))
280 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
281 $foo = `$locbin "$libname" | head -n 1`;
282 $foo =~ /(.+)\Q$libname\E/;
285 if ((defined $find) && ($find ne "") && ($find ne $packagename))
287 print "(\e[1;32mFound via search\e[0m) ";
298 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
300 $ret = "$foo " . $defaults;
303 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
305 my $key = "default_libdir_$packagename";
306 if (exists $main::config{$key})
308 $ret = $main::config{$key};
313 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
314 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
315 $main::config{$key} =~ s/^\s+//g;
316 $ret = $main::config{$key};
323 print "\e[1;32m$ret\e[0m (version $v)\n";
324 my $key = "default_libdir_$packagename";
325 $main::config{$key} = $ret;
326 $main::config{$key} =~ s/^\s+//g;
333 # Translate a $CompileFlags etc line and parse out function calls
334 # to functions within these modules at configure time.
335 sub translate_functions($$)
337 my ($line,$module) = @_;
341 $module =~ /modules*\/(.+?)$/;
344 if ($line =~ /ifuname\(\!"(\w+)"\)/)
353 $line =~ s/ifuname\(\!"(.+?)"\)//;
356 if ($line =~ /ifuname\("(\w+)"\)/)
365 $line =~ s/ifuname\("(.+?)"\)//;
368 if ($line =~ /if\("(\w+)"\)/)
370 if (defined $main::config{$1})
372 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
379 $line =~ s/if\("(.+?)"\)//;
381 if ($line =~ /if\(\!"(\w+)"\)/)
383 if (!exists $main::config{$1})
392 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
400 $line =~ s/if\(\!"(.+?)"\)//;
402 while ($line =~ /exec\("(.+?)"\)/)
404 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
406 die $replace if ($replace =~ /Configuration failed/);
408 $line =~ s/exec\("(.+?)"\)/$replace/;
410 while ($line =~ /execruntime\("(.+?)"\)/)
412 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
414 while ($line =~ /eval\("(.+?)"\)/)
416 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
421 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
422 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
425 my $replace = `perl $tmpfile`;
428 $line =~ s/eval\("(.+?)"\)/$replace/;
430 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
432 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
433 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
435 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
437 if (pkgconfig_check_version($1, $2, $module) != 1)
439 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
441 # This doesnt actually get replaced with anything
442 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
444 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
446 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
447 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
449 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
451 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
452 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
454 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
456 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
457 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
459 while ($line =~ /rpath\("(.+?)"\)/)
461 my $replace = make_rpath($1,$module);
462 $line =~ s/rpath\("(.+?)"\)/$replace/;
468 #$err =~ s/at .+? line \d+.*//g;
469 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
470 print "\nMake sure you have pkg-config installed\n";
471 print "\nIn the case of gnutls configuration errors on debian,\n";
472 print "Ubuntu, etc, you should ensure that you have installed\n";
473 print "gnutls-bin as well as libgnutls-dev and libgnutls.\n";