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';
39 our @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
41 my %already_added = ();
43 sub promptstring($$$$$)
45 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
47 if (!$main::interactive)
49 my $opt_commandlineswitch;
50 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
51 if (defined $opt_commandlineswitch)
53 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
54 $var = $opt_commandlineswitch;
58 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
63 print "\nPlease enter the $prompt?\n";
64 print "[\e[1;32m$default\e[0m] -> ";
65 chomp($var = <STDIN>);
71 $main::config{$configitem} = $var;
76 my ($executable, $module) = @_;
77 return "" if defined $ENV{DISABLE_RPATH};
78 chomp(my $data = `$executable`);
80 while ($data =~ /-L(\S+)/)
83 if (!exists $already_added{$libpath})
85 print "Adding runtime library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
86 $already_added{$libpath} = 1;
88 $output .= "-Wl,-rpath -Wl,$libpath -L$libpath ";
94 sub pkgconfig_get_include_dirs($$$;$)
96 my ($packagename, $headername, $defaults, $module) = @_;
98 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
100 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
101 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
103 if ((!defined $v) || ($v eq ""))
105 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
106 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
107 $foo = `$locbin "$headername" 2>/dev/null | head -n 1`;
108 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
110 if ((defined $find) && ($find ne "") && ($find ne $packagename))
112 print "(\e[1;32mFound via search\e[0m) ";
122 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
124 $ret = "$foo " . $defaults;
127 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
129 my $key = "default_includedir_$packagename";
130 if (exists $main::config{$key})
132 $ret = $main::config{$key};
136 $headername =~ s/^\///;
137 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
138 $packagename =~ tr/a-z/A-Z/;
141 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
145 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
147 $main::config{$key} =~ s/^\s+//g;
148 $ret = $main::config{$key};
155 my $key = "default_includedir_$packagename";
156 $packagename =~ tr/a-z/A-Z/;
157 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
158 $main::config{$key} =~ s/^\s+//g;
159 $ret = $main::config{$key};
160 print "\e[1;32m$ret\e[0m (version $v)\n";
166 sub pkgconfig_check_version($$;$)
168 my ($packagename, $version, $module) = @_;
170 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
172 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
177 if ((defined $v) && ($v ne ""))
179 if (!system "pkg-config --atleast-version $version $packagename")
181 print "\e[1;32mYes (version $v)\e[0m\n";
186 print "\e[1;32mNo (version $v)\e[0m\n";
190 # If we didnt find it, we cant definitively say its too old.
191 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
192 # the missing library later on.
193 print "\e[1;32mNo (not found)\e[0m\n";
197 sub pkgconfig_get_lib_dirs($$$;$)
199 my ($packagename, $libname, $defaults, $module) = @_;
201 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
203 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
204 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
207 if ((!defined $v) || ($v eq ""))
209 my $locbin = $^O eq 'solaris' ? 'slocate' : 'locate';
210 $foo = `$locbin "$libname" | head -n 1`;
211 $foo =~ /(.+)\Q$libname\E/;
214 if ((defined $find) && ($find ne "") && ($find ne $packagename))
216 print "(\e[1;32mFound via search\e[0m) ";
227 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
229 $ret = "$foo " . $defaults;
232 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
234 my $key = "default_libdir_$packagename";
235 if (exists $main::config{$key})
237 $ret = $main::config{$key};
242 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
243 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
244 $main::config{$key} =~ s/^\s+//g;
245 $ret = $main::config{$key};
252 print "\e[1;32m$ret\e[0m (version $v)\n";
253 my $key = "default_libdir_$packagename";
254 $main::config{$key} = $ret;
255 $main::config{$key} =~ s/^\s+//g;
262 # Translate a $CompileFlags etc line and parse out function calls
263 # to functions within these modules at configure time.
264 sub translate_functions($$)
266 my ($line,$module) = @_;
270 $module =~ /modules*\/(.+?)$/;
273 if ($line =~ /ifuname\(\!"(\w+)"\)/)
282 $line =~ s/ifuname\(\!"(.+?)"\)//;
285 if ($line =~ /ifuname\("(\w+)"\)/)
294 $line =~ s/ifuname\("(.+?)"\)//;
297 if ($line =~ /if\("(\w+)"\)/)
299 if (defined $main::config{$1})
301 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
308 $line =~ s/if\("(.+?)"\)//;
310 if ($line =~ /if\(\!"(\w+)"\)/)
312 if (!exists $main::config{$1})
321 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
329 $line =~ s/if\(\!"(.+?)"\)//;
331 while ($line =~ /exec\("(.+?)"\)/)
333 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
335 die $replace if ($replace =~ /Configuration failed/);
337 $line =~ s/exec\("(.+?)"\)/$replace/;
339 while ($line =~ /execruntime\("(.+?)"\)/)
341 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
343 while ($line =~ /eval\("(.+?)"\)/)
345 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
349 $tmpfile = File::Temp::tmpnam();
350 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
351 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
354 my $replace = `perl $tmpfile`;
357 $line =~ s/eval\("(.+?)"\)/$replace/;
359 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
361 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
362 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
364 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
366 if (pkgconfig_check_version($1, $2, $module) != 1)
368 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
370 # This doesnt actually get replaced with anything
371 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
373 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
375 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
376 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
378 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
380 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
381 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
383 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
385 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
386 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
388 while ($line =~ /rpath\("(.+?)"\)/)
390 my $replace = make_rpath($1,$module);
391 $line =~ s/rpath\("(.+?)"\)/$replace/;
397 #$err =~ s/at .+? line \d+.*//g;
398 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
399 print "\nMake sure you have pkg-config installed\n";
400 print "\nIn the case of gnutls configuration errors on debian,\n";
401 print "Ubuntu, etc, you should ensure that you have installed\n";
402 print "gnutls-bin as well as libgnutls-dev and libgnutls.\n";