]> git.donarmstrong.com Git - debhelper.git/blob - dh_install
r569: * Fix dh_install to install empty directories even if it is excluding some
[debhelper.git] / dh_install
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh_install - install files into package build directories
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_install> [B<-X>I<item>] [B<--autodest>] [B<--list-missing>] [B<--sourcedir=>I<dir>] [S<I<debhelper options>>] [S<I<file [...] dest>>]
16
17 =head1 DESCRIPTION
18
19 dh_install is a debhelper program that handles installing files into package
20 build directories. There are many dh_install* commands that handle installing
21 specific types of files such as documentation, examples, man pages, and so on,
22 and they should be used when possible as they often have extra intelligence for
23 those particular tasks. dh_install, then, is useful for installing everything
24 else, for which no particular intelligence is needed. It is a replacement for
25 the old dh_movefiles command.
26
27 Files named debian/package.install list the files to install into each
28 package and where they should be installed to. The format is a set of
29 lines, where each line lists a file or files to install, and at the end of
30 the line tells the directory it should be installed in. The name of the
31 files (or directories) to install should be given relative to the current
32 directory, while the installation directory is given relative to the
33 package build directory. You may use wildcards in the names of the files to
34 install (in v3 mode and above).
35
36 This program may be used in one of two ways. If you just have a file or two
37 that the upstream Makefile does not install for you, you can run dh_install
38 on them to move them into place. On the other hand, maybe you have a large
39 package that builds multiple binary packages. You can use the upstream Makefile
40 to install it all into debian/tmp, and then use dh_install to copy
41 directories and files from there into the proper package build directories.
42
43 =head1 OPTIONS
44
45 =over 4
46
47 =item B<-Xitem>, B<--exclude=item>
48
49 Exclude files that contain "item" anywhere in their filename from
50 being installed.
51
52 =item B<--autodest>
53
54 Guess as the destination directory to install things to. If this is
55 specified, you should not list destination directories in
56 debian/package.install files or on the command line. Instead, dh_install
57 will guess as follows:
58
59 Strip off debian/tmp (or the sourcedir if one is given) from the front of
60 the filename, if it is present, and install into the dirname of the
61 filename. So if the filename is debian/tmp/usr/bin, then that directory
62 will be copied to debian/package/usr/. If the filename is
63 debian/tmp/etc/passwd, it will be copied to debian/package/etc/.
64
65 Note that if you list only a filename on a line by itself in a
66 debian/package.install file, with no explicit destination, then dh_install
67 will automatically guess the destination even if this flag is not set.
68
69 =item B<--list-missing>
70
71 This option makes dh_install keep track of the files it installs, and then at
72 the end, compare that list with the files in the source directory. If any of
73 the files (and symlinks) in the source directory were not installed to
74 somewhere, it will warn on stderr about that.
75
76 This may be useful if you have a large package and want to make sure that
77 you don't miss installing newly added files in new upstream releases.
78
79 Note that files that are excluded from being moved via the -X option are not
80 warned about.
81
82 =item B<--sourcedir=dir>
83
84 Makes all source files be found under dir. If this is specified, it is
85 akin to all the source filenames having "dir/" prepended to them.
86
87 To make dh_install behave like the old dh_movefiles, move your
88 package.files file to package.install and call dh_install with
89 "--sourcedir=debian/tmp" appended to the command. This will
90 approximate dh_movefiles behaviour, except it will copy files instead
91 of moving them.
92
93 =item I<file [...] dest>
94
95 Lists files (or directories) to install and where to install them to.
96 The files will be installed into the first package dh_install acts on.
97
98 =back
99
100 =cut
101
102 init();
103
104 my @installed;
105
106 my $srcdir = '.';
107 $srcdir = $dh{SOURCEDIR}."/" if defined $dh{SOURCEDIR};
108
109 foreach my $package (@{$dh{DOPACKAGES}}) {
110         my $tmp=tmpdir($package);
111         my $file=pkgfile($package,"install");
112
113         my @install;
114         if ($file) {
115                 @install=filedoublearray($file); # no globbing yet
116         }
117         
118         if (($package eq $dh{FIRSTPACKAGE} || $dh{PARAMS_ALL}) && @ARGV) {
119                 push @install, [@ARGV];
120         }
121
122         # Support for -X flag.
123         my $exclude = '';
124         if ($dh{EXCLUDE_FIND}) {
125                 $exclude = '! \( '.$dh{EXCLUDE_FIND}.' \)';
126         }
127         
128         foreach my $set (@install) {
129                 my $dest;
130                 
131                 if (! defined $dh{AUTODEST} && @$set > 1) {
132                         $dest=pop @$set;
133                 }
134                 # glob now, relative to srcdir
135                 foreach my $src (map { glob "$srcdir/$_" } @$set) { 
136                         next if excludefile($src);
137                 
138                         if (! defined $dest) {
139                                 # Guess at destination directory.
140                                 $dest=$src;
141                                 my $strip=$srcdir;
142                                 if ($strip eq '.') {
143                                         $strip = "debian/tmp";
144                                 }
145                                 $dest=~s/^(.*\/)?\Q$strip\E//;
146                                 $dest=dirname($dest);
147                         }
148                         
149                         # Make sure the destination directory exists.
150                         if (! -e "$tmp/$dest") {
151                                 doit("install","-d","$tmp/$dest");
152                         }
153
154                         # Keep track of what's installed.
155                         if ($dh{LIST_MISSING}) {
156                                 # Kill any extra slashes. Makes the
157                                 # @installed stuff more robust.
158                                 $src=~y:/:/:s;
159                                 $src=~s:/+$::;
160                                 $src=~s:^(\./)*::;
161                                 push @installed, "\Q$src\E\/.*|\Q$src\E";
162                         }
163                         
164                         if (-d $src && $exclude) {
165                                 my ($dir_basename) = basename($src);
166                                 # Pity there's no cp --exclude ..
167                                 my $pwd=`pwd`;
168                                 chomp $pwd;
169                                 complex_doit("cd $src/.. && find $dir_basename $exclude \\( -type f -or -type l \\) -exec cp --parents -dp {} $pwd/$tmp/$dest/ \\;");
170                                 # cp is annoying so I need a separate pass
171                                 # just for empty directories
172                                 complex_doit("cd $src/.. && find $dir_basename $exclude \\( -type d -and -empty \\) -exec cp --parents -a {} $pwd/$tmp/$dest/ \\;");
173                         }
174                         else {
175                                 doit("cp", "-a", $src, "$tmp/$dest/");
176                         }
177                 }
178         }
179 }
180
181 if ($dh{LIST_MISSING}) {
182         
183         # . as srcdir makes no sense, so this is a special case.
184         if ($srcdir eq '.') {
185                 $srcdir='debian/tmp';
186         }
187         
188         my @missing;
189         my $installed=join("|", @installed);
190         $installed=qr{^$installed$};
191         find(sub {
192                 -f || -l || return;
193                 $_="$File::Find::dir/$_";
194                 push @missing, $_ unless /$installed/ || excludefile($_);
195         }, $srcdir);
196         if (@missing) {
197                 warning "$_ exists in debian/tmp but not installed to anywhere" foreach @missing;
198         }
199 }
200
201 =head1 EXAMPLE
202
203 Suppose your package's upstream Makefile installs a binary, a man page, and
204 a library into appropriate subdirectories of debian/tmp. You want to put
205 the library into package libfoo, and the rest into package foo. Your rules
206 file will run "dh_install --sourcedir=debian/tmp". Make debian/foo.install
207 contain:
208
209   usr/bin
210   usr/share/man/man1
211
212 While debian/libfoo.install contains:
213
214   usr/libfoo*.so.*
215
216 If you want a libfoo-dev package too, debian/libfoo-dev.install might contain:
217
218   usr/include
219   usr/lib/libfoo*.so
220   usr/share/man/man3
221
222 =head1 SEE ALSO
223
224 L<debhelper(1)>
225
226 This program is a part of debhelper.
227
228 =head1 AUTHOR
229
230 Joey Hess <joeyh@debian.org>
231
232 =cut