3 # Program: GenLibDeps.pl
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.
9 # Syntax: GenLibDeps.pl [-flat] <directory_with_libraries_in_it> [path_to_nm_binary]
18 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
20 last if /^--$/; # Stop processing arguments on --
22 # List command line options here...
23 if (/^-flat$/) { $FLAT = 1; next; }
24 if (/^-why/) { $WHY = 1; $FLAT = 1; next; }
25 if (/^-perobj$/) { $PEROBJ = 1; next; }
26 if (/^-perobjincl/) { $PEROBJINCL = 1; next;}
27 print "Unknown option: $_ : ignoring!\n";
30 # Give first option a name.
31 my $Directory = $ARGV[0];
32 if (!defined($Directory) || ! -d
"$Directory") {
33 die "First argument must specify the directory containing LLVM libs\n";
36 my $nmPath = $ARGV[1];
38 # Find the "dot" program
41 chomp($DotPath = `which dot`);
42 die "Can't find 'dot'" if (! -x
"$DotPath");
45 if (defined($ENV{NM
})) {
46 chomp($nmPath=$ENV{NM
});
49 if (!defined($nmPath) || $nmPath eq "") {
50 chomp($nmPath=`which nm`);
51 die "Can't find 'nm'" if (! -x
"$nmPath");
56 $ranlibPath = $ARGV[2];
57 if (defined($ENV{RANLIB
})) {
58 chomp($ranlibPath=$ENV{RANLIB
});
61 if (!defined($ranlibPath) || $ranlibPath eq "") {
62 chomp($ranlibPath=`which ranlib`);
63 die "Can't find 'ranlib'" if (! -x
"$ranlibPath");
67 # Open the directory and read its contents, sorting by name and differentiating
68 # by whether its a library (.a) or an object file (.o)
69 opendir DIR
,$Directory;
70 my @files = readdir DIR
;
72 my @libs = grep(/libLLVM.*\.(dylib|so|a)$/,sort(@files));
73 # Omit the all-of-llvm shared library.
74 @libs = grep(!/libLLVM-\d\.\d(svn)?\.(dylib|so)/, @libs);
75 my @objs = grep(/LLVM.*\.o$/,sort(@files));
77 # Declare the hashes we will use to keep track of the library and object file
84 # Gather library definitions at object file granularity (optional)
86 foreach my $lib (@libs ) {
87 `$ranlibPath $Directory/$lib`;
89 $libpath =~ s/^libLLVM(.*)\.a/$1/;
90 $libpath =~ s/(.+)CodeGen$/Target\/$1/;
91 $libpath =~ s/(.+)AsmPrinter$/Target\/$1\
/AsmPrinter/;
92 $libpath =~ s/(.+)AsmParser$/Target\/$1\
/AsmParser/;
93 $libpath =~ s/(.+)Info$/Target\/$1\
/TargetInfo/;
94 $libpath =~ s/(.+)Disassembler$/Target\/$1\
/Disassembler/;
95 $libpath =~ s/SelectionDAG/CodeGen\/SelectionDAG
/;
96 $libpath =~ s/^AsmPrinter/CodeGen\/AsmPrinter
/;
97 $libpath =~ s/^BitReader/Bitcode\/Reader
/;
98 $libpath =~ s/^BitWriter/Bitcode\/Writer
/;
99 $libpath =~ s/^CBackend/Target\/CBackend
/;
100 $libpath =~ s/^CppBackend/Target\/CppBackend
/;
101 $libpath =~ s/^MSIL/Target\/MSIL
/;
102 $libpath =~ s/^Core/VMCore/;
103 $libpath =~ s/^Instrumentation/Transforms\/Instrumentation
/;
104 $libpath =~ s/^Interpreter/ExecutionEngine\/Interpreter
/;
105 $libpath =~ s/^JIT/ExecutionEngine\/JIT
/;
106 $libpath =~ s/^ScalarOpts/Transforms\/Scalar
/;
107 $libpath =~ s/^TransformUtils/Transforms\/Utils
/;
108 $libpath =~ s/^ipa/Analysis\/IPA
/;
109 $libpath =~ s/^ipo/Transforms\/IPO
/;
110 $libpath = "lib/".$libpath."/";
111 open DEFS
, "$nmPath -sg $Directory/$lib|";
114 if (/^([^ ]*) in ([^ ]*)/) {
115 my $objfile = $libpath.$2;
116 $objdefs{$1} = $objfile;
117 $objdeps{$objfile} = {};
118 $libobjs{$lib}{$objfile}=1;
119 # my $p = "../llvm/".$objfile;
120 # $p =~ s/Support\/reg(.*).o/Support\/reg$1.c/;
127 close DEFS
or die "nm failed";
129 foreach my $lib (@libs ) {
131 $libpath =~ s/^libLLVM(.*)\.a/$1/;
132 $libpath =~ s/(.+)CodeGen$/Target\/$1/;
133 $libpath =~ s/(.+)AsmPrinter$/Target\/$1\
/AsmPrinter/;
134 $libpath =~ s/(.+)AsmParser$/Target\/$1\
/AsmParser/;
135 $libpath =~ s/(.+)Info$/Target\/$1\
/TargetInfo/;
136 $libpath =~ s/(.+)Disassembler$/Target\/$1\
/Disassembler/;
137 $libpath =~ s/SelectionDAG/CodeGen\/SelectionDAG
/;
138 $libpath =~ s/^AsmPrinter/CodeGen\/AsmPrinter
/;
139 $libpath =~ s/^BitReader/Bitcode\/Reader
/;
140 $libpath =~ s/^BitWriter/Bitcode\/Writer
/;
141 $libpath =~ s/^CBackend/Target\/CBackend
/;
142 $libpath =~ s/^CppBackend/Target\/CppBackend
/;
143 $libpath =~ s/^MSIL/Target\/MSIL
/;
144 $libpath =~ s/^Core/VMCore/;
145 $libpath =~ s/^Instrumentation/Transforms\/Instrumentation
/;
146 $libpath =~ s/^Interpreter/ExecutionEngine\/Interpreter
/;
147 $libpath =~ s/^JIT/ExecutionEngine\/JIT
/;
148 $libpath =~ s/^ScalarOpts/Transforms\/Scalar
/;
149 $libpath =~ s/^TransformUtils/Transforms\/Utils
/;
150 $libpath =~ s/^ipa/Analysis\/IPA
/;
151 $libpath =~ s/^ipo/Transforms\/IPO
/;
152 $libpath = "lib/".$libpath."/";
153 open UDEFS
, "$nmPath -Aup $Directory/$lib|";
157 my $obj = $libpath.$1;
159 if (defined($objdefs{$_})) {
160 $objdeps{$obj}{$objdefs{$_}}=1;
164 close UDEFS
or die "nm failed"
167 # Gather definitions from the libraries
168 foreach my $lib (@libs ) {
169 open DEFS
, "$nmPath -g $Directory/$lib|";
171 next if (! / [ABCDGRST] /);
172 s/^[^ ]* [ABCDGRST] //;
173 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
174 # this strips both LF and CRLF.
177 close DEFS
or die "nm failed";
181 # Gather definitions from the object files.
182 foreach my $obj (@objs ) {
183 open DEFS
, "$nmPath -g $Directory/$obj |";
185 next if (! / [ABCDGRST] /);
186 s/^[^ ]* [ABCDGRST] //;
187 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
188 # this strips both LF and CRLF.
191 close DEFS
or die "nm failed";
194 # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
195 # for one library or object file. The <dt> provides the name of the library or
196 # object. The <dd> provides a list of the libraries/objects it depends on.
200 $lib_ns =~ s/(.*)\.[oa]/$1/;
203 if ($WHY) { print "\n"; }
205 print " <dt><b>$lib</b</dt><dd><ul>\n";
208 "$nmPath -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
213 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
214 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
215 push(@
{$DepLibs{$libdefs{$_}}}, $_);
216 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
217 if ($PEROBJ && !$PEROBJINCL) {
218 # -perobjincl makes .a files depend on .o files they contain themselves
219 # default is don't depend on these.
220 next if defined $libobjs{$lib}{$objdefs{$_}};
223 $libroot =~ s/lib(.*).a/$1/;
224 if ($objdefs{$_} ne "$libroot.o") {
225 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
226 push(@
{$DepLibs{$objdefs{$_}}}, $_);
230 close UNDEFS
or die "nm failed";
231 unless(keys %DepLibs) {
233 open UNDEFS
, "$nmPath -u $Directory/$lib |";
235 # to bypass non-working sed
236 if (' ' eq substr($_,0,2) and index($_,'U ')) {
237 $_ = substr($_,index($_,'U ')+2)
239 $_ = substr($_,index($_,' *U ')+5) if -1!=index($_,' *U ');
243 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
244 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
245 push(@
{$DepLibs{$libdefs{$_}}}, $_);
246 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
248 $libroot =~ s/lib(.*).a/$1/;
249 if ($objdefs{$_} ne "$libroot.o") {
250 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
251 push(@
{$DepLibs{$objdefs{$_}}}, $_);
255 close UNDEFS
or die "nm failed";
258 # include the .a's objects
259 for my $obj (keys %{$libobjs{$lib}}) {
260 $DepLibs{$obj} = ["<.a object>"] unless exists $DepLibs{$obj};
266 foreach my $obj (keys %DepLibs) {
267 foreach my $objdeps (keys %{$objdeps{$obj}}) {
268 next if defined $temp{$objdeps};
269 push(@
{$temp{$objdeps}}, $obj);
277 for my $key (sort keys %DepLibs) {
282 my @syms = @
{$DepLibs{$key}};
283 foreach my $sym (@syms) {
288 print " <li>$key</li>\n";
290 my $suffix = substr($key,length($key)-1,1);
291 $key =~ s/(.*)\.[oa]/$1/;
292 if ($suffix eq "a") {
293 if (!$FLAT) { print DOT
"$lib_ns -> $key [ weight=0 ];\n" };
295 if (!$FLAT) { print DOT
"$lib_ns -> $key [ weight=10];\n" };
303 print " </ul></dd>\n";
307 # Make sure we flush on write. This is slower but correct based on the way we
308 # write I/O in gen_one_entry.
311 # Print the definition list tag
315 open DOT
, "| $DotPath -Tgif > libdeps.gif";
317 print DOT
"digraph LibDeps {\n";
318 print DOT
" size=\"40,15\"; \n";
319 print DOT
" ratio=\"1.33333\"; \n";
320 print DOT
" margin=\"0.25\"; \n";
321 print DOT
" rankdir=\"LR\"; \n";
322 print DOT
" mclimit=\"50.0\"; \n";
323 print DOT
" ordering=\"out\"; \n";
324 print DOT
" center=\"1\";\n";
325 print DOT
"node [shape=\"box\",\n";
326 print DOT
" color=\"#000088\",\n";
327 print DOT
" fillcolor=\"#FFFACD\",\n";
328 print DOT
" fontcolor=\"#3355BB\",\n";
329 print DOT
" style=\"filled\",\n";
330 print DOT
" fontname=\"sans\",\n";
331 print DOT
" fontsize=\"24\"\n";
333 print DOT
"edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
336 # Print libraries first
337 foreach my $lib (@libs) {
342 foreach my $obj (keys %objdeps) {
345 foreach my $dep (keys %{$objdeps{$obj}}) {
356 open DOT
, "| $DotPath -Tgif > objdeps.gif";
357 print DOT
"digraph ObjDeps {\n";
358 print DOT
" size=\"8,10\";\n";
359 print DOT
" margin=\"0.25\";\n";
360 print DOT
" rankdir=\"LR\";\n";
361 print DOT
" mclimit=\"50.0\";\n";
362 print DOT
" ordering=\"out\";\n";
363 print DOT
" center=\"1\";\n";
364 print DOT
"node [shape=\"box\",\n";
365 print DOT
" color=\"#000088\",\n";
366 print DOT
" fillcolor=\"#FFFACD\",\n";
367 print DOT
" fontcolor=\"#3355BB\",\n";
368 print DOT
" fontname=\"sans\",\n";
369 print DOT
" style=\"filled\",\n";
370 print DOT
" fontsize=\"24\"\n";
372 print DOT
"edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
375 # Print objects second
376 foreach my $obj (@objs) {
384 # Print end tag of definition list element