1 # +------------------------------------+
2 # | Inspire Internet Relay Chat Daemon |
3 # +------------------------------------+
5 # InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 # See: http://www.inspircd.org/wiki/index.php/Credits
8 # This program is free but copyrighted software; see
9 # the file COPYING for details.
11 # ---------------------------------------------------
14 package make::utilities;
15 use Exporter 'import';
18 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring vcheck);
20 # Parse the output of a *_config program,
21 # such as pcre_config, take out the -L
22 # directive and return an rpath for it.
24 # \033[1;32msrc/Makefile\033[0m
26 my %already_added = ();
28 sub promptstring($$$$$)
30 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
32 if (!$main::interactive)
34 undef $opt_commandlineswitch;
35 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
36 if (defined $opt_commandlineswitch)
38 print "\033[1;32m$opt_commandlineswitch\033[0m\n";
39 $var = $opt_commandlineswitch;
43 die "Could not detect $package! Please specify the $prompt via the command line option \033[1;32m--$commandlineswitch=\"/path/to/file\"\033[0m";
48 print "\nPlease enter the $prompt?\n";
49 print "[\033[1;32m$default\033[0m] -> ";
50 chomp($var = <STDIN>);
56 $main::config{$configitem} = $var;
61 my ($executable, $module) = @_;
62 chomp($data = `$executable`);
64 while ($data =~ /-L(\S+)/)
67 if (!exists $already_added{$libpath})
69 print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
70 $already_added{$libpath} = 1;
72 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
80 if (!exists $ENV{PKG_CONFIG_PATH})
82 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
86 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
90 sub pkgconfig_get_include_dirs($$$;$)
92 my ($packagename, $headername, $defaults, $module) = @_;
94 my $key = "default_includedir_$packagename";
95 if (exists $main::config{$key})
97 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
98 $ret = $main::config{$key};
99 print "\033[1;32m$ret\033[0m (cached)\n";
105 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
107 $v = `pkg-config --modversion $packagename 2>/dev/null`;
108 $ret = `pkg-config --cflags $packagename 2>/dev/null`;
110 if ((!defined $v) || ($v eq ""))
112 $foo = `locate "$headername" | head -n 1`;
113 $foo =~ /(.+)\Q$headername\E/;
116 if ((defined $find) && ($find ne "") && ($find ne $packagename))
118 print "(\033[1;32mFound via search\033[0m) ";
128 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
130 $ret = "$foo " . $defaults;
133 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
135 my $key = "default_includedir_$packagename";
136 if (exists $main::config{$key})
138 $ret = $main::config{$key};
142 $headername =~ s/^\///;
143 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
144 $packagename =~ tr/a-z/A-Z/;
145 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
146 $main::config{$key} =~ s/^\s+//g;
147 $ret = $main::config{$key};
154 my $key = "default_includedir_$packagename";
155 $packagename =~ tr/a-z/A-Z/;
156 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
157 $main::config{$key} =~ s/^\s+//g;
158 $ret = $main::config{$key};
159 print "\033[1;32m$ret\033[0m (version $v)\n";
167 my ($version1, $version2) = @_;
168 $version1 =~ s/\-r(\d+)/\.\1/g; # minor revs/patchlevels
169 $version2 =~ s/\-r(\d+)/\.\1/g;
170 $version1 =~ s/p(\d+)/\.\1/g;
171 $version2 =~ s/p(\d+)/\.\1/g;
172 $version1 =~ s/\-//g;
173 $version2 =~ s/\-//g;
174 $version1 =~ s/a-z//g;
175 $version2 =~ s/a-z//g;
176 my @v1 = split('\.', $version1);
177 my @v2 = split('\.', $version2);
178 for ($curr = 0; $curr < scalar(@v1); $curr++)
180 if ($v1[$curr] < $v2[$curr])
188 sub pkgconfig_check_version($$;$)
190 my ($packagename, $version, $module) = @_;
194 print "Checking version of package \033[1;32m$packagename\033[0m is >= \033[1;32m$version\033[0m... ";
196 $v = `pkg-config --modversion $packagename 2>/dev/null`;
201 if ((defined $v) && ($v ne ""))
203 if (vcheck($v,$version) == 1)
205 print "\033[1;32mYes (version $v)\033[0m\n";
210 print "\033[1;32mNo (version $v)\033[0m\n";
214 # If we didnt find it, we cant definitively say its too old.
215 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
216 # the missing library later on.
217 print "\033[1;32mNo (not found)\033[0m\n";
221 sub pkgconfig_get_lib_dirs($$$;$)
223 my ($packagename, $libname, $defaults, $module) = @_;
225 my $key = "default_libdir_$packagename";
226 if (exists $main::config{$key})
228 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
229 $ret = $main::config{$key};
230 print "\033[1;32m$ret\033[0m (cached)\n";
236 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
238 $v = `pkg-config --modversion $packagename 2>/dev/null`;
239 $ret = `pkg-config --libs $packagename 2>/dev/null`;
241 if ((!defined $v) || ($v eq ""))
243 $foo = `locate "$libname" | head -n 1`;
244 $foo =~ /(.+)\Q$libname\E/;
247 if ((defined $find) && ($find ne "") && ($find ne $packagename))
249 print "(\033[1;32mFound via search\033[0m) ";
260 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
262 $ret = "$foo " . $defaults;
265 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
267 my $key = "default_libdir_$packagename";
268 if (exists $main::config{$key})
270 $ret = $main::config{$key};
275 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
276 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
277 $main::config{$key} =~ s/^\s+//g;
278 $ret = $main::config{$key};
285 print "\033[1;32m$ret\033[0m (version $v)\n";
286 my $key = "default_libdir_$packagename";
287 $main::config{$key} = $ret;
288 $main::config{$key} =~ s/^\s+//g;
295 # Translate a $CompileFlags etc line and parse out function calls
296 # to functions within these modules at configure time.
297 sub translate_functions($$)
299 my ($line,$module) = @_;
303 $module =~ /modules*\/(.+?)$/;
306 # This is only a cursory check, just designed to catch casual accidental use of backticks.
307 # There are pleanty of ways around it, but its not supposed to be for security, just checking
308 # that people are using the new configuration api as theyre supposed to and not just using
309 # backticks instead of eval(), being as eval has accountability. People wanting to get around
310 # the accountability will do so anyway.
311 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
313 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
315 while ($line =~ /exec\("(.+?)"\)/)
317 print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
320 $line =~ s/exec\("(.+?)"\)/$replace/;
322 while ($line =~ /execruntime\("(.+?)"\)/)
324 $line =~ s/execruntime\("(.+?)"\)/`\1`/;
326 while ($line =~ /eval\("(.+?)"\)/)
328 print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
333 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
334 print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
337 my $replace = `perl $tmpfile`;
339 $line =~ s/eval\("(.+?)"\)/$replace/;
341 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
343 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
344 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
346 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
348 if (pkgconfig_check_version($1, $2, $module) != 1)
350 die "Version of package $1 is too old. Please upgrade it to version \033[1;32m$2\033[0m or greater and try again.";
352 # This doesnt actually get replaced with anything
353 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
355 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
357 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
358 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
360 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
362 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
363 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
365 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
367 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
368 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
370 while ($line =~ /rpath\("(.+?)"\)/)
372 my $replace = make_rpath($1,$module);
373 $replace = "" if ($^O =~ /darwin/i);
374 $line =~ s/rpath\("(.+?)"\)/$replace/;
380 $err =~ s/at .+? line \d+.*//g;
381 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";