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