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>] [B<--until> I<cmd>] [B<--before> I<cmd>] [B<--after> I<cmd>] [B<--remaining>] [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 Commands in the B<build-indep>, B<install-indep> and B<binary-indep>
24 sequences are passed the B<-i> option to ensure they only work on
25 architecture independent packages, and commands in the B<build-arch>,
26 B<install-arch> and B<binary-arch> sequences are passed the B<-a>
27 option to ensure they only work on architecture dependent packages.
29 If F<debian/rules> contains a target with a name like B<override_>I<dh_command>,
30 then when it would normally run I<dh_command>, B<dh> will instead call that
31 target. The override target can then run the command with additional options,
32 or run entirely different commands instead. See examples below. (Note that to
33 use this feature, you should Build-Depend on debhelper 7.0.50 or above.)
39 =item B<--with> I<addon>[B<,>I<addon> ...]
41 Add the debhelper commands specified by the given addon to appropriate places
42 in the sequence of commands that is run. This option can be repeated more
43 than once, or multiple addons can be listed, separated by commas.
44 This is used when there is a third-party package that provides
45 debhelper commands. See the F<PROGRAMMING> file for documentation about
46 the sequence addon interface.
48 =item B<--without> I<addon>
50 The inverse of B<--with>, disables using the given addon.
52 =item B<--list>, B<-l>
54 List all available addons.
56 =item B<--until> I<cmd>
58 Run commands in the sequence until and including I<cmd>, then stop.
61 =item B<--before> I<cmd>
63 Run commands in the sequence before I<cmd>, then stop.
66 =item B<--after> I<cmd>
68 Run commands in the sequence that come after I<cmd>.
73 Run all commands in the sequence that have yet to be run.
78 Prints commands that would run for a given sequence, but does not run them.
82 All other options passed to B<dh> are passed on to each command it runs. This
83 can be used to set an option like B<-v> or B<-X> or B<-N>, as well as for more
86 In the above options, I<cmd> can be a full name of a debhelper command, or
87 a substring. It'll first search for a command in the sequence exactly
88 matching the name, to avoid any ambiguity. If there are multiple substring
89 matches, the last one in the sequence will be used.
97 foreach my $i (0..$#sequence) {
98 if ($command eq $sequence[$i]) {
104 foreach my $i (0..$#sequence) {
105 if ($sequence[$i] =~ /\Q$command\E/) {
110 error "command specification \"$command\" does not match any command in the sequence"
119 To see what commands are included in a sequence, without actually doing
122 dh binary-arch --no-act
124 This is a very simple rules file, for packages where the default sequences of
125 commands work with no additional options.
131 Often you'll want to pass an option to a specific debhelper command. The
132 easy way to do with is by adding an override target for that command.
141 override_dh_installdocs:
142 dh_installdocs README TODO
144 Sometimes the automated L<dh_auto_configure(1)> and L<dh_auto_build(1)>
145 can't guess what to do for a strange package. Here's how to avoid running
146 either and instead run your own commands.
152 override_dh_auto_configure:
155 override_dh_auto_build:
156 make universe-explode-in-delight
158 Another common case is wanting to do something manually before or
159 after a particular debhelper command is run.
165 override_dh_fixperms:
167 chmod 4755 debian/foo/usr/bin/foo
169 Python tools are not run by dh by default, due to the continual change
170 in that area. (Before compatability level v9, dh does run B<dh_pysupport>.)
171 Here is how to use B<dh_python2>.
177 If your package uses autotools and you want to freshen F<config.sub> and
178 F<config.guess> with newer versions from the B<autotools-dev> package
179 at build time, you can use some commands provided in B<autotools-dev>
180 that automate it, like this.
184 dh $@ --with autotools_dev
186 Here is how to force use of Perl's B<Module::Build> build system,
187 which can be necessary if debhelper wrongly detects that the package
192 dh $@ --buildsystem=perl_build
194 To patch your package using quilt, you can tell B<dh> to use quilt's B<dh>
195 sequence addons like this:
201 Here is an example of overriding where the B<dh_auto_>I<*> commands find
202 the package's source, for a package where the source is located in a
207 dh $@ --sourcedirectory=src
209 And here is an example of how to tell the B<dh_auto_>I<*> commands to build
210 in a subdirectory, which will be removed on B<clean>.
214 dh $@ --builddirectory=build
216 If your package can be built in parallel, you can support parallel building
217 as follows. Then B<dpkg-buildpackage -j> will work.
223 Here is a way to prevent B<dh> from running several commands that you don't
224 want it to run, by defining empty override targets for each command.
230 # Commands not to run:
231 override_dh_auto_test override_dh_compress override_dh_fixperms:
233 Sometimes, you may need to make an override target only run commands when a
234 particular package is being built. This can be accomplished using
235 L<dh_listpackages(1)> to test what is being built. For example:
241 override_dh_fixperms:
243 ifneq (,$(filter foo, $(shell dh_listpackages)))
244 chmod 4755 debian/foo/usr/bin/foo
247 Finally, remember that you are not limited to using override targets in the
248 rules file when using B<dh>. You can also explicitly define any of the regular
249 rules file targets when it makes sense to do so. A common reason to do this
250 is when your package needs different B<build-arch> and B<build-indep> targets.
251 For example, a package with a long document build process can put it in
263 Note that in the example above, dh will arrange for "debian/rules build"
264 to call your build-indep and build-arch targets. You do not need to
265 explicitly define those dependencies in the rules file when using dh with
266 compatibility level v9. This example would be more complicated with
267 earlier compatibility levels.
271 If you're curious about B<dh>'s internals, here's how it works under the hood.
273 Each debhelper command will record when it's successfully run in
274 F<debian/package.debhelper.log>. (Which B<dh_clean> deletes.) So B<dh> can tell
275 which commands have already been run, for which packages, and skip running
276 those commands again.
278 Each time B<dh> is run, it examines the log, and finds the last logged command
279 that is in the specified sequence. It then continues with the next command
280 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
281 options can override this behavior.
283 A sequence can also run dependent targets in debian/rules. For
284 example, the "binary" sequence runs the "install" target.
286 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
287 through to debhelper commands that are run inside override targets. The
288 contents (and indeed, existence) of this environment variable, as the name
289 might suggest, is subject to change at any time.
293 # Stash this away before init modifies it.
297 # python-support was enabled by default before v9.
298 # (and comes first so python-central loads later and can disable it).
299 unshift @ARGV, "--with=python-support";
303 "until=s" => \$dh{UNTIL},
304 "after=s" => \$dh{AFTER},
305 "before=s" => \$dh{BEFORE},
306 "remaining" => \$dh{REMAINING},
308 my ($option,$value)=@_;
309 push @{$dh{WITH}},split(",", $value);
312 my ($option,$value)=@_;
313 @{$dh{WITH}} = grep { $_ ne $value } @{$dh{WITH}};
315 "l" => \&list_addons,
316 "list" => \&list_addons,
318 # Disable complaints about unknown options; they are passed on to
319 # the debhelper commands.
320 ignore_unknown_options => 1,
321 # Bundling does not work well since there are unknown options.
328 # If make is using a jobserver, but it is not available
329 # to this process, clean out MAKEFLAGS. This avoids
330 # ugly warnings when calling make.
331 if (is_make_jobserver_unavailable()) {
332 clean_jobserver_makeflags();
335 # Process the sequence parameter.
338 # From v8, the sequence is the very first parameter.
339 $sequence=shift @ARGV_orig;
340 if (defined $sequence && $sequence=~/^-/) {
341 error "Unknown sequence $sequence (options should not come before the sequence)";
345 # Before v8, the sequence could be at any position in the parameters,
346 # so was what was left after parsing.
348 if (defined $sequence) {
349 @ARGV_orig=grep { $_ ne $sequence } @ARGV_orig;
352 if (! defined $sequence) {
353 error "specify a sequence to run";
355 # make -B causes the rules file to be run as a target.
356 # Also support completly empty override targets.
357 # Note: it's not safe to use rules_explicit_target before this check,
358 # since it causes dh to be run.
359 my $dummy_target="debhelper-fail-me";
360 if ($sequence eq 'debian/rules' ||
361 $sequence =~ /^override_dh_/ ||
362 $sequence eq $dummy_target) {
367 # Definitions of sequences.
431 $sequences{clean} = [qw{
436 $sequences{'build-indep'} = [@bd];
437 $sequences{'build-arch'} = [@bd];
439 # From v9, sequences take standard rules targets into account.
440 $sequences{build} = [@bd_minimal, rules("build-arch"), rules("build-indep")];
441 $sequences{'install-indep'} = [rules("build-indep"), @i];
442 $sequences{'install-arch'} = [rules("build-arch"), @i];
443 $sequences{'install'} = [rules("build"), rules("install-arch"), rules("install-indep"), @i];
444 $sequences{'binary-indep'} = [rules("install-indep"), @b];
445 $sequences{'binary-arch'} = [rules("install-arch"), @ba, @b];
446 $sequences{binary} = [rules("install"), rules("binary-arch"), rules("binary-indep")];
449 $sequences{build} = [@bd];
450 $sequences{'install'} = [@{$sequences{build}}, @i];
451 $sequences{'install-indep'} = [@{$sequences{'build-indep'}}, @i];
452 $sequences{'install-arch'} = [@{$sequences{'build-arch'}}, @i];
453 $sequences{binary} = [@{$sequences{install}}, @ba, @b];
454 $sequences{'binary-indep'} = [@{$sequences{'install-indep'}}, @b];
455 $sequences{'binary-arch'} = [@{$sequences{'install-arch'}}, @ba, @b];
458 # Additional command options
461 # sequence addon interface
466 foreach my $sequence (keys %sequences) {
467 my @list=@{$sequences{$sequence}};
468 next unless grep $existing, @list;
470 foreach my $command (@list) {
471 if ($command eq $existing) {
472 push @new, $new if $offset < 0;
474 push @new, $new if $offset > 0;
480 $sequences{$sequence}=\@new;
491 foreach my $sequence (keys %sequences) {
492 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
499 unshift @{$sequences{$sequence}}, $command;
501 sub add_command_options {
503 push @{$command_opts{$command}}, @_;
505 sub remove_command_options {
508 # Remove only specified options
509 if (my $opts = $command_opts{$command}) {
510 foreach my $opt (@_) {
511 $opts = [ grep { $_ ne $opt } @$opts ];
513 $command_opts{$command} = $opts;
517 # Clear all additional options
518 delete $command_opts{$command};
526 eval q{use File::Spec};
527 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Sequence");
529 for my $module_path (glob "$path/*.pm") {
530 my $name = basename($module_path);
538 for my $name (sort keys %addons) {
545 # Load addons, which can modify sequences.
546 foreach my $addon (@{$dh{WITH}}) {
547 my $mod="Debian::Debhelper::Sequence::$addon";
551 error("unable to load addon $addon: $@");
555 if (! exists $sequences{$sequence}) {
556 error "Unknown sequence $sequence (choose from: ".
557 join(" ", sort keys %sequences).")";
559 my @sequence=optimize_sequence(@{$sequences{$sequence}});
561 # The list of all packages that can be acted on.
562 my @packages=@{$dh{DOPACKAGES}};
564 # Get the options to pass to commands in the sequence.
565 # Filter out options intended only for this program.
567 if ($sequence eq 'build-arch' ||
568 $sequence eq 'install-arch' ||
569 $sequence eq 'binary-arch') {
571 # as an optimisation, remove from the list any packages
572 # that are not arch dependent
573 my %arch_packages = map { $_ => 1 } getpackages("arch");
574 @packages = grep { $arch_packages{$_} } @packages;
576 elsif ($sequence eq 'build-indep' ||
577 $sequence eq 'install-indep' ||
578 $sequence eq 'binary-indep') {
580 # ditto optimisation for arch indep
581 my %indep_packages = map { $_ => 1 } getpackages("indep");
582 @packages = grep { $indep_packages{$_} } @packages;
585 my $opt=shift @ARGV_orig;
586 if ($opt =~ /^--?(after|until|before|with|without)$/) {
590 elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with|without)=)/) {
594 push @options, "-O".$opt;
597 if ($options[$#options]=~/^-O--/) {
598 $options[$#options].="=".$opt;
601 $options[$#options].=$opt;
606 # Figure out at what point in the sequence to start for each package.
609 foreach my $package (@packages) {
610 my @log=load_log($package, \%logged);
612 # Run commands in the sequence that come after the
614 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
615 # Write a dummy log entry indicating that the specified
616 # command was, in fact, run. This handles the case where
617 # no commands remain to run after it, communicating to
618 # future dh instances that the specified command should not
620 write_log($sequence[$startpoint{$package}-1], $package);
622 elsif ($dh{REMAINING}) {
623 # Start at the beginning so all remaining commands will get
625 $startpoint{$package}=0;
628 # Find the last logged command that is in the sequence, and
629 # continue with the next command after it. If no logged
630 # command is in the sequence, we're starting at the beginning..
631 $startpoint{$package}=0;
632 COMMAND: foreach my $command (reverse @log) {
633 foreach my $i (0..$#sequence) {
634 if ($command eq $sequence[$i]) {
635 $startpoint{$package}=$i+1;
643 # Figure out what point in the sequence to go to.
644 my $stoppoint=$#sequence;
646 $stoppoint=command_pos($dh{UNTIL}, @sequence);
648 elsif ($dh{BEFORE}) {
649 $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
652 # Now run the commands in the sequence.
653 foreach my $i (0..$stoppoint) {
654 # Figure out which packages need to run this command.
656 foreach my $package (@packages) {
657 if ($startpoint{$package} > $i ||
658 $logged{$package}{$sequence[$i]}) {
659 push @exclude, $package;
663 if (@exclude eq @packages) {
664 # Command already done for all packages.
668 run($sequence[$i], \@packages, \@exclude, @options);
673 my @packages=@{shift()};
674 my @exclude=@{shift()};
677 # If some packages are excluded, add flags
678 # to prevent them from being acted on.
679 push @options, map { "-N$_" } @exclude;
681 # Check for override targets in debian/rules and
682 # run them instead of running the command directly.
683 my $override_command;
684 my $has_explicit_target = rules_explicit_target("override_".$command);
686 my $rules_target = rules_target($command);
687 if (defined $rules_target) {
688 # Don't pass DH_ environment variables, since this is
689 # a fresh invocation of debian/rules and any sub-dh
691 $override_command=$command;
692 delete $ENV{DH_INTERNAL_OPTIONS};
693 delete $ENV{DH_INTERNAL_OVERRIDE};
694 $command="debian/rules";
695 @options=$rules_target;
697 elsif (defined $has_explicit_target) {
698 $override_command=$command;
699 # Check if target isn't noop
700 if ($has_explicit_target) {
701 # This passes the options through to commands called
703 $ENV{DH_INTERNAL_OPTIONS}=join("\x1e", @options);
704 $ENV{DH_INTERNAL_OVERRIDE}=$command;
705 $command="debian/rules";
706 @options="override_".$override_command;
713 # Pass additional command options if any
714 unshift @options, @{$command_opts{$command}} if exists $command_opts{$command};
717 if (defined $command) {
718 # 3 space indent lines the command being run up under the
719 # sequence name after "dh ".
720 print " ".escape_shell($command, @options)."\n";
723 print " ", "# Skipping ", $override_command, " - empty override", "\n";
727 if (defined $command) {
728 my $ret=system($command, @options);
730 if ($ret >> 8 != 0) {
738 if (defined $override_command) {
739 # Update log for overridden command now that it has
740 # finished successfully.
741 # (But avoid logging for dh_clean since it removes
743 if ($override_command ne 'dh_clean') {
744 my %packages=map { $_ => 1 } @packages;
745 map { delete $packages{$_} } @exclude;
746 write_log($override_command, keys %packages);
747 commit_override_log(keys %packages);
750 delete $ENV{DH_INTERNAL_OPTIONS};
751 delete $ENV{DH_INTERNAL_OVERRIDE};
756 sub optimize_sequence {
760 # commands can appear multiple times when sequences are
761 # inlined together; only the first should be needed
763 if (! $seen{$command}) {
765 push @sequence, $command;
768 foreach my $command (@_) {
769 my $rules_target=rules_target($command);
770 if (defined $rules_target &&
771 ! defined rules_explicit_target($rules_target)) {
772 # inline the sequence for this implicit target
773 $add->($_) foreach optimize_sequence(@{$sequences{$rules_target}});
784 if ($command =~ /^debian\/rules\s+(.*)/) {
793 return "debian/rules ".join(" ", @_);
800 sub rules_explicit_target {
801 # Checks if a specified target exists as an explicit target
803 # undef is returned if target does not exist, 0 if target is noop
804 # and 1 if target has dependencies or executes commands.
807 if (! $rules_parsed) {
808 my $processing_targets = 0;
809 my $not_a_target = 0;
811 open(MAKE, "LC_ALL=C make -Rrnpsf debian/rules $dummy_target 2>/dev/null |");
813 if ($processing_targets) {
814 if (/^# Not a target:/) {
818 if (!$not_a_target && /^([^#:]+)::?\s*(.*)$/) {
819 # Target is defined. NOTE: if it is a depenency of
820 # .PHONY it will be defined too but that's ok.
821 # $2 contains target dependencies if any.
822 $current_target = $1;
823 $targets{$current_target} = ($2) ? 1 : 0;
826 if (defined $current_target) {
828 # Check if target has commands to execute
829 if (/^#\s*(commands|recipe) to execute/) {
830 $targets{$current_target} = 1;
835 $current_target = undef;
839 # "Not a target:" is always followed by
840 # a target name, so resetting this one
845 elsif (/^# Files$/) {
846 $processing_targets = 1;
853 return $targets{$target};
858 sub warn_deprecated {
859 foreach my $deprecated ('until', 'after', 'before', 'remaining') {
860 if (defined $dh{uc $deprecated}) {
861 warning("The --$deprecated option is deprecated. Use override targets instead.");
870 This program is a part of debhelper.
874 Joey Hess <joeyh@debian.org>