blob: 74eedd3383bcac469dd2252a04e9de6a9d073bc9 [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#
Ted Kremenek8330b0e2007-11-27 19:31:11 +000011use strict;
Reid Spencer579b8de2004-12-30 23:07:56 +000012
Reid Spencer0bf11772006-03-19 22:08:01 +000013# Parse arguments...
Reid Spencerc152efd2006-07-25 19:12:06 +000014my $FLAT = 0;
15my $WHY = 0;
Reid Spencer0bf11772006-03-19 22:08:01 +000016while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
17 shift;
18 last if /^--$/; # Stop processing arguments on --
19
20 # List command line options here...
21 if (/^-flat$/) { $FLAT = 1; next; }
Reid Spencerc152efd2006-07-25 19:12:06 +000022 if (/^-why/) { $WHY = 1; $FLAT = 1; next; }
Reid Spencer0bf11772006-03-19 22:08:01 +000023 print "Unknown option: $_ : ignoring!\n";
24}
25
Reid Spencer579b8de2004-12-30 23:07:56 +000026# Give first option a name.
27my $Directory = $ARGV[0];
Reid Spencerd0fa46a2006-08-03 19:10:03 +000028if (!defined($Directory) || ! -d "$Directory") {
29 die "First argument must specify the directory containing LLVM libs\n";
Reid Spencer9bf2e7f2006-08-01 08:09:03 +000030}
Reid Spencerd0fa46a2006-08-03 19:10:03 +000031
Reid Spencer1bc68642006-07-27 23:00:30 +000032my $nmPath = $ARGV[1];
Reid Spencer579b8de2004-12-30 23:07:56 +000033
Reid Spencer62345822005-01-05 17:29:29 +000034# Find the "dot" program
Reid Spencerdd73e7f2006-05-13 02:48:45 +000035my $DotPath="";
Reid Spencer44fa6912006-04-20 23:09:57 +000036if (!$FLAT) {
Reid Spencerdd73e7f2006-05-13 02:48:45 +000037 chomp($DotPath = `which dot`);
Reid Spencer44fa6912006-04-20 23:09:57 +000038 die "Can't find 'dot'" if (! -x "$DotPath");
39}
Reid Spencer62345822005-01-05 17:29:29 +000040
Reid Spencer9bf2e7f2006-08-01 08:09:03 +000041if (!defined($nmPath) || $nmPath eq "") {
42 chomp($nmPath=`which nm`);
43 die "Can't find 'nm'" if (! -x "$nmPath");
Reid Spencer1bc68642006-07-27 23:00:30 +000044}
Reid Spencerdd73e7f2006-05-13 02:48:45 +000045
Reid Spencer579b8de2004-12-30 23:07:56 +000046# Open the directory and read its contents, sorting by name and differentiating
47# by whether its a library (.a) or an object file (.o)
48opendir DIR,$Directory;
49my @files = readdir DIR;
50closedir DIR;
Ted Kremenek8330b0e2007-11-27 19:31:11 +000051my @libs = grep(/libLLVM.*\.a$/,sort(@files));
52my @objs = grep(/LLVM.*\.o$/,sort(@files));
Reid Spencer579b8de2004-12-30 23:07:56 +000053
54# Declare the hashes we will use to keep track of the library and object file
55# symbol definitions.
56my %libdefs;
57my %objdefs;
58
59# Gather definitions from the libraries
Ted Kremenek8330b0e2007-11-27 19:31:11 +000060foreach my $lib (@libs ) {
61 open DEFS, "$nmPath -g $Directory/$lib|";
Reid Spencer579b8de2004-12-30 23:07:56 +000062 while (<DEFS>) {
Ted Kremenek8330b0e2007-11-27 19:31:11 +000063 next if (! / [ABCDGRST] /);
64 s/^[^ ]* [ABCDGRST] //;
Ted Kremenekec9e7162007-12-24 08:04:39 +000065 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
66 # this strips both LF and CRLF.
Reid Spencer579b8de2004-12-30 23:07:56 +000067 $libdefs{$_} = $lib;
68 }
69 close DEFS;
70}
71
72# Gather definitions from the object files.
Ted Kremenek8330b0e2007-11-27 19:31:11 +000073foreach my $obj (@objs ) {
74 open DEFS, "$nmPath -g $Directory/$obj |";
Reid Spencer579b8de2004-12-30 23:07:56 +000075 while (<DEFS>) {
Ted Kremenek8330b0e2007-11-27 19:31:11 +000076 next if (! / [ABCDGRST] /);
77 s/^[^ ]* [ABCDGRST] //;
Ted Kremenekec9e7162007-12-24 08:04:39 +000078 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
79 # this strips both LF and CRLF.
Reid Spencer579b8de2004-12-30 23:07:56 +000080 $objdefs{$_} = $obj;
81 }
82 close DEFS;
83}
84
85# Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
86# for one library or object file. The <dt> provides the name of the library or
87# object. The <dd> provides a list of the libraries/objects it depends on.
88sub gen_one_entry {
89 my $lib = $_[0];
Reid Spencer62345822005-01-05 17:29:29 +000090 my $lib_ns = $lib;
91 $lib_ns =~ s/(.*)\.[oa]/$1/;
Reid Spencer0bf11772006-03-19 22:08:01 +000092 if ($FLAT) {
93 print "$lib:";
Reid Spencerc152efd2006-07-25 19:12:06 +000094 if ($WHY) { print "\n"; }
Reid Spencer0bf11772006-03-19 22:08:01 +000095 } else {
96 print " <dt><b>$lib</b</dt><dd><ul>\n";
97 }
Reid Spencer579b8de2004-12-30 23:07:56 +000098 open UNDEFS,
Reid Spencerdd73e7f2006-05-13 02:48:45 +000099 "$nmPath -g -u $Directory/$lib | sed -e 's/^ *U //' | sort | uniq |";
Reid Spencerc152efd2006-07-25 19:12:06 +0000100 my %DepLibs;
Reid Spencer579b8de2004-12-30 23:07:56 +0000101 while (<UNDEFS>) {
102 chomp;
Reid Spencerc152efd2006-07-25 19:12:06 +0000103 my $lib_printed = 0;
Reid Spencer579b8de2004-12-30 23:07:56 +0000104 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000105 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
106 push(@{$DepLibs{$libdefs{$_}}}, $_);
Reid Spencer579b8de2004-12-30 23:07:56 +0000107 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000108 my $libroot = $lib;
Reid Spencer579b8de2004-12-30 23:07:56 +0000109 $libroot =~ s/lib(.*).a/$1/;
110 if ($objdefs{$_} ne "$libroot.o") {
Reid Spencerc152efd2006-07-25 19:12:06 +0000111 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
112 push(@{$DepLibs{$objdefs{$_}}}, $_);
Reid Spencer579b8de2004-12-30 23:07:56 +0000113 }
114 }
115 }
116 close UNDEFS;
Reid Spencerc152efd2006-07-25 19:12:06 +0000117 for my $key (sort keys %DepLibs) {
Reid Spencer0bf11772006-03-19 22:08:01 +0000118 if ($FLAT) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000119 print " $key";
120 if ($WHY) {
121 print "\n";
122 my @syms = @{$DepLibs{$key}};
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000123 foreach my $sym (@syms) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000124 print " $sym\n";
125 }
126 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000127 } else {
Reid Spencerc152efd2006-07-25 19:12:06 +0000128 print " <li>$key</li>\n";
Reid Spencer0bf11772006-03-19 22:08:01 +0000129 }
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000130 my $suffix = substr($key,length($key)-1,1);
Reid Spencerc152efd2006-07-25 19:12:06 +0000131 $key =~ s/(.*)\.[oa]/$1/;
Reid Spencer62345822005-01-05 17:29:29 +0000132 if ($suffix eq "a") {
Reid Spencerc152efd2006-07-25 19:12:06 +0000133 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
Reid Spencer62345822005-01-05 17:29:29 +0000134 } else {
Reid Spencerc152efd2006-07-25 19:12:06 +0000135 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
Reid Spencer62345822005-01-05 17:29:29 +0000136 }
Reid Spencer579b8de2004-12-30 23:07:56 +0000137 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000138 if ($FLAT) {
Reid Spencerc152efd2006-07-25 19:12:06 +0000139 if (!$WHY) {
140 print "\n";
141 }
Reid Spencer0bf11772006-03-19 22:08:01 +0000142 } else {
143 print " </ul></dd>\n";
144 }
Reid Spencer579b8de2004-12-30 23:07:56 +0000145}
146
147# Make sure we flush on write. This is slower but correct based on the way we
148# write I/O in gen_one_entry.
149$| = 1;
150
151# Print the definition list tag
Reid Spencer0bf11772006-03-19 22:08:01 +0000152if (!$FLAT) {
Reid Spencer44fa6912006-04-20 23:09:57 +0000153 print "<dl>\n";
154
155 open DOT, "| $DotPath -Tgif > libdeps.gif";
156
Reid Spencer9bf2e7f2006-08-01 08:09:03 +0000157 print DOT "digraph LibDeps {\n";
158 print DOT " size=\"40,15\"; \n";
159 print DOT " ratio=\"1.33333\"; \n";
160 print DOT " margin=\"0.25\"; \n";
161 print DOT " rankdir=\"LR\"; \n";
162 print DOT " mclimit=\"50.0\"; \n";
163 print DOT " ordering=\"out\"; \n";
164 print DOT " center=\"1\";\n";
165 print DOT "node [shape=\"box\",\n";
166 print DOT " color=\"#000088\",\n";
167 print DOT " fillcolor=\"#FFFACD\",\n";
168 print DOT " fontcolor=\"#3355BB\",\n";
169 print DOT " style=\"filled\",\n";
170 print DOT " fontname=\"sans\",\n";
171 print DOT " fontsize=\"24\"\n";
172 print DOT "];\n";
173 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
Reid Spencer0bf11772006-03-19 22:08:01 +0000174}
Reid Spencer579b8de2004-12-30 23:07:56 +0000175
176# Print libraries first
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000177foreach my $lib (@libs) {
Reid Spencer579b8de2004-12-30 23:07:56 +0000178 gen_one_entry($lib);
179}
Reid Spencer44fa6912006-04-20 23:09:57 +0000180
181if (!$FLAT) {
182 print DOT "}\n";
183 close DOT;
184 open DOT, "| $DotPath -Tgif > objdeps.gif";
Reid Spencer9bf2e7f2006-08-01 08:09:03 +0000185 print DOT "digraph ObjDeps {\n";
186 print DOT " size=\"8,10\";\n";
187 print DOT " margin=\"0.25\";\n";
188 print DOT " rankdir=\"LR\";\n";
189 print DOT " mclimit=\"50.0\";\n";
190 print DOT " ordering=\"out\";\n";
191 print DOT " center=\"1\";\n";
192 print DOT "node [shape=\"box\",\n";
193 print DOT " color=\"#000088\",\n";
194 print DOT " fillcolor=\"#FFFACD\",\n";
195 print DOT " fontcolor=\"#3355BB\",\n";
196 print DOT " fontname=\"sans\",\n";
197 print DOT " style=\"filled\",\n";
198 print DOT " fontsize=\"24\"\n";
199 print DOT "];\n";
200 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
Reid Spencer44fa6912006-04-20 23:09:57 +0000201}
Reid Spencer579b8de2004-12-30 23:07:56 +0000202
203# Print objects second
Ted Kremenek8330b0e2007-11-27 19:31:11 +0000204foreach my $obj (@objs) {
Reid Spencer579b8de2004-12-30 23:07:56 +0000205 gen_one_entry($obj);
206}
207
Reid Spencer44fa6912006-04-20 23:09:57 +0000208if (!$FLAT) {
209 print DOT "}\n";
210 close DOT;
Reid Spencer62345822005-01-05 17:29:29 +0000211
Reid Spencer579b8de2004-12-30 23:07:56 +0000212# Print end tag of definition list element
Reid Spencer0bf11772006-03-19 22:08:01 +0000213 print "</dl>\n";
214}