Fix a problem with use of undefined variables. Print an error message if
[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
12 # Parse arguments... 
13 my $FLAT = 0;
14 my $WHY = 0;
15 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
16   shift;
17   last if /^--$/;  # Stop processing arguments on --
18
19   # List command line options here...
20   if (/^-flat$/)     { $FLAT = 1; next; }
21   if (/^-why/)       { $WHY = 1; $FLAT = 1; next; }
22   print "Unknown option: $_ : ignoring!\n";
23 }
24
25 # Give first option a name.
26 my $Directory = $ARGV[0];
27 if (!defined($Directory)) {
28   die "First argument must be the directory containing LLVM libs\n";
29 }
30 my $nmPath = $ARGV[1];
31
32 # Find the "dot" program
33 my $DotPath="";
34 if (!$FLAT) {
35   chomp($DotPath = `which dot`);
36   die "Can't find 'dot'" if (! -x "$DotPath");
37 }
38
39 if (!defined($nmPath) || $nmPath eq "") {
40   chomp($nmPath=`which nm`);
41   die "Can't find 'nm'" if (! -x "$nmPath");
42 }
43
44 # Open the directory and read its contents, sorting by name and differentiating
45 # by whether its a library (.a) or an object file (.o)
46 opendir DIR,$Directory;
47 my @files = readdir DIR;
48 closedir DIR;
49 @libs = grep(/libLLVM.*\.a$/,sort(@files));
50 @objs = grep(/LLVM.*\.o$/,sort(@files));
51
52 # Declare the hashes we will use to keep track of the library and object file
53 # symbol definitions.
54 my %libdefs;
55 my %objdefs;
56
57 # Gather definitions from the libraries
58 foreach $lib (@libs ) {
59   open DEFS, 
60     "$nmPath -g $Directory/$lib | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
61   while (<DEFS>) {
62     chomp($_);
63     $libdefs{$_} = $lib;
64   }
65   close DEFS;
66 }
67
68 # Gather definitions from the object files.
69 foreach $obj (@objs ) {
70   open DEFS, 
71     "$nmPath -g $Directory/$obj | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
72   while (<DEFS>) {
73     chomp($_);
74     $objdefs{$_} = $obj;
75   }
76   close DEFS;
77 }
78
79 # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
80 # for one library or object file. The <dt> provides the name of the library or
81 # object. The <dd> provides a list of the libraries/objects it depends on.
82 sub gen_one_entry {
83   my $lib = $_[0];
84   my $lib_ns = $lib;
85   $lib_ns =~ s/(.*)\.[oa]/$1/;
86   if ($FLAT) {
87     print "$lib:";
88     if ($WHY) { print "\n"; }
89   } else {
90     print "  <dt><b>$lib</b</dt><dd><ul>\n";
91   }
92   open UNDEFS, 
93     "$nmPath -g -u $Directory/$lib | sed -e 's/^  *U //' | sort | uniq |";
94   my %DepLibs;
95   while (<UNDEFS>) {
96     chomp;
97     my $lib_printed = 0;
98     if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
99       $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
100       push(@{$DepLibs{$libdefs{$_}}}, $_);
101     } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
102       $libroot = $lib;
103       $libroot =~ s/lib(.*).a/$1/;
104       if ($objdefs{$_} ne "$libroot.o") {
105         $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
106         push(@{$DepLibs{$objdefs{$_}}}, $_);
107       }
108     }
109   }
110   close UNDEFS;
111   for my $key (sort keys %DepLibs) {
112     if ($FLAT) {
113       print " $key";
114       if ($WHY) {
115         print "\n";
116         my @syms = @{$DepLibs{$key}};
117         foreach $sym (@syms) {
118           print "  $sym\n";
119         }
120       }
121     } else {
122       print "    <li>$key</li>\n";
123     }
124     $suffix = substr($key,length($key)-1,1);
125     $key =~ s/(.*)\.[oa]/$1/;
126     if ($suffix eq "a") {
127       if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
128     } else {
129       if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
130     }
131   }
132   if ($FLAT) {
133     if (!$WHY) {
134       print "\n";
135     }
136   } else {
137     print "  </ul></dd>\n";
138   }
139 }
140
141 # Make sure we flush on write. This is slower but correct based on the way we
142 # write I/O in gen_one_entry.
143 $| = 1;
144
145 # Print the definition list tag
146 if (!$FLAT) {
147     print "<dl>\n";
148
149   open DOT, "| $DotPath -Tgif > libdeps.gif";
150
151   print DOT "digraph LibDeps {\n";
152   print DOT "  size=\"40,15\"; \n";
153   print DOT "  ratio=\"1.33333\"; \n";
154   print DOT "  margin=\"0.25\"; \n";
155   print DOT "  rankdir=\"LR\"; \n";
156   print DOT "  mclimit=\"50.0\"; \n";
157   print DOT "  ordering=\"out\"; \n";
158   print DOT "  center=\"1\";\n";
159   print DOT "node [shape=\"box\",\n";
160   print DOT "      color=\"#000088\",\n";
161   print DOT "      fillcolor=\"#FFFACD\",\n";
162   print DOT "      fontcolor=\"#3355BB\",\n";
163   print DOT "      style=\"filled\",\n";
164   print DOT "      fontname=\"sans\",\n";
165   print DOT "      fontsize=\"24\"\n";
166   print DOT "];\n";
167   print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
168 }
169
170 # Print libraries first
171 foreach $lib (@libs) {
172   gen_one_entry($lib);
173 }
174
175 if (!$FLAT) {
176   print DOT "}\n";
177   close DOT;
178   open DOT, "| $DotPath -Tgif > objdeps.gif";
179   print DOT "digraph ObjDeps {\n";
180   print DOT "  size=\"8,10\";\n";
181   print DOT "  margin=\"0.25\";\n";
182   print DOT "  rankdir=\"LR\";\n";
183   print DOT "  mclimit=\"50.0\";\n";
184   print DOT "  ordering=\"out\";\n";
185   print DOT "  center=\"1\";\n";
186   print DOT "node [shape=\"box\",\n";
187   print DOT "      color=\"#000088\",\n";
188   print DOT "      fillcolor=\"#FFFACD\",\n";
189   print DOT "      fontcolor=\"#3355BB\",\n";
190   print DOT "      fontname=\"sans\",\n";
191   print DOT "      style=\"filled\",\n";
192   print DOT "      fontsize=\"24\"\n";
193   print DOT "];\n";
194   print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
195 }
196
197 # Print objects second
198 foreach $obj (@objs) {
199   gen_one_entry($obj);
200 }
201
202 if (!$FLAT) {
203   print DOT "}\n";
204   close DOT;
205
206 # Print end tag of definition list element
207   print "</dl>\n";
208 }