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