3 # graph-includes - create a graphviz graph of source-files
4 # dependencies, with an emphasis on getting usable graphs even for
7 # (c) 2005 Yann Dirson <ydirson@altern.org>
8 # Distributed under version 2 of the GNU GPL.
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
;
27 our (@colors, %colorstyles);
28 our ($outfile, $prefixstrip, $paper);
29 our $rendererclass = 'graphincludes::renderer::dot';
32 Usage: $0 [options] src/*.[ch]
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 -group <min>-<max> Draw file groups of levels <min> through <max> (default: 1 1)
44 -color <n>:<label>=<color>[,<label>=<color>...]
45 Use specified colors to show members of level-<n> group labelled <label>
46 -alldeps Do not apply transitive reduction to the graph
48 -showdropped Show in special color edges dropped during transitive reduction
49 -focus <node-label> Like -showdropped but only for edges starting from given node
51 -renderer <engine> Select the rendering program to produce a graph for (default: dot)
52 -output <outfile>.<fmt>
53 Format to output file, using <fmt> as target format
54 -paper a4|a3|letter Select paper size of multi-paged postscript output
56 -verbose Show progress
57 -debug Loads of debuging output
59 -version Display this program's version
65 # memorize command-line for the report
66 our @commandline = @ARGV;
68 GetOptions
('alldeps' => \
$showalldeps,
69 'showdropped' => \
$graphincludes::params
::showdropped
,
71 'focus=s' => \
@graphincludes::params
::focus
,
73 'language=s' => \
$language,
74 'fileregexp=s' => \
$graphincludes::params
::filename_regexp
,
77 my (undef, $renderer) = @_;
78 $rendererclass = 'graphincludes::renderer::' . $renderer;
81 'Include=s' => \
@graphincludes::params
::inclpath
,
82 'sysInclude=s' => \
@graphincludes::params
::sysinclpath
,
85 my (undef, $range) = @_;
86 ($graphincludes::params
::minshow
, $graphincludes::params
::maxshow
) = split /-/, $range;
89 my (undef, $colspec) = @_;
90 my @temp = split /:/, $colspec;
91 push @colspecs, [$temp[0], $temp[1]];
93 'output=s' => \
$outfile,
96 'prefixstrip=s' => \
$prefixstrip,
98 'verbose+' => \
$graphincludes::params
::verbose
,
99 'debug' => \
$graphincludes::params
::debug
,
100 'help' => sub { print $usage; exit 0; },
101 'version' => sub { print "$0 version $graphincludes::params::VERSION\n"; exit 0; },
103 ) or print STDERR
$usage and exit 1;
110 eval "require $rendererclass" or die "cannot load $rendererclass from " . join ':', @INC;
111 my $renderer = new
$rendererclass;
113 # deal with non-default output formats
115 $renderer->set_multipage($paper) if defined $paper;
116 $renderer->set_outputfile($outfile) if defined $outfile;
118 # create a project with specified files
119 our $classmodule = "graphincludes::project::" . $class;
120 eval "require $classmodule" or die "cannot load $classmodule from " . join ':', @INC;
121 $classmodule->set_language ($language) or die "cannot set language to '$language'";
123 foreach my $arg (@ARGV) {
125 find
( { no_chdir
=> 0,
127 if ($classmodule->accepts_file ($_)) {
128 push @files, $File::Find
::name
;
129 print STDERR
"Adding $File::Find::name\n" if $graphincludes::params
::debug
;
135 die "file does not exist: $arg";
138 our $project = ($classmodule)->new(prefixstrip
=> $prefixstrip,
140 push @graphincludes::params
::sysinclpath
, $project->get_default_sysincludes();
143 @colors = $project->defaultcolors();
144 foreach my $colspec (@colspecs) {
145 foreach my $coldef (split /,/, $colspec->[1]) {
146 my @coldef = split /=/, $coldef;
147 $colors[$colspec->[0]]->{$coldef[0]} = $coldef[1];
151 # assign a role to each color: background, outline
153 my @roles = qw(bg border); my $role=0;
154 for (my $i=$#colors; $i >= $graphincludes::params
::minshow
; $i--) {
155 if (defined($colors[$i])) {
156 die "not enough supported color roles to color level $i"
158 $colorstyles{$roles[$role]} = $i;
164 our $stat_nfiles = scalar $project->{GRAPHS
}[0]->get_nodes;
165 # NOTE: $stat_nedges below is a cut'n'paste of $stat_ndeps
166 our $stat_ndeps = sum
(map { scalar ($project->{GRAPHS
}[0]->get_edges($_)) }
167 ($project->{GRAPHS
}[0]->get_edge_origins) );
169 if (!defined $stat_ndeps) {
170 print STDERR
"$0: found no dependency\n";
174 # maybe get rid of shortcut deps (transitive reduction)
175 #FIXME: run reduce on meaningful graph
176 push @
{$project->{GRAPHS
}}, $project->{GRAPHS
}[0]->reduced unless ($showalldeps);
179 our $stat_nnodes = scalar $project->{GRAPHS
}[0]->get_nodes;
180 our $stat_nleaves = $stat_nnodes - scalar ($project->{GRAPHS
}[0]->get_edge_origins);
181 # NOTE: $stat_ndeps above is a cut'n'paste of $stat_nedges
182 our $stat_nedges = sum
(map { scalar ($project->{GRAPHS
}[0]->get_edges($_)) }
183 ($project->{GRAPHS
}[0]->get_edge_origins));
187 $renderer->printgraph($project, \
@colors, \
%colorstyles);
191 our $report = 'graph-includes.report';
192 $report = $outfile . '.' . $report if defined $outfile;
193 open REPORT
, ">$report" or die "cannot open $report for writing: $!";
194 print REPORT
"\n Graph-includes report";
195 print REPORT
"\n =====================\n";
197 print REPORT
"\nGeneral statistics:";
198 print REPORT
"\n-------------------\n\n";
199 print REPORT
"$stat_nfiles files, $stat_nnodes nodes (",
200 int(100*($stat_nfiles-$stat_nnodes)/$stat_nfiles), "% dropped)\n";
201 print REPORT
"$stat_ndeps dependencies, $stat_nedges edges (",
202 int(100*($stat_ndeps-$stat_nedges)/$stat_ndeps), "% dropped)\n";
203 print REPORT
"$stat_nleaves leaf node(s)\n";
206 print REPORT
scalar keys %{$project->{REPORT
}->{HDR
}}, " dependencies not found\n";
207 print REPORT
scalar keys %{$project->{REPORT
}->{SYS
}}, " dependencies identified as system headers\n";
209 print REPORT
"\nDeclared dependencies not found:";
210 print REPORT
"\n--------------------------------\n\n";
211 for my $dep (sort keys %{$project->{REPORT
}->{HDR
}}) {
212 print REPORT
" $dep\n";
213 for my $src (@
{$project->{REPORT
}->{HDR
}->{$dep}}) {
214 print REPORT
" from $src\n";
218 print REPORT
"\nUsed system headers:";
219 print REPORT
"\n--------------------\n\n";
220 for my $dep (sort keys %{$project->{REPORT
}->{SYS
}}) {
221 print REPORT
" $dep\n";
224 print REPORT
"\nCommand-line used:";
225 print REPORT
"\n------------------\n\n";
226 # display arguments separated by space, quoting any argument with embedded whitespace
227 print REPORT
"$0 ", join ' ', map { m/\s/ ?
"\"$_\"" : $_ } @commandline;
229 print REPORT
"\n\nThis was $0 version $graphincludes::params::VERSION\n";
230 print REPORT
"\n=== End of report ===\n";
233 # wait for renderer to finish if needed