From 1be3da32d24f82d7c8e2109d2ddd405832719ce8 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Sun, 12 May 2002 21:38:05 +0000 Subject: [PATCH] Dependency-generation script --- mkdep.pl | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100755 mkdep.pl diff --git a/mkdep.pl b/mkdep.pl new file mode 100755 index 0000000..8092654 --- /dev/null +++ b/mkdep.pl @@ -0,0 +1,86 @@ +#!/usr/bin/perl +# +# Script to create Makefile-style dependencies. +# +# Usage: perl [-s path-separator] [-o obj-ext] mkdep.pl dir... > deps +# + +sub scandeps($) { + my($file) = @_; + my($line, $nf); + my(@xdeps) = (); + my(@mdeps) = (); + + open(FILE, "< $file\0") or return; # If not openable, assume generated + while ( defined($line = ) ) { + chomp $line; + $line =~ s:/\*.*\*/::g; + $line =~ s://.*$::; + if ( $line =~ /^\s*\#\s*include\s+\"(.*)\"\s*$/ ) { + $nf = $1; + push(@mdeps, $nf); + push(@xdeps, $nf) unless ( defined($deps{$nf}) ); + } + } + close(FILE); + $deps{$file} = [@mdeps]; + + foreach $file ( @xdeps ) { + scandeps($file); + } +} + +# %deps contains direct dependencies. This subroutine resolves +# indirect dependencies that result. +sub alldeps($) { + my($file) = @_; + my(%adeps); + my($dep,$idep); + + foreach $dep ( @{$deps{$file}} ) { + $adeps{$dep} = 1; + foreach $idep ( alldeps($dep) ) { + $adeps{$idep} = 1; + } + } + return keys(%adeps); +} + +%deps = (); +@files = (); +$sep = '/'; # Default, and works on most systems +$obj = 'o'; # Object file extension + +while ( defined($arg = shift(@ARGV)) ) { + if ( $arg =~ /^\-s$/ ) { + $sep = shift(@ARGV); + } elsif ( $arg =~ /^\-o$/ ) { + $obj = shift(@ARGV); + } elsif ( $arg =~ /^-/ ) { + die "Unknown option: $arg\n"; + } else { + push(@files, $arg); + } +} + +foreach $dir ( @files ) { + opendir(DIR, $dir) or die "$0: Cannot open directory: $dir"; + + while ( $file = readdir(DIR) ) { + $path = ($dir eq '.') ? $file : $dir.$sep.$file; + if ( $file =~ /\.[Cc]$/ ) { + scandeps($path); + } + } + closedir(DIR); +} + +foreach $file ( sort(keys(%deps)) ) { + if ( $file =~ /\.[Cc]$/ && scalar(@{$deps{$file}}) ) { + $ofile = $file; $ofile =~ s/\.[Cc]$/\./; $ofile .= $obj; + print $ofile, ': '; + print join(' ', alldeps($file)); + print "\n"; + } +} + -- 2.7.4