]> git.donarmstrong.com Git - debhelper.git/blob - dh_strip
r546: * dh_builddeb(1): It's --filename, not --name. Closes: #160151
[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>] [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 It assumes that files that have names like lib*_g.a are static libraries
24 used in debugging, and will not strip them.
25
26 =head1 OPTIONS
27
28 =over 4
29
30 =item B<-X>I<item>, B<--exclude=>I<item>
31
32 Exclude files that contain "item" anywhere in their filename from being
33 stripped. You may use this option multiple times to build up a list of
34 things to exclude.
35
36 =back
37
38 =head1 NOTES
39
40 If the DEB_BUILD_OPTIONS environment variable contains "nostrip", nothing
41 will be stripped, in accordance with Debian policy.
42
43 =head1 CONFORMS TO
44
45 Debian policy, version 3.0.1
46
47 =cut
48
49 init();
50
51 # This variable can be used to turn off stripping (see Policy).
52 if (defined $ENV{DEB_BUILD_OPTIONS} && $ENV{DEB_BUILD_OPTIONS} =~ /nostrip/) {
53         exit;
54 }
55
56 # I could just use `file $_[0]`, but this is safer
57 sub get_file_type {
58         my $file=shift;
59         open (FILE, '-|') # handle all filenames safely
60                 || exec('file', $file)
61                 || die "can't exec file: $!";
62         my $type=<FILE>;
63         close FILE;
64         return $type;
65 }
66
67 # Check if a file is an elf binary, shared library, or static library,
68 # for use by File::Find. It'll fill the following 3 arrays with anything
69 # it finds:
70 my (@shared_libs, @executables, @static_libs);
71 sub testfile {
72         return if -l $_ or -d $_; # Skip directories and symlinks always.
73
74         # See if we were asked to exclude this file.
75         # Note that we have to test on the full filename, including directory.
76         my $fn="$File::Find::dir/$_";
77         foreach my $f (@{$dh{EXCLUDE}}) {
78                 return if ($fn=~m/\Q$f\E/);
79         }
80
81         # Does its filename look like a shared library?
82         if (m/.*\.so.*?/) {
83                 # Ok, do the expensive test.
84                 my $type=get_file_type($_);
85                 if ($type=~m/.*ELF.*shared.*/) {
86                         push @shared_libs, $fn;
87                         return;
88                 }
89         }
90         
91         # Is it executable? -x isn't good enough, so we need to use stat.
92         my (undef,undef,$mode,undef)=stat(_);
93         if ($mode & 0111) {
94                 # Ok, expensive test.
95                 my $type=get_file_type($_);
96                 if ($type=~m/.*ELF.*(executable|shared).*/) {
97                         push @executables, $fn;
98                         return;
99                 }
100         }
101         
102         # Is it a static library, and not a debug library?
103         if (m/lib.*\.a$/ && ! m/.*_g\.a$/) {
104                 push @static_libs, $fn;
105                 return;
106         }
107 }
108
109 foreach my $package (@{$dh{DOPACKAGES}}) {
110         my $tmp=tmpdir($package);
111
112         find(\&testfile,$tmp);
113
114         foreach (@shared_libs) {
115                 # Note that all calls to strip on shared libs
116                 # *must* inclde the --strip-unneeded.
117                 doit("strip","--remove-section=.comment",
118                         "--remove-section=.note","--strip-unneeded",$_);
119         }
120         
121         foreach (@executables) {
122                 doit("strip","--remove-section=.comment",
123                         "--remove-section=.note",$_);
124         }
125
126         foreach (@static_libs) {
127                 doit("strip","--strip-debug",$_);
128         }
129 }
130
131 =head1 SEE ALSO
132
133 L<debhelper(1)>
134
135 This program is a part of debhelper.
136
137 =head1 AUTHOR
138
139 Joey Hess <joeyh@debian.org>
140
141 =cut