]> git.donarmstrong.com Git - debhelper.git/blob - dh
cbc5f10c12deb0e47a4bdbf48fdeb1ad311f335d
[debhelper.git] / dh
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 dh - debhelper command sequencer
6
7 =cut
8
9 use strict;
10 use Debian::Debhelper::Dh_Lib;
11
12 =head1 SYNOPSIS
13
14 B<dh> sequence [B<--with> I<addon>] [B<--until> I<cmd>] [B<--before> I<cmd>] [B<--after> I<cmd>] [B<--remaining>] [S<I<debhelper options>>]
15
16 =head1 DESCRIPTION
17
18 dh runs a sequence of debhelper commands. The supported sequences
19 correspond to the targets of a debian/rules file: "build", "clean",
20 "install", "binary-arch", "binary-indep", and "binary".
21
22 Commands in the binary-indep sequence are passed the "-i" option to ensure
23 they only work on binary independent packages, and commands in the
24 binary-arch sequences are passed the "-a" option to ensure they only work
25 on architecture dependent packages.
26
27 Each debhelper command will record when it's successfully run in
28 debian/package.debhelper.log. (Which dh_clean deletes.) So dh can tell
29 which commands have already been run, for which packages, and skip running
30 those commands again.
31
32 Each time dh is run, it examines the log, and finds the last logged command
33 that is in the specified sequence. It then continues with the next command
34 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
35 options can override this behavior.
36
37 If debian/rules contains a target with a name like "override_I<dh_command>",
38 then when it gets to that command in the sequence, dh will run that
39 target from the rules file, rather than running the actual command. The
40 override target can then run the command with additional options, or run
41 entirely different commands instead. (Note that to use this feature,
42 you should Build-Depend on debhelper 7.2 or above.)
43
44 =head1 OPTIONS
45
46 =over 4
47
48 =item B<--with> I<addon>
49
50 Add the debhelper commands specified by the given addon to appropriate places
51 in the sequence of commands that is run. This option can be repeated more
52 than once, and is used when there is a third-party package that provides
53 debhelper commands. See "SEQUENCE ADDONS" below for documentation about what
54 such packages should do to be supported by --with.
55
56 =item B<--until> I<cmd>
57
58 Run commands in the sequence until and including I<cmd>, then stop.
59
60 =item B<--before> I<cmd>
61
62 Run commands in the sequence before I<cmd>, then stop.
63
64 =item B<--after> I<cmd>
65
66 Run commands in the sequence that come after I<cmd>.
67
68 =item B<--remaining>
69
70 Run all commands in the sequence that have yet to be run.
71
72 =back
73
74 All other options passed to dh are passed on to each command it runs. This
75 can be used to set an option like "-v" or "-X" or "-N", as well as for more
76 specialised options.
77
78 =head1 COMMAND SPECIFICATION
79
80 I<cmd> can be a full name of a debhelper command, or a substring. It'll first
81 search for a command in the sequence exactly matching the name, to avoid any
82 ambiguity. If there are multiple substring matches, the last one in the
83 sequence will be used.
84
85 =head1 SEQUENCE ADDONS
86
87 When B<--with> I<addon> is used, dh loads the perl module
88 Debian::Debhelper::Sequence::I<addon>. Two functions are provided to let
89 the module add its commands to sequences:
90
91 =over 4
92
93 =item Debian::Debhelper::Dh_Lib::insert_before(existing_command, new_command)
94
95 Insert I<new_command> in sequences before I<existing_command>.
96
97 =item Debian::Debhelper::Dh_Lib::insert_after(existing_command, new_command)
98
99 Insert I<new_command> in sequences after I<existing_command>.
100
101 =item Debian::Debhelper::Dh_Lib::remove_command(existing_command)
102
103 Remove I<existing_command> from the list of commands to run.
104
105 =back
106
107 =cut
108
109 sub command_pos {
110         my $command=shift;
111         my @sequence=@_;
112
113         foreach my $i (0..$#sequence) {
114                 if ($command eq $sequence[$i]) {
115                         return $i;
116                 }
117         }
118
119         my @matches;
120         foreach my $i (0..$#sequence) {
121                 if ($sequence[$i] =~ /\Q$command\E/) {
122                         push @matches, $i;
123                 }
124         }
125         if (! @matches) {
126                 error "command specification \"$command\" does not match any command in the sequence"
127         }
128         else {
129                 return pop @matches;
130         }
131 }
132
133 =head1 EXAMPLES
134
135 To see what commands are included in a sequence, without actually doing
136 anything:
137
138         dh binary-arch --no-act
139
140 This is a very simple rules file, for packages where the default sequences of
141 commands work with no additional options.
142
143         #!/usr/bin/make -f
144         %:
145                 dh $@
146
147 Often you'll want to pass an option to a specific debhelper command. The
148 easy way to do with is by adding an override target for that command.
149         
150         #!/usr/bin/make -f
151         %:
152                 dh $@
153
154         override_dh_strip:
155                 dh_strip -Xfoo
156                 
157         override_dh_installdocs:
158                 dh_installdocs README TODO
159
160 Sometimes the automated dh_auto_configure and dh_auto_build can't guess
161 what to do for a strange package. Here's how to avoid running either
162 and instead run your own commands.
163
164         #!/usr/bin/make -f
165         %:
166                 dh $@
167
168         override_dh_auto_configure:
169                 ./mondoconfig
170
171         override_dh_auto_build:
172                 make universe-explode-in-delight
173
174 Another common case is wanting to do something manually before or
175 after a particular debhelper command is run.
176
177         #!/usr/bin/make -f
178         %:
179                 dh $@
180
181         override_dh_fixperms:
182                 dh_fixperms
183                 chmod 4755 debian/foo/usr/bin/foo
184
185 If the package's source tree might get files with names
186 like build or clean in it, the rules file would not
187 run targets with the same names. This issue can be worked
188 around by passing -B to make.
189
190         #!/usr/bin/make -Bf
191         %:
192                 dh $@
193
194 =cut
195
196 # Stash this away before init modifies it.
197 my @ARGV_orig=@ARGV;
198
199 init(options => {
200         "until=s" => \$dh{UNTIL},
201         "after=s" => \$dh{AFTER},
202         "before=s" => \$dh{BEFORE},
203         "remaining" => \$dh{REMAINING},
204         "with=s" => sub {
205                 my ($option,$value)=@_;
206                 push @{$dh{WITH}},$value;
207         },
208 });
209 inhibit_log();
210
211 # Definitions of sequences.
212 my %sequences;
213 $sequences{build} = [qw{
214         dh_testdir
215         dh_auto_configure
216         dh_auto_build
217         dh_auto_test
218 }],
219 $sequences{clean} = [qw{
220         dh_testdir
221         dh_auto_clean
222         dh_clean
223 }];
224 $sequences{install} = [@{$sequences{build}}, qw{
225         dh_testroot
226         dh_prep
227         dh_installdirs
228         dh_auto_install
229
230         dh_install
231         dh_installdocs
232         dh_installchangelogs
233         dh_installexamples
234         dh_installman
235
236         dh_installcatalogs
237         dh_installcron
238         dh_installdebconf
239         dh_installcatalogs
240         dh_installemacsen
241         dh_installifupdown
242         dh_installinfo
243         dh_installinit
244         dh_installmenu
245         dh_installmime
246         dh_installmodules
247         dh_installlogcheck
248         dh_installlogrotate
249         dh_installpam
250         dh_installppp
251         dh_installudev
252         dh_installwm
253         dh_installxfonts
254         dh_lintian
255         dh_desktop
256         dh_gconf
257         dh_icons
258         dh_perl
259         dh_scrollkeeper
260         dh_usrlocal
261
262         dh_link
263         dh_compress
264         dh_fixperms
265 }];
266 my @b=qw{
267         dh_installdeb
268         dh_gencontrol
269         dh_md5sums
270         dh_builddeb
271 };
272 $sequences{'binary-indep'} = [@{$sequences{install}}, @b];
273 $sequences{binary} = [@{$sequences{install}}, qw{
274         dh_strip
275         dh_makeshlibs
276         dh_shlibdeps
277 }, @b];
278 $sequences{'binary-arch'} = [@{$sequences{binary}}];
279
280 # --with python-support is enabled by default, at least for now
281 unshift @{$dh{WITH}}, "python-support";
282
283 # sequence addon interface
284 sub _insert {
285         my $offset=shift;
286         my $existing=shift;
287         my $new=shift;
288         foreach my $sequence (keys %sequences) {
289                 my @list=@{$sequences{$sequence}};
290                 next unless grep $existing, @list;
291                 my @new;
292                 foreach my $command (@list) {
293                         if ($command eq $existing) {
294                                 push @new, $new if $offset < 0;
295                                 push @new, $command;
296                                 push @new, $new if $offset > 0;
297                         }
298                         else {
299                                 push @new, $command;
300                         }
301                 }
302                 $sequences{$sequence}=\@new;
303         }
304 }
305 sub insert_before {
306         _insert(-1, @_);
307 }
308 sub insert_after {
309         _insert(1, @_);
310 }
311 sub remove_command {
312         my $command=shift;
313         foreach my $sequence (keys %sequences) {
314                 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
315         }
316         
317 }
318 foreach my $addon (@{$dh{WITH}}) {
319         my $mod="Debian::Debhelper::Sequence::$addon";
320         $mod=~s/-/_/g;
321         eval "use $mod";
322         if ($@) {
323                 error("--with $addon not supported or failed to load module $mod");
324         }
325 }
326
327 # Get the sequence of commands to run.
328 if (! @ARGV) {
329         error "specify a sequence to run";
330 }
331 my $sequence=shift;
332 if ($sequence eq 'debian/rules' ||
333     $sequence =~ /^override_dh_/) {
334         # make -B causes the rules file to be run as a target
335         # and support completly empty override targets
336         exit 0
337 }       
338 elsif (! exists $sequences{$sequence}) {
339         error "Unknown sequence $sequence (choose from: ".
340                 join(" ", sort keys %sequences).")";
341 }
342 my @sequence=@{$sequences{$sequence}};
343
344 # The list of all packages that can be acted on.
345 my @packages=@{$dh{DOPACKAGES}};
346
347 # Get the options to pass to commands in the sequence.
348 # Filter out options intended only for this program.
349 my @options;
350 if ($sequence eq 'binary-arch') {
351         push @options, "-a";
352         # as an optimisation, remove from the list any packages
353         # that are not arch dependent
354         my %arch_packages = map { $_ => 1 } getpackages("arch");
355         @packages = grep { $arch_packages{$_} } @packages;
356 }
357 elsif ($sequence eq 'binary-indep') {
358         push @options, "-i";
359         # ditto optimisation for arch indep
360         my %indep_packages = map { $_ => 1 } getpackages("indep");
361         @packages = grep { $indep_packages{$_} } @packages;
362 }
363 while (@ARGV_orig) {
364         my $opt=shift @ARGV_orig;
365         next if $opt eq $sequence;
366         if ($opt =~ /^--?(after|until|before|with)$/) {
367                 shift @ARGV_orig;
368                 next;
369         }
370         elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with)=)/) {
371                 next;
372         }
373         push @options, $opt;
374 }
375
376 # Figure out at what point in the sequence to start for each package.
377 my %logged;
378 my %startpoint;
379 foreach my $package (@packages) {
380         my @log=loadlog($package);
381         if ($dh{AFTER}) {
382                 # Run commands in the sequence that come after the
383                 # specified command.
384                 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
385                 # Write a dummy log entry indicating that the specified
386                 # command was, in fact, run. This handles the case where
387                 # no commands remain to run after it, communicating to
388                 # future dh instances that the specified command should not
389                 # be run again.
390                 writelog($package, $sequence[$startpoint{$package}-1]);
391         }
392         elsif ($dh{REMAINING}) {
393                 # Start at the beginning so all remaining commands will get
394                 # run.
395                 $startpoint{$package}=0;
396         }
397         else {
398                 # Find the last logged command that is in the sequence, and
399                 # continue with the next command after it. If no logged
400                 # command is in the sequence, we're starting at the beginning..                         
401                 $startpoint{$package}=0;
402 COMMAND:        foreach my $command (reverse @log) {
403                         foreach my $i (0..$#sequence) {
404                                 if ($command eq $sequence[$i]) {
405                                         $startpoint{$package}=$i+1;
406                                         last COMMAND;
407                                 }
408                         }
409                 }
410         }
411 }
412
413 # Figure out what point in the sequence to go to.
414 my $stoppoint=$#sequence;
415 if ($dh{UNTIL}) {
416         $stoppoint=command_pos($dh{UNTIL}, @sequence);
417 }
418 elsif ($dh{BEFORE}) {
419         $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
420 }
421
422 # Now run the commands in the sequence.
423 foreach my $i (0..$stoppoint) {
424         # Figure out which packages need to run this command.
425         my @exclude;
426         foreach my $package (@packages) {
427                 if ($startpoint{$package} > $i ||
428                     $logged{$package}{$sequence[$i]}) {
429                         push @exclude, $package;
430                 }
431         }
432         
433         if (@exclude eq @packages) {
434                 # Command already done for all packages.
435                 next;
436         }
437
438         run($sequence[$i], \@packages, \@exclude, @options);
439 }
440
441 sub run {
442         my $command=shift;
443         my @packages=@{shift()};
444         my @exclude=@{shift()};
445         my @options=@_;
446         
447         # Check for override targets in debian/rules and
448         # run them instead of running the command directly.
449         my $override_command;
450         if (rules_explicit_target("override_".$command)) {
451                 $override_command=$command;
452                 # This passes the options through to commands called
453                 # inside the target.
454                 $ENV{DH_INTERNAL_OPTIONS}=join(" ", @options);
455                 $command="debian/rules";
456                 @options="override_".$override_command;
457         }
458         else {
459                 # If some packages are excluded, add flags
460                 # to prevent them from being acted on.
461                 push @options, map { "-N$_" } @exclude;
462         }
463
464         # 3 space indent lines the command being run up under the 
465         # sequence name after "dh ".
466         print "   ".escape_shell($command, @options)."\n";
467
468         if (! $dh{NO_ACT}) {
469                 my $ret=system($command, @options);
470                 if ($ret >> 8 != 0) {
471                         exit $ret >> 8;
472                 }
473                 elsif ($ret) {
474                         exit 1;
475                 }
476
477                 if (defined $override_command) {
478                         delete $ENV{DH_INTERNAL_OPTIONS};
479                         # Need to handle logging for overriden commands here,
480                         # because the actual debhelper command may not have
481                         # been run by the rules file target.
482                         my %packages=map { $_ => 1 } @packages;
483                         map { delete $packages{$_} } @exclude;
484                         Debian::Debhelper::Dh_Lib::write_log($override_command, keys %packages);
485                 }
486         }
487 }
488
489 sub loadlog {
490         my $package=shift;
491         my $ext=pkgext($package);
492         
493         my @log;
494         open(LOG, "<", "debian/${ext}debhelper.log") || return;
495         while (<LOG>) {
496                 chomp;
497                 push @log, $_;
498                 $logged{$package}{$_}=1;
499         }
500         close LOG;
501         return @log;
502 }
503                 
504 sub writelog {
505         my $package=shift;
506         my $cmd=shift;
507         my $ext=pkgext($package);
508         
509         open(LOG, ">>", "debian/${ext}debhelper.log") || error("failed to write to log");
510         print LOG $cmd."\n";
511         close LOG;
512 }
513
514 {
515 my %targets;
516 my $rules_parsed;
517
518 sub rules_explicit_target {
519         # Checks if a specified target exists as an explicit target
520         # in debian/rules. 
521         my $target=shift;
522         
523         if (! $rules_parsed) {  
524                 my $processing_targets = 0;
525                 my $not_a_target = 0;
526                 open(MAKE, "make -Rrnpsf debian/rules debhelper-fail-me 2>/dev/null |");
527                 while (<MAKE>) {
528                         if ($processing_targets) {
529                                 if (/^# Not a target:/) {
530                                         $not_a_target = 1;
531                                 }
532                                 else {
533                                         if (!$not_a_target && /^([^#:]+)::?/) {
534                                                 # Target is defined.
535                                                 # NOTE: if it is a depenency
536                                                 # of .PHONY it will be
537                                                 # defined too but that's ok.
538                                                 $targets{$1} = 1;
539                                         }
540                                         # "Not a target:" is always followed by
541                                         # a target name, so resetting this one
542                                         # here is safe.
543                                         $not_a_target = 0;
544                                 }
545                         } elsif (/^# Files$/) {
546                                 $processing_targets = 1;
547                         }
548                 }
549                 close MAKE;
550                 $rules_parsed = 1;
551         }
552
553         return exists $targets{$target};
554 }
555
556 }
557
558 =head1 SEE ALSO
559
560 L<debhelper(7)>
561
562 This program is a part of debhelper.
563
564 =head1 AUTHOR
565
566 Joey Hess <joeyh@debian.org>
567
568 =cut