deps: don't break if run in a build directory; don't delete unconfig.h

Don't break Makefile.dep generation if run from a separate build
directory.

config/unconfig.h is a bit special; it is kept in the repository for
the benefit of non-configure users. Therefore, don't have "make
spotless" delete it.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
This commit is contained in:
H. Peter Anvin 2024-04-12 13:17:40 -07:00
parent 392f47d06e
commit caaf81c22e
4 changed files with 147 additions and 92 deletions

View File

@ -537,8 +537,9 @@ EXTERNAL_DEPENDENCIES = 1
# the dependency information will remain external, so it doesn't
# pollute the git logs.
#
Makefile.dep: $(PERLREQ) $(tools)/mkdep.pl config.status
$(RUNPERL) $(tools)/mkdep.pl -M Makefile.in -- $(DEPDIRS)
Makefile.dep: $(tools)/mkdep.pl config.status $(PERLREQ)
$(RUNPERL) $(tools)/mkdep.pl -s '$(top_srcdir)' \
-M $(srcdir)/Makefile.in -- $(DEPDIRS)
dep: Makefile.dep

View File

@ -157,7 +157,7 @@ WARNSRCS = $(patsubst %.obj,%.c,$(LIBOBJ_NW))
# have Perl just to recompile NASM from the distribution.
# Perl-generated source files
PERLREQ = config\unconfig.h \
PERLREQ_CLEANABLE = \
x86\insnsb.c x86\insnsa.c x86\insnsd.c x86\insnsi.h x86\insnsn.c \
x86\regs.c x86\regs.h x86\regflags.c x86\regdis.c x86\regdis.h \
x86\regvals.c asm\tokhash.c asm\tokens.h asm\pptok.h asm\pptok.c \
@ -168,10 +168,14 @@ PERLREQ = config\unconfig.h \
misc\nasmtok.el \
version.h version.mac version.mak nsis\version.nsh
# Special hack to keep config\unconfig.h from getting deleted
# by "make spotless"...
PERLREQ = config\unconfig.h $(PERLREQ_CLEANABLE)
INSDEP = x86\insns.dat x86\insns.pl x86\insns-iflags.ph x86\iflags.ph
config\unconfig.h: config\config.h.in
$(RUNPERL) $(tools)\unconfig.pl \
config\unconfig.h: config\config.h.in autoconf\unconfig.pl
$(RUNPERL) '$(srcdir)'\autoconf\unconfig.pl \
'$(srcdir)' config\config.h.in config\unconfig.h
x86\iflag.c: $(INSDEP)
@ -243,11 +247,12 @@ x86\regs.h: x86\regs.dat x86\regs.pl
# reasonable, but doesn't update the time stamp if the files aren't
# changed, to avoid rebuilding everything every time. Track the actual
# dependency by the empty file asm\warnings.time.
warnings:
$(RM_F) $(WARNFILES) $(WARNTIMES)
.PHONY: warnings
warnings: dirs
$(RM_F) $(WARNFILES) $(WARNTIMES) asm\warnings.time
$(MAKE) asm\warnings.time
asm\warnings.time: $(WARNSRCS)
asm\warnings.time: $(WARNSRCS) asm\warnings.pl
$(EMPTY) asm\warnings.time
$(MAKE) $(WARNTIMES)

View File

