]> git.donarmstrong.com Git - debhelper.git/blob - dh_strip
r598: * dh_strip: do not strip files multiple times.
[debhelper.git] / dh_strip
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_strip - strip executables, shared libraries, and some static libraries
6
7 =cut
8
9 use strict;
10 use File::Find;
11 use Debian::Debhelper::Dh_Lib;
12
13 =head1 SYNOPSIS
14
15 B<dh_strip> [S<I<debhelper options>>] [B<-X>I<item>]
16
17 =head1 DESCRIPTION
18
19 dh_strip is a debhelper program that is responsible for stripping
20 executables, shared libraries, and static libraries that are not used for
21 debugging.
22
23 This program examines your package build directories and works out what
24 to strip on its own. It uses L<file(1)> and file permisions and filenames
25 to figure out what files are shared libraries (*.so), executable binaries,
26 and static (lib*.a) and debugging libraries (lib*_g.a, debug/*.so), and
27 strips each as much as is possible. (Which is not at all for debugging
28 libraries.) In general it seems to make very good guesses, and will do the
29 right thing in almost all cases.
30
31 Since it is very hard to automatically guess if a file is a
32 module, and hard to determine how to strip a module, dh_strip does not
33 currently deal with stripping binary modules such as .o files.
34
35 =head1 OPTIONS
36
37 =over 4
38
39 =item B<-X>I<item>, B<--exclude=>I<item>
40
41 Exclude files that contain "item" anywhere in their filename from being
42 stripped. You may use this option multiple times to build up a list of
43 things to exclude.
44
45 =back
46
47 =head1 NOTES
48
49 If the DEB_BUILD_OPTIONS environment variable contains "nostrip", nothing
50 will be stripped, in accordance with Debian policy.
51
52 =head1 CONFORMS TO
53
54 Debian policy, version 3.0.1
55
56 =cut
57
58 init();
59
60 # This variable can be used to turn off stripping (see Policy).
61 if (defined $ENV{DEB_BUILD_OPTIONS} && $ENV{DEB_BUILD_OPTIONS} =~ /nostrip/) {
62         exit;
63 }
64
65 # I could just use `file $_[0]`, but this is safer
66 sub get_file_type {
67         my $file=shift;
68         open (FILE, '-|') # handle all filenames safely
69                 || exec('file', $file)
70                 || die "can't exec file: $!";
71         my $type=<FILE>;
72         close FILE;
73         return $type;
74 }
75
76 # Check if a file is an elf binary, shared library, or static library,
77 # for use by File::Find. It'll fill the following 3 arrays with anything
78 # it finds:
79 my (@shared_libs, @executables, @static_libs);
80 sub testfile {
81         return if -l $_ or -d $_; # Skip directories and symlinks always.
82
83         # See if we were asked to exclude this file.
84         # Note that we have to test on the full filename, including directory.
85         my $fn="$File::Find::dir/$_";
86         foreach my $f (@{$dh{EXCLUDE}}) {
87                 return if ($fn=~m/\Q$f\E/);
88         }
89
90         # Is it a debug library in a debug subdir?
91         return if $fn=~m/debug\/.*\.so/;
92
93         # Does its filename look like a shared library?
94         if (m/.*\.so.*?/) {
95                 # Ok, do the expensive test.
96                 my $type=get_file_type($_);
97                 if ($type=~m/.*ELF.*shared.*/) {
98                         push @shared_libs, $fn;
99                         return;
100                 }
101         }
102         
103         # Is it executable? -x isn't good enough, so we need to use stat.
104         my (undef,undef,$mode,undef)=stat(_);
105         if ($mode & 0111) {
106                 # Ok, expensive test.
107                 my $type=get_file_type($_);
108                 if ($type=~m/.*ELF.*(executable|shared).*/) {
109                         push @executables, $fn;
110                         return;
111                 }
112         }
113         
114         # Is it a static library, and not a debug library?
115         if (m/lib.*\.a$/ && ! m/.*_g\.a$/) {
116                 push @static_libs, $fn;
117                 return;
118         }
119 }
120
121 foreach my $package (@{$dh{DOPACKAGES}}) {
122         my $tmp=tmpdir($package);
123
124         @shared_libs=@executables=@static_libs=();
125         find(\&testfile,$tmp);
126
127         foreach (@shared_libs) {
128                 # Note that all calls to strip on shared libs
129                 # *must* inclde the --strip-unneeded.
130                 doit("strip","--remove-section=.comment",
131                         "--remove-section=.note","--strip-unneeded",$_);
132         }
133         
134         foreach (@executables) {
135                 doit("strip","--remove-section=.comment",
136                         "--remove-section=.note",$_);
137         }
138
139         foreach (@static_libs) {
140                 doit("strip","--strip-debug",$_);
141         }
142 }
143
144 =head1 SEE ALSO
145
146 L<debhelper(7)>
147
148 This program is a part of debhelper.
149
150 =head1 AUTHOR
151
152 Joey Hess <joeyh@debian.org>
153
154 =cut