Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 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 | |
Reid Spencer | 89f33a1 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 12 | # Parse arguments... |
| 13 | while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) { |
| 14 | shift; |
| 15 | last if /^--$/; # Stop processing arguments on -- |
| 16 | |
| 17 | # List command line options here... |
| 18 | if (/^-flat$/) { $FLAT = 1; next; } |
| 19 | print "Unknown option: $_ : ignoring!\n"; |
| 20 | } |
| 21 | |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 22 | # Give first option a name. |
| 23 | my $Directory = $ARGV[0]; |
| 24 | |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 25 | # Find the "dot" program |
| 26 | chomp(my $DotPath = `which dot`); |
| 27 | die "Can't find 'dot'" if (! -x "$DotPath"); |
| 28 | |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 29 | # Open the directory and read its contents, sorting by name and differentiating |
| 30 | # by whether its a library (.a) or an object file (.o) |
| 31 | opendir DIR,$Directory; |
| 32 | my @files = readdir DIR; |
| 33 | closedir DIR; |
| 34 | @libs = grep(/libLLVM.*\.a$/,sort(@files)); |
| 35 | @objs = grep(/LLVM.*\.o$/,sort(@files)); |
| 36 | |
| 37 | # Declare the hashes we will use to keep track of the library and object file |
| 38 | # symbol definitions. |
| 39 | my %libdefs; |
| 40 | my %objdefs; |
| 41 | |
| 42 | # Gather definitions from the libraries |
| 43 | foreach $lib (@libs ) { |
| 44 | open DEFS, |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 45 | "nm -g --defined-only $Directory/$lib | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |"; |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 46 | while (<DEFS>) { |
| 47 | chomp($_); |
| 48 | $libdefs{$_} = $lib; |
| 49 | } |
| 50 | close DEFS; |
| 51 | } |
| 52 | |
| 53 | # Gather definitions from the object files. |
| 54 | foreach $obj (@objs ) { |
| 55 | open DEFS, |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 56 | "nm -g --defined-only $Directory/$obj | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |"; |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 57 | while (<DEFS>) { |
| 58 | chomp($_); |
| 59 | $objdefs{$_} = $obj; |
| 60 | } |
| 61 | close DEFS; |
| 62 | } |
| 63 | |
| 64 | # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements |
| 65 | # for one library or object file. The <dt> provides the name of the library or |
| 66 | # object. The <dd> provides a list of the libraries/objects it depends on. |
| 67 | sub gen_one_entry { |
| 68 | my $lib = $_[0]; |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 69 | my $lib_ns = $lib; |
| 70 | $lib_ns =~ s/(.*)\.[oa]/$1/; |
Reid Spencer | 89f33a1 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 71 | if ($FLAT) { |
| 72 | print "$lib:"; |
| 73 | } else { |
| 74 | print " <dt><b>$lib</b</dt><dd><ul>\n"; |
| 75 | } |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 76 | open UNDEFS, |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 77 | "nm -u $Directory/$lib | grep ' U ' | sed -e 's/ U //' | sort | uniq |"; |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 78 | open DEPENDS, |
| 79 | "| sort | uniq > GenLibDeps.out"; |
| 80 | while (<UNDEFS>) { |
| 81 | chomp; |
| 82 | if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) { |
| 83 | print DEPENDS "$libdefs{$_}\n"; |
| 84 | } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) { |
| 85 | $libroot = $lib; |
| 86 | $libroot =~ s/lib(.*).a/$1/; |
| 87 | if ($objdefs{$_} ne "$libroot.o") { |
| 88 | print DEPENDS "$objdefs{$_}\n"; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | close UNDEFS; |
| 93 | close DEPENDS; |
| 94 | open DF, "<GenLibDeps.out"; |
| 95 | while (<DF>) { |
| 96 | chomp; |
Reid Spencer | 89f33a1 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 97 | if ($FLAT) { |
| 98 | print " $_"; |
| 99 | } else { |
| 100 | print " <li>$_</li>\n"; |
| 101 | } |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 102 | $suffix = substr($_,length($_)-1,1); |
| 103 | $_ =~ s/(.*)\.[oa]/$1/; |
| 104 | if ($suffix eq "a") { |
| 105 | print DOT "$lib_ns -> $_ [ weight=0 ];\n"; |
| 106 | } else { |
| 107 | print DOT "$lib_ns -> $_ [ weight=10];\n"; |
| 108 | } |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 109 | } |
| 110 | close DF; |
Reid Spencer | 89f33a1 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 111 | if ($FLAT) { |
| 112 | print "\n"; |
| 113 | } else { |
| 114 | print " </ul></dd>\n"; |
| 115 | } |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | # Make sure we flush on write. This is slower but correct based on the way we |
| 119 | # write I/O in gen_one_entry. |
| 120 | $| = 1; |
| 121 | |
| 122 | # Print the definition list tag |
Reid Spencer | 89f33a1 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 123 | if (!$FLAT) { |
| 124 | print "<dl>\n"; |
| 125 | } |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 126 | |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 127 | open DOT, "| $DotPath -Tgif > libdeps.gif"; |
| 128 | |
| 129 | print DOT "digraph LibDeps {size=\"40,15\"; ratio=\"1.33333\"; margin=\"0.25\"; rankdir=\"LR\"; mclimit=\"50.0\"; ordering=\"out\"; center=\"1\";\n"; |
| 130 | print DOT "node [shape=\"box\",color=\"#000088\",fillcolor=\"#FFFACD\",fontcolor=\"#5577DD\",style=\"filled\",fontsize=\"24\"];\n"; |
| 131 | print DOT "edge [style=\"solid\",color=\"#000088\"];\n"; |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 132 | # Print libraries first |
| 133 | foreach $lib (@libs) { |
| 134 | gen_one_entry($lib); |
| 135 | } |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 136 | print DOT "}\n"; |
| 137 | close DOT; |
| 138 | open DOT, "| $DotPath -Tgif > objdeps.gif"; |
| 139 | print DOT "digraph ObjDeps {size=\"40,15\"; ratio=\"1.33333\"; margin=\"0.25\"; rankdir=\"LR\"; mclimit=\"50.0\"; ordering=\"out\"; center=\"1\";\n"; |
| 140 | print DOT "node [shape=\"box\",color=\"#000088\",fillcolor=\"#FFFACD\",fontcolor=\"#5577DD\",style=\"filled\",fontsize=\"24\"];\n"; |
| 141 | print DOT "edge [style=\"solid\",color=\"#000088\"];\n"; |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 142 | |
| 143 | # Print objects second |
| 144 | foreach $obj (@objs) { |
| 145 | gen_one_entry($obj); |
| 146 | } |
| 147 | |
Reid Spencer | e153fb3 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 148 | print DOT "}\n"; |
| 149 | close DOT; |
| 150 | |
Reid Spencer | 271ed74 | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 151 | # Print end tag of definition list element |
Reid Spencer | 89f33a1 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 152 | if (!$FLAT) { |
| 153 | print "</dl>\n"; |
| 154 | } |