Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:31:47

0001 #!/usr/bin/env perl 
0002 #
0003 #  Script to identify unused direct dependencies which are NOT unused direct
0004 #  dependencies of the direct dependencies. (Somewhat kludgey and filled
0005 #  with hardcoded stuff, but at least it is a start....)
0006 #
0007 #  Peter Elmer, Princeton University                         11 July, 2009
0008 #
0009 #############################################################################
0010 
0011   use Getopt::Std;
0012   use File::Basename;
0013   use lib dirname($0);
0014   use SCRAMGenUtils;
0015   getopts("rv");
0016 
0017   $libname = shift; chomp($libname);
0018   $tmpcache = $ENV{CMSSW_BASE}."/tmp/".$ENV{SCRAM_ARCH}."/libcheck";
0019   system("mkdir -p $tmpcache");
0020   # Should check that library actually exists
0021 
0022   # Get the list of direct dependencies
0023   
0024   my $LddData=&getLddData($libname,$tmpcache);
0025   foreach my $data (@$LddData) {
0026     my $AAA=$data->[0]; my $CCC=$data->[1];
0027     $ldddepmap{$AAA} = $CCC;
0028     $lddrevdepmap{$CCC} = $AAA;
0029     $lddneedmap{$AAA} = 0;
0030     $lddrevneedmap{$CCC} = 0;
0031   }
0032 
0033   for my $bkey ( keys %lddrevdepmap ) {
0034     my $LddData=&getLddData($bkey,$tmpcache);
0035     foreach my $data (@$LddData) {
0036       my $AAA=$data->[0]; my $CCC=$data->[1];
0037       $lddrevneedmap{$CCC} += 1;  # Mark this dep as needed by another dep
0038     }
0039   }
0040 
0041   # Get the list of unneeded libraries (includes some header lines)
0042   @UNUSEDLIST = `ldd -u -r $libname`;
0043  
0044   # Loop and print only unused libraries which are not deps of other deps
0045   # (i.e. those that must have been added as direct deps of the library 
0046   # in question, not things that come in indirectly via its deps)
0047   foreach $dkey (@UNUSEDLIST) {
0048     chomp($dkey);
0049     $dkey =~ s/(^\s+|\s+$)//g; # Drop the whitespace
0050     if (exists($lddrevdepmap{$dkey})) {
0051       if ($opt_v) {print "Found unneeded library => $dkey\n"};
0052       if ($lddrevneedmap{$dkey}==0) {
0053         print "Unnecessary direct dependence ".$lddrevdepmap{$dkey}."\n";
0054       }
0055     } else {
0056       if ($opt_v) {print "Reject header line => $dkey\n"};
0057       if ($opt_v) {print "  With => ".$lddrevdepmap{$dkey}."\n"};
0058     }
0059   }
0060   
0061   sub getLddData() {
0062     my ($lib,$cache)=@_;
0063     my $cfile=$cache."/".basename($lib);
0064     my $data=[];
0065     my $mtime=(stat($lib))[9];
0066     if ((-f $cfile) && ($mtime==(stat($cfile))[9])){$data=&SCRAMGenUtils::readHashCache($cfile);}
0067     else{
0068       my @LDDLIST = `ldd $lib`;
0069       foreach my $akey (@LDDLIST) {
0070         chomp($akey);
0071         $akey=~s/\s+\(.+\)\s*$//;
0072         if ($akey=~/^\s*\/.+/){next;}
0073         my ($AAA,$BBB,$CCC) = split(' ',$akey);
0074         if (($BBB eq "=>") && ($CCC=~/^\//) && ($AAA!~/(linux-gate\.so|linux-vdso\.so|libgcc_s.so)/)) {
0075       push @$data,[$AAA,$CCC];
0076         }
0077       }
0078       &SCRAMGenUtils::writeHashCache($data,$cfile);
0079       utime($mtime, $mtime, $cfile);
0080     }
0081     return $data;
0082   }