From 8ef2fa22a20aa4fcbad809a5367bf2b31857b975 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Sat, 27 Jul 2024 17:43:34 -0700 Subject: [PATCH] mkdep: handle breakage from srcdir changes The handling of "path" and "fullpath" was inconsistent, resulting in a lot of missing dependencies regardless if a separate build directory was in use. Signed-off-by: H. Peter Anvin --- tools/mkdep.pl | 64 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/tools/mkdep.pl b/tools/mkdep.pl index 3c1b6acb..44ed0f68 100755 --- a/tools/mkdep.pl +++ b/tools/mkdep.pl @@ -52,6 +52,7 @@ my $barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n"; # This converts from filenames to full pathnames for our dependencies +# These are arrays of [full path, Makefile path] my %dep_path = (); # List of files that cannot be found; these *must* be excluded @@ -73,16 +74,21 @@ my $debug = 0; # # Scan files for dependencies +# $path is the full filesystem path, $file Makefile name # sub scandeps { - # path is the filesystem path, file what the output should call it my($path, $file) = @_; my $line; my %xdeps; my %mdeps; - open(my $fh, '<', $path) - or return; # If not openable, assume generated + print STDERR "mkdep: scanning file: $path\n" if ( $debug ); + + my $fh; + if (!open($fh, '<', $path)) { + print STDERR " -> missing, assume generated\n" if ( $debug ); + return; + } while ( defined($line = <$fh>) ) { chomp $line; @@ -90,35 +96,46 @@ sub scandeps { $line =~ s://.*$::; if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) { my $nf = $1; - if (!defined($dep_path{$nf})) { + my $dp = $dep_path{$nf}; + if (!defined($dp)) { push(@must_exclude, $nf); + print STDERR " -> $nf [missing]\n" if ( $debug ); next; } - $nf = $dep_path{$nf}; - $mdeps{$nf}++; - $xdeps{$nf}++ unless ( defined($deps{$nf}) ); + my $dn = $dp->[1]; + $mdeps{$dn}++; + $xdeps{$dn} = $dp unless ( defined($deps{$dn}) ); + printf STDERR " -> %s (%s)\n", $nf, $dn if ( $debug ); } } close($fh); $deps{$file} = [keys(%mdeps)]; foreach my $xf ( keys(%xdeps) ) { - scandeps($xf); + scandeps(@{$xdeps{$xf}}); } } # %deps contains direct dependencies. This subroutine resolves # indirect dependencies that result. -sub alldeps($$) { - my($file, $level) = @_; - my %adeps; +sub _alldeps($$$) { + my($file, $level, $adeps) = @_; + + return if ($adeps->{$file}); + + printf STDERR " %s-> %s\n", (' ' x $level), $file; + $adeps->{$file}++; foreach my $dep ( @{$deps{$file}} ) { - $adeps{$dep} = 1; - foreach my $idep ( alldeps($dep, $level+1) ) { - $adeps{$idep} = 1; - } + _alldeps($dep, $level+1, $adeps); } +} + +sub alldeps($) { + my($file) = @_; + + my %adeps; + _alldeps($file, 1, \%adeps); return sort(keys(%adeps)); } @@ -177,7 +194,7 @@ sub insert_deps($) { my $fpath = $1; my $fbase = basename($fpath); if (!defined($dep_path{$fbase})) { - $dep_path{$fbase} = $fpath; + $dep_path{$fbase} = [$fpath, $fpath]; print STDERR "Makefile: $fbase -> $fpath\n"; } } elsif ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) { @@ -249,7 +266,8 @@ sub insert_deps($) { @deps = sort(keys(%deps)); } elsif ( $dfile =~ /^(.*)\.[Cc]$/ ) { $ofile = convert_file($1, $sep).$obj.':'; - @deps = ($dfile,alldeps($dfile,1)); + print STDERR "mkdep: dependencies for: $dfile\n"; + @deps = alldeps($dfile); } if (defined($ofile)) { @@ -332,12 +350,15 @@ foreach my $fdir ( @files ) { foreach my $sdir ( @searchdirs ) { my $dir = mycatdir($sdir, $fdir); + if ($scanned{$dir}) { # Have already been here $found = 1; next; } + print STDERR "mkdep: scanning directory $dir\n" if ( $debug ); + opendir(DIR, $dir) or next; $scanned{$dir}++; $found++; @@ -351,9 +372,12 @@ foreach my $fdir ( @files ) { if ( $file =~ /\.[Cc]$/ ) { push(@cfiles, [$fullpath, $path]); } elsif ( $file =~ /\.[Hh]$/ ) { - print STDERR "Filesystem: $file -> $path\n" if ( $debug ); - $dep_path{$file} = $path; # Allow the blank filename - $dep_path{$path} = $path; # Also allow the full pathname + print STDERR "mkdep: filesystem: $file -> $path\n" if ( $debug ); + if (defined($dep_path{$file})) { + print STDERR "mkdep: warning: more than one instance of filename $file!\n"; + } + $dep_path{$file} = [$fullpath, $path]; + $dep_path{$path} = [$fullpath, $path]; } } closedir(DIR);