MANIFEST update
[deps.git] / graph-includes
blobb796b6aa06332c3534013294460043f0cb18452b
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,2006 Yann Dirson <ydirson@altern.org>
8 # Distributed under version 2 of the GNU GPL.
10 use warnings;
11 use strict;
13 use File::Basename qw(dirname);
14 use File::Spec::Functions qw(catdir);
15 use lib catdir(dirname($0), 'lib');
17 #BEGIN { print STDERR '@INC=', join (':', @INC)}
19 use Getopt::Long qw(GetOptions);
20 use List::Util qw(sum);
21 use File::Find qw(find);
22 use graphincludes::params;
24 our $showalldeps=0;
25 our $class='default';
26 our $language='C';
27 our (@colors, @nodestylers, @edgestylers);
28 our ($outfile, $prefixstrip, $paper);
29 our $rendererclass = 'graphincludes::renderer::dot';
31 our $usage = <<EOF;
32 Usage: $0 [options] src/*.[ch]
33 Options:
34 -class {default|uniqueincludes|<your-own-class>}
35 Select "class" of source code
36 -language <lang> Select language syntax for dependency extraction (default: C)
37 -fileregexp <perl-regexp>
38 Use this regexp to identify interesting files inside directories
39 (overrides per-language default regexp)
40 -Include <directory> Adds a directory to the path where to look for project's include files
41 -sysInclude <directory> Adds a directory to the path where to look for system include files
42 -prefixstrip <prefix> Strip <prefix> (eg. "src/") from filenames in the graph
43 -consolidate <min>-<max>
44 Consolidate file groups of levels <min> through <max> (default: 1-1)
45 -color <n>:<label>=<color>[,<label>=<color>...]
46 Use specified colors to show members of level-<n> group labelled <label>
47 -alldeps Do not apply transitive reduction to the graph
49 -showdropped Show in special color edges dropped during transitive reduction
50 -focus <node-label> Like -showdropped but only for edges starting from given node
52 -renderer <engine> Select the rendering program to produce a graph for (default: dot)
53 -output <outfile>.<fmt>
54 Format to output file, using <fmt> as target format
55 -paper a4|a3|letter Select paper size of multi-paged postscript output
57 -verbose Show progress
58 -debug Loads of debuging output
60 -version Display this program's version
61 -help This help text
62 EOF
64 our @colspecs;
66 # memorize command-line for the report
67 our @commandline = @ARGV;
69 GetOptions ('alldeps' => \$showalldeps,
70 'showdropped' => \$graphincludes::params::showdropped,
72 'focus=s' => \@graphincludes::params::focus,
73 'class=s' => \$class,
74 'language=s' => \$language,
75 'fileregexp=s' => \$graphincludes::params::filename_regexp,
77 'renderer=s' => sub {
78 my (undef, $renderer) = @_;
79 $rendererclass = 'graphincludes::renderer::' . $renderer;
82 'Include=s' => \@graphincludes::params::inclpath,
83 'sysInclude=s' => \@graphincludes::params::sysinclpath,
85 'consolidate=s' => sub {
86 my (undef, $range) = @_;
87 ($graphincludes::params::minshow, $graphincludes::params::maxshow) = split /-/, $range;
89 'color=s@' => sub {
90 my (undef, $colspec) = @_;
91 my @temp = split /:/, $colspec;
92 push @colspecs, [$temp[0], $temp[1]];
94 'output=s' => \$outfile,
95 'paper=s' => \$paper,
97 'prefixstrip=s' => \$prefixstrip,
99 'verbose+' => \$graphincludes::params::verbose,
100 'debug' => \$graphincludes::params::debug,
101 'help' => sub { print $usage; exit 0; },
102 'version' => sub { print "$0 version $graphincludes::params::VERSION\n"; exit 0; },
104 ) or print STDERR $usage and exit 1;
106 if (@ARGV == 0) {
107 print STDERR $usage;
108 exit 1;
111 eval "require $rendererclass" or die "cannot load '$rendererclass': $@";
112 my $renderer = new $rendererclass;
114 # deal with non-default output formats
116 $renderer->set_multipage($paper) if defined $paper;
117 $renderer->set_outputfile($outfile) if defined $outfile;
119 # create a project with specified files
120 our $classmodule = "graphincludes::project::" . $class;
121 eval "require $classmodule" or die "cannot load '$classmodule': $@";
122 $classmodule->set_language ($language) or die "cannot set language to '$language'";
123 our @files;
124 foreach my $arg (@ARGV) {
125 if (-d $arg) {
126 find ( { no_chdir => 0,
127 wanted => sub {
128 if ($classmodule->accepts_file ($_)) {
129 push @files, $File::Find::name;
130 print STDERR "Adding $File::Find::name\n" if $graphincludes::params::debug;
132 } }, $arg);
133 } elsif (-r $arg) {
134 push @files, $arg;
135 } else {
136 die "file does not exist: $arg";
140 # generate "level 0" graph
141 our $project = ($classmodule)->new(prefixstrip => $prefixstrip,
142 files => \@files);
143 push @graphincludes::params::sysinclpath, $project->get_default_sysincludes();
144 $project->init();
146 # Generate group graphs according to filelabel()
147 # Since we may use coloring according to groups, regardless of which groups the
148 # nodes we draw are from, we must compute graphs for all group levels
149 my @previous = ('files');
150 for (my $i = 1; $i <= $project->nlevels; $i++) {
151 $project->apply_transform('graphincludes::transform::compatgroup',
152 {level => $i,
153 labeller => $project,
154 previous => \@previous},
155 "level$i-groups",
156 'files' );
157 graphincludes::transform::compatgroup::fixup_dep($project->{TRANSGRAPH},
158 $previous[$#previous], "level$i-groups", 'files');
159 push @previous, "level$i-groups";
162 my $graphtodraw;
164 # consolidate graphs as requested by --group flag
165 if ($graphincludes::params::maxshow != $graphincludes::params::minshow) {
166 my @graphnames = map { "level$_-groups" } ($graphincludes::params::minshow .. $graphincludes::params::maxshow);
167 $graphtodraw = "consolidation $graphincludes::params::minshow-$graphincludes::params::maxshow";
168 $project->apply_transform('graphincludes::transform::consolidate',
170 $graphtodraw,
171 @graphnames );
172 } elsif ($graphincludes::params::maxshow == 0) {
173 $graphtodraw = 'files';
174 } else {
175 $graphtodraw = 'level'.$graphincludes::params::maxshow.'-groups';
178 # maybe get rid of shortcut deps (transitive reduction)
179 unless ($showalldeps) {
180 $project->apply_transform('graphincludes::transform::transitivereduction',
182 'reduction',
183 $graphtodraw );
184 $graphtodraw = 'reduction';
187 @colors = $project->defaultcolors();
188 foreach my $colspec (@colspecs) {
189 foreach my $coldef (split /,/, $colspec->[1]) {
190 my @coldef = split /=/, $coldef;
191 $colors[$colspec->[0]]->{$coldef[0]} = $coldef[1];
195 # assign a role to each color: background, outline
197 use DEPS::Styler::Node::Group;
198 my @roles = qw(bgcolor bordercolor); my $role=0;
199 for (my $i=$#colors; $i >= $graphincludes::params::minshow; $i--) {
200 if (defined($colors[$i])) {
201 die "not enough supported color roles to color level $i"
202 if $role >= 2;
203 push @nodestylers, new DEPS::Styler::Node::Group(attribute => $roles[$role],
204 valuemap => $colors[$i],
205 transgraph => $project->{TRANSGRAPH},
206 graph => 'files',
207 refgraph => "level$i-groups");
208 $role++;
213 our $stat_nfiles = scalar $project->{ROOTGRAPH}->get_nodes;
214 # NOTE: $stat_nedges below is a cut'n'paste of $stat_ndeps
215 our $stat_ndeps = sum (map { scalar ($project->{ROOTGRAPH}->get_edges_from($_)) }
216 ($project->{ROOTGRAPH}->get_edge_origins) );
218 if (!defined $stat_ndeps or $stat_ndeps == 0) {
219 print STDERR "$0: found no dependency\n";
220 exit 0;
223 # the graph to be drawn
224 my $thegraph = $project->{TRANSGRAPH}->get_node_from_name($graphtodraw)->{DATA};
226 #FIXME: ...
227 our $stat_nnodes = scalar $thegraph->get_nodes;
228 our $stat_nroots = $stat_nnodes - scalar ($thegraph->get_edge_origins);
229 # NOTE: $stat_ndeps above is a cut'n'paste of $stat_nedges
230 our $stat_nedges = sum (map { scalar ($thegraph->get_edges_from($_)) }
231 ($thegraph->get_edge_origins) );
233 # print graph
234 $renderer->printgraph($project->{TRANSGRAPH}->get_node_from_name($graphtodraw),
235 \@nodestylers, \@edgestylers);
237 # print report
239 our $report = 'graph-includes.report';
240 $report = $outfile . '.' . $report if defined $outfile;
241 open REPORT, ">$report" or die "cannot open $report for writing: $!";
242 print REPORT "\n Graph-includes report";
243 print REPORT "\n =====================\n";
245 print REPORT "\nGeneral statistics:";
246 print REPORT "\n-------------------\n\n";
247 print REPORT "$stat_nfiles files, $stat_nnodes nodes (",
248 int(100*($stat_nfiles-$stat_nnodes)/$stat_nfiles), "% dropped)\n";
249 print REPORT "$stat_ndeps dependencies, $stat_nedges edges (",
250 int(100*($stat_ndeps-$stat_nedges)/$stat_ndeps), "% dropped)\n";
251 print REPORT "$stat_nroots root node(s)\n";
253 print REPORT "\n";
254 print REPORT scalar keys %{$project->{REPORT}->{HDR}}, " dependencies not found\n";
255 print REPORT scalar keys %{$project->{REPORT}->{SYS}}, " dependencies identified as system headers\n";
257 print REPORT "\nDeclared dependencies not found:";
258 print REPORT "\n--------------------------------\n\n";
259 for my $dep (sort keys %{$project->{REPORT}->{HDR}}) {
260 print REPORT " $dep\n";
261 for my $src (@{$project->{REPORT}->{HDR}->{$dep}}) {
262 print REPORT " from $src\n";
266 print REPORT "\nUsed system headers:";
267 print REPORT "\n--------------------\n\n";
268 for my $dep (sort keys %{$project->{REPORT}->{SYS}}) {
269 print REPORT " $dep\n";
272 print REPORT "\nCommand-line used:";
273 print REPORT "\n------------------\n\n";
274 # display arguments separated by space, quoting any argument with embedded whitespace
275 print REPORT "$0 ", join ' ', map { m/\s/ ? "\"$_\"" : $_ } @commandline;
277 print REPORT "\n\nThis was $0 version $graphincludes::params::VERSION\n";
278 print REPORT "\n=== End of report ===\n";
279 close REPORT;
281 # wait for renderer to finish if needed
282 $renderer->wait();