4 # by Paul Merrill <napalminc@gmail.com>
6 # Read a list of GNU make dependencies from STDIN.
7 # Remove dependencies not matching a @whitelist pattern.
8 # Print what's left to STDOUT.
14 "^[^/]" # Only track inner-project dependencies.
17 # Output is wrapped to this many columns.
21 # Read a line that's been split with backslashes (\) over multiple lines.
22 sub read_wrapped_line
{
23 my (@tokens, $line, $more);
25 ($line, $more) = <> =~ /^(.*?)(\\)?$/;
26 push(@tokens, split(/\s+/, $line));
31 # Return true if a string matches any pattern in @whitelist.
34 return grep { $s =~ m/$_/ } @whitelist;
37 # Given a list of tokens, return only those that are whitelisted.
39 return grep { whitelisted
($_) } @_;
42 # Word wrap a string to $width columns. If split over multiple lines, all but
43 # the last line are suffixed with a backslash and all but the first line are
44 # indented with a space.
46 my ($space, $token, @rest) = @_;
47 my $len = length($token);
48 if ($space-2 < $len) {
49 return "\\\n " . wrap
($width-1, $token, @rest);
51 return "$token " . wrap
($space-$len-1, @rest);
57 # Prefer files in the current directory, then files with only one "/", etc.
60 $c++ while ($a =~ /\//g
);
61 $d++ while ($b =~ /\//g
);
62 $c <=> $d or $a cmp $b
65 # Process STDIN one line at a time.
67 my ($target, @deps) = read_wrapped_line
();
68 print wrap
($width, $target, sort by_locality filter
(@deps));