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