+sub _read_config_file
+{
+ use File::Basename;
+ use Cwd;
+
+ my $srcdir = shift;
+ my $builddir = shift;
+ my $filename = shift;
+ my @dirlist;
+
+ # We need to change our working directory because config.mk files can
+ # give shell commands as the argument to "include". These shell
+ # commands can take arguments that are relative paths and we don't have
+ # a way of sensibly rewriting these.
+ my $cwd = getcwd;
+ chomp $cwd;
+
+ if ($srcdir ne $builddir) {
+ # Push the builddir path on the front, so we prefer builddir
+ # to srcdir when the file exists in both.
+ @dirlist = ($builddir, $srcdir);
+ } else {
+ @dirlist = ($srcdir);
+ }
+
+ foreach my $d (@dirlist) {
+ my @lines;
+ my $basedir;
+
+ chdir $cwd;
+ chdir $d;
+
+ # We need to catch the exception from open in the case where
+ # the filename is actually a shell pipeline. Why is this
+ # different to opening a regular file? Because this is perl!
+ eval {
+ open(CONFIG_MK, "./$filename");
+ @lines = <CONFIG_MK>;
+ close(CONFIG_MK);
+ };
+
+ chdir $cwd;
+ next unless (@lines);
+
+ # I blame abartlett for this crazy hack -- jpeach
+ if ($filename =~ /\|$/) {
+ $basedir = $builddir;
+ } else {
+ $basedir = dirname($filename);
+ push(@parsed_files, $filename);
+ }
+ $basedir =~ s!^($builddir|$srcdir)[/]!!;
+ return ($filename, $basedir, @lines);
+ }
+
+ chdir $cwd;
+ return;
+}
+