]> git.donarmstrong.com Git - debhelper.git/blob - dh_strip
r1875: * dh_strip: remove binutils build-dep lines since stable has a new enough
[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>] [--dbg-package=package] [--keep-debug]
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 permissions 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 =item B<--dbg-package=>I<package>
46
47 Causes dh_strip to save debug symbols stripped from the packages it acts on
48 as independent files in the package build directory of the specified debugging
49 package.
50
51 For example, if your packages are lifoo and foo and you want to include a
52 foo-dbg package with debugging symbols, use dh_strip --dbg-package=foo-dbg.
53
54 Note that this option behaves significantly different in debhelper
55 compatibility levels 4 and below. Instead of specifying the name of a debug
56 package to put symbols in, it specifies a package (or packages) which
57 should have separated debug symbols, and the separated symbols are placed
58 in packages with "-dbg" added to their name.
59
60 =item B<-k>, B<--keep-debug>
61
62 Debug symbols will be retained, but split into an independent
63 file in usr/lib/debug/ in the package build directory. --dbg-package
64 is easier to use than this option, but this option is more flexible.
65
66 =back
67
68 =head1 NOTES
69
70 If the DEB_BUILD_OPTIONS environment variable contains "nostrip", nothing
71 will be stripped, in accordance with Debian policy.
72
73 =head1 CONFORMS TO
74
75 Debian policy, version 3.0.1
76
77 =cut
78
79 init();
80
81 # This variable can be used to turn off stripping (see Policy).
82 if (defined $ENV{DEB_BUILD_OPTIONS} && $ENV{DEB_BUILD_OPTIONS} =~ /nostrip/) {
83         exit;
84 }
85
86 # I could just use `file $_[0]`, but this is safer
87 sub get_file_type {
88         my $file=shift;
89         open (FILE, '-|') # handle all filenames safely
90                 || exec('file', $file)
91                 || die "can't exec file: $!";
92         my $type=<FILE>;
93         close FILE;
94         return $type;
95 }
96
97 # Check if a file is an elf binary, shared library, or static library,
98 # for use by File::Find. It'll fill the following 3 arrays with anything
99 # it finds:
100 my (@shared_libs, @executables, @static_libs);
101 sub testfile {
102         return if -l $_ or -d $_; # Skip directories and symlinks always.
103         
104         # See if we were asked to exclude this file.
105         # Note that we have to test on the full filename, including directory.
106         my $fn="$File::Find::dir/$_";
107         foreach my $f (@{$dh{EXCLUDE}}) {
108                 return if ($fn=~m/\Q$f\E/);
109         }
110
111         # Is it a debug library in a debug subdir?
112         return if $fn=~m/debug\/.*\.so/;
113
114         # Does its filename look like a shared library?
115         if (m/.*\.so.*?/) {
116                 # Ok, do the expensive test.
117                 my $type=get_file_type($_);
118                 if ($type=~m/.*ELF.*shared.*/) {
119                         push @shared_libs, $fn;
120                         return;
121                 }
122         }
123         
124         # Is it executable? -x isn't good enough, so we need to use stat.
125         my (undef,undef,$mode,undef)=stat(_);
126         if ($mode & 0111) {
127                 # Ok, expensive test.
128                 my $type=get_file_type($_);
129                 if ($type=~m/.*ELF.*(executable|shared).*/) {
130                         push @executables, $fn;
131                         return;
132                 }
133         }
134         
135         # Is it a static library, and not a debug library?
136         if (m/lib.*\.a$/ && ! m/.*_g\.a$/) {
137                 # Is it a binary file, or something else (maybe a liner
138                 # script on Hurd, for example? I don't use file, because
139                 # file returns a varity of things on static libraries.
140                 if (-B $_) {
141                         push @static_libs, $fn;
142                         return;
143                 }
144         }
145 }
146
147 sub make_debug {
148         my $file=shift;
149         my $tmp=shift;
150         my $desttmp=shift;
151
152         my ($base_file)=$file=~/^\Q$tmp\E(.*)/;
153         my $debug_path=$desttmp."/usr/lib/debug/".$base_file;
154         my $debug_dir=dirname($debug_path);
155         if (! -d $debug_dir) {
156                 doit("install", "-d", $debug_dir);
157         }
158         doit("objcopy", "--only-keep-debug", $file, $debug_path);
159         # No reason for this to be executable.
160         doit("chmod", 644, $debug_path);
161         return $debug_path;
162 }
163
164 sub attach_debug {
165         my $file=shift;
166         my $debug_path=shift;
167         doit("objcopy", "--add-gnu-debuglink", $debug_path, $file);
168 }
169
170 foreach my $package (@{$dh{DOPACKAGES}}) {
171         my $tmp=tmpdir($package);
172
173         # Support for keeping the debugging symbols in a detached file.
174         my $keep_debug=$dh{KEEP_DEBUG};
175         my $debugtmp=$tmp;
176         if (! compat(4)) {
177                 if (ref $dh{DEBUGPACKAGES}) {
178                         $keep_debug=1;
179                         # Note that it's only an array for the v4 stuff;
180                         # for v5 only one value is used.
181                         $debugtmp=tmpdir(@{$dh{DEBUGPACKAGES}}[0]);
182                 }
183         }
184         else {
185                 if (ref $dh{DEBUGPACKAGES} && grep { $_ eq $package } @{$dh{DEBUGPACKAGES}}) {
186                         $keep_debug=1;
187                         $debugtmp=tmpdir($package."-dbg");
188                 }
189         }
190         
191         @shared_libs=@executables=@static_libs=();
192         find(\&testfile,$tmp);
193
194         foreach (@shared_libs) {
195                 my $debug_path = make_debug($_, $tmp, $debugtmp) if $keep_debug;
196                 # Note that all calls to strip on shared libs
197                 # *must* inclde the --strip-unneeded.
198                 doit("strip","--remove-section=.comment",
199                         "--remove-section=.note","--strip-unneeded",$_);
200                 attach_debug($_, $debug_path) if $keep_debug;
201         }
202         
203         foreach (@executables) {
204                 my $debug_path = make_debug($_, $tmp, $debugtmp) if $keep_debug;
205                 doit("strip","--remove-section=.comment",
206                         "--remove-section=.note",$_);
207                 attach_debug($_, $debug_path) if $keep_debug
208         }
209
210         foreach (@static_libs) {
211                 doit("strip","--strip-debug",$_);
212         }
213 }
214
215 =head1 SEE ALSO
216
217 L<debhelper(7)>
218
219 This program is a part of debhelper.
220
221 =head1 AUTHOR
222
223 Joey Hess <joeyh@debian.org>
224
225 =cut