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