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(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
40 my %already_added = ();
42 sub promptstring($$$$$)
44 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
46 if (!$main::interactive)
48 my $opt_commandlineswitch;
49 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
50 if (defined $opt_commandlineswitch)
52 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
53 $var = $opt_commandlineswitch;
57 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
62 print "\nPlease enter the $prompt?\n";
63 print "[\e[1;32m$default\e[0m] -> ";
64 chomp($var = <STDIN>);
70 $main::config{$configitem} = $var;
75 my ($executable, $module) = @_;
76 return "" if defined $ENV{DISABLE_RPATH};
77 chomp(my $data = `$executable`);
79 while ($data =~ /-L(\S+)/)
82 if (!exists $already_added{$libpath})
84 print "Adding runtime library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
85 $already_added{$libpath} = 1;
87 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath ";
95 if (!exists $ENV{PKG_CONFIG_PATH})
97 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
101 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
105 sub pkgconfig_get_include_dirs($$$;$)
107 my ($packagename, $headername, $defaults, $module) = @_;
111 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
113 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
114 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
116 if ((!defined $v) || ($v eq ""))
118 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
119 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
120 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
121 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
123 if ((defined $find) && ($find ne "") && ($find ne $packagename))
125 print "(\e[1;32mFound via search\e[0m) ";
135 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
137 $ret = "$foo " . $defaults;
140 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
142 my $key = "default_includedir_$packagename";
143 if (exists $main::config{$key})
145 $ret = $main::config{$key};
149 $headername =~ s/^\///;
150 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
151 $packagename =~ tr/a-z/A-Z/;
154 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
158 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
160 $main::config{$key} =~ s/^\s+//g;
161 $ret = $main::config{$key};
168 my $key = "default_includedir_$packagename";
169 $packagename =~ tr/a-z/A-Z/;
170 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
171 $main::config{$key} =~ s/^\s+//g;
172 $ret = $main::config{$key};
173 print "\e[1;32m$ret\e[0m (version $v)\n";
179 sub pkgconfig_check_version($$;$)
181 my ($packagename, $version, $module) = @_;
185 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
187 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
192 if ((defined $v) && ($v ne ""))
194 if (!system "pkg-config --atleast-version $version $packagename")
196 print "\e[1;32mYes (version $v)\e[0m\n";
201 print "\e[1;32mNo (version $v)\e[0m\n";
205 # If we didnt find it, we cant definitively say its too old.
206 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
207 # the missing library later on.
208 print "\e[1;32mNo (not found)\e[0m\n";
212 sub pkgconfig_get_lib_dirs($$$;$)
214 my ($packagename, $libname, $defaults, $module) = @_;
218 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
220 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
221 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
224 if ((!defined $v) || ($v eq ""))
226 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
227 $foo = `$locbin "$libname" | head -n 1`;
228 $foo =~ /(.+)\Q$libname\E/;
231 if ((defined $find) && ($find ne "") && ($find ne $packagename))
233 print "(\e[1;32mFound via search\e[0m) ";
244 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
246 $ret = "$foo " . $defaults;
249 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
251 my $key = "default_libdir_$packagename";
252 if (exists $main::config{$key})
254 $ret = $main::config{$key};
259 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
260 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
261 $main::config{$key} =~ s/^\s+//g;
262 $ret = $main::config{$key};
269 print "\e[1;32m$ret\e[0m (version $v)\n";
270 my $key = "default_libdir_$packagename";
271 $main::config{$key} = $ret;
272 $main::config{$key} =~ s/^\s+//g;
279 # Translate a $CompileFlags etc line and parse out function calls
280 # to functions within these modules at configure time.
281 sub translate_functions($$)
283 my ($line,$module) = @_;
287 $module =~ /modules*\/(.+?)$/;
290 if ($line =~ /ifuname\(\!"(\w+)"\)/)
299 $line =~ s/ifuname\(\!"(.+?)"\)//;
302 if ($line =~ /ifuname\("(\w+)"\)/)
311 $line =~ s/ifuname\("(.+?)"\)//;
314 if ($line =~ /if\("(\w+)"\)/)
316 if (defined $main::config{$1})
318 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
325 $line =~ s/if\("(.+?)"\)//;
327 if ($line =~ /if\(\!"(\w+)"\)/)
329 if (!exists $main::config{$1})
338 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
346 $line =~ s/if\(\!"(.+?)"\)//;
348 while ($line =~ /exec\("(.+?)"\)/)
350 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
352 die $replace if ($replace =~ /Configuration failed/);
354 $line =~ s/exec\("(.+?)"\)/$replace/;
356 while ($line =~ /execruntime\("(.+?)"\)/)
358 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
360 while ($line =~ /eval\("(.+?)"\)/)
362 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
367 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
368 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
371 my $replace = `perl $tmpfile`;
374 $line =~ s/eval\("(.+?)"\)/$replace/;
376 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
378 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
379 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
381 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
383 if (pkgconfig_check_version($1, $2, $module) != 1)
385 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
387 # This doesnt actually get replaced with anything
388 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
390 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
392 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
393 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
395 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
397 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
398 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
400 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
402 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
403 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
405 while ($line =~ /rpath\("(.+?)"\)/)
407 my $replace = make_rpath($1,$module);
408 $line =~ s/rpath\("(.+?)"\)/$replace/;
414 #$err =~ s/at .+? line \d+.*//g;
415 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
416 print "\nMake sure you have pkg-config installed\n";
417 print "\nIn the case of gnutls configuration errors on debian,\n";
418 print "Ubuntu, etc, you should ensure that you have installed\n";
419 print "gnutls-bin as well as libgnutls-dev and libgnutls.\n";