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