Tweak GenLibDeps.pl so it works on solaris.
[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($ENV{NM})) {
42   chomp($nmPath=$ENV{NM});
43 }
44
45 if (!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)
52 opendir DIR,$Directory;
53 my @files = readdir DIR;
54 closedir DIR;
55 my @libs = grep(/libLLVM.*\.(dylib|so|a)$/,sort(@files));
56 my @objs = grep(/LLVM.*\.o$/,sort(@files));
57
58 # Declare the hashes we will use to keep track of the library and object file
59 # symbol definitions.
60 my %libdefs;
61 my %objdefs;
62
63 # Gather definitions from the libraries
64 foreach my $lib (@libs ) {
65   open DEFS, "$nmPath -g $Directory/$lib|";
66   while (<DEFS>) {
67     next if (! / [ABCDGRST] /);
68     s/^[^ ]* [ABCDGRST] //;    
69     s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
70                    # this strips both LF and CRLF.
71     $libdefs{$_} = $lib;
72   }
73   close DEFS or die "nm failed";
74 }
75
76 # Gather definitions from the object files.
77 foreach my $obj (@objs ) {
78   open DEFS, "$nmPath -g $Directory/$obj |";
79   while (<DEFS>) {
80     next if (! / [ABCDGRST] /);
81     s/^[^ ]* [ABCDGRST] //;
82     s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
83                    # this strips both LF and CRLF.    
84     $objdefs{$_} = $obj;
85   }
86   close DEFS or die "nm failed";
87 }
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.
92 sub 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, 
103     "$nmPath -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
104   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) {
112       my $libroot = $lib;
113       $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   }
120   close UNDEFS or die "nm failed";
121   unless(keys %DepLibs) {
122     # above failed
123     open UNDEFS, "$nmPath -u $Directory/$lib |";
124     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     }
145     close UNDEFS or die "nm failed";
146   }
147
148   for my $key (sort keys %DepLibs) {
149     if ($FLAT) {
150       print " $key";
151       if ($WHY) {
152         print "\n";
153         my @syms = @{$DepLibs{$key}};
154         foreach my $sym (@syms) {
155           print "  $sym\n";
156         }
157       }
158     } else {
159       print "    <li>$key</li>\n";
160     }
161     my $suffix = substr($key,length($key)-1,1);
162     $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
183 if (!$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
208 foreach my $lib (@libs) {
209   gen_one_entry($lib);
210 }
211
212 if (!$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
235 foreach my $obj (@objs) {
236   gen_one_entry($obj);
237 }
238
239 if (!$FLAT) {
240   print DOT "}\n";
241   close DOT;
242
243 # Print end tag of definition list element
244   print "</dl>\n";
245 }