Blame view
scripts/headerdep.pl
3.46 KB
179efcb47 kbuild: add heade... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#! /usr/bin/perl # # Detect cycles in the header file dependency graph # Vegard Nossum <vegardno@ifi.uio.no> # use strict; use warnings; use Getopt::Long; my $opt_all; my @opt_include; my $opt_graph; &Getopt::Long::Configure(qw(bundling pass_through)); &GetOptions( help => \&help, version => \&version, all => \$opt_all, |
79ff807cf kbuild: fix optio... |
22 |
"I=s" => \@opt_include, |
179efcb47 kbuild: add heade... |
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
graph => \$opt_graph, ); push @opt_include, 'include'; my %deps = (); my %linenos = (); my @headers = grep { strip($_) } @ARGV; parse_all(@headers); if($opt_graph) { graph(); } else { detect_cycles(@headers); } sub help { print "Usage: $0 [options] file... "; print " "; print "Options: "; print " --all "; print " --graph "; print " "; print " -I includedir "; print " "; print "To make nice graphs, try: "; print " $0 --graph include/linux/kernel.h | dot -Tpng -o graph.png "; exit; } sub version { print "headerdep version 2 "; exit; } # Get a file name that is relative to our include paths sub strip { my $filename = shift; for my $i (@opt_include) { my $stripped = $filename; $stripped =~ s/^$i\///; return $stripped if $stripped ne $filename; } return $filename; } # Search for the file name in the list of include paths sub search { my $filename = shift; return $filename if -f $filename; for my $i (@opt_include) { my $path = "$i/$filename"; return $path if -f $path; } |
1dcd81002 headerdep: perlcr... |
94 |
return; |
179efcb47 kbuild: add heade... |
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
} sub parse_all { # Parse all the headers. my @queue = @_; while(@queue) { my $header = pop @queue; next if exists $deps{$header}; $deps{$header} = [] unless exists $deps{$header}; my $path = search($header); next unless $path; open(my $file, '<', $path) or die($!); chomp(my @lines = <$file>); close($file); for my $i (0 .. $#lines) { my $line = $lines[$i]; if(my($dep) = ($line =~ m/^#\s*include\s*<(.*?)>/)) { push @queue, $dep; push @{$deps{$header}}, [$i + 1, $dep]; } } } } sub print_cycle { # $cycle[n] includes $cycle[n + 1]; # $cycle[-1] will be the culprit my $cycle = shift; # Adjust the line numbers for my $i (0 .. $#$cycle - 1) { $cycle->[$i]->[0] = $cycle->[$i + 1]->[0]; } $cycle->[-1]->[0] = 0; my $first = shift @$cycle; my $last = pop @$cycle; my $msg = "In file included"; printf "%s from %s, ", $msg, $last->[1] if defined $last; for my $header (reverse @$cycle) { printf "%s from %s:%d%s ", " " x length $msg, $header->[1], $header->[0], $header->[1] eq $last->[1] ? ' <-- here' : ''; } printf "%s:%d: warning: recursive header inclusion ", $first->[1], $first->[0]; } # Find and print the smallest cycle starting in the specified node. sub detect_cycles { my @queue = map { [[0, $_]] } @_; while(@queue) { my $top = pop @queue; my $name = $top->[-1]->[1]; for my $dep (@{$deps{$name}}) { my $chain = [@$top, [$dep->[0], $dep->[1]]]; # If the dep already exists in the chain, we have a # cycle... if(grep { $_->[1] eq $dep->[1] } @$top) { print_cycle($chain); next if $opt_all; return; } push @queue, $chain; } } } sub mangle { $_ = shift; s/\//__/g; s/\./_/g; s/-/_/g; $_; } # Output dependency graph in GraphViz language. sub graph { print "digraph { "; print "\t/* vertices */ "; for my $header (keys %deps) { printf "\t%s [label=\"%s\"]; ", mangle($header), $header; } print " "; print "\t/* edges */ "; for my $header (keys %deps) { for my $dep (@{$deps{$header}}) { printf "\t%s -> %s; ", mangle($header), mangle($dep->[1]); } } print "} "; } |