Updated GenLibDeps.pl to employ "use strict" to help prevent uses of variables
[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.*\.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     chomp($_);
66     $libdefs{$_} = $lib;
67   }
68   close DEFS;
69 }
70
71 # Gather definitions from the object files.
72 foreach my $obj (@objs ) {
73   open DEFS, "$nmPath -g $Directory/$obj |";
74   while (<DEFS>) {
75     next if (! / [ABCDGRST] /);
76     s/^[^ ]* [ABCDGRST] //;
77     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.
86 sub 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) {
106       my $libroot = $lib;
107       $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}};
121         foreach my $sym (@syms) {
122           print "  $sym\n";
123         }
124       }
125     } else {
126       print "    <li>$key</li>\n";
127     }
128     my $suffix = substr($key,length($key)-1,1);
129     $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
150 if (!$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
175 foreach my $lib (@libs) {
176   gen_one_entry($lib);
177 }
178
179 if (!$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
202 foreach my $obj (@objs) {
203   gen_one_entry($obj);
204 }
205
206 if (!$FLAT) {
207   print DOT "}\n";
208   close DOT;
209
210 # Print end tag of definition list element
211   print "</dl>\n";
212 }