blob: 6d0b13ee4361eb5183bf417f5b03c1d50d5580c3 [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
Duncan Sands8c6d1462009-06-12 14:23:42 +000041if (defined($ENV{NM})) {
42 chomp($nmPath=$ENV{NM});
43}
44
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045if (!defined($nmPath) || $nmPath eq "") {
46 chomp($nmPath=`which nm`);
47 die "Can't find 'nm'" if (! -x "$nmPath");
48}
49
50# Open the directory and read its contents, sorting by name and differentiating
51# by whether its a library (.a) or an object file (.o)
52opendir DIR,$Directory;
53my @files = readdir DIR;
54closedir DIR;
Oscar Fuentescb9f6b82008-11-12 20:39:06 +000055my @libs = grep(/libLLVM.*\.(dylib|so|a)$/,sort(@files));
Ted Kremeneka887b222007-11-27 19:31:11 +000056my @objs = grep(/LLVM.*\.o$/,sort(@files));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58# Declare the hashes we will use to keep track of the library and object file
59# symbol definitions.
60my %libdefs;
61my %objdefs;
62
63# Gather definitions from the libraries
Ted Kremeneka887b222007-11-27 19:31:11 +000064foreach my $lib (@libs ) {
65 open DEFS, "$nmPath -g $Directory/$lib|";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 while (<DEFS>) {
Ted Kremeneka887b222007-11-27 19:31:11 +000067 next if (! / [ABCDGRST] /);
68 s/^[^ ]* [ABCDGRST] //;
Ted Kremenekacf24d72007-12-24 08:04:39 +000069 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
70 # this strips both LF and CRLF.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 $libdefs{$_} = $lib;
72 }
Anton Korobeynikov95f73472009-04-21 16:04:14 +000073 close DEFS or die "nm failed";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074}
75
76# Gather definitions from the object files.
Ted Kremeneka887b222007-11-27 19:31:11 +000077foreach my $obj (@objs ) {
78 open DEFS, "$nmPath -g $Directory/$obj |";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 while (<DEFS>) {
Ted Kremeneka887b222007-11-27 19:31:11 +000080 next if (! / [ABCDGRST] /);
81 s/^[^ ]* [ABCDGRST] //;
Ted Kremenekacf24d72007-12-24 08:04:39 +000082 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
83 # this strips both LF and CRLF.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 $objdefs{$_} = $obj;
85 }
Anton Korobeynikov95f73472009-04-21 16:04:14 +000086 close DEFS or die "nm failed";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087}
88
89# Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
90# for one library or object file. The <dt> provides the name of the library or
91# object. The <dd> provides a list of the libraries/objects it depends on.
92sub gen_one_entry {
93 my $lib = $_[0];
94 my $lib_ns = $lib;
95 $lib_ns =~ s/(.*)\.[oa]/$1/;
96 if ($FLAT) {
97 print "$lib:";
98 if ($WHY) { print "\n"; }
99 } else {
100 print " <dt><b>$lib</b</dt><dd><ul>\n";
101 }
102 open UNDEFS,
Duncan Sands8c6d1462009-06-12 14:23:42 +0000103 "$nmPath -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 my %DepLibs;
105 while (<UNDEFS>) {
106 chomp;
107 my $lib_printed = 0;
108 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
109 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
110 push(@{$DepLibs{$libdefs{$_}}}, $_);
111 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
Ted Kremeneka887b222007-11-27 19:31:11 +0000112 my $libroot = $lib;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 $libroot =~ s/lib(.*).a/$1/;
114 if ($objdefs{$_} ne "$libroot.o") {
115 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
116 push(@{$DepLibs{$objdefs{$_}}}, $_);
117 }
118 }
119 }
Anton Korobeynikov95f73472009-04-21 16:04:14 +0000120 close UNDEFS or die "nm failed";
Chris Lattner58923602008-10-04 18:03:46 +0000121 unless(keys %DepLibs) {
122 # above failed
Duncan Sands8c6d1462009-06-12 14:23:42 +0000123 open UNDEFS, "$nmPath -u $Directory/$lib |";
Chris Lattner58923602008-10-04 18:03:46 +0000124 while (<UNDEFS>) {
125 # to bypass non-working sed
126 if (' ' eq substr($_,0,2) and index($_,'U ')) {
127 $_ = substr($_,index($_,'U ')+2)
128 };
129 $_ = substr($_,index($_,' *U ')+5) if -1!=index($_,' *U ');
130
131 chomp;
132 my $lib_printed = 0;
133 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
134 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
135 push(@{$DepLibs{$libdefs{$_}}}, $_);
136 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
137 my $libroot = $lib;
138 $libroot =~ s/lib(.*).a/$1/;
139 if ($objdefs{$_} ne "$libroot.o") {
140 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
141 push(@{$DepLibs{$objdefs{$_}}}, $_);
142 }
143 }
144 }
Anton Korobeynikov95f73472009-04-21 16:04:14 +0000145 close UNDEFS or die "nm failed";
Chris Lattner58923602008-10-04 18:03:46 +0000146 }
147
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 for my $key (sort keys %DepLibs) {
149 if ($FLAT) {
150 print " $key";
151 if ($WHY) {
152 print "\n";
153 my @syms = @{$DepLibs{$key}};
Ted Kremeneka887b222007-11-27 19:31:11 +0000154 foreach my $sym (@syms) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 print " $sym\n";
156 }
157 }
158 } else {
159 print " <li>$key</li>\n";
160 }
Ted Kremeneka887b222007-11-27 19:31:11 +0000161 my $suffix = substr($key,length($key)-1,1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 $key =~ s/(.*)\.[oa]/$1/;
163 if ($suffix eq "a") {
164 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
165 } else {
166 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
167 }
168 }
169 if ($FLAT) {
170 if (!$WHY) {
171 print "\n";
172 }
173 } else {
174 print " </ul></dd>\n";
175 }
176}
177
178# Make sure we flush on write. This is slower but correct based on the way we
179# write I/O in gen_one_entry.
180$| = 1;
181
182# Print the definition list tag
183if (!$FLAT) {
184 print "<dl>\n";
185
186 open DOT, "| $DotPath -Tgif > libdeps.gif";
187
188 print DOT "digraph LibDeps {\n";
189 print DOT " size=\"40,15\"; \n";
190 print DOT " ratio=\"1.33333\"; \n";
191 print DOT " margin=\"0.25\"; \n";
192 print DOT " rankdir=\"LR\"; \n";
193 print DOT " mclimit=\"50.0\"; \n";
194 print DOT " ordering=\"out\"; \n";
195 print DOT " center=\"1\";\n";
196 print DOT "node [shape=\"box\",\n";
197 print DOT " color=\"#000088\",\n";
198 print DOT " fillcolor=\"#FFFACD\",\n";
199 print DOT " fontcolor=\"#3355BB\",\n";
200 print DOT " style=\"filled\",\n";
201 print DOT " fontname=\"sans\",\n";
202 print DOT " fontsize=\"24\"\n";
203 print DOT "];\n";
204 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
205}
206
207# Print libraries first
Ted Kremeneka887b222007-11-27 19:31:11 +0000208foreach my $lib (@libs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 gen_one_entry($lib);
210}
211
212if (!$FLAT) {
213 print DOT "}\n";
214 close DOT;
215 open DOT, "| $DotPath -Tgif > objdeps.gif";
216 print DOT "digraph ObjDeps {\n";
217 print DOT " size=\"8,10\";\n";
218 print DOT " margin=\"0.25\";\n";
219 print DOT " rankdir=\"LR\";\n";
220 print DOT " mclimit=\"50.0\";\n";
221 print DOT " ordering=\"out\";\n";
222 print DOT " center=\"1\";\n";
223 print DOT "node [shape=\"box\",\n";
224 print DOT " color=\"#000088\",\n";
225 print DOT " fillcolor=\"#FFFACD\",\n";
226 print DOT " fontcolor=\"#3355BB\",\n";
227 print DOT " fontname=\"sans\",\n";
228 print DOT " style=\"filled\",\n";
229 print DOT " fontsize=\"24\"\n";
230 print DOT "];\n";
231 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
232}
233
234# Print objects second
Ted Kremeneka887b222007-11-27 19:31:11 +0000235foreach my $obj (@objs) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 gen_one_entry($obj);
237}
238
239if (!$FLAT) {
240 print DOT "}\n";
241 close DOT;
242
243# Print end tag of definition list element
244 print "</dl>\n";
245}