blob: b14e5e07d812dbf12b0af507cef23d5d5051f877 [file] [log] [blame]
Reid Spencer271ed742004-12-30 23:07:56 +00001#!/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 Spencerbf98e9f2006-05-03 18:16:01 +00009# Syntax: GenLibDeps.pl [-flat] <directory_with_libraries_in_it>
Reid Spencer271ed742004-12-30 23:07:56 +000010#
11
Reid Spencer89f33a12006-03-19 22:08:01 +000012# Parse arguments...
Reid Spencer238caef2006-07-25 19:12:06 +000013my $FLAT = 0;
14my $WHY = 0;
Reid Spencer89f33a12006-03-19 22:08:01 +000015while (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 Spencer238caef2006-07-25 19:12:06 +000021 if (/^-why/) { $WHY = 1; $FLAT = 1; next; }
Reid Spencer89f33a12006-03-19 22:08:01 +000022 print "Unknown option: $_ : ignoring!\n";
23}
24
Reid Spencer271ed742004-12-30 23:07:56 +000025# Give first option a name.
26my $Directory = $ARGV[0];
27
Reid Spencere153fb32005-01-05 17:29:29 +000028# Find the "dot" program
Reid Spencer11b00232006-05-13 02:48:45 +000029my $DotPath="";
Reid Spencer5e6f97b2006-04-20 23:09:57 +000030if (!$FLAT) {
Reid Spencer11b00232006-05-13 02:48:45 +000031 chomp($DotPath = `which dot`);
Reid Spencer5e6f97b2006-04-20 23:09:57 +000032 die "Can't find 'dot'" if (! -x "$DotPath");
33}
Reid Spencere153fb32005-01-05 17:29:29 +000034
Reid Spencer11b00232006-05-13 02:48:45 +000035chomp(my $nmPath=`which nm`);
36die "Can't find 'nm'" if (! -x "$nmPath");
37
Reid Spencer271ed742004-12-30 23:07:56 +000038# Open the directory and read its contents, sorting by name and differentiating
39# by whether its a library (.a) or an object file (.o)
40opendir DIR,$Directory;
41my @files = readdir DIR;
42closedir DIR;
43@libs = grep(/libLLVM.*\.a$/,sort(@files));
44@objs = grep(/LLVM.*\.o$/,sort(@files));
45
46# Declare the hashes we will use to keep track of the library and object file
47# symbol definitions.
48my %libdefs;
49my %objdefs;
50
51# Gather definitions from the libraries
52foreach $lib (@libs ) {
53 open DEFS,
Reid Spencer11b00232006-05-13 02:48:45 +000054 "$nmPath -g $Directory/$lib | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
Reid Spencer271ed742004-12-30 23:07:56 +000055 while (<DEFS>) {
56 chomp($_);
57 $libdefs{$_} = $lib;
58 }
59 close DEFS;
60}
61
62# Gather definitions from the object files.
63foreach $obj (@objs ) {
64 open DEFS,
Reid Spencer11b00232006-05-13 02:48:45 +000065 "$nmPath -g $Directory/$obj | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
Reid Spencer271ed742004-12-30 23:07:56 +000066 while (<DEFS>) {
67 chomp($_);
68 $objdefs{$_} = $obj;
69 }
70 close DEFS;
71}
72
73# Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
74# for one library or object file. The <dt> provides the name of the library or
75# object. The <dd> provides a list of the libraries/objects it depends on.
76sub gen_one_entry {
77 my $lib = $_[0];
Reid Spencere153fb32005-01-05 17:29:29 +000078 my $lib_ns = $lib;
79 $lib_ns =~ s/(.*)\.[oa]/$1/;
Reid Spencer89f33a12006-03-19 22:08:01 +000080 if ($FLAT) {
81 print "$lib:";
Reid Spencer238caef2006-07-25 19:12:06 +000082 if ($WHY) { print "\n"; }
Reid Spencer89f33a12006-03-19 22:08:01 +000083 } else {
84 print " <dt><b>$lib</b</dt><dd><ul>\n";
85 }
Reid Spencer271ed742004-12-30 23:07:56 +000086 open UNDEFS,
Reid Spencer11b00232006-05-13 02:48:45 +000087 "$nmPath -g -u $Directory/$lib | sed -e 's/^ *U //' | sort | uniq |";
Reid Spencer238caef2006-07-25 19:12:06 +000088 my %DepLibs;
Reid Spencer271ed742004-12-30 23:07:56 +000089 while (<UNDEFS>) {
90 chomp;
Reid Spencer238caef2006-07-25 19:12:06 +000091 my $lib_printed = 0;
Reid Spencer271ed742004-12-30 23:07:56 +000092 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
Reid Spencer238caef2006-07-25 19:12:06 +000093 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
94 push(@{$DepLibs{$libdefs{$_}}}, $_);
Reid Spencer271ed742004-12-30 23:07:56 +000095 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
96 $libroot = $lib;
97 $libroot =~ s/lib(.*).a/$1/;
98 if ($objdefs{$_} ne "$libroot.o") {
Reid Spencer238caef2006-07-25 19:12:06 +000099 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
100 push(@{$DepLibs{$objdefs{$_}}}, $_);
Reid Spencer271ed742004-12-30 23:07:56 +0000101 }
102 }
103 }
104 close UNDEFS;
Reid Spencer238caef2006-07-25 19:12:06 +0000105 for my $key (sort keys %DepLibs) {
Reid Spencer89f33a12006-03-19 22:08:01 +0000106 if ($FLAT) {
Reid Spencer238caef2006-07-25 19:12:06 +0000107 print " $key";
108 if ($WHY) {
109 print "\n";
110 my @syms = @{$DepLibs{$key}};
111 foreach $sym (@syms) {
112 print " $sym\n";
113 }
114 }
Reid Spencer89f33a12006-03-19 22:08:01 +0000115 } else {
Reid Spencer238caef2006-07-25 19:12:06 +0000116 print " <li>$key</li>\n";
Reid Spencer89f33a12006-03-19 22:08:01 +0000117 }
Reid Spencer238caef2006-07-25 19:12:06 +0000118 $suffix = substr($key,length($key)-1,1);
119 $key =~ s/(.*)\.[oa]/$1/;
Reid Spencere153fb32005-01-05 17:29:29 +0000120 if ($suffix eq "a") {
Reid Spencer238caef2006-07-25 19:12:06 +0000121 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
Reid Spencere153fb32005-01-05 17:29:29 +0000122 } else {
Reid Spencer238caef2006-07-25 19:12:06 +0000123 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
Reid Spencere153fb32005-01-05 17:29:29 +0000124 }
Reid Spencer271ed742004-12-30 23:07:56 +0000125 }
Reid Spencer89f33a12006-03-19 22:08:01 +0000126 if ($FLAT) {
Reid Spencer238caef2006-07-25 19:12:06 +0000127 if (!$WHY) {
128 print "\n";
129 }
Reid Spencer89f33a12006-03-19 22:08:01 +0000130 } else {
131 print " </ul></dd>\n";
132 }
Reid Spencer271ed742004-12-30 23:07:56 +0000133}
134
135# Make sure we flush on write. This is slower but correct based on the way we
136# write I/O in gen_one_entry.
137$| = 1;
138
139# Print the definition list tag
Reid Spencer89f33a12006-03-19 22:08:01 +0000140if (!$FLAT) {
Reid Spencer5e6f97b2006-04-20 23:09:57 +0000141 print "<dl>\n";
142
143 open DOT, "| $DotPath -Tgif > libdeps.gif";
144
145 print DOT "digraph LibDeps {size=\"40,15\"; ratio=\"1.33333\"; margin=\"0.25\"; rankdir=\"LR\"; mclimit=\"50.0\"; ordering=\"out\"; center=\"1\";\n";
146 print DOT "node [shape=\"box\",color=\"#000088\",fillcolor=\"#FFFACD\",fontcolor=\"#5577DD\",style=\"filled\",fontsize=\"24\"];\n";
147 print DOT "edge [style=\"solid\",color=\"#000088\"];\n";
Reid Spencer89f33a12006-03-19 22:08:01 +0000148}
Reid Spencer271ed742004-12-30 23:07:56 +0000149
150# Print libraries first
151foreach $lib (@libs) {
152 gen_one_entry($lib);
153}
Reid Spencer5e6f97b2006-04-20 23:09:57 +0000154
155if (!$FLAT) {
156 print DOT "}\n";
157 close DOT;
158 open DOT, "| $DotPath -Tgif > objdeps.gif";
159 print DOT "digraph ObjDeps {size=\"40,15\"; ratio=\"1.33333\"; margin=\"0.25\"; rankdir=\"LR\"; mclimit=\"50.0\"; ordering=\"out\"; center=\"1\";\n";
160 print DOT "node [shape=\"box\",color=\"#000088\",fillcolor=\"#FFFACD\",fontcolor=\"#5577DD\",style=\"filled\",fontsize=\"24\"];\n";
161 print DOT "edge [style=\"solid\",color=\"#000088\"];\n";
162}
Reid Spencer271ed742004-12-30 23:07:56 +0000163
164# Print objects second
165foreach $obj (@objs) {
166 gen_one_entry($obj);
167}
168
Reid Spencer5e6f97b2006-04-20 23:09:57 +0000169if (!$FLAT) {
170 print DOT "}\n";
171 close DOT;
Reid Spencere153fb32005-01-05 17:29:29 +0000172
Reid Spencer271ed742004-12-30 23:07:56 +0000173# Print end tag of definition list element
Reid Spencer89f33a12006-03-19 22:08:01 +0000174 print "</dl>\n";
175}