blob: 9bb47520235f2c8be907a152e0518fada5f52421 [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 Spencereca6f732006-07-27 23:00:30 +00009# Syntax: GenLibDeps.pl [-flat] <directory_with_libraries_in_it> [path_to_nm_binary]
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];
Reid Spencer227a8422006-08-01 08:09:03 +000027if (!defined($Directory)) {
28 die "First argument must be the directory containing LLVM libs\n";
29}
Reid Spencereca6f732006-07-27 23:00:30 +000030my $nmPath = $ARGV[1];
Reid Spencer271ed742004-12-30 23:07:56 +000031
Reid Spencere153fb32005-01-05 17:29:29 +000032# Find the "dot" program
Reid Spencer11b00232006-05-13 02:48:45 +000033my $DotPath="";
Reid Spencer5e6f97b2006-04-20 23:09:57 +000034if (!$FLAT) {
Reid Spencer11b00232006-05-13 02:48:45 +000035 chomp($DotPath = `which dot`);
Reid Spencer5e6f97b2006-04-20 23:09:57 +000036 die "Can't find 'dot'" if (! -x "$DotPath");
37}
Reid Spencere153fb32005-01-05 17:29:29 +000038
Reid Spencer227a8422006-08-01 08:09:03 +000039if (!defined($nmPath) || $nmPath eq "") {
40 chomp($nmPath=`which nm`);
41 die "Can't find 'nm'" if (! -x "$nmPath");
Reid Spencereca6f732006-07-27 23:00:30 +000042}
Reid Spencer11b00232006-05-13 02:48:45 +000043
Reid Spencer271ed742004-12-30 23:07:56 +000044# Open the directory and read its contents, sorting by name and differentiating
45# by whether its a library (.a) or an object file (.o)
46opendir DIR,$Directory;
47my @files = readdir DIR;
48closedir 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.
54my %libdefs;
55my %objdefs;
56
57# Gather definitions from the libraries
58foreach $lib (@libs ) {
59 open DEFS,
Reid Spencer11b00232006-05-13 02:48:45 +000060 "$nmPath -g $Directory/$lib | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
Reid Spencer271ed742004-12-30 23:07:56 +000061 while (<DEFS>) {
62 chomp($_);
63 $libdefs{$_} = $lib;
64 }
65 close DEFS;
66}
67
68# Gather definitions from the object files.
69foreach $obj (@objs ) {
70 open DEFS,
Reid Spencer11b00232006-05-13 02:48:45 +000071 "$nmPath -g $Directory/$obj | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
Reid Spencer271ed742004-12-30 23:07:56 +000072 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.
82sub gen_one_entry {
83 my $lib = $_[0];
Reid Spencere153fb32005-01-05 17:29:29 +000084 my $lib_ns = $lib;
85 $lib_ns =~ s/(.*)\.[oa]/$1/;
Reid Spencer89f33a12006-03-19 22:08:01 +000086 if ($FLAT) {
87 print "$lib:";
Reid Spencer238caef2006-07-25 19:12:06 +000088 if ($WHY) { print "\n"; }
Reid Spencer89f33a12006-03-19 22:08:01 +000089 } else {
90 print " <dt><b>$lib</b</dt><dd><ul>\n";
91 }
Reid Spencer271ed742004-12-30 23:07:56 +000092 open UNDEFS,
Reid Spencer11b00232006-05-13 02:48:45 +000093 "$nmPath -g -u $Directory/$lib | sed -e 's/^ *U //' | sort | uniq |";
Reid Spencer238caef2006-07-25 19:12:06 +000094 my %DepLibs;
Reid Spencer271ed742004-12-30 23:07:56 +000095 while (<UNDEFS>) {
96 chomp;
Reid Spencer238caef2006-07-25 19:12:06 +000097 my $lib_printed = 0;
Reid Spencer271ed742004-12-30 23:07:56 +000098 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
Reid Spencer238caef2006-07-25 19:12:06 +000099 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
100 push(@{$DepLibs{$libdefs{$_}}}, $_);
Reid Spencer271ed742004-12-30 23:07:56 +0000101 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
102 $libroot = $lib;
103 $libroot =~ s/lib(.*).a/$1/;
104 if ($objdefs{$_} ne "$libroot.o") {
Reid Spencer238caef2006-07-25 19:12:06 +0000105 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
106 push(@{$DepLibs{$objdefs{$_}}}, $_);
Reid Spencer271ed742004-12-30 23:07:56 +0000107 }
108 }
109 }
110 close UNDEFS;
Reid Spencer238caef2006-07-25 19:12:06 +0000111 for my $key (sort keys %DepLibs) {
Reid Spencer89f33a12006-03-19 22:08:01 +0000112 if ($FLAT) {
Reid Spencer238caef2006-07-25 19:12:06 +0000113 print " $key";
114 if ($WHY) {
115 print "\n";
116 my @syms = @{$DepLibs{$key}};
117 foreach $sym (@syms) {
118 print " $sym\n";
119 }
120 }
Reid Spencer89f33a12006-03-19 22:08:01 +0000121 } else {
Reid Spencer238caef2006-07-25 19:12:06 +0000122 print " <li>$key</li>\n";
Reid Spencer89f33a12006-03-19 22:08:01 +0000123 }
Reid Spencer238caef2006-07-25 19:12:06 +0000124 $suffix = substr($key,length($key)-1,1);
125 $key =~ s/(.*)\.[oa]/$1/;
Reid Spencere153fb32005-01-05 17:29:29 +0000126 if ($suffix eq "a") {
Reid Spencer238caef2006-07-25 19:12:06 +0000127 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
Reid Spencere153fb32005-01-05 17:29:29 +0000128 } else {
Reid Spencer238caef2006-07-25 19:12:06 +0000129 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
Reid Spencere153fb32005-01-05 17:29:29 +0000130 }
Reid Spencer271ed742004-12-30 23:07:56 +0000131 }
Reid Spencer89f33a12006-03-19 22:08:01 +0000132 if ($FLAT) {
Reid Spencer238caef2006-07-25 19:12:06 +0000133 if (!$WHY) {
134 print "\n";
135 }
Reid Spencer89f33a12006-03-19 22:08:01 +0000136 } else {
137 print " </ul></dd>\n";
138 }
Reid Spencer271ed742004-12-30 23:07:56 +0000139}
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 Spencer89f33a12006-03-19 22:08:01 +0000146if (!$FLAT) {
Reid Spencer5e6f97b2006-04-20 23:09:57 +0000147 print "<dl>\n";
148
149 open DOT, "| $DotPath -Tgif > libdeps.gif";
150
Reid Spencer227a8422006-08-01 08:09:03 +0000151 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 Spencer89f33a12006-03-19 22:08:01 +0000168}
Reid Spencer271ed742004-12-30 23:07:56 +0000169
170# Print libraries first
171foreach $lib (@libs) {
172 gen_one_entry($lib);
173}
Reid Spencer5e6f97b2006-04-20 23:09:57 +0000174
175if (!$FLAT) {
176 print DOT "}\n";
177 close DOT;
178 open DOT, "| $DotPath -Tgif > objdeps.gif";
Reid Spencer227a8422006-08-01 08:09:03 +0000179 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 Spencer5e6f97b2006-04-20 23:09:57 +0000195}
Reid Spencer271ed742004-12-30 23:07:56 +0000196
197# Print objects second
198foreach $obj (@objs) {
199 gen_one_entry($obj);
200}
201
Reid Spencer5e6f97b2006-04-20 23:09:57 +0000202if (!$FLAT) {
203 print DOT "}\n";
204 close DOT;
Reid Spencere153fb32005-01-05 17:29:29 +0000205
Reid Spencer271ed742004-12-30 23:07:56 +0000206# Print end tag of definition list element
Reid Spencer89f33a12006-03-19 22:08:01 +0000207 print "</dl>\n";
208}