blob: 16aed89474895983f348a5b4d80cf4f3eab53165 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001#!/usr/bin/perl -w
2#
3# Program: GenLibDeps.pl
4#
5# Synopsis: Generate HTML output that shows the dependencies between a set of
6# libraries. The output of this script should periodically replace
7# the similar content in the UsingLibraries.html document.
8#
9# Syntax: GenLibDeps.pl [-flat] <directory_with_libraries_in_it> [path_to_nm_binary]
10#
Ted Kremeneka887b222007-11-27 19:31:11 +000011use strict;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000012
13# Parse arguments...
14my $FLAT = 0;
15my $WHY = 0;
16while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
17 shift;
18 last if /^--$/; # Stop processing arguments on --
19
20 # List command line options here...
21 if (/^-flat$/) { $FLAT = 1; next; }
22 if (/^-why/) { $WHY = 1; $FLAT = 1; next; }
23 print "Unknown option: $_ : ignoring!\n";
24}
25
26# Give first option a name.
27my $Directory = $ARGV[0];
28if (!defined($Directory) || ! -d "$Directory") {
29 die "First argument must specify the directory containing LLVM libs\n";
30}
31
32my $nmPath = $ARGV[1];
33
34# Find the "dot" program
35my $DotPath="";
36if (!$FLAT) {
37 chomp($DotPath = `which dot`);
38 die "Can't find 'dot'" if (! -x "$DotPath");
39}
40
41if (!defined($nmPath) || $nmPath eq "") {
42 chomp($nmPath=`which nm`);
43 die "Can't find 'nm'" if (! -x "$nmPath");
44}
45
46# Open the directory and read its contents, sorting by name and differentiating
47# by whether its a library (.a) or an object file (.o)
48opendir DIR,$Directory;
49my @files = readdir DIR;
50closedir DIR;
Ted Kremeneka887b222007-11-27 19:31:11 +000051my @libs = grep(/libLLVM.*\.a$/,sort(@files));
52my @objs = grep(/LLVM.*\.o$/,sort(@files));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053
54# Declare the hashes we will use to keep track of the library and object file
55# symbol definitions.
56my %libdefs;
57my %objdefs;
58
59# Gather definitions from the libraries
Ted Kremeneka887b222007-11-27 19:31:11 +000060foreach my $lib (@libs ) {
61 open DEFS, "$nmPath -g $Directory/$lib|";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 while (<DEFS>) {
Ted Kremeneka887b222007-11-27 19:31:11 +000063 next if (! / [ABCDGRST] /);
64 s/^[^ ]* [ABCDGRST] //;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 chomp($_);
66 $libdefs{$_} = $lib;
67 }
68 close DEFS;
69}
70
71# Gather definitions from the object files.
Ted Kremeneka887b222007-11-27 19:31:11 +000072foreach my $obj (@objs ) {
73 open DEFS, "$nmPath -g $Directory/$obj |";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 while (<DEFS>) {
Ted Kremeneka887b222007-11-27 19:31:11 +000075 next if (! / [ABCDGRST] /);
76 s/^[^ ]* [ABCDGRST] //;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 chomp($_);
78 $objdefs{$_} = $obj;
79 }
80 close DEFS;
81}
82
83# Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
84# for one library or object file. The <dt> provides the name of the library or
85# object. The <dd> provides a list of the libraries/objects it depends on.
86sub gen_one_entry {
87 my $lib = $_[0];
88 my $lib_ns = $lib;
89 $lib_ns =~ s/(.*)\.[oa]/$1/;
90 if ($FLAT) {
91 print "$lib:";
92 if ($WHY) { print "\n"; }
93 } else {
94 print " <dt><b>$lib</b</dt><dd><ul>\n";
95 }
96 open UNDEFS,
97 "$nmPath -g -u $Directory/$lib | sed -e 's/^ *U //' | sort | uniq |";
98 my %DepLibs;
99 while (<UNDEFS>) {
100 chomp;
101 my $lib_printed = 0;
102 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
103 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
104 push(@{$DepLibs{$libdefs{$_}}}, $_);
105 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
Ted Kremeneka887b222007-11-27 19:31:11 +0000106 my $libroot = $lib;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 $libroot =~ s/lib(.*).a/$1/;
108 if ($objdefs{$_} ne "$libroot.o") {
109 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
110 push(@{$DepLibs{$objdefs{$_}}}, $_);
111 }
112 }
113 }
114 close UNDEFS;
115 for my $key (sort keys %DepLibs) {
116 if ($FLAT) {
117 print " $key";
118 if ($WHY) {
119 print "\n";
120 my @syms = @{$DepLibs{$key}};
Ted Kremeneka887b222007-11-27 19:31:11 +0000121 foreach my $sym (@syms) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 print " $sym\n";
123 }
124 }
125 } else {
126 print " <li>$key</li>\n";
127 }
Ted Kremeneka887b222007-11-27 19:31:11 +0000128 my $suffix = substr($key,length($key)-1,1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 $key =~ s/(.*)\.[oa]/$1/;
130 if ($suffix eq "a") {
131 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
132 } else {
133 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
134 }
135 }
136 if ($FLAT) {
137 if (!$WHY) {
138 print "\n";
139 }
140 } else {
141 print " </ul></dd>\n";
142 }
143}
144
145# Make sure we flush on write. This is slower but correct based on the way we
146# write I/O in gen_one_entry.
147$| = 1;
148
149# Print the definition list tag
150if (!$FLAT) {
151 print "<dl>\n";
152
153 open DOT, "| $DotPath -Tgif > libdeps.gif";
154
155 print DOT "digraph LibDeps {\n";
156 print DOT " size=\"40,15\"; \n";
157 print DOT " ratio=\"1.33333\"; \n";
158 print DOT " margin=\"0.25\"; \n";
159 print DOT " rankdir=\"LR\"; \n";
160 print DOT " mclimit=\"50.0\"; \n";
161 print DOT " ordering=\"out\"; \n";
162 print DOT " center=\"1\";\n";
163 print DOT "node [shape=\"box\",\n";
164 print DOT " color=\"#000088\",\n";
165 print DOT " fillcolor=\"#FFFACD\",\n";
166 print DOT " fontcolor=\"#3355BB\",\n";
167 print DOT " style=\"filled\",\n";
168 print DOT " fontname=\"sans\",\n";
169 print DOT " fontsize=\"24\"\n";
170 print DOT "];\n";
171 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
172}
173
174# Print libraries first
Ted Kremeneka887b222007-11-27 19:31:11 +0000175foreach my $lib (@libs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 gen_one_entry($lib);
177}
178
179if (!$FLAT) {
180 print DOT "}\n";
181 close DOT;
182 open DOT, "| $DotPath -Tgif > objdeps.gif";
183 print DOT "digraph ObjDeps {\n";
184 print DOT " size=\"8,10\";\n";
185 print DOT " margin=\"0.25\";\n";
186 print DOT " rankdir=\"LR\";\n";
187 print DOT " mclimit=\"50.0\";\n";
188 print DOT " ordering=\"out\";\n";
189 print DOT " center=\"1\";\n";
190 print DOT "node [shape=\"box\",\n";
191 print DOT " color=\"#000088\",\n";
192 print DOT " fillcolor=\"#FFFACD\",\n";
193 print DOT " fontcolor=\"#3355BB\",\n";
194 print DOT " fontname=\"sans\",\n";
195 print DOT " style=\"filled\",\n";
196 print DOT " fontsize=\"24\"\n";
197 print DOT "];\n";
198 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
199}
200
201# Print objects second
Ted Kremeneka887b222007-11-27 19:31:11 +0000202foreach my $obj (@objs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 gen_one_entry($obj);
204}
205
206if (!$FLAT) {
207 print DOT "}\n";
208 close DOT;
209
210# Print end tag of definition list element
211 print "</dl>\n";
212}