blob: 419d8f92d866d62cf43d074f8d702a8cbfa924f7 [file] [log] [blame]
Reid Spencer579b8de2004-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 Spencer1bc68642006-07-27 23:00:30 +00009# Syntax: GenLibDeps.pl [-flat] <directory_with_libraries_in_it> [path_to_nm_binary]
Reid Spencer579b8de2004-12-30 23:07:56 +000010#
11
Reid Spencer0bf11772006-03-19 22:08:01 +000012# Parse arguments...
Reid Spencerc152efd2006-07-25 19:12:06 +000013my $FLAT = 0;
14my $WHY = 0;
Reid Spencer0bf11772006-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 Spencerc152efd2006-07-25 19:12:06 +000021 if (/^-why/) { $WHY = 1; $FLAT = 1; next; }
Reid Spencer0bf11772006-03-19 22:08:01 +000022 print "Unknown option: $_ : ignoring!\n";
23}
24
Reid Spencer579b8de2004-12-30 23:07:56 +000025# Give first option a name.
26my $Directory = $ARGV[0];
Reid Spencerd0fa46a2006-08-03 19:10:03 +000027if (!defined($Directory) || ! -d "$Directory") {
28 die "First argument must specify the directory containing LLVM libs\n";
Reid Spencer9bf2e7f2006-08-01 08:09:03 +000029}
Reid Spencerd0fa46a2006-08-03 19:10:03 +000030
Reid Spencer1bc68642006-07-27 23:00:30 +000031my $nmPath = $ARGV[1];
Reid Spencer579b8de2004-12-30 23:07:56 +000032
Reid Spencer62345822005-01-05 17:29:29 +000033# Find the "dot" program
Reid Spencerdd73e7f2006-05-13 02:48:45 +000034my $DotPath="";
Reid Spencer44fa6912006-04-20 23:09:57 +000035if (!$FLAT) {
Reid Spencerdd73e7f2006-05-13 02:48:45 +000036 chomp($DotPath = `which dot`);
Reid Spencer44fa6912006-04-20 23:09:57 +000037 die "Can't find 'dot'" if (! -x "$DotPath");
38}
Reid Spencer62345822005-01-05 17:29:29 +000039
Reid Spencer9bf2e7f2006-08-01 08:09:03 +000040if (!defined($nmPath) || $nmPath eq "") {
41 chomp($nmPath=`which nm`);
42 die "Can't find 'nm'" if (! -x "$nmPath");
Reid Spencer1bc68642006-07-27 23:00:30 +000043}
Reid Spencerdd73e7f2006-05-13 02:48:45 +000044
Reid Spencer579b8de2004-12-30 23:07:56 +000045# Open the directory and read its contents, sorting by name and differentiating
46# by whether its a library (.a) or an object file (.o)
47opendir DIR,$Directory;
48my @files = readdir DIR;
49closedir DIR;
50@libs = grep(/libLLVM.*\.a$/,sort(@files));
51@objs = grep(/LLVM.*\.o$/,sort(@files));
52
53# Declare the hashes we will use to keep track of the library and object file
54# symbol definitions.
55my %libdefs;
56my %objdefs;
57
58# Gather definitions from the libraries
59foreach $lib (@libs ) {
60 open DEFS,
Reid Spencerdd73e7f2006-05-13 02:48:45 +000061 "$nmPath -g $Directory/$lib | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
Reid Spencer579b8de2004-12-30 23:07:56 +000062 while (<DEFS>) {
63 chomp($_);
64 $libdefs{$_} = $lib;
65 }
66 close DEFS;
67}
68
69# Gather definitions from the object files.
70foreach $obj (@objs ) {
71 open DEFS,
Reid Spencerdd73e7f2006-05-13 02:48:45 +000072 "$nmPath -g $Directory/$obj | grep ' [ABCDGRST] ' | sed -e 's/^[0-9A-Fa-f]* [ABCDGRST] //' | sort | uniq |";
Reid Spencer579b8de2004-12-30 23:07:56 +000073 while (<DEFS>) {
74 chomp($_);
75 $objdefs{$_} = $obj;
76 }
77 close DEFS;
78}
79
80# Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
81# for one library or object file. The <dt> provides the name of the library or
82# object. The <dd> provides a list of the libraries/objects it depends on.
83sub gen_one_entry {
84 my $lib = $_[0];
Reid Spencer62345822005-01-05 17:29:29 +000085 my $lib_ns = $lib;
86 $lib_ns =~ s/(.*)\.[oa]/$1/;
Reid Spencer0bf11772006-03-19 22:08:01 +000087 if ($FLAT) {
88 print "$lib:";
Reid Spencerc152efd2006-07-25 19:12:06 +000089 if ($WHY) { print "\n"; }
Reid Spencer0bf11772006-03-19 22:08:01 +000090 } else {
91 print " <dt><b>$lib</b</dt><dd><ul>\n";
92 }
Reid Spencer579b8de2004-12-30 23:07:56 +000093 open UNDEFS,
Reid Spencerdd73e7f2006-05-13 02:48:45 +000094 "$nmPath -g -u $Directory/$lib | sed -e 's/^ *U //' | sort | uniq |";
Reid Spencerc152efd2006-07-25 19:12:06 +000095 my %DepLibs;
Reid Spencer579b8de2004-12-30 23:07:56 +000096 while (<UNDEFS>) {
97 chomp;
Reid Spencerc152efd2006-07-25 19:12:06 +000098 my $lib_printed = 0;
Reid Spencer579b8de2004-12-30 23:07:56 +000099 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000100 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
101 push(@{$DepLibs{$libdefs{$_}}}, $_);
Reid Spencer579b8de2004-12-30 23:07:56 +0000102 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
103 $libroot = $lib;
104 $libroot =~ s/lib(.*).a/$1/;
105 if ($objdefs{$_} ne "$libroot.o") {
Reid Spencerc152efd2006-07-25 19:12:06 +0000106 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
107 push(@{$DepLibs{$objdefs{$_}}}, $_);
Reid Spencer579b8de2004-12-30 23:07:56 +0000108 }
109 }
110 }
111 close UNDEFS;
Reid Spencerc152efd2006-07-25 19:12:06 +0000112 for my $key (sort keys %DepLibs) {
Reid Spencer0bf11772006-03-19 22:08:01 +0000113 if ($FLAT) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000114 print " $key";
115 if ($WHY) {
116 print "\n";
117 my @syms = @{$DepLibs{$key}};
118 foreach $sym (@syms) {
119 print " $sym\n";
120 }
121 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000122 } else {
Reid Spencerc152efd2006-07-25 19:12:06 +0000123 print " <li>$key</li>\n";
Reid Spencer0bf11772006-03-19 22:08:01 +0000124 }
Reid Spencerc152efd2006-07-25 19:12:06 +0000125 $suffix = substr($key,length($key)-1,1);
126 $key =~ s/(.*)\.[oa]/$1/;
Reid Spencer62345822005-01-05 17:29:29 +0000127 if ($suffix eq "a") {
Reid Spencerc152efd2006-07-25 19:12:06 +0000128 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
Reid Spencer62345822005-01-05 17:29:29 +0000129 } else {
Reid Spencerc152efd2006-07-25 19:12:06 +0000130 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
Reid Spencer62345822005-01-05 17:29:29 +0000131 }
Reid Spencer579b8de2004-12-30 23:07:56 +0000132 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000133 if ($FLAT) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000134 if (!$WHY) {
135 print "\n";
136 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000137 } else {
138 print " </ul></dd>\n";
139 }
Reid Spencer579b8de2004-12-30 23:07:56 +0000140}
141
142# Make sure we flush on write. This is slower but correct based on the way we
143# write I/O in gen_one_entry.
144$| = 1;
145
146# Print the definition list tag
Reid Spencer0bf11772006-03-19 22:08:01 +0000147if (!$FLAT) {
Reid Spencer44fa6912006-04-20 23:09:57 +0000148 print "<dl>\n";
149
150 open DOT, "| $DotPath -Tgif > libdeps.gif";
151
Reid Spencer9bf2e7f2006-08-01 08:09:03 +0000152 print DOT "digraph LibDeps {\n";
153 print DOT " size=\"40,15\"; \n";
154 print DOT " ratio=\"1.33333\"; \n";
155 print DOT " margin=\"0.25\"; \n";
156 print DOT " rankdir=\"LR\"; \n";
157 print DOT " mclimit=\"50.0\"; \n";
158 print DOT " ordering=\"out\"; \n";
159 print DOT " center=\"1\";\n";
160 print DOT "node [shape=\"box\",\n";
161 print DOT " color=\"#000088\",\n";
162 print DOT " fillcolor=\"#FFFACD\",\n";
163 print DOT " fontcolor=\"#3355BB\",\n";
164 print DOT " style=\"filled\",\n";
165 print DOT " fontname=\"sans\",\n";
166 print DOT " fontsize=\"24\"\n";
167 print DOT "];\n";
168 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
Reid Spencer0bf11772006-03-19 22:08:01 +0000169}
Reid Spencer579b8de2004-12-30 23:07:56 +0000170
171# Print libraries first
172foreach $lib (@libs) {
173 gen_one_entry($lib);
174}
Reid Spencer44fa6912006-04-20 23:09:57 +0000175
176if (!$FLAT) {
177 print DOT "}\n";
178 close DOT;
179 open DOT, "| $DotPath -Tgif > objdeps.gif";
Reid Spencer9bf2e7f2006-08-01 08:09:03 +0000180 print DOT "digraph ObjDeps {\n";
181 print DOT " size=\"8,10\";\n";
182 print DOT " margin=\"0.25\";\n";
183 print DOT " rankdir=\"LR\";\n";
184 print DOT " mclimit=\"50.0\";\n";
185 print DOT " ordering=\"out\";\n";
186 print DOT " center=\"1\";\n";
187 print DOT "node [shape=\"box\",\n";
188 print DOT " color=\"#000088\",\n";
189 print DOT " fillcolor=\"#FFFACD\",\n";
190 print DOT " fontcolor=\"#3355BB\",\n";
191 print DOT " fontname=\"sans\",\n";
192 print DOT " style=\"filled\",\n";
193 print DOT " fontsize=\"24\"\n";
194 print DOT "];\n";
195 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
Reid Spencer44fa6912006-04-20 23:09:57 +0000196}
Reid Spencer579b8de2004-12-30 23:07:56 +0000197
198# Print objects second
199foreach $obj (@objs) {
200 gen_one_entry($obj);
201}
202
Reid Spencer44fa6912006-04-20 23:09:57 +0000203if (!$FLAT) {
204 print DOT "}\n";
205 close DOT;
Reid Spencer62345822005-01-05 17:29:29 +0000206
Reid Spencer579b8de2004-12-30 23:07:56 +0000207# Print end tag of definition list element
Reid Spencer0bf11772006-03-19 22:08:01 +0000208 print "</dl>\n";
209}