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