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 | 1bc6864 | 2006-07-27 23:00:30 +0000 | [diff] [blame] | 9 | # Syntax: GenLibDeps.pl [-flat] <directory_with_libraries_in_it> [path_to_nm_binary] |
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... |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 13 | my $FLAT = 0; |
| 14 | my $WHY = 0; |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 15 | while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) { |
| 16 | shift; |
| 17 | last if /^--$/; # Stop processing arguments on -- |
| 18 | |
| 19 | # List command line options here... |
| 20 | if (/^-flat$/) { $FLAT = 1; next; } |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 21 | if (/^-why/) { $WHY = 1; $FLAT = 1; next; } |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 22 | print "Unknown option: $_ : ignoring!\n"; |
| 23 | } |
| 24 | |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 25 | # Give first option a name. |
| 26 | my $Directory = $ARGV[0]; |
Reid Spencer | d0fa46a | 2006-08-03 19:10:03 +0000 | [diff] [blame] | 27 | if (!defined($Directory) || ! -d "$Directory") { |
| 28 | die "First argument must specify the directory containing LLVM libs\n"; |
Reid Spencer | 9bf2e7f | 2006-08-01 08:09:03 +0000 | [diff] [blame] | 29 | } |
Reid Spencer | d0fa46a | 2006-08-03 19:10:03 +0000 | [diff] [blame] | 30 | |
Reid Spencer | 1bc6864 | 2006-07-27 23:00:30 +0000 | [diff] [blame] | 31 | my $nmPath = $ARGV[1]; |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 32 | |
Reid Spencer | 6234582 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 33 | # Find the "dot" program |
Reid Spencer | dd73e7f | 2006-05-13 02:48:45 +0000 | [diff] [blame] | 34 | my $DotPath=""; |
Reid Spencer | 44fa691 | 2006-04-20 23:09:57 +0000 | [diff] [blame] | 35 | if (!$FLAT) { |
Reid Spencer | dd73e7f | 2006-05-13 02:48:45 +0000 | [diff] [blame] | 36 | chomp($DotPath = `which dot`); |
Reid Spencer | 44fa691 | 2006-04-20 23:09:57 +0000 | [diff] [blame] | 37 | die "Can't find 'dot'" if (! -x "$DotPath"); |
| 38 | } |
Reid Spencer | 6234582 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 39 | |
Reid Spencer | 9bf2e7f | 2006-08-01 08:09:03 +0000 | [diff] [blame] | 40 | if (!defined($nmPath) || $nmPath eq "") { |
| 41 | chomp($nmPath=`which nm`); |
| 42 | die "Can't find 'nm'" if (! -x "$nmPath"); |
Reid Spencer | 1bc6864 | 2006-07-27 23:00:30 +0000 | [diff] [blame] | 43 | } |
Reid Spencer | dd73e7f | 2006-05-13 02:48:45 +0000 | [diff] [blame] | 44 | |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 45 | # Open the directory and read its contents, sorting by name and differentiating |
| 46 | # by whether its a library (.a) or an object file (.o) |
| 47 | opendir DIR,$Directory; |
| 48 | my @files = readdir DIR; |
| 49 | closedir DIR; |
| 50 | @libs = grep(/libLLVM.*\.a$/,sort(@files)); |
| 51 | @objs = grep(/LLVM.*\.o$/,sort(@files)); |
| 52 | |
| 53 | # Declare the hashes we will use to keep track of the library and object file |
| 54 | # symbol definitions. |
| 55 | my %libdefs; |
| 56 | my %objdefs; |
| 57 | |
| 58 | # Gather definitions from the libraries |
| 59 | foreach $lib (@libs ) { |
| 60 | open DEFS, |
Reid Spencer | dd73e7f | 2006-05-13 02:48:45 +0000 | [diff] [blame] | 61 | "$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] | 62 | while (<DEFS>) { |
| 63 | chomp($_); |
| 64 | $libdefs{$_} = $lib; |
| 65 | } |
| 66 | close DEFS; |
| 67 | } |
| 68 | |
| 69 | # Gather definitions from the object files. |
| 70 | foreach $obj (@objs ) { |
| 71 | open DEFS, |
Reid Spencer | dd73e7f | 2006-05-13 02:48:45 +0000 | [diff] [blame] | 72 | "$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] | 73 | while (<DEFS>) { |
| 74 | chomp($_); |
| 75 | $objdefs{$_} = $obj; |
| 76 | } |
| 77 | close DEFS; |
| 78 | } |
| 79 | |
| 80 | # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements |
| 81 | # for one library or object file. The <dt> provides the name of the library or |
| 82 | # object. The <dd> provides a list of the libraries/objects it depends on. |
| 83 | sub gen_one_entry { |
| 84 | my $lib = $_[0]; |
Reid Spencer | 6234582 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 85 | my $lib_ns = $lib; |
| 86 | $lib_ns =~ s/(.*)\.[oa]/$1/; |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 87 | if ($FLAT) { |
| 88 | print "$lib:"; |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 89 | if ($WHY) { print "\n"; } |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 90 | } else { |
| 91 | print " <dt><b>$lib</b</dt><dd><ul>\n"; |
| 92 | } |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 93 | open UNDEFS, |
Reid Spencer | dd73e7f | 2006-05-13 02:48:45 +0000 | [diff] [blame] | 94 | "$nmPath -g -u $Directory/$lib | sed -e 's/^ *U //' | sort | uniq |"; |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 95 | my %DepLibs; |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 96 | while (<UNDEFS>) { |
| 97 | chomp; |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 98 | my $lib_printed = 0; |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 99 | if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) { |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 100 | $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}}; |
| 101 | push(@{$DepLibs{$libdefs{$_}}}, $_); |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 102 | } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) { |
| 103 | $libroot = $lib; |
| 104 | $libroot =~ s/lib(.*).a/$1/; |
| 105 | if ($objdefs{$_} ne "$libroot.o") { |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 106 | $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}}; |
| 107 | push(@{$DepLibs{$objdefs{$_}}}, $_); |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | } |
| 111 | close UNDEFS; |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 112 | for my $key (sort keys %DepLibs) { |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 113 | if ($FLAT) { |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 114 | print " $key"; |
| 115 | if ($WHY) { |
| 116 | print "\n"; |
| 117 | my @syms = @{$DepLibs{$key}}; |
| 118 | foreach $sym (@syms) { |
| 119 | print " $sym\n"; |
| 120 | } |
| 121 | } |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 122 | } else { |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 123 | print " <li>$key</li>\n"; |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 124 | } |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 125 | $suffix = substr($key,length($key)-1,1); |
| 126 | $key =~ s/(.*)\.[oa]/$1/; |
Reid Spencer | 6234582 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 127 | if ($suffix eq "a") { |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 128 | if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" }; |
Reid Spencer | 6234582 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 129 | } else { |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 130 | if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" }; |
Reid Spencer | 6234582 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 131 | } |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 132 | } |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 133 | if ($FLAT) { |
Reid Spencer | c152efd | 2006-07-25 19:12:06 +0000 | [diff] [blame] | 134 | if (!$WHY) { |
| 135 | print "\n"; |
| 136 | } |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 137 | } else { |
| 138 | print " </ul></dd>\n"; |
| 139 | } |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | # Make sure we flush on write. This is slower but correct based on the way we |
| 143 | # write I/O in gen_one_entry. |
| 144 | $| = 1; |
| 145 | |
| 146 | # Print the definition list tag |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 147 | if (!$FLAT) { |
Reid Spencer | 44fa691 | 2006-04-20 23:09:57 +0000 | [diff] [blame] | 148 | print "<dl>\n"; |
| 149 | |
| 150 | open DOT, "| $DotPath -Tgif > libdeps.gif"; |
| 151 | |
Reid Spencer | 9bf2e7f | 2006-08-01 08:09:03 +0000 | [diff] [blame] | 152 | print DOT "digraph LibDeps {\n"; |
| 153 | print DOT " size=\"40,15\"; \n"; |
| 154 | print DOT " ratio=\"1.33333\"; \n"; |
| 155 | print DOT " margin=\"0.25\"; \n"; |
| 156 | print DOT " rankdir=\"LR\"; \n"; |
| 157 | print DOT " mclimit=\"50.0\"; \n"; |
| 158 | print DOT " ordering=\"out\"; \n"; |
| 159 | print DOT " center=\"1\";\n"; |
| 160 | print DOT "node [shape=\"box\",\n"; |
| 161 | print DOT " color=\"#000088\",\n"; |
| 162 | print DOT " fillcolor=\"#FFFACD\",\n"; |
| 163 | print DOT " fontcolor=\"#3355BB\",\n"; |
| 164 | print DOT " style=\"filled\",\n"; |
| 165 | print DOT " fontname=\"sans\",\n"; |
| 166 | print DOT " fontsize=\"24\"\n"; |
| 167 | print DOT "];\n"; |
| 168 | print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n"; |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 169 | } |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 170 | |
| 171 | # Print libraries first |
| 172 | foreach $lib (@libs) { |
| 173 | gen_one_entry($lib); |
| 174 | } |
Reid Spencer | 44fa691 | 2006-04-20 23:09:57 +0000 | [diff] [blame] | 175 | |
| 176 | if (!$FLAT) { |
| 177 | print DOT "}\n"; |
| 178 | close DOT; |
| 179 | open DOT, "| $DotPath -Tgif > objdeps.gif"; |
Reid Spencer | 9bf2e7f | 2006-08-01 08:09:03 +0000 | [diff] [blame] | 180 | print DOT "digraph ObjDeps {\n"; |
| 181 | print DOT " size=\"8,10\";\n"; |
| 182 | print DOT " margin=\"0.25\";\n"; |
| 183 | print DOT " rankdir=\"LR\";\n"; |
| 184 | print DOT " mclimit=\"50.0\";\n"; |
| 185 | print DOT " ordering=\"out\";\n"; |
| 186 | print DOT " center=\"1\";\n"; |
| 187 | print DOT "node [shape=\"box\",\n"; |
| 188 | print DOT " color=\"#000088\",\n"; |
| 189 | print DOT " fillcolor=\"#FFFACD\",\n"; |
| 190 | print DOT " fontcolor=\"#3355BB\",\n"; |
| 191 | print DOT " fontname=\"sans\",\n"; |
| 192 | print DOT " style=\"filled\",\n"; |
| 193 | print DOT " fontsize=\"24\"\n"; |
| 194 | print DOT "];\n"; |
| 195 | print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n"; |
Reid Spencer | 44fa691 | 2006-04-20 23:09:57 +0000 | [diff] [blame] | 196 | } |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 197 | |
| 198 | # Print objects second |
| 199 | foreach $obj (@objs) { |
| 200 | gen_one_entry($obj); |
| 201 | } |
| 202 | |
Reid Spencer | 44fa691 | 2006-04-20 23:09:57 +0000 | [diff] [blame] | 203 | if (!$FLAT) { |
| 204 | print DOT "}\n"; |
| 205 | close DOT; |
Reid Spencer | 6234582 | 2005-01-05 17:29:29 +0000 | [diff] [blame] | 206 | |
Reid Spencer | 579b8de | 2004-12-30 23:07:56 +0000 | [diff] [blame] | 207 | # Print end tag of definition list element |
Reid Spencer | 0bf1177 | 2006-03-19 22:08:01 +0000 | [diff] [blame] | 208 | print "</dl>\n"; |
| 209 | } |