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);
38 our @EXPORT = qw(module_installed prompt_bool prompt_dir prompt_string make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
40 # Parse the output of a *_config program,
41 # such as pcre_config, take out the -L
42 # directive and return an rpath for it.
44 # \e[1;32msrc/Makefile\e[0m
46 my %already_added = ();
47 my $if_skip_lines = 0;
49 sub module_installed($)
56 sub prompt_bool($$$) {
57 my ($interactive, $question, $default) = @_;
58 my $answer = prompt_string($interactive, $question, $default ? 'y' : 'n');
59 return $answer =~ /y/i;
63 my ($interactive, $question, $default) = @_;
64 my ($answer, $create) = (undef, 'y');
66 $answer = rel2abs(prompt_string($interactive, $question, $default));
67 $create = prompt_bool($interactive && !-d $answer, "$answer does not exist. Create it?", 'y');
69 mkpath($answer, 0, 0750);
72 unless (defined $mkpath) {
73 print "Error: unable to create $answer!\n\n";
80 sub prompt_string($$$) {
81 my ($interactive, $question, $default) = @_;
82 return $default unless $interactive;
83 print $question, "\n";
84 print "[\e[1;32m$default\e[0m] => ";
85 chomp(my $answer = <STDIN>);
87 return $answer ? $answer : $default;
90 sub promptstring($$$$$)
92 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
94 if (!$main::interactive)
96 my $opt_commandlineswitch;
97 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
98 if (defined $opt_commandlineswitch)
100 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
101 $var = $opt_commandlineswitch;
105 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
110 print "\nPlease enter the $prompt?\n";
111 print "[\e[1;32m$default\e[0m] -> ";
112 chomp($var = <STDIN>);
118 $main::config{$configitem} = $var;
123 my ($executable, $module) = @_;
124 chomp(my $data = `$executable`);
126 while ($data =~ /-L(\S+)/)
129 if (!exists $already_added{$libpath})
131 print "Adding extra library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
132 $already_added{$libpath} = 1;
134 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath " unless defined $main::opt_disablerpath;
135 $data =~ s/-L(\S+)//;
140 sub extend_pkg_path()
142 if (!exists $ENV{PKG_CONFIG_PATH})
144 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
148 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
152 sub pkgconfig_get_include_dirs($$$;$)
154 my ($packagename, $headername, $defaults, $module) = @_;
158 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
160 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
161 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
163 if ((!defined $v) || ($v eq ""))
165 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
166 $foo = `locate "$headername" 2>/dev/null | head -n 1`;
167 my $find = $foo =~ /(.+)\Q$headername\E/ ? $1 : '';
169 if ((defined $find) && ($find ne "") && ($find ne $packagename))
171 print "(\e[1;32mFound via search\e[0m) ";
181 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
183 $ret = "$foo " . $defaults;
186 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
188 my $key = "default_includedir_$packagename";
189 if (exists $main::config{$key})
191 $ret = $main::config{$key};
195 $headername =~ s/^\///;
196 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
197 $packagename =~ tr/a-z/A-Z/;
200 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
204 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"0.0\"";
206 $main::config{$key} =~ s/^\s+//g;
207 $ret = $main::config{$key};
214 my $key = "default_includedir_$packagename";
215 $packagename =~ tr/a-z/A-Z/;
216 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
217 $main::config{$key} =~ s/^\s+//g;
218 $ret = $main::config{$key};
219 print "\e[1;32m$ret\e[0m (version $v)\n";
225 sub pkgconfig_check_version($$;$)
227 my ($packagename, $version, $module) = @_;
231 print "Checking version of package \e[1;32m$packagename\e[0m is >= \e[1;32m$version\e[0m... ";
233 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
238 if ((defined $v) && ($v ne ""))
240 if (!system "pkg-config --atleast-version $version $packagename")
242 print "\e[1;32mYes (version $v)\e[0m\n";
247 print "\e[1;32mNo (version $v)\e[0m\n";
251 # If we didnt find it, we cant definitively say its too old.
252 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
253 # the missing library later on.
254 print "\e[1;32mNo (not found)\e[0m\n";
258 sub pkgconfig_get_lib_dirs($$$;$)
260 my ($packagename, $libname, $defaults, $module) = @_;
264 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
266 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
267 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
270 if ((!defined $v) || ($v eq ""))
272 $foo = `locate "$libname" | head -n 1`;
273 $foo =~ /(.+)\Q$libname\E/;
276 if ((defined $find) && ($find ne "") && ($find ne $packagename))
278 print "(\e[1;32mFound via search\e[0m) ";
289 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
291 $ret = "$foo " . $defaults;
294 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
296 my $key = "default_libdir_$packagename";
297 if (exists $main::config{$key})
299 $ret = $main::config{$key};
304 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
305 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
306 $main::config{$key} =~ s/^\s+//g;
307 $ret = $main::config{$key};
314 print "\e[1;32m$ret\e[0m (version $v)\n";
315 my $key = "default_libdir_$packagename";
316 $main::config{$key} = $ret;
317 $main::config{$key} =~ s/^\s+//g;
324 # Translate a $CompileFlags etc line and parse out function calls
325 # to functions within these modules at configure time.
326 sub translate_functions($$)
328 my ($line,$module) = @_;
332 $module =~ /modules*\/(.+?)$/;
335 if ($line =~ /ifuname\(\!"(\w+)"\)/)
344 $line =~ s/ifuname\(\!"(.+?)"\)//;
347 if ($line =~ /ifuname\("(\w+)"\)/)
356 $line =~ s/ifuname\("(.+?)"\)//;
359 if ($line =~ /if\("(\w+)"\)/)
361 if (defined $main::config{$1})
363 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
370 $line =~ s/if\("(.+?)"\)//;
372 if ($line =~ /if\(\!"(\w+)"\)/)
374 if (!exists $main::config{$1})
383 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
391 $line =~ s/if\(\!"(.+?)"\)//;
393 while ($line =~ /exec\("(.+?)"\)/)
395 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
397 die $replace if ($replace =~ /Configuration failed/);
399 $line =~ s/exec\("(.+?)"\)/$replace/;
401 while ($line =~ /execruntime\("(.+?)"\)/)
403 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
405 while ($line =~ /eval\("(.+?)"\)/)
407 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
412 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
413 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
416 my $replace = `perl $tmpfile`;
418 $line =~ s/eval\("(.+?)"\)/$replace/;
420 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
422 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
423 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
425 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
427 if (pkgconfig_check_version($1, $2, $module) != 1)
429 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
431 # This doesnt actually get replaced with anything
432 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
434 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
436 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
437 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
439 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
441 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
442 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
444 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
446 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
447 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
449 while ($line =~ /rpath\("(.+?)"\)/)
451 my $replace = make_rpath($1,$module);
452 $replace = "" if ($^O =~ /darwin/i);
453 $line =~ s/rpath\("(.+?)"\)/$replace/;
459 #$err =~ s/at .+? line \d+.*//g;
460 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
461 print "\nMake sure you have pkg-config installed\n";
462 print "\nIn the case of gnutls configuration errors on debian,\n";
463 print "Ubuntu, etc, you should ensure that you have installed\n";
464 print "gnutls-bin as well as gnutls-dev and gnutls.\n";