blob: 6d0b13ee4361eb5183bf417f5b03c1d50d5580c3 [file] [log] [blame]
Reid Spencer579b8de2004-12-30 23:07:56 +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#
Reid Spencer1bc68642006-07-27 23:00:30 +00009# Syntax: GenLibDeps.pl [-flat] <directory_with_libraries_in_it> [path_to_nm_binary]
Reid Spencer579b8de2004-12-30 23:07:56 +000010#
Ted Kremenek8330b0e2007-11-27 19:31:11 +000011use strict;
Reid Spencer579b8de2004-12-30 23:07:56 +000012
Reid Spencer0bf11772006-03-19 22:08:01 +000013# Parse arguments...
Reid Spencerc152efd2006-07-25 19:12:06 +000014my $FLAT = 0;
15my $WHY = 0;
Reid Spencer0bf11772006-03-19 22:08:01 +000016while (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; }
Reid Spencerc152efd2006-07-25 19:12:06 +000022 if (/^-why/) { $WHY = 1; $FLAT = 1; next; }
Reid Spencer0bf11772006-03-19 22:08:01 +000023 print "Unknown option: $_ : ignoring!\n";
24}
25
Reid Spencer579b8de2004-12-30 23:07:56 +000026# Give first option a name.
27my $Directory = $ARGV[0];
Reid Spencerd0fa46a2006-08-03 19:10:03 +000028if (!defined($Directory) || ! -d "$Directory") {
29 die "First argument must specify the directory containing LLVM libs\n";
Reid Spencer9bf2e7f2006-08-01 08:09:03 +000030}
Reid Spencerd0fa46a2006-08-03 19:10:03 +000031
Reid Spencer1bc68642006-07-27 23:00:30 +000032my $nmPath = $ARGV[1];
Reid Spencer579b8de2004-12-30 23:07:56 +000033
Reid Spencer62345822005-01-05 17:29:29 +000034# Find the "dot" program
Reid Spencerdd73e7f2006-05-13 02:48:45 +000035my $DotPath="";
Reid Spencer44fa6912006-04-20 23:09:57 +000036if (!$FLAT) {
Reid Spencerdd73e7f2006-05-13 02:48:45 +000037 chomp($DotPath = `which dot`);
Reid Spencer44fa6912006-04-20 23:09:57 +000038 die "Can't find 'dot'" if (! -x "$DotPath");
39}
Reid Spencer62345822005-01-05 17:29:29 +000040
Duncan Sands7e0842f2009-06-12 14:23:42 +000041if (defined($ENV{NM})) {
42 chomp($nmPath=$ENV{NM});
43}
44
Reid Spencer9bf2e7f2006-08-01 08:09:03 +000045if (!defined($nmPath) || $nmPath eq "") {
46 chomp($nmPath=`which nm`);
47 die "Can't find 'nm'" if (! -x "$nmPath");
Reid Spencer1bc68642006-07-27 23:00:30 +000048}
Reid Spencerdd73e7f2006-05-13 02:48:45 +000049
Reid Spencer579b8de2004-12-30 23:07:56 +000050# 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 Fuentese40db4a2008-11-12 20:39:06 +000055my @libs = grep(/libLLVM.*\.(dylib|so|a)$/,sort(@files));
Ted Kremenek8330b0e2007-11-27 19:31:11 +000056my @objs = grep(/LLVM.*\.o$/,sort(@files));
Reid Spencer579b8de2004-12-30 23:07:56 +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 Kremenek8330b0e2007-11-27 19:31:11 +000064foreach my $lib (@libs ) {
65 open DEFS, "$nmPath -g $Directory/$lib|";
Reid Spencer579b8de2004-12-30 23:07:56 +000066 while (<DEFS>) {
Ted Kremenek8330b0e2007-11-27 19:31:11 +000067 next if (! / [ABCDGRST] /);
68 s/^[^ ]* [ABCDGRST] //;
Ted Kremenekec9e7162007-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.
Reid Spencer579b8de2004-12-30 23:07:56 +000071 $libdefs{$_} = $lib;
72 }
Anton Korobeynikov8d8fbf22009-04-21 16:04:14 +000073 close DEFS or die "nm failed";
Reid Spencer579b8de2004-12-30 23:07:56 +000074}
75
76# Gather definitions from the object files.
Ted Kremenek8330b0e2007-11-27 19:31:11 +000077foreach my $obj (@objs ) {
78 open DEFS, "$nmPath -g $Directory/$obj |";
Reid Spencer579b8de2004-12-30 23:07:56 +000079 while (<DEFS>) {
Ted Kremenek8330b0e2007-11-27 19:31:11 +000080 next if (! / [ABCDGRST] /);
81 s/^[^ ]* [ABCDGRST] //;
Ted Kremenekec9e7162007-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.
Reid Spencer579b8de2004-12-30 23:07:56 +000084 $objdefs{$_} = $obj;
85 }
Anton Korobeynikov8d8fbf22009-04-21 16:04:14 +000086 close DEFS or die "nm failed";
Reid Spencer579b8de2004-12-30 23:07:56 +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];
Reid Spencer62345822005-01-05 17:29:29 +000094 my $lib_ns = $lib;
95 $lib_ns =~ s/(.*)\.[oa]/$1/;
Reid Spencer0bf11772006-03-19 22:08:01 +000096 if ($FLAT) {
97 print "$lib:";
Reid Spencerc152efd2006-07-25 19:12:06 +000098 if ($WHY) { print "\n"; }
Reid Spencer0bf11772006-03-19 22:08:01 +000099 } else {
100 print " <dt><b>$lib</b</dt><dd><ul>\n";
101 }
Reid Spencer579b8de2004-12-30 23:07:56 +0000102 open UNDEFS,
Duncan Sands7e0842f2009-06-12 14:23:42 +0000103 "$nmPath -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
Reid Spencerc152efd2006-07-25 19:12:06 +0000104 my %DepLibs;
Reid Spencer579b8de2004-12-30 23:07:56 +0000105 while (<UNDEFS>) {
106 chomp;
Reid Spencerc152efd2006-07-25 19:12:06 +0000107 my $lib_printed = 0;
Reid Spencer579b8de2004-12-30 23:07:56 +0000108 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000109 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
110 push(@{$DepLibs{$libdefs{$_}}}, $_);
Reid Spencer579b8de2004-12-30 23:07:56 +0000111 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000112 my $libroot = $lib;
Reid Spencer579b8de2004-12-30 23:07:56 +0000113 $libroot =~ s/lib(.*).a/$1/;
114 if ($objdefs{$_} ne "$libroot.o") {
Reid Spencerc152efd2006-07-25 19:12:06 +0000115 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
116 push(@{$DepLibs{$objdefs{$_}}}, $_);
Reid Spencer579b8de2004-12-30 23:07:56 +0000117 }
118 }
119 }
Anton Korobeynikov8d8fbf22009-04-21 16:04:14 +0000120 close UNDEFS or die "nm failed";
Chris Lattner8d59af32008-10-04 18:03:46 +0000121 unless(keys %DepLibs) {
122 # above failed
Duncan Sands7e0842f2009-06-12 14:23:42 +0000123 open UNDEFS, "$nmPath -u $Directory/$lib |";
Chris Lattner8d59af32008-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 Korobeynikov8d8fbf22009-04-21 16:04:14 +0000145 close UNDEFS or die "nm failed";
Chris Lattner8d59af32008-10-04 18:03:46 +0000146 }
147
Reid Spencerc152efd2006-07-25 19:12:06 +0000148 for my $key (sort keys %DepLibs) {
Reid Spencer0bf11772006-03-19 22:08:01 +0000149 if ($FLAT) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000150 print " $key";
151 if ($WHY) {
152 print "\n";
153 my @syms = @{$DepLibs{$key}};
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000154 foreach my $sym (@syms) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000155 print " $sym\n";
156 }
157 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000158 } else {
Reid Spencerc152efd2006-07-25 19:12:06 +0000159 print " <li>$key</li>\n";
Reid Spencer0bf11772006-03-19 22:08:01 +0000160 }
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000161 my $suffix = substr($key,length($key)-1,1);
Reid Spencerc152efd2006-07-25 19:12:06 +0000162 $key =~ s/(.*)\.[oa]/$1/;
Reid Spencer62345822005-01-05 17:29:29 +0000163 if ($suffix eq "a") {
Reid Spencerc152efd2006-07-25 19:12:06 +0000164 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
Reid Spencer62345822005-01-05 17:29:29 +0000165 } else {
Reid Spencerc152efd2006-07-25 19:12:06 +0000166 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
Reid Spencer62345822005-01-05 17:29:29 +0000167 }
Reid Spencer579b8de2004-12-30 23:07:56 +0000168 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000169 if ($FLAT) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000170 if (!$WHY) {
171 print "\n";
172 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000173 } else {
174 print " </ul></dd>\n";
175 }
Reid Spencer579b8de2004-12-30 23:07:56 +0000176}
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
Reid Spencer0bf11772006-03-19 22:08:01 +0000183if (!$FLAT) {
Reid Spencer44fa6912006-04-20 23:09:57 +0000184 print "<dl>\n";
185
186 open DOT, "| $DotPath -Tgif > libdeps.gif";
187
Reid Spencer9bf2e7f2006-08-01 08:09:03 +0000188 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";
Reid Spencer0bf11772006-03-19 22:08:01 +0000205}
Reid Spencer579b8de2004-12-30 23:07:56 +0000206
207# Print libraries first
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000208foreach my $lib (@libs) {
Reid Spencer579b8de2004-12-30 23:07:56 +0000209 gen_one_entry($lib);
210}
Reid Spencer44fa6912006-04-20 23:09:57 +0000211
212if (!$FLAT) {
213 print DOT "}\n";
214 close DOT;
215 open DOT, "| $DotPath -Tgif > objdeps.gif";
Reid Spencer9bf2e7f2006-08-01 08:09:03 +0000216 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";
Reid Spencer44fa6912006-04-20 23:09:57 +0000232}
Reid Spencer579b8de2004-12-30 23:07:56 +0000233
234# Print objects second
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000235foreach my $obj (@objs) {
Reid Spencer579b8de2004-12-30 23:07:56 +0000236 gen_one_entry($obj);
237}
238
Reid Spencer44fa6912006-04-20 23:09:57 +0000239if (!$FLAT) {
240 print DOT "}\n";
241 close DOT;
Reid Spencer62345822005-01-05 17:29:29 +0000242
Reid Spencer579b8de2004-12-30 23:07:56 +0000243# Print end tag of definition list element
Reid Spencer0bf11772006-03-19 22:08:01 +0000244 print "</dl>\n";
245}