* Don't include weak definitions as a definition
[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 <directory_with_libraries_in_it>
10 #
11
12 # Give first option a name.
13 my $Directory = $ARGV[0];
14
15 # Open the directory and read its contents, sorting by name and differentiating
16 # by whether its a library (.a) or an object file (.o)
17 opendir DIR,$Directory;
18 my @files = readdir DIR;
19 closedir DIR;
20 @libs = grep(/libLLVM.*\.a$/,sort(@files));
21 @objs = grep(/LLVM.*\.o$/,sort(@files));
22
23 # Declare the hashes we will use to keep track of the library and object file
24 # symbol definitions.
25 my %libdefs;
26 my %objdefs;
27
28 # Gather definitions from the libraries
29 foreach $lib (@libs ) {
30   open DEFS, 
31     "nm -g --defined-only $lib | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
32   while (<DEFS>) {
33     chomp($_);
34     $libdefs{$_} = $lib;
35   }
36   close DEFS;
37 }
38
39 # Gather definitions from the object files.
40 foreach $obj (@objs ) {
41   open DEFS, 
42     "nm -g --defined-only $obj | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
43   while (<DEFS>) {
44     chomp($_);
45     $objdefs{$_} = $obj;
46   }
47   close DEFS;
48 }
49
50 # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
51 # for one library or object file. The <dt> provides the name of the library or
52 # object. The <dd> provides a list of the libraries/objects it depends on.
53 sub gen_one_entry {
54   my $lib = $_[0];
55   print "  <dt><b>$lib</b</dt><dd><ul>\n";
56   open UNDEFS, 
57     "nm -u $lib | grep ' U ' | sed -e 's/         U //' | sort | uniq |";
58   open DEPENDS,
59     "| sort | uniq > GenLibDeps.out";
60   while (<UNDEFS>) {
61     chomp;
62     if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
63       print DEPENDS "$libdefs{$_}\n";
64     } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
65       $libroot = $lib;
66       $libroot =~ s/lib(.*).a/$1/;
67       if ($objdefs{$_} ne "$libroot.o") {
68         print DEPENDS "$objdefs{$_}\n";
69       }
70     }
71   }
72   close UNDEFS;
73   close DEPENDS;
74   open DF, "<GenLibDeps.out";
75   while (<DF>) {
76     chomp;
77     print "    <li>$_</li>\n";
78   }
79   close DF;
80   print "  </ul></dd>\n";
81 }
82
83 # Make sure we flush on write. This is slower but correct based on the way we
84 # write I/O in gen_one_entry.
85 $| = 1;
86
87 # Print the definition list tag
88 print "<dl>\n";
89
90 # Print libraries first
91 foreach $lib (@libs) {
92   gen_one_entry($lib);
93 }
94
95 # Print objects second
96 foreach $obj (@objs) {
97   gen_one_entry($obj);
98 }
99
100 # Print end tag of definition list element
101 print "</dl>\n";