VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

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