[CMake] Install import libraries
[llvm-complete.git] / utils / GenLibDeps.pl
blob9b65e900c53e8d0b931fc8eb6c5248e6cd7ef228
1 #!/usr/bin/perl -w
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]
11 use strict;
12 use warnings;
13 # Parse arguments...
14 my $FLAT = 0;
15 my $WHY = 0;
16 my $PEROBJ = 0;
17 my $PEROBJINCL = 0;
18 while (scalar(@ARGV) and ($_ = $ARGV[0], /^[-+]/)) {
19 shift;
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
39 my $DotPath="";
40 if (!$FLAT) {
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");
54 my $ranlibPath;
55 if ($PEROBJ) {
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;
71 closedir 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
78 # symbol definitions.
79 my %libdefs;
80 my %objdefs;
82 my %libobjs;
83 my %objdeps=();
84 # Gather library definitions at object file granularity (optional)
85 if ($PEROBJ) {
86 foreach my $lib (@libs ) {
87 `$ranlibPath $Directory/$lib`;
88 my $libpath = $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/^MSIL/Target\/MSIL/;
100 $libpath =~ s/^Core/IR/;
101 $libpath =~ s/^Instrumentation/Transforms\/Instrumentation/;
102 $libpath =~ s/^Interpreter/ExecutionEngine\/Interpreter/;
103 $libpath =~ s/^JIT/ExecutionEngine\/JIT/;
104 $libpath =~ s/^ScalarOpts/Transforms\/Scalar/;
105 $libpath =~ s/^TransformUtils/Transforms\/Utils/;
106 $libpath =~ s/^ipa/Analysis\/IPA/;
107 $libpath =~ s/^ipo/Transforms\/IPO/;
108 $libpath = "lib/".$libpath."/";
109 open DEFS, "$nmPath -sg $Directory/$lib|";
110 while (<DEFS>) {
111 chomp;
112 if (/^([^ ]*) in ([^ ]*)/) {
113 my $objfile = $libpath.$2;
114 $objdefs{$1} = $objfile;
115 $objdeps{$objfile} = {};
116 $libobjs{$lib}{$objfile}=1;
117 # my $p = "../llvm/".$objfile;
118 # $p =~ s/Support\/reg(.*).o/Support\/reg$1.c/;
119 # $p =~ s/.o$/.cpp/;
120 # unless (-e $p) {
121 # die "$p\n"
125 close DEFS or die "nm failed";
127 foreach my $lib (@libs ) {
128 my $libpath = $lib;
129 $libpath =~ s/^libLLVM(.*)\.a/$1/;
130 $libpath =~ s/(.+)CodeGen$/Target\/$1/;
131 $libpath =~ s/(.+)AsmPrinter$/Target\/$1\/AsmPrinter/;
132 $libpath =~ s/(.+)AsmParser$/Target\/$1\/AsmParser/;
133 $libpath =~ s/(.+)Info$/Target\/$1\/TargetInfo/;
134 $libpath =~ s/(.+)Disassembler$/Target\/$1\/Disassembler/;
135 $libpath =~ s/SelectionDAG/CodeGen\/SelectionDAG/;
136 $libpath =~ s/^AsmPrinter/CodeGen\/AsmPrinter/;
137 $libpath =~ s/^BitReader/Bitcode\/Reader/;
138 $libpath =~ s/^BitWriter/Bitcode\/Writer/;
139 $libpath =~ s/^MSIL/Target\/MSIL/;
140 $libpath =~ s/^Core/VMCore/;
141 $libpath =~ s/^Instrumentation/Transforms\/Instrumentation/;
142 $libpath =~ s/^Interpreter/ExecutionEngine\/Interpreter/;
143 $libpath =~ s/^JIT/ExecutionEngine\/JIT/;
144 $libpath =~ s/^ScalarOpts/Transforms\/Scalar/;
145 $libpath =~ s/^TransformUtils/Transforms\/Utils/;
146 $libpath =~ s/^ipa/Analysis\/IPA/;
147 $libpath =~ s/^ipo/Transforms\/IPO/;
148 $libpath = "lib/".$libpath."/";
149 open UDEFS, "$nmPath -Aup $Directory/$lib|";
150 while (<UDEFS>) {
151 chomp;
152 if (/:([^:]+):/) {
153 my $obj = $libpath.$1;
154 s/[^ ]+: *U //;
155 if (defined($objdefs{$_})) {
156 $objdeps{$obj}{$objdefs{$_}}=1;
160 close UDEFS or die "nm failed"
162 } else {
163 # Gather definitions from the libraries
164 foreach my $lib (@libs ) {
165 open DEFS, "$nmPath -g $Directory/$lib|";
166 while (<DEFS>) {
167 next if (! / [ABCDGRST] /);
168 s/^[^ ]* [ABCDGRST] //;
169 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
170 # this strips both LF and CRLF.
171 $libdefs{$_} = $lib;
173 close DEFS or die "nm failed";
177 # Gather definitions from the object files.
178 foreach my $obj (@objs ) {
179 open DEFS, "$nmPath -g $Directory/$obj |";
180 while (<DEFS>) {
181 next if (! / [ABCDGRST] /);
182 s/^[^ ]* [ABCDGRST] //;
183 s/\015?\012//; # not sure if <DEFS> is in binmode and uses LF or CRLF.
184 # this strips both LF and CRLF.
185 $objdefs{$_} = $obj;
187 close DEFS or die "nm failed";
190 # Generate one entry in the <dl> list. This generates the <dt> and <dd> elements
191 # for one library or object file. The <dt> provides the name of the library or
192 # object. The <dd> provides a list of the libraries/objects it depends on.
193 sub gen_one_entry {
194 my $lib = $_[0];
195 my $lib_ns = $lib;
196 $lib_ns =~ s/(.*)\.[oa]/$1/;
197 if ($FLAT) {
198 print "$lib:";
199 if ($WHY) { print "\n"; }
200 } else {
201 print " <dt><b>$lib</b></dt><dd><ul>\n";
203 open UNDEFS,
204 "$nmPath -u $Directory/$lib | sed -e 's/^[ 0]* U //' | sort | uniq |";
205 my %DepLibs;
206 while (<UNDEFS>) {
207 chomp;
208 my $lib_printed = 0;
209 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
210 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
211 push(@{$DepLibs{$libdefs{$_}}}, $_);
212 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
213 if ($PEROBJ && !$PEROBJINCL) {
214 # -perobjincl makes .a files depend on .o files they contain themselves
215 # default is don't depend on these.
216 next if defined $libobjs{$lib}{$objdefs{$_}};
218 my $libroot = $lib;
219 $libroot =~ s/lib(.*).a/$1/;
220 if ($objdefs{$_} ne "$libroot.o") {
221 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
222 push(@{$DepLibs{$objdefs{$_}}}, $_);
226 close UNDEFS or die "nm failed";
227 unless(keys %DepLibs) {
228 # above failed
229 open UNDEFS, "$nmPath -u $Directory/$lib |";
230 while (<UNDEFS>) {
231 # to bypass non-working sed
232 if (' ' eq substr($_,0,2) and index($_,'U ')) {
233 $_ = substr($_,index($_,'U ')+2)
235 $_ = substr($_,index($_,' *U ')+5) if -1!=index($_,' *U ');
237 chomp;
238 my $lib_printed = 0;
239 if (defined($libdefs{$_}) && $libdefs{$_} ne $lib) {
240 $DepLibs{$libdefs{$_}} = [] unless exists $DepLibs{$libdefs{$_}};
241 push(@{$DepLibs{$libdefs{$_}}}, $_);
242 } elsif (defined($objdefs{$_}) && $objdefs{$_} ne $lib) {
243 my $libroot = $lib;
244 $libroot =~ s/lib(.*).a/$1/;
245 if ($objdefs{$_} ne "$libroot.o") {
246 $DepLibs{$objdefs{$_}} = [] unless exists $DepLibs{$objdefs{$_}};
247 push(@{$DepLibs{$objdefs{$_}}}, $_);
251 close UNDEFS or die "nm failed";
253 if ($PEROBJINCL) {
254 # include the .a's objects
255 for my $obj (keys %{$libobjs{$lib}}) {
256 $DepLibs{$obj} = ["<.a object>"] unless exists $DepLibs{$obj};
258 my $madechange = 1;
259 while($madechange) {
260 $madechange = 0;
261 my %temp = %DepLibs;
262 foreach my $obj (keys %DepLibs) {
263 foreach my $objdeps (keys %{$objdeps{$obj}}) {
264 next if defined $temp{$objdeps};
265 push(@{$temp{$objdeps}}, $obj);
266 $madechange = 1;
269 %DepLibs = %temp;
273 for my $key (sort keys %DepLibs) {
274 if ($FLAT) {
275 print " $key";
276 if ($WHY) {
277 print "\n";
278 my @syms = @{$DepLibs{$key}};
279 foreach my $sym (@syms) {
280 print " $sym\n";
283 } else {
284 print " <li>$key</li>\n";
286 my $suffix = substr($key,length($key)-1,1);
287 $key =~ s/(.*)\.[oa]/$1/;
288 if ($suffix eq "a") {
289 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=0 ];\n" };
290 } else {
291 if (!$FLAT) { print DOT "$lib_ns -> $key [ weight=10];\n" };
294 if ($FLAT) {
295 if (!$WHY) {
296 print "\n";
298 } else {
299 print " </ul></dd>\n";
303 # Make sure we flush on write. This is slower but correct based on the way we
304 # write I/O in gen_one_entry.
305 $| = 1;
307 # Print the definition list tag
308 if (!$FLAT) {
309 print "<dl>\n";
311 open DOT, "| $DotPath -Tgif > libdeps.gif";
313 print DOT "digraph LibDeps {\n";
314 print DOT " size=\"40,15\"; \n";
315 print DOT " ratio=\"1.33333\"; \n";
316 print DOT " margin=\"0.25\"; \n";
317 print DOT " rankdir=\"LR\"; \n";
318 print DOT " mclimit=\"50.0\"; \n";
319 print DOT " ordering=\"out\"; \n";
320 print DOT " center=\"1\";\n";
321 print DOT "node [shape=\"box\",\n";
322 print DOT " color=\"#000088\",\n";
323 print DOT " fillcolor=\"#FFFACD\",\n";
324 print DOT " fontcolor=\"#3355BB\",\n";
325 print DOT " style=\"filled\",\n";
326 print DOT " fontname=\"sans\",\n";
327 print DOT " fontsize=\"24\"\n";
328 print DOT "];\n";
329 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
332 # Print libraries first
333 foreach my $lib (@libs) {
334 gen_one_entry($lib);
337 if ($PEROBJ) {
338 foreach my $obj (keys %objdeps) {
339 print "$obj:";
340 if (!$PEROBJINCL) {
341 foreach my $dep (keys %{$objdeps{$obj}}) {
342 print " $dep";
345 print "\n";
349 if (!$FLAT) {
350 print DOT "}\n";
351 close DOT;
352 open DOT, "| $DotPath -Tgif > objdeps.gif";
353 print DOT "digraph ObjDeps {\n";
354 print DOT " size=\"8,10\";\n";
355 print DOT " margin=\"0.25\";\n";
356 print DOT " rankdir=\"LR\";\n";
357 print DOT " mclimit=\"50.0\";\n";
358 print DOT " ordering=\"out\";\n";
359 print DOT " center=\"1\";\n";
360 print DOT "node [shape=\"box\",\n";
361 print DOT " color=\"#000088\",\n";
362 print DOT " fillcolor=\"#FFFACD\",\n";
363 print DOT " fontcolor=\"#3355BB\",\n";
364 print DOT " fontname=\"sans\",\n";
365 print DOT " style=\"filled\",\n";
366 print DOT " fontsize=\"24\"\n";
367 print DOT "];\n";
368 print DOT "edge [dir=\"forward\",style=\"solid\",color=\"#000088\"];\n";
371 # Print objects second
372 foreach my $obj (@objs) {
373 gen_one_entry($obj);
376 if (!$FLAT) {
377 print DOT "}\n";
378 close DOT;
380 # Print end tag of definition list element
381 print "</dl>\n";