GenLibDeps.pl revision b05942cec9803ae3d26dcc443074fb69d776d3a4
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#
11use strict;
12use warnings;
13# Parse arguments...
14my $FLAT = 0;
15my $WHY = 0;
16my $PEROBJ = 0;
17my $PEROBJINCL = 0;
18while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
19  shift;
20  last if /^--$/;  # Stop processing arguments on --
21
22  # List command line options here...
23  if (/^-flat$/)     { $FLAT = 1; next; }
24  if (/^-why/)       { $WHY = 1; $FLAT = 1; next; }
25  if (/^-perobj$/)    { $PEROBJ = 1; next; }
26  if (/^-perobjincl/) { $PEROBJINCL = 1; next;}
27  print "Unknown option: $_ : ignoring!\n";
28}
29
30# Give first option a name.
31my $Directory = $ARGV[0];
32if (!defined($Directory) || ! -d "$Directory") {
33  die "First argument must specify the directory containing LLVM libs\n";
34}
35
36my $nmPath = $ARGV[1];
37
38# Find the "dot" program
39my $DotPath="";
40if (!$FLAT) {
41  chomp($DotPath = `which dot`);
42  die "Can't find 'dot'" if (! -x "$DotPath");
43}
44
45if (defined($ENV{NM})) {
46  chomp($nmPath=$ENV{NM});
47}
48
49if (!defined($nmPath) || $nmPath eq "") {
50  chomp($nmPath=`which nm`);
51  die "Can't find 'nm'" if (! -x "$nmPath");
52}
53
54my $ranlibPath;
55if ($PEROBJ) {
56  $ranlibPath = $ARGV[2];
57  if (defined($ENV{RANLIB})) {
58    chomp($ranlibPath=$ENV{RANLIB});
59  }
60
61  if (!defined($ranlibPath) || $ranlibPath eq "") {
62    chomp($ranlibPath=`which ranlib`);
63    die "Can't find 'ranlib'" if (! -x "$ranlibPath");
64  }
65}
66
67# Open the directory and read its contents, sorting by name and differentiating
68# by whether its a library (.a) or an object file (.o)
69opendir DIR,$Directory;
70my @files = readdir DIR;
71closedir DIR;
72my @libs = grep(/libLLVM.*\.(dylib|so|a)$/,sort(@files));
73# Omit the all-of-llvm shared library.
74@libs = grep(!/libLLVM-\d\.\d(svn)?\.(dylib|so)/, @libs);
75my @objs = grep(/LLVM.*\.o$/,sort(@files));
76
77# Declare the hashes we will use to keep track of the library and object file
78# symbol definitions.
79my %libdefs;
80my %objdefs;
81
82my %libobjs;
83my %objdeps=();
84# Gather library definitions at object file granularity (optional)
85if ($PEROBJ) {
86  foreach my $lib (@libs ) {
87    `$ranlibPath $Directory/$lib`;
88    my $libpath = $lib;
89    $libpath =~ s/^libLLVM(.*)\.a/$1/;
90    $libpath =~ s/(.+)CodeGen$/Target\/$1/;
91    $libpath =~ s/(.+)AsmPrinter$/Target\/$1\/AsmPrinter/;
92    $libpath =~ s/(.+)AsmParser$/Target\/$1\/AsmParser/;
93    $libpath =~ s/(.+)Info$/Target\/$1\/TargetInfo/;
94    $libpath =~ s/(.+)Disassembler$/Target\/$1\/Disassembler/;
95    $libpath =~ s/SelectionDAG/CodeGen\/SelectionDAG/;
96    $libpath =~ s/^AsmPrinter/CodeGen\/AsmPrinter/;
97    $libpath =~ s/^BitReader/Bitcode\/Reader/;
98    $libpath =~ s/^BitWriter/Bitcode\/Writer/;
99    $libpath =~ s/^CBackend/Target\/CBackend/;
100    $libpath =~ s/^CppBackend/Target\/CppBackend/;
101    $libpath =~ s/^MSIL/Target\/MSIL/;
102    $libpath =~ s/^Core/VMCore/;
103    $libpath =~ s/^Instrumentation/Transforms\/Instrumentation/;
104    $libpath =~ s/^Interpreter/ExecutionEngine\/Interpreter/;
105    $libpath =~ s/^JIT/ExecutionEngine\/JIT/;
106    $libpath =~ s/^ScalarOpts/Transforms\/Scalar/;
107    $libpath =~ s/^TransformUtils/Transforms\/Utils/;
108    $libpath =~ s/^ipa/Analysis\/IPA/;
109    $libpath =~ s/^ipo/Transforms\/IPO/;
110    $libpath =~ s/^pic16passes/Target\/PIC16\/PIC16Passes/;
111    $libpath = "lib/".$libpath."/";
112    open DEFS, "$nmPath -sg $Directory/$lib|";
113    while (<DEFS>) {
114      chomp;
115      if (/^([^ ]*) in ([^ ]*)/) {
116        my $objfile = $libpath.$2;
117        $objdefs{$1} = $objfile;
118        $objdeps{$objfile} = {};
119        $libobjs{$lib}{$objfile}=1;
120#        my $p = "../llvm/".$objfile;
121#        $p =~ s/Support\/reg(.*).o/Support\/reg$1.c/;
122#        $p =~ s/.o$/.cpp/;
123#        unless (-e $p) {
124#          die "$p\n"
125#        }
126      }
127    }
128    close DEFS or die "nm failed";
129  }
130  foreach my $lib (@libs ) {
131    my $libpath = $lib;
132    $libpath =~ s/^libLLVM(.*)\.a/$1/;
133    $libpath =~ s/(.+)CodeGen$/Target\/$1/;
134    $libpath =~ s/(.+)AsmPrinter$/Target\/$1\/AsmPrinter/;
135    $libpath =~ s/(.+)AsmParser$/Target\/$1\/AsmParser/;
136    $libpath =~ s/(.+)Info$/Target\/$1\/TargetInfo/;
137    $libpath =~ s/(.+)Disassembler$/Target\/$1\/Disassembler/;
138    $libpath =~ s/SelectionDAG/CodeGen\/SelectionDAG/;
139    $libpath =~ s/^AsmPrinter/CodeGen\/AsmPrinter/;
140    $libpath =~ s/^BitReader/Bitcode\/Reader/;
141    $libpath =~ s/^BitWriter/Bitcode\/Writer/;
142    $libpath =~ s/^CBackend/Target\/CBackend/;
143    $libpath =~ s/^CppBackend/Target\/CppBackend/;
144    $libpath =~ s/^MSIL/Target\/MSIL/;
145    $libpath =~ s/^Core/VMCore/;
146    $libpath =~ s/^Instrumentation/Transforms\/Instrumentation/;
147    $libpath =~ s/^Interpreter/ExecutionEngine\/Interpreter/;
148    $libpath =~ s/^JIT/ExecutionEngine\/JIT/;
149    $libpath =~ s/^ScalarOpts/Transforms\/Scalar/;
150    $libpath =~ s/^TransformUtils/Transforms\/Utils/;
151    $libpath =~ s/^ipa/Analysis\/IPA/;
152    $libpath =~ s/^ipo/Transforms\/IPO/;
153    $libpath =~ s/^pic16passes/Target\/PIC16\/PIC16Passes/;
154    $libpath = "lib/".$libpath."/";
155    open UDEFS, "$nmPath -Aup $Directory/$lib|";
156    while (<UDEFS>) {
157      chomp;
158      if (/:([^:]+):/) {
159        my $obj = $libpath.$1;
160        s/[^ ]+: *U //;
161        if (defined($objdefs{$_})) {
162          $objdeps{$obj}{$objdefs{$_}}=1;
163        }
164      }
165    }
166    close UDEFS or die "nm failed"
167  }
168} else {
169# Gather definitions from the libraries
170foreach my $lib (@libs ) {
171  open DEFS, "$nmPath -g $Directory/$lib|";
172  while (<DEFS>) {
173    next if (! / [ABCDGRST] /);
174    s/^[^ ]* [ABCDGRST] //;
175    s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
176                   # this strips both LF and CRLF.
177    $libdefs{$_} = $lib;
178  }
179  close DEFS or die "nm failed";
180}
181}
182
183# Gather definitions from the object files.
184foreach my $obj (@objs ) {
185  open DEFS, "$nmPath -g $Directory/$obj |";
186  while (<DEFS>) {
187    next if (! / [ABCDGRST] /);
188    s/^[^ ]* [ABCDGRST] //;
189    s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
190                   # this strips both LF and CRLF.
191    $objdefs{$_} = $obj;
192  }
193  close DEFS or die "nm failed";
194}
195
196# Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
197# for one library or object file. The <dt> provides the name of the library or
198# object. The <dd> provides a list of the libraries/objects it depends on.
199sub gen_one_entry {
200  my $lib = $_[0];
201  my $lib_ns = $lib;
202  $lib_ns =~ s/(.*)\.[oa]/$1/;
203  if ($FLAT) {
204    print "$lib:";
205    if ($WHY) { print "\n"; }
206  } else {
207    print "  <dt><b>$lib</b</dt><dd><ul>\n";
208  }
209  open UNDEFS,
210    "$nmPath -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
211  my %DepLibs;
212  while (<UNDEFS>) {
213    chomp;
214    my $lib_printed = 0;
215    if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
216      $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
217      push(@{$DepLibs{$libdefs{$_}}}, $_);
218    } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
219      if ($PEROBJ && !$PEROBJINCL) {
220        # -perobjincl makes .a files depend on .o files they contain themselves
221        # default is don't depend on these.
222        next if defined $libobjs{$lib}{$objdefs{$_}};
223      }
224      my $libroot = $lib;
225      $libroot =~ s/lib(.*).a/$1/;
226      if ($objdefs{$_} ne "$libroot.o") {
227        $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
228        push(@{$DepLibs{$objdefs{$_}}}, $_);
229      }
230    }
231  }
232  close UNDEFS or die "nm failed";
233  unless(keys %DepLibs) {
234    # above failed
235    open UNDEFS, "$nmPath -u $Directory/$lib |";
236    while (<UNDEFS>) {
237      # to bypass non-working sed
238      if ('  ' eq substr($_,0,2) and index($_,'U ')) {
239        $_ = substr($_,index($_,'U ')+2)
240      };
241      $_ = substr($_,index($_,'  *U ')+5) if -1!=index($_,'  *U ');
242
243      chomp;
244      my $lib_printed = 0;
245      if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
246        $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
247        push(@{$DepLibs{$libdefs{$_}}}, $_);
248      } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
249        my $libroot = $lib;
250        $libroot =~ s/lib(.*).a/$1/;
251        if ($objdefs{$_} ne "$libroot.o") {
252          $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
253          push(@{$DepLibs{$objdefs{$_}}}, $_);
254        }
255      }
256    }
257    close UNDEFS or die "nm failed";
258  }
259  if ($PEROBJINCL) {
260     # include the .a's objects
261     for my $obj (keys %{$libobjs{$lib}}) {
262        $DepLibs{$obj} = ["<.a object>"] unless exists $DepLibs{$obj};
263     }
264     my $madechange = 1;
265     while($madechange) {
266      $madechange = 0;
267      my %temp = %DepLibs;
268      foreach my $obj (keys %DepLibs) {
269        foreach my $objdeps (keys %{$objdeps{$obj}}) {
270          next if defined $temp{$objdeps};
271          push(@{$temp{$objdeps}}, $obj);
272          $madechange = 1;
273        }
274      }
275      %DepLibs = %temp;
276     }
277  }
278
279  for my $key (sort keys %DepLibs) {
280    if ($FLAT) {
281      print " $key";
282      if ($WHY) {
283        print "\n";
284        my @syms = @{$DepLibs{$key}};
285        foreach my $sym (@syms) {
286          print "  $sym\n";
287        }
288      }
289    } else {
290      print "    <li>$key</li>\n";
291    }
292    my $suffix = substr($key,length($key)-1,1);
293    $key =~ s/(.*)\.[oa]/$1/;
294    if ($suffix eq "a") {
295      if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
296    } else {
297      if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
298    }
299  }
300  if ($FLAT) {
301    if (!$WHY) {
302      print "\n";
303    }
304  } else {
305    print "  </ul></dd>\n";
306  }
307}
308
309# Make sure we flush on write. This is slower but correct based on the way we
310# write I/O in gen_one_entry.
311$| = 1;
312
313# Print the definition list tag
314if (!$FLAT) {
315    print "<dl>\n";
316
317  open DOT, "| $DotPath -Tgif > libdeps.gif";
318
319  print DOT "digraph LibDeps {\n";
320  print DOT "  size=\"40,15\"; \n";
321  print DOT "  ratio=\"1.33333\"; \n";
322  print DOT "  margin=\"0.25\"; \n";
323  print DOT "  rankdir=\"LR\"; \n";
324  print DOT "  mclimit=\"50.0\"; \n";
325  print DOT "  ordering=\"out\"; \n";
326  print DOT "  center=\"1\";\n";
327  print DOT "node [shape=\"box\",\n";
328  print DOT "      color=\"#000088\",\n";
329  print DOT "      fillcolor=\"#FFFACD\",\n";
330  print DOT "      fontcolor=\"#3355BB\",\n";
331  print DOT "      style=\"filled\",\n";
332  print DOT "      fontname=\"sans\",\n";
333  print DOT "      fontsize=\"24\"\n";
334  print DOT "];\n";
335  print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
336}
337
338# Print libraries first
339foreach my $lib (@libs) {
340  gen_one_entry($lib);
341}
342
343if ($PEROBJ) {
344  foreach my $obj (keys %objdeps) {
345     print "$obj:";
346     if (!$PEROBJINCL) {
347      foreach my $dep (keys %{$objdeps{$obj}}) {
348          print " $dep";
349      }
350    }
351     print "\n";
352  }
353}
354
355if (!$FLAT) {
356  print DOT "}\n";
357  close DOT;
358  open DOT, "| $DotPath -Tgif > objdeps.gif";
359  print DOT "digraph ObjDeps {\n";
360  print DOT "  size=\"8,10\";\n";
361  print DOT "  margin=\"0.25\";\n";
362  print DOT "  rankdir=\"LR\";\n";
363  print DOT "  mclimit=\"50.0\";\n";
364  print DOT "  ordering=\"out\";\n";
365  print DOT "  center=\"1\";\n";
366  print DOT "node [shape=\"box\",\n";
367  print DOT "      color=\"#000088\",\n";
368  print DOT "      fillcolor=\"#FFFACD\",\n";
369  print DOT "      fontcolor=\"#3355BB\",\n";
370  print DOT "      fontname=\"sans\",\n";
371  print DOT "      style=\"filled\",\n";
372  print DOT "      fontsize=\"24\"\n";
373  print DOT "];\n";
374  print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
375}
376
377# Print objects second
378foreach my $obj (@objs) {
379  gen_one_entry($obj);
380}
381
382if (!$FLAT) {
383  print DOT "}\n";
384  close DOT;
385
386# Print end tag of definition list element
387  print "</dl>\n";
388}
389