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