@ -170,7 +170,7 @@ WARNSRCS = $(LIBOBJ_NW:.obj=.c)
# have Perl just to recompile NASM from the distribution.
# Perl-generated source files
PERLREQ = config\unconfig.h &
PERLREQ_CLEANABLE = &
x86\insnsb.c x86\insnsa.c x86\insnsd.c x86\insnsi.h x86\insnsn.c &
x86\regs.c x86\regs.h x86\regflags.c x86\regdis.c x86\regdis.h &
x86\regvals.c asm\tokhash.c asm\tokens.h asm\pptok.h asm\pptok.c &
@ -181,10 +181,14 @@ PERLREQ = config\unconfig.h &
misc\nasmtok.el &
version.h version.mac version.mak nsis\version.nsh
# Special hack to keep config\unconfig.h from getting deleted
# by "make spotless"...
PERLREQ = config\unconfig.h $(PERLREQ_CLEANABLE)
INSDEP = x86\insns.dat x86\insns.pl x86\insns-iflags.ph x86\iflags.ph
config\unconfig.h: config\config.h.in
$(RUNPERL) $(tools)\unconfig.pl &
config\unconfig.h: config\config.h.in autoconf\unconfig.pl
$(RUNPERL) '$(srcdir)'\autoconf\unconfig.pl &
'$(srcdir)' config\config.h.in config\unconfig.h
x86\iflag.c: $(INSDEP)
@ -256,11 +260,12 @@ x86\regs.h: x86\regs.dat x86\regs.pl
# reasonable, but doesn't update the time stamp if the files aren't
# changed, to avoid rebuilding everything every time. Track the actual
# dependency by the empty file asm\warnings.time.
warnings:
$(RM_F) $(WARNFILES) $(WARNTIMES)
.PHONY: warnings
warnings: dirs
$(RM_F) $(WARNFILES) $(WARNTIMES) asm\warnings.time
$(MAKE) asm\warnings.time
asm\warnings.time: $(WARNSRCS)
asm\warnings.time: $(WARNSRCS) asm\warnings.pl
$(EMPTY) asm\warnings.time
$(MAKE) $(WARNTIMES)

View File

