5 dh - debhelper command sequencer
10 use Debian::Debhelper::Dh_Lib;
14 B<dh> I<sequence> [B<--with> I<addon>[B<,>I<addon> ...]] [B<--list>] [S<I<debhelper options>>]
18 B<dh> runs a sequence of debhelper commands. The supported I<sequence>s
19 correspond to the targets of a F<debian/rules> file: B<build-arch>,
20 B<build-indep>, B<build>, B<clean>, B<install-indep>, B<install-arch>,
21 B<install>, B<binary-arch>, B<binary-indep>, and B<binary>.
23 =head1 OVERRIDE TARGETS
25 A F<debian/rules> file using B<dh> can override the command that is run
26 at any step in a sequence, by defining an override target.
28 To override I<dh_command>, add a target named B<override_>I<dh_command> to
29 the rules file. When it would normally run I<dh_command>, B<dh> will
30 instead call that target. The override target can then run the command with
31 additional options, or run entirely different commands instead. See
33 (Note that to use this feature, you should Build-Depend on
34 debhelper 7.0.50 or above.)
36 Override targets can also be defined to run only when building
37 architecture dependent or architecture independent packages.
38 Use targets with names like B<override_>I<dh_command>B<-arch>
39 and B<override_>I<dh_command>B<-indep>.
40 (Note that to use this feature, you should Build-Depend on
41 debhelper 8.9.7 or above.)
47 =item B<--with> I<addon>[B<,>I<addon> ...]
49 Add the debhelper commands specified by the given addon to appropriate places
50 in the sequence of commands that is run. This option can be repeated more
51 than once, or multiple addons can be listed, separated by commas.
52 This is used when there is a third-party package that provides
53 debhelper commands. See the F<PROGRAMMING> file for documentation about
54 the sequence addon interface.
56 =item B<--without> I<addon>
58 The inverse of B<--with>, disables using the given addon.
60 =item B<--list>, B<-l>
62 List all available addons.
66 Prints commands that would run for a given sequence, but does not run them.
70 Other options passed to B<dh> are passed on to each command it runs. This
71 can be used to set an option like B<-v> or B<-X> or B<-N>, as well as for more
76 To see what commands are included in a sequence, without actually doing
79 dh binary-arch --no-act
81 This is a very simple rules file, for packages where the default sequences of
82 commands work with no additional options.
88 Often you'll want to pass an option to a specific debhelper command. The
89 easy way to do with is by adding an override target for that command.
98 override_dh_installdocs:
99 dh_installdocs README TODO
101 Sometimes the automated L<dh_auto_configure(1)> and L<dh_auto_build(1)>
102 can't guess what to do for a strange package. Here's how to avoid running
103 either and instead run your own commands.
109 override_dh_auto_configure:
112 override_dh_auto_build:
113 make universe-explode-in-delight
115 Another common case is wanting to do something manually before or
116 after a particular debhelper command is run.
122 override_dh_fixperms:
124 chmod 4755 debian/foo/usr/bin/foo
126 Python tools are not run by dh by default, due to the continual change
127 in that area. (Before compatability level v9, dh does run B<dh_pysupport>.)
128 Here is how to use B<dh_python2>.
134 If your package uses autotools and you want to freshen F<config.sub> and
135 F<config.guess> with newer versions from the B<autotools-dev> package
136 at build time, you can use some commands provided in B<autotools-dev>
137 that automate it, like this.
141 dh $@ --with autotools_dev
143 Here is how to force use of Perl's B<Module::Build> build system,
144 which can be necessary if debhelper wrongly detects that the package
149 dh $@ --buildsystem=perl_build
151 Here is an example of overriding where the B<dh_auto_>I<*> commands find
152 the package's source, for a package where the source is located in a
157 dh $@ --sourcedirectory=src
159 And here is an example of how to tell the B<dh_auto_>I<*> commands to build
160 in a subdirectory, which will be removed on B<clean>.
164 dh $@ --builddirectory=build
166 If your package can be built in parallel, you can support parallel building
167 as follows. Then B<dpkg-buildpackage -j> will work.
173 Here is a way to prevent B<dh> from running several commands that you don't
174 want it to run, by defining empty override targets for each command.
180 # Commands not to run:
181 override_dh_auto_test override_dh_compress override_dh_fixperms:
183 A long build process for a separate documentation package can
184 be separated out using architecture independent overrides.
185 These will be skipped when running build-arch and binary-arch sequences.
191 override_dh_auto_build-indep:
194 # No tests needed for docs
195 override_dh_auto_test-indep:
197 override_dh_auto_install-indep:
198 $(MAKE) -C docs install
200 Adding to the example above, suppose you need to chmod a file, but only
201 when building the architecture dependent package, as it's not present
202 when building only documentation.
204 override_dh_fixperms-arch:
206 chmod 4755 debian/foo/usr/bin/foo
210 If you're curious about B<dh>'s internals, here's how it works under the hood.
212 Each debhelper command will record when it's successfully run in
213 F<debian/package.debhelper.log>. (Which B<dh_clean> deletes.) So B<dh> can tell
214 which commands have already been run, for which packages, and skip running
215 those commands again.
217 Each time B<dh> is run, it examines the log, and finds the last logged command
218 that is in the specified sequence. It then continues with the next command
219 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
220 options can override this behavior.
222 A sequence can also run dependent targets in debian/rules. For
223 example, the "binary" sequence runs the "install" target.
225 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
226 through to debhelper commands that are run inside override targets. The
227 contents (and indeed, existence) of this environment variable, as the name
228 might suggest, is subject to change at any time.
230 Commands in the B<build-indep>, B<install-indep> and B<binary-indep>
231 sequences are passed the B<-i> option to ensure they only work on
232 architecture independent packages, and commands in the B<build-arch>,
233 B<install-arch> and B<binary-arch> sequences are passed the B<-a>
234 option to ensure they only work on architecture dependent packages.
236 =head1 DEPRECATED OPTIONS
238 The following options are deprecated. It's much
239 better to use override targets instead.
243 =item B<--until> I<cmd>
245 Run commands in the sequence until and including I<cmd>, then stop.
247 =item B<--before> I<cmd>
249 Run commands in the sequence before I<cmd>, then stop.
251 =item B<--after> I<cmd>
253 Run commands in the sequence that come after I<cmd>.
257 Run all commands in the sequence that have yet to be run.
261 In the above options, I<cmd> can be a full name of a debhelper command, or
262 a substring. It'll first search for a command in the sequence exactly
263 matching the name, to avoid any ambiguity. If there are multiple substring
264 matches, the last one in the sequence will be used.
268 # Stash this away before init modifies it.
272 # python-support was enabled by default before v9.
273 # (and comes first so python-central loads later and can disable it).
274 unshift @ARGV, "--with=python-support";
278 "until=s" => \$dh{UNTIL},
279 "after=s" => \$dh{AFTER},
280 "before=s" => \$dh{BEFORE},
281 "remaining" => \$dh{REMAINING},
283 my ($option,$value)=@_;
284 push @{$dh{WITH}},split(",", $value);
287 my ($option,$value)=@_;
288 @{$dh{WITH}} = grep { $_ ne $value } @{$dh{WITH}};
290 "l" => \&list_addons,
291 "list" => \&list_addons,
293 # Disable complaints about unknown options; they are passed on to
294 # the debhelper commands.
295 ignore_unknown_options => 1,
296 # Bundling does not work well since there are unknown options.
303 # If make is using a jobserver, but it is not available
304 # to this process, clean out MAKEFLAGS. This avoids
305 # ugly warnings when calling make.
306 if (is_make_jobserver_unavailable()) {
307 clean_jobserver_makeflags();
310 # Process the sequence parameter.
313 # From v8, the sequence is the very first parameter.
314 $sequence=shift @ARGV_orig;
315 if (defined $sequence && $sequence=~/^-/) {
316 error "Unknown sequence $sequence (options should not come before the sequence)";
320 # Before v8, the sequence could be at any position in the parameters,
321 # so was what was left after parsing.
323 if (defined $sequence) {
324 @ARGV_orig=grep { $_ ne $sequence } @ARGV_orig;
327 if (! defined $sequence) {
328 error "specify a sequence to run";
330 # make -B causes the rules file to be run as a target.
331 # Also support completly empty override targets.
332 # Note: it's not safe to use rules_explicit_target before this check,
333 # since it causes dh to be run.
334 my $dummy_target="debhelper-fail-me";
335 if ($sequence eq 'debian/rules' ||
336 $sequence =~ /^override_dh_/ ||
337 $sequence eq $dummy_target) {
342 # Definitions of sequences.
400 if (! getpackages("arch")) {
409 $sequences{clean} = [qw{
414 $sequences{'build-indep'} = [@bd];
415 $sequences{'build-arch'} = [@bd];
417 # From v9, sequences take standard rules targets into account.
418 $sequences{build} = [@bd_minimal, rules("build-arch"), rules("build-indep")];
419 $sequences{'install-indep'} = [rules("build-indep"), @i];
420 $sequences{'install-arch'} = [rules("build-arch"), @i];
421 $sequences{'install'} = [rules("build"), rules("install-arch"), rules("install-indep"), @i];
422 $sequences{'binary-indep'} = [rules("install-indep"), @b];
423 $sequences{'binary-arch'} = [rules("install-arch"), @ba, @b];
424 $sequences{binary} = [rules("install"), rules("binary-arch"), rules("binary-indep")];
427 $sequences{build} = [@bd];
428 $sequences{'install'} = [@{$sequences{build}}, @i];
429 $sequences{'install-indep'} = [@{$sequences{'build-indep'}}, @i];
430 $sequences{'install-arch'} = [@{$sequences{'build-arch'}}, @i];
431 $sequences{binary} = [@{$sequences{install}}, @ba, @b];
432 $sequences{'binary-indep'} = [@{$sequences{'install-indep'}}, @b];
433 $sequences{'binary-arch'} = [@{$sequences{'install-arch'}}, @ba, @b];
436 # Additional command options
439 # sequence addon interface
444 foreach my $sequence (keys %sequences) {
445 my @list=@{$sequences{$sequence}};
446 next unless grep $existing, @list;
448 foreach my $command (@list) {
449 if ($command eq $existing) {
450 push @new, $new if $offset < 0;
452 push @new, $new if $offset > 0;
458 $sequences{$sequence}=\@new;
469 foreach my $sequence (keys %sequences) {
470 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
477 unshift @{$sequences{$sequence}}, $command;
479 sub add_command_options {
481 push @{$command_opts{$command}}, @_;
483 sub remove_command_options {
486 # Remove only specified options
487 if (my $opts = $command_opts{$command}) {
488 foreach my $opt (@_) {
489 $opts = [ grep { $_ ne $opt } @$opts ];
491 $command_opts{$command} = $opts;
495 # Clear all additional options
496 delete $command_opts{$command};
504 eval q{use File::Spec};
505 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Sequence");
507 for my $module_path (glob "$path/*.pm") {
508 my $name = basename($module_path);
516 for my $name (sort keys %addons) {
523 # Load addons, which can modify sequences.
524 foreach my $addon (@{$dh{WITH}}) {
525 my $mod="Debian::Debhelper::Sequence::$addon";
529 error("unable to load addon $addon: $@");
533 if (! exists $sequences{$sequence}) {
534 error "Unknown sequence $sequence (choose from: ".
535 join(" ", sort keys %sequences).")";
537 my @sequence=optimize_sequence(@{$sequences{$sequence}});
539 # The list of all packages that can be acted on.
540 my @packages=@{$dh{DOPACKAGES}};
542 # Get the options to pass to commands in the sequence.
543 # Filter out options intended only for this program.
545 if ($sequence eq 'build-arch' ||
546 $sequence eq 'install-arch' ||
547 $sequence eq 'binary-arch') {
549 # as an optimisation, remove from the list any packages
550 # that are not arch dependent
551 my %arch_packages = map { $_ => 1 } getpackages("arch");
552 @packages = grep { $arch_packages{$_} } @packages;
554 elsif ($sequence eq 'build-indep' ||
555 $sequence eq 'install-indep' ||
556 $sequence eq 'binary-indep') {
558 # ditto optimisation for arch indep
559 my %indep_packages = map { $_ => 1 } getpackages("indep");
560 @packages = grep { $indep_packages{$_} } @packages;
563 my $opt=shift @ARGV_orig;
564 if ($opt =~ /^--?(after|until|before|with|without)$/) {
568 elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with|without)=)/) {
572 push @options, "-O".$opt;
575 if ($options[$#options]=~/^-O--/) {
576 $options[$#options].="=".$opt;
579 $options[$#options].=$opt;
584 # Figure out at what point in the sequence to start for each package.
587 foreach my $package (@packages) {
588 my @log=load_log($package, \%logged);
590 # Run commands in the sequence that come after the
592 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
593 # Write a dummy log entry indicating that the specified
594 # command was, in fact, run. This handles the case where
595 # no commands remain to run after it, communicating to
596 # future dh instances that the specified command should not
598 write_log($sequence[$startpoint{$package}-1], $package);
600 elsif ($dh{REMAINING}) {
601 # Start at the beginning so all remaining commands will get
603 $startpoint{$package}=0;
606 # Find the last logged command that is in the sequence, and
607 # continue with the next command after it. If no logged
608 # command is in the sequence, we're starting at the beginning..
609 $startpoint{$package}=0;
610 COMMAND: foreach my $command (reverse @log) {
611 foreach my $i (0..$#sequence) {
612 if ($command eq $sequence[$i]) {
613 $startpoint{$package}=$i+1;
621 # Figure out what point in the sequence to go to.
622 my $stoppoint=$#sequence;
624 $stoppoint=command_pos($dh{UNTIL}, @sequence);
626 elsif ($dh{BEFORE}) {
627 $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
630 # Now run the commands in the sequence.
631 foreach my $i (0..$stoppoint) {
632 my $command=$sequence[$i];
634 # Figure out which packages need to run this command.
637 foreach my $package (@packages) {
638 if ($startpoint{$package} > $i ||
639 $logged{$package}{$sequence[$i]}) {
640 push @opts, "-N$package";
643 push @todo, $package;
648 my $rules_target = rules_target($command);
649 if (defined $rules_target) {
650 # Don't pass DH_ environment variables, since this is
651 # a fresh invocation of debian/rules and any sub-dh commands.
652 delete $ENV{DH_INTERNAL_OPTIONS};
653 delete $ENV{DH_INTERNAL_OVERRIDE};
654 run("debian/rules", $rules_target);
658 # Check for override targets in debian/rules, and run instead of
659 # the usual command. (The non-arch-specific override is tried first,
660 # for simplest semantics; mixing it with arch-specific overrides
661 # makes little sense.)
662 foreach my $override_type (undef, "arch", "indep") {
663 @todo = run_override($override_type, $command, \@todo, @opts);
667 run($command, @opts);
674 # Include additional command options if any
675 unshift @options, @{$command_opts{$command}}
676 if exists $command_opts{$command};
678 # 3 space indent lines the command being run up under the
679 # sequence name after "dh ".
680 print " ".escape_shell($command, @options)."\n";
682 return if $dh{NO_ACT};
684 my $ret=system($command, @options);
685 if ($ret >> 8 != 0) {
693 # Tries to run an override target for a command. Returns the list of
694 # packages that it was unable to run an override target for.
696 my $override_type=shift; # arch, indep, or undef
698 my @packages=@{shift()};
701 my $override="override_$command".
702 (defined $override_type ? "-".$override_type : "");
704 # Check which packages are of the right architecture for the
707 if (defined $override_type) {
708 foreach my $package (@packages) {
709 my $isall=package_arch($package) eq 'all';
710 if (($override_type eq 'indep' && $isall) ||
711 ($override_type eq 'arch' && !$isall)) {
712 push @todo, $package;
715 push @rest, $package;
716 push @options, "-N$package";
724 my $has_explicit_target = rules_explicit_target($override);
725 return @packages unless defined $has_explicit_target; # no such override
726 return @rest if ! $has_explicit_target; # has empty override
727 return @rest unless @todo; # has override, but no packages to act on
729 # This passes the options through to commands called
731 $ENV{DH_INTERNAL_OPTIONS}=join("\x1e", @options);
732 $ENV{DH_INTERNAL_OVERRIDE}=$command;
733 run("debian/rules", $override);
734 delete $ENV{DH_INTERNAL_OPTIONS};
735 delete $ENV{DH_INTERNAL_OVERRIDE};
737 # Update log for overridden command now that it has
738 # finished successfully.
739 # (But avoid logging for dh_clean since it removes
741 if (! $dh{NO_ACT} && $command ne 'dh_clean') {
742 write_log($command, @todo);
743 commit_override_log(@todo);
749 sub optimize_sequence {
753 # commands can appear multiple times when sequences are
754 # inlined together; only the first should be needed
756 if (! $seen{$command}) {
758 push @sequence, $command;
761 foreach my $command (@_) {
762 my $rules_target=rules_target($command);
763 if (defined $rules_target &&
764 ! defined rules_explicit_target($rules_target)) {
765 # inline the sequence for this implicit target
766 $add->($_) foreach optimize_sequence(@{$sequences{$rules_target}});
777 if ($command =~ /^debian\/rules\s+(.*)/) {
786 return "debian/rules ".join(" ", @_);
793 sub rules_explicit_target {
794 # Checks if a specified target exists as an explicit target
796 # undef is returned if target does not exist, 0 if target is noop
797 # and 1 if target has dependencies or executes commands.
800 if (! $rules_parsed) {
801 my $processing_targets = 0;
802 my $not_a_target = 0;
804 open(MAKE, "LC_ALL=C make -Rrnpsf debian/rules $dummy_target 2>/dev/null |");
806 if ($processing_targets) {
807 if (/^# Not a target:/) {
811 if (!$not_a_target && /^([^#:]+)::?\s*(.*)$/) {
812 # Target is defined. NOTE: if it is a depenency of
813 # .PHONY it will be defined too but that's ok.
814 # $2 contains target dependencies if any.
815 $current_target = $1;
816 $targets{$current_target} = ($2) ? 1 : 0;
819 if (defined $current_target) {
821 # Check if target has commands to execute
822 if (/^#\s*(commands|recipe) to execute/) {
823 $targets{$current_target} = 1;
828 $current_target = undef;
832 # "Not a target:" is always followed by
833 # a target name, so resetting this one
838 elsif (/^# Files$/) {
839 $processing_targets = 1;
846 return $targets{$target};
851 sub warn_deprecated {
852 foreach my $deprecated ('until', 'after', 'before', 'remaining') {
853 if (defined $dh{uc $deprecated}) {
854 warning("The --$deprecated option is deprecated. Use override targets instead.");
863 foreach my $i (0..$#sequence) {
864 if ($command eq $sequence[$i]) {
870 foreach my $i (0..$#sequence) {
871 if ($sequence[$i] =~ /\Q$command\E/) {
876 error "command specification \"$command\" does not match any command in the sequence"
887 This program is a part of debhelper.
891 Joey Hess <joeyh@debian.org>