1 package make::utilities;
5 @EXPORT = qw(make_rpath pkgconfig_get_include_dirs pkgconfig_get_lib_dirs pkgconfig_check_version translate_functions promptstring vcheck);
7 # Parse the output of a *_config program,
8 # such as pcre_config, take out the -L
9 # directive and return an rpath for it.
11 # \033[1;32msrc/Makefile\033[0m
13 my %already_added = ();
15 sub promptstring($$$$$)
17 my ($prompt, $configitem, $default, $package, $commandlineswitch) = @_;
19 if (!$main::interactive)
21 undef $opt_commandlineswitch;
22 GetOptions ("$commandlineswitch=s" => \$opt_commandlineswitch);
23 if (defined $opt_commandlineswitch)
25 print "\033[1;32m$opt_commandlineswitch\033[0m\n";
26 $var = $opt_commandlineswitch;
30 die "Could not detect $package! Please specify the $prompt via the command line option \033[1;32m--$commandlineswitch=\"/path/to/file\"\033[0m";
35 print "\nPlease enter the $prompt?\n";
36 print "[\033[1;32m$default\033[0m] -> ";
37 chomp($var = <STDIN>);
43 $main::config{$configitem} = $var;
48 my ($executable, $module) = @_;
49 chomp($data = `$executable`);
51 while ($data =~ /-L(\S+)/)
54 if (!exists $already_added{$libpath})
56 print "Adding extra library path to \033[1;32m$module\033[0m ... \033[1;32m$libpath\033[0m\n";
57 $already_added{$libpath} = 1;
59 $output .= "-Wl,--rpath -Wl,$libpath -L$libpath ";
67 if (!exists $ENV{PKG_CONFIG_PATH})
69 $ENV{PKG_CONFIG_PATH} = "/usr/lib/pkgconfig:/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
73 $ENV{PKG_CONFIG_PATH} .= ":/usr/local/lib/pkgconfig:/usr/local/libdata/pkgconfig:/usr/X11R6/libdata/pkgconfig";
77 sub pkgconfig_get_include_dirs($$$;$)
79 my ($packagename, $headername, $defaults, $module) = @_;
81 my $key = "default_includedir_$packagename";
82 if (exists $main::config{$key})
84 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
85 $ret = $main::config{$key};
86 print "\033[1;32m$ret\033[0m (cached)\n";
92 print "Locating include directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
94 $v = `pkg-config --modversion $packagename 2>/dev/null`;
95 $ret = `pkg-config --cflags $packagename 2>/dev/null`;
97 if ((!defined $v) || ($v eq ""))
99 $foo = `locate "$headername" | head -n 1`;
100 $foo =~ /(.+)\Q$headername\E/;
103 if ((defined $find) && ($find ne "") && ($find ne $packagename))
105 print "(\033[1;32mFound via search\033[0m) ";
115 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
117 $ret = "$foo " . $defaults;
120 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
122 my $key = "default_includedir_$packagename";
123 if (exists $main::config{$key})
125 $ret = $main::config{$key};
129 $headername =~ s/^\///;
130 promptstring("path to the directory containing $headername", $key, "/usr/include",$packagename,"$packagename-includes");
131 $packagename =~ tr/a-z/A-Z/;
132 $main::config{$key} = "-I$main::config{$key}" . " $defaults -DVERSION_$packagename=\"$v\"";
133 $main::config{$key} =~ s/^\s+//g;
134 $ret = $main::config{$key};
141 my $key = "default_includedir_$packagename";
142 $packagename =~ tr/a-z/A-Z/;
143 $main::config{$key} = "$ret -DVERSION_$packagename=\"$v\"";
144 $main::config{$key} =~ s/^\s+//g;
145 $ret = $main::config{$key};
146 print "\033[1;32m$ret\033[0m (version $v)\n";
154 my ($version1, $version2) = @_;
155 $version1 =~ s/\-r(\d+)/\.\1/g; # minor revs/patchlevels
156 $version2 =~ s/\-r(\d+)/\.\1/g;
157 $version1 =~ s/p(\d+)/\.\1/g;
158 $version2 =~ s/p(\d+)/\.\1/g;
159 $version1 =~ s/\-//g;
160 $version2 =~ s/\-//g;
161 $version1 =~ s/a-z//g;
162 $version2 =~ s/a-z//g;
163 my @v1 = split('\.', $version1);
164 my @v2 = split('\.', $version2);
165 for ($curr = 0; $curr < scalar(@v1); $curr++)
167 if ($v1[$curr] < $v2[$curr])
175 sub pkgconfig_check_version($$;$)
177 my ($packagename, $version, $module) = @_;
181 print "Checking version of package \033[1;32m$packagename\033[0m is >= \033[1;32m$version\033[0m... ";
183 $v = `pkg-config --modversion $packagename 2>/dev/null`;
188 if ((defined $v) && ($v ne ""))
190 if (vcheck($v,$version) == 1)
192 print "\033[1;32mYes (version $v)\033[0m\n";
197 print "\033[1;32mNo (version $v)\033[0m\n";
201 # If we didnt find it, we cant definitively say its too old.
202 # Return ok, and let pkgconflibs() or pkgconfincludes() pick up
203 # the missing library later on.
204 print "\033[1;32mNo (not found)\033[0m\n";
208 sub pkgconfig_get_lib_dirs($$$;$)
210 my ($packagename, $libname, $defaults, $module) = @_;
212 my $key = "default_libdir_$packagename";
213 if (exists $main::config{$key})
215 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
216 $ret = $main::config{$key};
217 print "\033[1;32m$ret\033[0m (cached)\n";
223 print "Locating library directory for package \033[1;32m$packagename\033[0m for module \033[1;32m$module\033[0m... ";
225 $v = `pkg-config --modversion $packagename 2>/dev/null`;
226 $ret = `pkg-config --libs $packagename 2>/dev/null`;
228 if ((!defined $v) || ($v eq ""))
230 $foo = `locate "$libname" | head -n 1`;
231 $foo =~ /(.+)\Q$libname\E/;
234 if ((defined $find) && ($find ne "") && ($find ne $packagename))
236 print "(\033[1;32mFound via search\033[0m) ";
247 if (($defaults ne "") && (($ret eq "") || (!defined $ret)))
249 $ret = "$foo " . $defaults;
252 if ((($ret eq " ") || (!defined $ret)) && ((!defined $v) || ($v eq "")))
254 my $key = "default_libdir_$packagename";
255 if (exists $main::config{$key})
257 $ret = $main::config{$key};
262 promptstring("path to the directory containing $libname", $key, "/usr/lib",$packagename,"$packagename-libs");
263 $main::config{$key} = "-L$main::config{$key}" . " $defaults";
264 $main::config{$key} =~ s/^\s+//g;
265 $ret = $main::config{$key};
272 print "\033[1;32m$ret\033[0m (version $v)\n";
273 my $key = "default_libdir_$packagename";
274 $main::config{$key} = $ret;
275 $main::config{$key} =~ s/^\s+//g;
282 # Translate a $CompileFlags etc line and parse out function calls
283 # to functions within these modules at configure time.
284 sub translate_functions($$)
286 my ($line,$module) = @_;
290 $module =~ /modules*\/(.+?)$/;
293 # This is only a cursory check, just designed to catch casual accidental use of backticks.
294 # There are pleanty of ways around it, but its not supposed to be for security, just checking
295 # that people are using the new configuration api as theyre supposed to and not just using
296 # backticks instead of eval(), being as eval has accountability. People wanting to get around
297 # the accountability will do so anyway.
298 if (($line =~ /`/) && ($line !~ /eval\(.+?`.+?\)/))
300 die "Developers should no longer use backticks in configuration macros. Please use exec() and eval() macros instead. Offending line: $line (In module: $module)";
302 while ($line =~ /exec\("(.+?)"\)/)
304 print "Executing program for module \033[1;32m$module\033[0m ... \033[1;32m$1\033[0m\n";
307 $line =~ s/exec\("(.+?)"\)/$replace/;
309 while ($line =~ /eval\("(.+?)"\)/)
311 print "Evaluating perl code for module \033[1;32m$module\033[0m ... ";
316 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
317 print "(Created and executed \033[1;32m$tmpfile\033[0m)\n";
320 my $replace = `perl $tmpfile`;
322 $line =~ s/eval\("(.+?)"\)/$replace/;
324 while ($line =~ /pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/)
326 my $replace = pkgconfig_get_lib_dirs($1, $2, $3, $module);
327 $line =~ s/pkgconflibs\("(.+?)","(.+?)","(.+?)"\)/$replace/;
329 while ($line =~ /pkgconfversion\("(.+?)","(.+?)"\)/)
331 if (pkgconfig_check_version($1, $2, $module) != 1)
333 die "Version of package $1 is too old. Please upgrade it to version \033[1;32m$2\033[0m or greater and try again.";
335 # This doesnt actually get replaced with anything
336 $line =~ s/pkgconfversion\("(.+?)","(.+?)"\)//;
338 while ($line =~ /pkgconflibs\("(.+?)","(.+?)",""\)/)
340 my $replace = pkgconfig_get_lib_dirs($1, $2, "", $module);
341 $line =~ s/pkgconflibs\("(.+?)","(.+?)",""\)/$replace/;
343 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)",""\)/)
345 my $replace = pkgconfig_get_include_dirs($1, $2, "", $module);
346 $line =~ s/pkgconfincludes\("(.+?)","(.+?)",""\)/$replace/;
348 while ($line =~ /pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/)
350 my $replace = pkgconfig_get_include_dirs($1, $2, $3, $module);
351 $line =~ s/pkgconfincludes\("(.+?)","(.+?)","(.+?)"\)/$replace/;
353 while ($line =~ /rpath\("(.+?)"\)/)
355 my $replace = make_rpath($1,$module);
356 $replace = "" if ($^O =~ /darwin/i);
357 $line =~ s/rpath\("(.+?)"\)/$replace/;
363 $err =~ s/at .+? line \d+.*//g;
364 print "\n\nConfiguration failed. The following error occured:\n\n$err\n";