1 # +------------------------------------+
2 # | Inspire Internet Relay Chat Daemon |
3 # +------------------------------------+
5 # InspIRCd: (C) 2002-2010 InspIRCd Development Team
6 # See: http://wiki.inspircd.org/Credits
8 # This program is free but copyrighted software; see
9 # the file COPYING for details.
11 # ---------------------------------------------------
13 package make::utilities;
18 use warnings FATAL => qw(all);
20 use Exporter 'import';
24 our @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring);
26 # Parse the output of a *_config program,
27 # such as pcre_config, take out the -L
28 # directive and return an rpath for it.
30 # \e[1;32msrc/Makefile\e[0m
32 my %already_added = ();
33 my $if_skip_lines = 0;
35 sub promptstring($$$$$)
37 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
39 if (!$main::interactive)
41 my $opt_commandlineswitch;
42 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
43 if (defined $opt_commandlineswitch)
45 print "\e[1;32m$opt_commandlineswitch\e[0m\n";
46 $var = $opt_commandlineswitch;
50 die "Could not detect $package! Please specify the $prompt via the command line option \e[1;32m--$commandlineswitch=\"/path/to/file\"\e[0m";
55 print "\nPlease enter the $prompt?\n";
56 print "[\e[1;32m$default\e[0m] -> ";
57 chomp($var = <STDIN>);
63 $main::config{$configitem} = $var;
68 my ($executable, $module) = @_;
69 chomp(my $data = `$executable`);
71 while ($data =~ /-L(\S+)/)
74 if (!exists $already_added{$libpath})
76 print "Adding extra library path to \e[1;32m$module\e[0m ... \e[1;32m$libpath\e[0m\n";
77 $already_added{$libpath} = 1;
79 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath " unless defined $main::opt_disablerpath;
87 if (!exists $ENV{PKG_CONFIG_PATH})
89 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
93 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
97 sub pkgconfig_get_include_dirs($$$;$)
99 my ($packagename, $headername, $defaults, $module) = @_;
101 my $key = "default_includedir_$packagename";
102 if (exists $main::config{$key})
104 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
105 my $ret = $main::config{$key};
106 print "\e[1;32m$ret\e[0m (cached)\n";
112 print "Locating include directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
114 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
115 my $ret = `pkg-config --cflags $packagename 2>/dev/null`;
117 if ((!defined $v) || ($v eq ""))
119 print "\e[31mCould not find $packagename via pkg-config\e[m (\e[1;32mplease install pkg-config\e[m)\n";
120 $foo = `locate "$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) = @_;
216 my $key = "default_libdir_$packagename";
217 if (exists $main::config{$key})
219 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
220 my $ret = $main::config{$key};
221 print "\e[1;32m$ret\e[0m (cached)\n";
227 print "Locating library directory for package \e[1;32m$packagename\e[0m for module \e[1;32m$module\e[0m... ";
229 my $v = `pkg-config --modversion $packagename 2>/dev/null`;
230 my $ret = `pkg-config --libs $packagename 2>/dev/null`;
233 if ((!defined $v) || ($v eq ""))
235 $foo = `locate "$libname" | head -n 1`;
236 $foo =~ /(.+)\Q$libname\E/;
239 if ((defined $find) && ($find ne "") && ($find ne $packagename))
241 print "(\e[1;32mFound via search\e[0m) ";
252 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
254 $ret = "$foo " . $defaults;
257 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
259 my $key = "default_libdir_$packagename";
260 if (exists $main::config{$key})
262 $ret = $main::config{$key};
267 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
268 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
269 $main::config{$key} =~ s/^\s+//g;
270 $ret = $main::config{$key};
277 print "\e[1;32m$ret\e[0m (version $v)\n";
278 my $key = "default_libdir_$packagename";
279 $main::config{$key} = $ret;
280 $main::config{$key} =~ s/^\s+//g;
287 # Translate a $CompileFlags etc line and parse out function calls
288 # to functions within these modules at configure time.
289 sub translate_functions($$)
291 my ($line,$module) = @_;
295 $module =~ /modules*\/(.+?)$/;
298 # This is only a cursory check, just designed to catch casual accidental use of backticks.
299 # There are pleanty of ways around it, but its not supposed to be for security, just checking
300 # that people are using the new configuration api as theyre supposed to and not just using
301 # backticks instead of eval(), being as eval has accountability. People wanting to get around
302 # the accountability will do so anyway.
303 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
305 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
308 if ($line =~ /ifuname\(\!"(\w+)"\)/)
317 $line =~ s/ifuname\(\!"(.+?)"\)//;
320 if ($line =~ /ifuname\("(\w+)"\)/)
329 $line =~ s/ifuname\("(.+?)"\)//;
332 if ($line =~ /if\("(\w+)"\)/)
334 if (defined $main::config{$1})
336 if (($main::config{$1} !~ /y/i) and ($main::config{$1} ne "1"))
343 $line =~ s/if\("(.+?)"\)//;
345 if ($line =~ /if\(\!"(\w+)"\)/)
347 if (!exists $main::config{$1})
356 if (exists ($main::config{$1}) and (($main::config{$1} =~ /y/i) or ($main::config{$1} eq "1")))
364 $line =~ s/if\(\!"(.+?)"\)//;
366 while ($line =~ /exec\("(.+?)"\)/)
368 print "Executing program for module \e[1;32m$module\e[0m ... \e[1;32m$1\e[0m\n";
370 die $replace if ($replace =~ /Configuration failed/);
372 $line =~ s/exec\("(.+?)"\)/$replace/;
374 while ($line =~ /execruntime\("(.+?)"\)/)
376 $line =~ s/execruntime\("(.+?)"\)/`$1`/;
378 while ($line =~ /eval\("(.+?)"\)/)
380 print "Evaluating perl code for module \e[1;32m$module\e[0m ... ";
385 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
386 print "(Created and executed \e[1;32m$tmpfile\e[0m)\n";
389 my $replace = `perl $tmpfile`;
391 $line =~ s/eval\("(.+?)"\)/$replace/;
393 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
395 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
396 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
398 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
400 if (pkgconfig_check_version($1, $2, $module) != 1)
402 die "Version of package $1 is too old. Please upgrade it to version \e[1;32m$2\e[0m or greater and try again.";
404 # This doesnt actually get replaced with anything
405 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
407 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
409 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
410 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
412 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
414 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
415 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
417 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
419 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
420 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
422 while ($line =~ /rpath\("(.+?)"\)/)
424 my $replace = make_rpath($1,$module);
425 $replace = "" if ($^O =~ /darwin/i);
426 $line =~ s/rpath\("(.+?)"\)/$replace/;
432 #$err =~ s/at .+? line \d+.*//g;
433 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";
434 print "\nMake sure you have pkg-config installed\n";
435 print "\nIn the case of gnutls configuration errors on debian,\n";
436 print "Ubuntu, etc, you should ensure that you have installed\n";
437 print "gnutls-bin as well as gnutls-dev and gnutls.\n";