| 1 |
package Analizo::Command::graph; |
| 2 |
use Analizo -command; |
| 3 |
use parent qw(Analizo::Command); |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
use Analizo::Extractor; |
| 7 |
use Graph::Writer::Dot; |
| 8 |
use File::Basename; |
| 9 |
|
| 10 |
# ABSTRACT: dependency graph generator |
| 11 |
|
| 12 |
=head1 NAME |
| 13 |
|
| 14 |
analizo-graph - dependency graph generator |
| 15 |
|
| 16 |
=head1 USAGE |
| 17 |
|
| 18 |
analizo graph [OPTIONS] <input> [<input> [<input> ...]] |
| 19 |
|
| 20 |
=cut |
| 21 |
|
| 22 |
sub usage_desc { "%c graph %o <input> [<input> [<input> ...]]" } |
| 23 |
|
| 24 |
sub opt_spec { |
| 25 |
return ( |
| 26 |
[ 'extractor=s', 'which extractor method use to parse the source code' ], |
| 27 |
[ 'output|o=s', 'output file name' ], |
| 28 |
[ 'omit=s', 'omit the given functions from the call graph', { default => '' } ], |
| 29 |
[ 'cluster', 'cluster the functions into files' ], |
| 30 |
[ 'modules', 'group the functions by modules(files)' ], |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
sub validate { |
| 35 |
my ($self, $opt, $args) = @_; |
| 36 |
$self->usage_error("No input files!") unless @$args; |
| 37 |
my @unreadable = grep { ! -r $_ || ! -e $_ } @$args; |
| 38 |
if (@unreadable) { |
| 39 |
foreach my $file (@unreadable) { |
| 40 |
$self->usage_error("$file is not readable"); |
| 41 |
} |
| 42 |
} |
| 43 |
if ($opt->output && ! -w dirname($opt->output)) { |
| 44 |
$self->usage_error("No such file or directory"); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
sub execute { |
| 49 |
my ($self, $opt, $args) = @_; |
| 50 |
my $extractor = Analizo::Extractor->load($opt->extractor); |
| 51 |
$extractor->process(@$args); |
| 52 |
my @omitted = split /,/, $opt->omit; |
| 53 |
my $graph = $extractor->model->callgraph( |
| 54 |
group_by_module => $opt->modules, |
| 55 |
omit => \@omitted, |
| 56 |
); |
| 57 |
if ($opt->output) { |
| 58 |
open STDOUT, '>', $opt->output or die "$!"; |
| 59 |
} |
| 60 |
my $stdout = \*STDOUT; |
| 61 |
my $graph_writer = Graph::Writer::Dot->new( |
| 62 |
cluster => ($opt->cluster ? 'group' : undef), |
| 63 |
); |
| 64 |
$graph_writer->write_graph($graph, $stdout); |
| 65 |
close STDOUT; |
| 66 |
} |
| 67 |
|
| 68 |
=head1 DESCRIPTION |
| 69 |
|
| 70 |
analizo graph reads the dependency information from one or more source code |
| 71 |
directories passed as arguments, and produces as output the graph of |
| 72 |
dependencies between the modules of the software in the graphviz(1) format. |
| 73 |
|
| 74 |
analizo graph is part of the analizo suite. |
| 75 |
|
| 76 |
=head1 REQUIRED ARGUMENTS |
| 77 |
|
| 78 |
=over |
| 79 |
|
| 80 |
=item <input>... |
| 81 |
|
| 82 |
The input directories (or files) with source code to be processed. |
| 83 |
|
| 84 |
Although you can pass individual files as input, this tool is more useful if |
| 85 |
you pass entire source directories. If you pass just a couple of files, their |
| 86 |
dependencies on modules that are not declared and/or implemented in those |
| 87 |
files will not be calculated. |
| 88 |
|
| 89 |
=back |
| 90 |
|
| 91 |
=head1 OPTIONS |
| 92 |
|
| 93 |
=over |
| 94 |
|
| 95 |
=item --omit <functions> |
| 96 |
|
| 97 |
Omit the given functions from the call graph. Multiple function names |
| 98 |
may be given separated by commas. |
| 99 |
|
| 100 |
=item --cluster |
| 101 |
|
| 102 |
Cluster the functions into files, so you can see in which files are the calling |
| 103 |
and called functions. |
| 104 |
|
| 105 |
=item --modules |
| 106 |
|
| 107 |
Group the functions by modules (files or OO classes), and only represent calls between |
| 108 |
modules. This is useful to see the dependencies between the modules of the |
| 109 |
program, instead of focusing on specific functions. The arrows between the |
| 110 |
modules will be labelled with a number that represents the number of different |
| 111 |
places in which the calling module calls functions in the called module (i.e. |
| 112 |
how many times module A calls module B). |
| 113 |
|
| 114 |
It doesn't make much sense to use --modules together with --cluster. |
| 115 |
|
| 116 |
=item --extractor <extractor> |
| 117 |
|
| 118 |
Define which extractor method use to parse the source code. Currently "Doxyparse" |
| 119 |
is the only extractor available. Default is Doxyparse. |
| 120 |
|
| 121 |
=item --output <file>, -o <file> |
| 122 |
|
| 123 |
Use a file as output |
| 124 |
|
| 125 |
=back |
| 126 |
|
| 127 |
=head1 VIEWING THE GRAPH |
| 128 |
|
| 129 |
To view the generated graph, pipe analizo's output to one of the |
| 130 |
Graphviz tools. You can use B<dotty(1)> to display the graph in a |
| 131 |
X11 window: |
| 132 |
|
| 133 |
$ analizo graph src/ | dotty - |
| 134 |
|
| 135 |
You can also generate a file to print or include in a document |
| 136 |
by using the B<dot(1)>. |
| 137 |
|
| 138 |
To generate a PNG image called F<graph.png>: |
| 139 |
|
| 140 |
$ analizo graph src/ | dot -Tpng -ograph.png - |
| 141 |
|
| 142 |
To generate a PostScript version of the dependency graph for printing, you can |
| 143 |
also use the B<dot>. For example, to generate a dependency graph in the file |
| 144 |
F<graph.ps> fitting everything on a US letter size page in landscape mode, try |
| 145 |
|
| 146 |
$ analizo graph src/ | dot -Grotate=90 -Gsize=11,8.5 -Tps -o graph.ps |
| 147 |
|
| 148 |
Sometimes, the graph will fit better if the dependencies arrows go from left to |
| 149 |
right instead of top to bottom. The B<dot> option B<-Grankdir=LR> will do |
| 150 |
that: |
| 151 |
|
| 152 |
$ analizo graph src/ | dot -Gsize=8.5,11 -Grankdir=LR -Tps -o graph.ps |
| 153 |
|
| 154 |
For large software, the graph may end up too small |
| 155 |
to comfortably read. If that happens, try N-up printing: |
| 156 |
|
| 157 |
$ analizo graph src/ | dot -Gpage=8.5,11 -Tps -o graph.ps |
| 158 |
|
| 159 |
You can also try playing with other B<dot> options such as B<-Gratio>, |
| 160 |
or for a different style of graph, try using B<neato> instead of |
| 161 |
B<dot>. See the Graphviz documentation for more information about the |
| 162 |
various options available for customizing the style of the graph. |
| 163 |
|
| 164 |
=head1 READING THE GRAPH |
| 165 |
|
| 166 |
When generating a graph in the function level (i.g. without the I<--modules> |
| 167 |
option), function calls are displayed as solid arrows. A dotted arrow means |
| 168 |
that the function the arrow points from takes the address of the function the |
| 169 |
arrow points to; this typically indicates that the latter function is being |
| 170 |
used as a callback. |
| 171 |
|
| 172 |
When the I<--modules> option is on, then there are only solid arrows. An arrow |
| 173 |
from I<A> to I<B> means that I<A> depends on I<B>. |
| 174 |
|
| 175 |
=head1 SEE ALSO |
| 176 |
|
| 177 |
B<dotty(1)>, B<dot(1)>, B<neato(1)>, B<analizo(1)> |
| 178 |
|
| 179 |
=head1 COPYRIGHT AND AUTHORS |
| 180 |
|
| 181 |
See B<analizo(1)>. |
| 182 |
|
| 183 |
=cut |
| 184 |
|
| 185 |
1; |