@ -36,34 +36,52 @@
# Script to create Makefile-style dependencies.
#
# Usage:
# perl mkdep.pl [-s path-separator][-o obj-ext] dir... > deps
# perl mkdep.pl [-i][-e][-m makefile]...[-M makefile... --] dir...
#
use strict;
use integer;
use File::Spec;
use File::Basename;
use File::Copy;
use File::Temp;
use Fcntl;
$barrier = "#-- Everything below is generated by mkdep.pl - do not edit --#\n";
my $barrier =
"#-- Everything below is generated by mkdep.pl - do not edit --#\n";
# This converts from filenames to full pathnames for our dependencies
%dep_path = ();
my %dep_path = ();
# List of files that cannot be found; these *must* be excluded
@must_exclude = ();
my @must_exclude = ();
#
# Variables derived from the command line
#
my %deps;
my %excludes;
my @files;
my @mkfiles;
my $mkmode = 0;
my @searchdirs = (File::Spec->curdir());
my %searchdirs = (File::Spec->curdir() => 1);
my $force_inline = 0;
my $externalize = 0;
my $debug = 0;
#
# Scan files for dependencies
#
sub scandeps($) {
my($file) = @_;
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, '<', $file)
open(my $fh, '<', $path)
or return; # If not openable, assume generated
while ( defined($line = <$fh>) ) {
@ -128,8 +146,8 @@ sub convert_file($$) {
#
# Insert dependencies into a Makefile
#
sub _insert_deps($$) {
my($file, $out) = @_;
sub insert_deps($) {
my($file) = @_;
open(my $in, '<', $file)
or die "$0: Cannot open input: $file\n";
@ -192,8 +210,13 @@ sub _insert_deps($$) {
$is_external = $is_external && defined($external);
undef $external if ( !$is_external );
my $out;
my $outpath;
if ( !$is_external || $externalize ) {
$out = File::Temp->new(DIR => dirname($outpath = $file));
print $out @outfile;
} else {
$out = File::Temp->new(DIR => dirname($outpath = $external));
}
print $out $barrier;
@ -204,78 +227,60 @@ sub _insert_deps($$) {
print $out "$include_command $external\n";
}
unlink($external);
return undef;
}
} else {
my $e;
my $e;
foreach my $dfile ($external, sort(keys(%deps)) ) {
my $ofile;
my @deps;
foreach my $dfile ($external, sort(keys(%deps)) ) {
my $ofile;
my @deps;
next unless (defined($dfile));
next unless (defined($dfile));
if ( $selfrule && $dfile eq $external ) {
$ofile = convert_file($dfile, $sep).':';
@deps = sort(keys(%deps));
} elsif ( $dfile =~ /^(.*)\.[Cc]$/ ) {
$ofile = convert_file($1, $sep).$obj.':';
@deps = ($dfile,alldeps($dfile,1));
}
if ( $selfrule && $dfile eq $external ) {
$ofile = convert_file($dfile, $sep).':';
@deps = sort(keys(%deps));
} elsif ( $dfile =~ /^(.*)\.[Cc]$/ ) {
$ofile = convert_file($1, $sep).$obj.':';
@deps = ($dfile,alldeps($dfile,1));
}
if (defined($ofile)) {
my $len = length($ofile);
print $out $ofile;
foreach my $dep (@deps) {
unless ($excludes{$dep}) {
my $str = convert_file($dep, $sep);
my $sl = length($str)+1;
if ( $len+$sl > $maxline-2 ) {
print $out ' ', $cont, "\n ", $str;
$len = $sl;
} else {
print $out ' ', $str;
$len += $sl;
if (defined($ofile)) {
my $len = length($ofile);
print $out $ofile;
foreach my $dep (@deps) {
unless ($excludes{$dep}) {
my $str = convert_file($dep, $sep);
my $sl = length($str)+1;
if ( $len+$sl > $maxline-2 ) {
print $out ' ', $cont, "\n ", $str;
$len = $sl;
} else {
print $out ' ', $str;
$len += $sl;
}
}
}
print $out "\n";
}
print $out "\n";
}
}
return $external;
}
sub insert_deps($)
{
my($mkfile) = @_;
my $tmp = File::Temp->new(DIR => dirname($mkfile));
my $tmpname = $tmp->filename;
my $newname = _insert_deps($mkfile, $tmp);
close($tmp);
$newname = $mkfile unless(defined($newname));
move($tmpname, $newname);
close($out);
move($out->filename, $outpath);
}
#
# Main program
#
my %deps = ();
my @files = ();
my @mkfiles = ();
my $mkmode = 0;
$force_inline = 0;
$externalize = 0;
$debug = 0;
while ( defined(my $arg = shift(@ARGV)) ) {
if ( $arg eq '-m' ) {
$arg = shift(@ARGV);
push(@mkfiles, $arg);
} elsif ( $arg eq '-s' ) {
$arg = shift(@ARGV);
push(@searchdirs, $arg);
} elsif ( $arg eq '-i' ) {
$force_inline = 1;
} elsif ( $arg eq '-e' ) {
@ -297,27 +302,66 @@ while ( defined(my $arg = shift(@ARGV)) ) {
}
}
my @cfiles = ();
foreach my $dir ( @files ) {
opendir(DIR, $dir) or die "$0: Cannot open directory: $dir";
while ( my $file = readdir(DIR) ) {
$path = ($dir eq File::Spec->curdir())
? $file : File::Spec->catfile($dir,$file);
if ( $file =~ /\.[Cc]$/ ) {
push(@cfiles, $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
}
}
closedir(DIR);
sub mycatdir($$) {
my($a,$b) = @_;
return $b if ($a eq File::Spec->curdir());
return $a if ($b eq File::Spec->curdir());
return File::Spec->catdir($a,$b);
}
sub mycatfile($$) {
my($d,$f) = @_;
return $f if ($d eq File::Spec->curdir());
return File::Spec->catfile($d,$f);
}
my @cfiles = ();
my $err = 0;
my %scanned;
foreach my $fdir ( @files ) {
my $found = 0;
foreach my $sdir ( @searchdirs ) {
my $dir = mycatdir($sdir, $fdir);
if ($scanned{$dir}) {
# Have already been here
$found = 1;
next;
}
opendir(DIR, $dir) or next;
$scanned{$dir}++;
$found++;
while ( my $file = readdir(DIR) ) {
# $fdir is correct here, because we expect VPATH to do
# its job, and the output filename depends on that, not
# on the full source dir path.
my $path = mycatfile($fdir, $file);
my $fullpath = mycatfile($dir, $file);
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
}
}
closedir(DIR);
}
if (!$found) {
print STDERR "$0: cannot find directory: $fdir\n";
$err++;
}
}
exit(1) if ($err);
foreach my $cfile ( @cfiles ) {
scandeps($cfile);
scandeps(@$cfile);
}
foreach my $mkfile ( @mkfiles ) {