]> git.donarmstrong.com Git - debhelper.git/blob - dh_strip
r421: use strict
[debhelper.git] / dh_strip
1 #!/usr/bin/perl -w
2 #
3 # Strip files.
4
5 use strict;
6 use File::Find;
7 use Debian::Debhelper::Dh_Lib;
8 init();
9
10 # This variable can be used to turn off stripping (see Policy).
11 if (defined $ENV{DEB_BUILD_OPTIONS} && $ENV{DEB_BUILD_OPTIONS} =~ /nostrip/) {
12         exit;
13 }
14
15 # Check if a file is an elf binary, shared library, or static library,
16 # for use by File::Find. It'll fill the following 3 arrays with anything
17 # it finds:
18 my (@shared_libs, @executables, @static_libs);
19 sub testfile {
20         return if -l $_ or -d $_; # Skip directories and symlinks always.
21
22         # See if we were asked to exclude this file.
23         # Note that we have to test on the full filename, including directory.
24         my $fn="$File::Find::dir/$_";
25         foreach my $f (@{$dh{EXCLUDE}}) {
26                 return if ($fn=~m/\Q$f\E/);
27         }
28
29         # Does its filename look like a shared library?
30         if (m/.*\.so.*?/) {
31                 # Ok, do the expensive test.
32                 my $type=`file $_`;
33                 if ($type=~m/.*ELF.*shared.*/) {
34                         push @shared_libs, $fn;
35                         return;
36                 }
37         }
38         
39         # Is it executable? -x isn't good enough, so we need to use stat.
40         my (undef,undef,$mode,undef)=stat(_);
41         if ($mode & 0111) {
42                 # Ok, expensive test.
43                 my $type=`file $_`;
44                 if ($type=~m/.*ELF.*executable.*/) {
45                         push @executables, $fn;
46                         return;
47                 }
48         }
49         
50         # Is it a static library, and not a debug library?
51         if (m/lib.*\.a/ && ! m/.*_g\.a/) {
52                 push @static_libs, $fn;
53                 return;
54         }
55 }
56
57 foreach my $package (@{$dh{DOPACKAGES}}) {
58         my $tmp=tmpdir($package);
59
60         my (@shared_libs, @executables, @static_libs);
61         find(\&testfile,$tmp);
62
63         foreach (@shared_libs) {
64                 # Note that all calls to strip on shared libs
65                 # *must* inclde the --strip-unneeded.
66                 doit("strip","--remove-section=.comment",
67                         "--remove-section=.note","--strip-unneeded",$_);
68         }
69         
70         foreach (@executables) {
71                 doit("strip","--remove-section=.comment",
72                         "--remove-section=.note",$_);
73         }
74
75         foreach (@static_libs) {
76                 doit("strip","--strip-debug",$_);
77         }
78 }