Tell GenLibDeps.pl to inspect .so and .dylib shared files.
[oota-llvm.git] / utils / GenLibDeps.pl
1 #!/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 #
11 use strict;
12
13 # Parse arguments... 
14 my $FLAT = 0;
15 my $WHY = 0;
16 while (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.
27 my $Directory = $ARGV[0];
28 if (!defined($Directory) || ! -d "$Directory") {
29   die "First argument must specify the directory containing LLVM libs\n";
30 }
31
32 my $nmPath = $ARGV[1];
33
34 # Find the "dot" program
35 my $DotPath="";
36 if (!$FLAT) {
37   chomp($DotPath = `which dot`);
38   die "Can't find 'dot'" if (! -x "$DotPath");
39 }
40
41 if (!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)
48 opendir DIR,$Directory;
49 my @files = readdir DIR;
50 closedir DIR;
51 my @libs = grep(/libLLVM.*\.(dylib|so|a)$/,sort(@files));
52 my @objs = grep(/LLVM.*\.o$/,sort(@files));
53
54 # Declare the hashes we will use to keep track of the library and object file
55 # symbol definitions.
56 my %libdefs;
57 my %objdefs;
58
59 # Gather definitions from the libraries
60 foreach my $lib (@libs ) {
61   open DEFS, "$nmPath -g $Directory/$lib|";
62   while (<DEFS>) {
63     next if (! / [ABCDGRST] /);
64     s/^[^ ]* [ABCDGRST] //;    
65     s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
66                    # this strips both LF and CRLF.
67     $libdefs{$_} = $lib;
68   }
69   close DEFS;
70 }
71
72 # Gather definitions from the object files.
73 foreach my $obj (@objs ) {
74   open DEFS, "$nmPath -g $Directory/$obj |";
75   while (<DEFS>) {
76     next if (! / [ABCDGRST] /);
77     s/^[^ ]* [ABCDGRST] //;
78     s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
79                    # this strips both LF and CRLF.    
80     $objdefs{$_} = $obj;
81   }
82   close DEFS;
83 }
84
85 # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
86 # for one library or object file. The <dt> provides the name of the library or
87 # object. The <dd> provides a list of the libraries/objects it depends on.
88 sub gen_one_entry {
89   my $lib = $_[0];
90   my $lib_ns = $lib;
91   $lib_ns =~ s/(.*)\.[oa]/$1/;
92   if ($FLAT) {
93     print "$lib:";
94     if ($WHY) { print "\n"; }
95   } else {
96     print "  <dt><b>$lib</b</dt><dd><ul>\n";
97   }
98   open UNDEFS, 
99     "$nmPath -g -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
100   my %DepLibs;
101   while (<UNDEFS>) {
102     chomp;
103     my $lib_printed = 0;
104     if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
105       $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
106       push(@{$DepLibs{$libdefs{$_}}}, $_);
107     } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
108       my $libroot = $lib;
109       $libroot =~ s/lib(.*).a/$1/;
110       if ($objdefs{$_} ne "$libroot.o") {
111         $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
112         push(@{$DepLibs{$objdefs{$_}}}, $_);
113       }
114     }
115   }
116   close UNDEFS;
117   unless(keys %DepLibs) {
118     # above failed
119     open UNDEFS, "$nmPath -g -u $Directory/$lib |";
120     while (<UNDEFS>) {
121       # to bypass non-working sed
122       if ('  ' eq substr($_,0,2) and index($_,'U ')) {
123         $_ = substr($_,index($_,'U ')+2)
124       };
125       $_ = substr($_,index($_,'  *U ')+5) if -1!=index($_,'  *U ');
126
127       chomp;
128       my $lib_printed = 0;
129       if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
130         $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
131         push(@{$DepLibs{$libdefs{$_}}}, $_);
132       } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
133         my $libroot = $lib;
134         $libroot =~ s/lib(.*).a/$1/;
135         if ($objdefs{$_} ne "$libroot.o") {
136           $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
137           push(@{$DepLibs{$objdefs{$_}}}, $_);
138         }
139       }
140     }
141   }
142
143   for my $key (sort keys %DepLibs) {
144     if ($FLAT) {
145       print " $key";
146       if ($WHY) {
147         print "\n";
148         my @syms = @{$DepLibs{$key}};
149         foreach my $sym (@syms) {
150           print "  $sym\n";
151         }
152       }
153     } else {
154       print "    <li>$key</li>\n";
155     }
156     my $suffix = substr($key,length($key)-1,1);
157     $key =~ s/(.*)\.[oa]/$1/;
158     if ($suffix eq "a") {
159       if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
160     } else {
161       if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
162     }
163   }
164   if ($FLAT) {
165     if (!$WHY) {
166       print "\n";
167     }
168   } else {
169     print "  </ul></dd>\n";
170   }
171 }
172
173 # Make sure we flush on write. This is slower but correct based on the way we
174 # write I/O in gen_one_entry.
175 $| = 1;
176
177 # Print the definition list tag
178 if (!$FLAT) {
179     print "<dl>\n";
180
181   open DOT, "| $DotPath -Tgif > libdeps.gif";
182
183   print DOT "digraph LibDeps {\n";
184   print DOT "  size=\"40,15\"; \n";
185   print DOT "  ratio=\"1.33333\"; \n";
186   print DOT "  margin=\"0.25\"; \n";
187   print DOT "  rankdir=\"LR\"; \n";
188   print DOT "  mclimit=\"50.0\"; \n";
189   print DOT "  ordering=\"out\"; \n";
190   print DOT "  center=\"1\";\n";
191   print DOT "node [shape=\"box\",\n";
192   print DOT "      color=\"#000088\",\n";
193   print DOT "      fillcolor=\"#FFFACD\",\n";
194   print DOT "      fontcolor=\"#3355BB\",\n";
195   print DOT "      style=\"filled\",\n";
196   print DOT "      fontname=\"sans\",\n";
197   print DOT "      fontsize=\"24\"\n";
198   print DOT "];\n";
199   print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
200 }
201
202 # Print libraries first
203 foreach my $lib (@libs) {
204   gen_one_entry($lib);
205 }
206
207 if (!$FLAT) {
208   print DOT "}\n";
209   close DOT;
210   open DOT, "| $DotPath -Tgif > objdeps.gif";
211   print DOT "digraph ObjDeps {\n";
212   print DOT "  size=\"8,10\";\n";
213   print DOT "  margin=\"0.25\";\n";
214   print DOT "  rankdir=\"LR\";\n";
215   print DOT "  mclimit=\"50.0\";\n";
216   print DOT "  ordering=\"out\";\n";
217   print DOT "  center=\"1\";\n";
218   print DOT "node [shape=\"box\",\n";
219   print DOT "      color=\"#000088\",\n";
220   print DOT "      fillcolor=\"#FFFACD\",\n";
221   print DOT "      fontcolor=\"#3355BB\",\n";
222   print DOT "      fontname=\"sans\",\n";
223   print DOT "      style=\"filled\",\n";
224   print DOT "      fontsize=\"24\"\n";
225   print DOT "];\n";
226   print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
227 }
228
229 # Print objects second
230 foreach my $obj (@objs) {
231   gen_one_entry($obj);
232 }
233
234 if (!$FLAT) {
235   print DOT "}\n";
236   close DOT;
237
238 # Print end tag of definition list element
239   print "</dl>\n";
240 }