classify more files in wesnoth
[deps.git] / graph-includes
bloba5f2a6c54a8e8d476a40f6402c79b0e8307ee33f
1 #!/usr/bin/env perl
3 # graph-includes - create a graphviz graph of source-files
4 # dependencies, with an emphasis on getting usable graphs even for
5 # large projects
7 # (c) 2005 Yann Dirson <ydirson@altern.org>
8 # Distributed under version 2 of the GNU GPL.
10 use warnings;
11 use strict;
13 use Getopt::Long;
15 our $showalldeps=0;
16 our $showdropped=0;
17 our $verbose=0;
18 our $debug=0;
19 our $class='default';
20 our ($minshow, $maxshow) = (1, 1);
21 our @focus;
22 our @colors;
23 our $outfile;
24 our $prefixstrip;
26 our $usage = <<EOF;
27 Usage: $0 [options] src/*.[ch]
28 Options:
29 --class {default|uniqueincludes|<your-own-class>}
30 Select "class" of source code
31 --prefixstrip <prefix> Strip <prefix> (eg. "src/") from filenames in the graph
32 --group <min>-<max> Draw file groups of levels <min> through <max> (default: 1 1)
33 --color <n>:<label>=<color>[,<label>=<color>...]
34 Use specified colors to show members of level-<n> group labelled <label>
35 --alldeps Do not apply transitive reduction to the graph
37 --showdropped Show in special color edges dropped during transitive reduction
38 --focus <node-label> Like -showdropped but only for edges starting from given node
40 --output <outfile>.<fmt>
41 Format to output file, using <fmt> as target format
43 --verbose Show progress
44 --verbose Show details of ignored include directives
45 --debug Loads of debuging output
47 --help This help text
48 EOF
50 our @colspecs;
52 GetOptions ('alldeps' => \$showalldeps,
53 'showdropped' => \$showdropped,
55 'focus=s' => \@focus,
56 'class=s' => \$class,
58 'group=s' => sub {
59 my (undef, $range) = @_;
60 ($minshow, $maxshow) = split /-/, $range;
62 'color=s@' => sub {
63 my (undef, $colspec) = @_;
64 my @temp = split /:/, $colspec;
65 push @colspecs, [$temp[1], $temp[2]];
67 'output=s' => \$outfile,
69 'prefixstrip=s' => \$prefixstrip,
71 'verbose+' => \$verbose,
72 'debug' => \$debug,
73 'help' => sub { print $usage; exit 0; },
75 ) or die "Could not parse command-line: $!";
77 # create a project with specified files
78 our $classmodule = "graphincludes::project::" . $class;
79 eval "require $classmodule" or die "cannot locate $classmodule";
80 our $project = ($classmodule)->new($prefixstrip, @ARGV);
81 $project->init();
83 @colors = $project->defaultcolors();
84 foreach my $colspec (@colspecs) {
85 foreach my $coldef (split /,/, $colspec->[2]) {
86 my @coldef = split /=/, $coldef;
87 $colors[$colspec->[1]]->{$coldef[0]} = $coldef[1];
91 # maybe get rid of shortcut deps (transitive reduction)
92 $project->reduce() unless ($showalldeps);
95 # print graph
97 if (defined $outfile) {
98 $outfile =~ m/.*\.([^.]+)$/ or die "cannot guess output format";
99 open STDOUT, "| dot -T$1 -o $outfile";
102 print "strict digraph dependencies {\nrankdir=LR;\n";
104 sub sprintnode {
105 my ($file, $min, $max) = @_;
106 my $node = $project->filelabel($file,$max);
107 if (!defined $node and $max > $min) {
108 return sprintnode($file, $min, $max-1);
109 } elsif ($min == $max) {
110 my $fill='';
111 $fill=",style=filled,fillcolor=" . $colors[2]->{$project->filelabel($file,2)}
112 if defined $colors[2] and defined $project->filelabel($file,2) and
113 defined $colors[2]->{$project->filelabel($file,2)};
114 return $project->{NODEIDS}->{$node} . " [label=\"$node\"" . $fill . "];";
115 } else {
116 return "subgraph \"cluster $node\" {" . sprintnode($file, $min, $max-1) . '}';
120 foreach my $file (@{$project->{FILES}}) {
121 print sprintnode($file, $minshow, $maxshow), "\n";
124 foreach my $file (keys %{$project->{DEPS}}) {
125 foreach my $dest (@{$project->{DEPS}->{$file}}) {
126 print "$file -> $dest";
127 my $special = $project->special_edge($file, $dest);
128 print " [$special]" if defined $special;
129 print ";\n";
133 print "}\n";