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