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