dtneededsize: +$computed
[massrebuild.git] / dtneededsize
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 use File::Basename qw(&dirname);
5 use File::Find;
6 use Data::Dumper;
7
8 $|=1;
9 sub readfile {
10   my($fname)=@_;
11   local *F;
12   open F,"$fname" or die $fname;
13   local $/=undef();
14   defined(my $r=<F>) or die $fname;
15   close F or die $fname;
16   return $r;
17 }
18
19 my %D;
20 # ==> build/Cadence-1.0.0-0.12.20200504git5787908.fc33.src.rpm.dtneeded <==
21 # /usr/lib/debug/.dwz/Cadence isdwzcommon 479079 NA
22 # /usr/bin/cadence-jackmeter /usr/lib/debug/.dwz/Cadence 717792 1329040
23 # /usr/bin/cadence-xycontroller /usr/lib/debug/.dwz/Cadence 1763616 2369832
24 # ==> build/CVector-1.0.3.1-21.fc33.src.rpm.dtneeded <==
25 # /usr/lib64/libCVector-1.0.3.so.2.0.0 nodwzcommon 28088 28896
26 for my $dtneededfn (glob "build/*.dtneeded") {
27   local *F;
28   open F,$dtneededfn or die "$dtneededfn: $1";
29   local $_;
30   while (<F>) {
31     chomp;
32     my($fn,$dwzcommon,$dwzsize,$dtsize)=/^(\S+) (\S+) (\S+) (\S+)$/ or die "$dtneededfn: $_";
33     die if $dwzcommon eq "isdwzcommon" && $dtsize ne "NA";
34     my $ref=\$D{$fn};
35     if ($$ref) {
36       die if $dwzcommon eq "isdwzcommon";
37       $$ref=[];
38     } else {
39       $$ref=[$dwzcommon,$dwzsize,$dtsize];
40     }
41   }
42   close F or die "close $dtneededfn: $1";
43 }
44
45 chdir "dtneeded.out" or die "dtneeded.out: $!";
46
47 my %F;
48 my %SONAME;
49 my @DEBUG;
50 find {
51   "no_chdir"=>1,
52   "wanted"=>sub {
53     die $File::Find::dir if $File::Find::dir=~m{/$};
54     my $binfn=$File::Find::name;
55     return if !-f $binfn;
56     die $binfn if $binfn!~m{^[.]/};
57     my $bin=readfile $binfn;
58     $binfn=~s{^[.]}{} or die;
59     my $rpath=($bin=~m{^.*\Q(R\E(?:UN)?\QPATH)\E\s*Library r(?:un)?path: \Q[\E(.*)\Q]\E$}m)[0];
60     if ($rpath) {
61       my $dirname=$File::Find::dir;
62       die if $dirname!~s{^[.]/}{/};
63       $rpath=~s{\$ORIGIN}{$dirname}g;
64     }
65     my $soname=($bin=~m{^.*\Q(SONAME)\E\s*Library soname: \Q[\E(.*)\Q]\E$}m)[0];
66     my $h={
67       "binfn"=>$binfn,
68       "needed"=>[$bin=~m{^\s*0x0000000000000001\s*\Q(NEEDED)\E\s*\QShared library: [\E(.*)\Q]\E$}gm],
69     };
70     $h->{"rpath"}=$rpath if $rpath;
71     $F{$binfn}=$h;
72 #    my $total=keys %F; warn "$total...\n" if 0==$total%1000;
73     if ($soname) {
74       my $sonamefn=$File::Find::dir."/".$soname;
75       $sonamefn=~s{^[.]/}{/} or die $sonamefn;
76       my $ref=\$SONAME{$sonamefn};
77 #      warn "soname=<$sonamefn> <$binfn> vs. <".$$ref->{"binfn"}.">\n" if $$ref;
78       $$ref=$h;
79     }
80     push @DEBUG,$binfn if $bin=~m{ \Q(DEBUG) \E };
81   },
82 },".";
83
84 my $dwzsizeall=0;
85 my $dtsizeall=0;
86 my $computed=0;
87 BINFN: for my $binfn (@DEBUG) {
88 #  warn "$binfn...\n".Dumper([sort @{$F{$binfn}{"needed"}}]);
89   my @l=$binfn;
90   my %l=($binfn=>1);
91   while (@l) {
92     my $l=shift @l;
93     my $h=$F{$l};
94     for my $needed (@{$h->{"needed"}}) {
95       my $found;
96       if ($needed=~m{^/}) {
97         $found=$needed;
98       } else {
99 #       die "$binfn: $l: $needed" if $needed=~m{/};
100         my @rpath;
101         my $rpath=$h->{"rpath"};
102         push @rpath,split /:/,$rpath if $rpath;
103         push @rpath,qw(/lib64 /usr/lib64);
104         for my $rpath (@rpath) {
105           if ($rpath!~m{^/}) {
106             warn "$binfn: $l: $rpath";
107             next;
108           }
109           my $fn="$rpath/$needed";
110           next if !$SONAME{$fn};
111           $found=$fn;
112           last;
113         }
114         warn "$binfn: $l: $needed not found; rpath=".join(":",@rpath)."\n" if !$found;
115       }
116       push @l,$found if $found&&!$l{$found}++;
117     }
118   }
119 #warn Dumper $binfn,\%l;
120   my $dwzsizetot=0;
121   my $dtsizetot=0;
122   my %dwzcommons;
123   for my $l (keys(%l)) {
124     my $ref=$D{$l};
125     if (!defined $ref) {
126       warn "$binfn: $l: missing\n";
127       next BINFN;
128     }
129     if (0==@$ref) {
130       warn "$binfn: $l: ambiguous\n";
131       next BINFN;
132     }
133     my $dtsize=$ref->[2];
134     die if !defined $dtsize;
135     $dtsizetot+=$dtsize;
136     my $dwzsize=$ref->[1];
137     die if !defined $dwzsize;
138     $dwzsizetot+=$dwzsize;
139     $computed++;
140     my $dwzcommon=$ref->[0];
141     next if $dwzcommon eq "nodwzcommon";
142     die if $dwzcommon eq "isdwzcommon";
143     next if $dwzcommons{$dwzcommon}++;
144     my $dwzcommonref=$D{$dwzcommon};
145     die if !$dwzcommonref;
146     die if $dwzcommonref->[0] ne "isdwzcommon";
147     die if $dwzcommonref->[2] ne "NA";
148     my $dwzcommonsize=$dwzcommonref->[1];
149     $dwzsizetot+=$dwzcommonsize;
150   }
151   print "$binfn: dwzsizetot=$dwzsizetot dtsizetot=$dtsizetot\n";
152 warn "$binfn: ".Dumper(\%dwzcommons);
153   $dwzsizeall+=$dwzsizetot;
154   $dtsizeall+=$dtsizetot;
155 #  warn "$binfn done\n".Dumper([sort keys(%l)]);
156 }
157 print "dwzsizeall=$dwzsizeall dtsizeall=$dtsizeall\n";
158 print "computed=$computed of DEBUG=".(0+@DEBUG)."\n";