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.
60 =item B<--before> I<cmd>
62 Run commands in the sequence before I<cmd>, then stop.
64 =item B<--after> I<cmd>
66 Run commands in the sequence that come after I<cmd>.
70 Run all commands in the sequence that have yet to be run.
74 Prints commands that would run for a given sequence, but does not run them.
78 All other options passed to B<dh> are passed on to each command it runs. This
79 can be used to set an option like B<-v> or B<-X> or B<-N>, as well as for more
82 In the above options, I<cmd> can be a full name of a debhelper command, or
83 a substring. It'll first search for a command in the sequence exactly
84 matching the name, to avoid any ambiguity. If there are multiple substring
85 matches, the last one in the sequence will be used.
93 foreach my $i (0..$#sequence) {
94 if ($command eq $sequence[$i]) {
100 foreach my $i (0..$#sequence) {
101 if ($sequence[$i] =~ /\Q$command\E/) {
106 error "command specification \"$command\" does not match any command in the sequence"
115 To see what commands are included in a sequence, without actually doing
118 dh binary-arch --no-act
120 This is a very simple rules file, for packages where the default sequences of
121 commands work with no additional options.
127 Often you'll want to pass an option to a specific debhelper command. The
128 easy way to do with is by adding an override target for that command.
137 override_dh_installdocs:
138 dh_installdocs README TODO
140 Sometimes the automated L<dh_auto_configure(1)> and L<dh_auto_build(1)>
141 can't guess what to do for a strange package. Here's how to avoid running
142 either and instead run your own commands.
148 override_dh_auto_configure:
151 override_dh_auto_build:
152 make universe-explode-in-delight
154 If running a configure script, it may be necessary to prevent it being
155 run twice, once for architecture-independent packages, and again for
156 architecture-dependent packages. This may be accomplished by
157 overriding L<dh_autoconfigure(1)>:
159 override_dh_auto_configure: config.status
162 dh_auto_configure -- $configure_options
164 Another common case is wanting to do something manually before or
165 after a particular debhelper command is run.
171 override_dh_fixperms:
173 chmod 4755 debian/foo/usr/bin/foo
175 If your package is a Python package, B<dh> will use B<dh_pysupport> by
176 default. This is how to use B<dh_pycentral> instead.
180 dh $@ --with python-central
182 If your package uses autotools and you want to freshen F<config.sub> and
183 F<config.guess> with newer versions from the B<autotools-dev> package
184 at build time, you can use some commands provided in B<autotools-dev>
185 that automate it, like this.
189 dh $@ --with autotools_dev
191 Here is how to force use of Perl's B<Module::Build> build system,
192 which can be necessary if debhelper wrongly detects that the package
197 dh $@ --buildsystem=perl_build
199 To patch your package using quilt, you can tell B<dh> to use quilt's B<dh>
200 sequence addons like this:
206 Here is an example of overriding where the B<dh_auto_>I<*> commands find
207 the package's source, for a package where the source is located in a
212 dh $@ --sourcedirectory=src
214 And here is an example of how to tell the B<dh_auto_>I<*> commands to build
215 in a subdirectory, which will be removed on B<clean>.
219 dh $@ --builddirectory=build
221 If your package can be built in parallel, you can support parallel building
222 as follows. Then B<dpkg-buildpackage -j> will work.
228 Here is a way to prevent B<dh> from running several commands that you don't
229 want it to run, by defining empty override targets for each command.
235 # Commands not to run:
236 override_dh_auto_test override_dh_compress override_dh_fixperms:
238 Sometimes, you may need to make an override target only run commands when a
239 particular package is being built. This can be accomplished using
240 L<dh_listpackages(1)> to test what is being built. For example:
246 override_dh_fixperms:
248 ifneq (,$(filter foo, $(shell dh_listpackages)))
249 chmod 4755 debian/foo/usr/bin/foo
252 Finally, remember that you are not limited to using override targets in the
253 rules file when using B<dh>. You can also explicitly define the regular
254 rules file targets when it makes sense to do so. A common reason to do this
255 is if your package needs different B<build-arch> and B<build-indep> targets.
256 For example, a package with a long document build process can put it in
263 binary: binary-arch binary-indep ;
264 binary-arch:: build-arch
265 binary-indep:: build-indep
266 build: build-arch build-indep ;
274 If you're curious about B<dh>'s internals, here's how it works under the hood.
276 Each debhelper command will record when it's successfully run in
277 F<debian/package.debhelper.log>. (Which B<dh_clean> deletes.) So B<dh> can tell
278 which commands have already been run, for which packages, and skip running
279 those commands again.
281 Each time B<dh> is run, it examines the log, and finds the last logged command
282 that is in the specified sequence. It then continues with the next command
283 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
284 options can override this behavior.
286 A sequence can also run dependent targets in debian/rules. For
287 example, the "binary" sequence runs the "install" target. This will
288 show up in the dh output as "debian/rules install", but internally
289 will be called "rules:install" in the sequence. The "install"
290 sequence likewise runs "debian/rules build", internally named
293 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
294 through to debhelper commands that are run inside override targets. The
295 contents (and indeed, existence) of this environment variable, as the name
296 might suggest, is subject to change at any time.
300 # Stash this away before init modifies it.
303 # python-support is enabled by default, at least for now
304 # (and comes first so python-central loads later and can disable it).
305 unshift @ARGV, "--with=python-support";
308 "until=s" => \$dh{UNTIL},
309 "after=s" => \$dh{AFTER},
310 "before=s" => \$dh{BEFORE},
311 "remaining" => \$dh{REMAINING},
313 my ($option,$value)=@_;
314 push @{$dh{WITH}},split(",", $value);
317 my ($option,$value)=@_;
318 @{$dh{WITH}} = grep { $_ ne $value } @{$dh{WITH}};
320 "l" => \&list_addons,
321 "list" => \&list_addons,
323 # Disable complaints about unknown options; they are passed on to
324 # the debhelper commands.
325 ignore_unknown_options => 1,
326 # Bundling does not work well since there are unknown options.
332 # If make is using a jobserver, but it is not available
333 # to this process, clean out MAKEFLAGS. This avoids
334 # ugly warnings when calling make.
335 if (is_make_jobserver_unavailable()) {
336 clean_jobserver_makeflags();
339 # Definitions of sequences.
350 # rules:build-arch and rules:build-indep are not called by build,
351 # as an optimisation (code below will adjust this if explicit targets exist).
352 $sequences{build} = [@bd];
353 $sequences{'build-indep'} = [@bd];
354 $sequences{'build-arch'} = [@bd];
355 $sequences{clean} = [qw{
405 # The install sequences will call rules:build before running
406 # the standard sequence. rules:install-arch and rules:install-indep
407 # are not called by install, as an optimisation (code below will adjust
408 # this if explicit targets exist).
409 $sequences{'install'} = ['rules:build', @i, 'rules:install-arch', 'rules:install-indep'];
410 $sequences{'install-indep'} = ['rules:build-indep', @i];
411 $sequences{'install-arch'} = ['rules:build-arch', @i];
423 # The binary sequences will call 'debian/rules install' before running
424 # the standard sequence.
425 $sequences{binary} = ['rules:install', 'rules:binary-arch', 'rules:binary-indep'];
426 $sequences{'binary-indep'} = ['rules:install-indep', @b];
427 $sequences{'binary-arch'} = ['rules:install-arch', @ba, @b];
429 # Additional command options
432 # sequence addon interface
437 foreach my $sequence (keys %sequences) {
438 my @list=@{$sequences{$sequence}};
439 next unless grep $existing, @list;
441 foreach my $command (@list) {
442 if ($command eq $existing) {
443 push @new, $new if $offset < 0;
445 push @new, $new if $offset > 0;
451 $sequences{$sequence}=\@new;
462 foreach my $sequence (keys %sequences) {
463 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
470 unshift @{$sequences{$sequence}}, $command;
472 sub add_command_options {
474 push @{$command_opts{$command}}, @_;
476 sub remove_command_options {
479 # Remove only specified options
480 if (my $opts = $command_opts{$command}) {
481 foreach my $opt (@_) {
482 $opts = [ grep { $_ ne $opt } @$opts ];
484 $command_opts{$command} = $opts;
488 # Clear all additional options
489 delete $command_opts{$command};
497 eval q{use File::Spec};
498 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Sequence");
500 for my $module_path (glob "$path/*.pm") {
501 my $name = basename($module_path);
509 for my $name (sort keys %addons) {
516 foreach my $addon (@{$dh{WITH}}) {
517 my $mod="Debian::Debhelper::Sequence::$addon";
521 error("unable to load addon $addon: $@");
527 # From v8, the sequence is the very first parameter.
528 $sequence=shift @ARGV_orig;
529 if ($sequence=~/^-/) {
530 error "Unknown sequence $sequence (options should not come before the sequence)";
534 # Before v8, the sequence could be at any position in the parameters,
535 # so was what was left after parsing.
537 if (defined $sequence) {
538 @ARGV_orig=grep { $_ ne $sequence } @ARGV_orig;
541 if (! defined $sequence) {
542 error "specify a sequence to run";
544 # make -B causes the rules file to be run as a target.
545 # Also support completly empty override targets.
546 # Note: it's not safe to use rules_explicit_target before this check.
547 if ($sequence eq 'debian/rules' ||
548 $sequence =~ /^override_dh_/) {
551 elsif (! exists $sequences{$sequence}) {
552 error "Unknown sequence $sequence (choose from: ".
553 join(" ", sort keys %sequences).")";
556 # If debian/rules defines build-arch or build-indep, run sequences separately.
557 if (rules_explicit_target('build-arch') ||
558 rules_explicit_target('build-indep')) {
559 $sequences{build} = [@bd_minimal, 'rules:build-arch', 'rules:build-indep'];
561 # If debian/rules defines install-arch or install-indep, run sequences
563 if (rules_explicit_target('install-arch') ||
564 rules_explicit_target('install-indep')) {
565 $sequences{'install'} = ['rules:build', @i_minimal, 'rules:install-arch', 'rules:install-indep'];
568 my @sequence=@{$sequences{$sequence}};
570 # The list of all packages that can be acted on.
571 my @packages=@{$dh{DOPACKAGES}};
573 # Get the options to pass to commands in the sequence.
574 # Filter out options intended only for this program.
576 if ($sequence eq 'build-arch' ||
577 $sequence eq 'install-arch' ||
578 $sequence eq 'binary-arch') {
580 # as an optimisation, remove from the list any packages
581 # that are not arch dependent
582 my %arch_packages = map { $_ => 1 } getpackages("arch");
583 @packages = grep { $arch_packages{$_} } @packages;
585 elsif ($sequence eq 'build-indep' ||
586 $sequence eq 'install-indep' ||
587 $sequence eq 'binary-indep') {
589 # ditto optimisation for arch indep
590 my %indep_packages = map { $_ => 1 } getpackages("indep");
591 @packages = grep { $indep_packages{$_} } @packages;
594 my $opt=shift @ARGV_orig;
595 if ($opt =~ /^--?(after|until|before|with|without)$/) {
599 elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with|without)=)/) {
603 push @options, "-O".$opt;
606 if ($options[$#options]=~/^-O--/) {
607 $options[$#options].="=".$opt;
610 $options[$#options].=$opt;
615 # Figure out at what point in the sequence to start for each package.
618 foreach my $package (@packages) {
619 my @log=load_log($package, \%logged);
621 # Run commands in the sequence that come after the
623 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
624 # Write a dummy log entry indicating that the specified
625 # command was, in fact, run. This handles the case where
626 # no commands remain to run after it, communicating to
627 # future dh instances that the specified command should not
629 write_log($sequence[$startpoint{$package}-1], $package);
631 elsif ($dh{REMAINING}) {
632 # Start at the beginning so all remaining commands will get
634 $startpoint{$package}=0;
637 # Find the last logged command that is in the sequence, and
638 # continue with the next command after it. If no logged
639 # command is in the sequence, we're starting at the beginning..
640 $startpoint{$package}=0;
641 COMMAND: foreach my $command (reverse @log) {
642 foreach my $i (0..$#sequence) {
643 if ($command eq $sequence[$i]) {
644 $startpoint{$package}=$i+1;
652 # Figure out what point in the sequence to go to.
653 my $stoppoint=$#sequence;
655 $stoppoint=command_pos($dh{UNTIL}, @sequence);
657 elsif ($dh{BEFORE}) {
658 $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
661 # Now run the commands in the sequence.
662 foreach my $i (0..$stoppoint) {
663 # Figure out which packages need to run this command.
665 foreach my $package (@packages) {
666 if ($startpoint{$package} > $i ||
667 $logged{$package}{$sequence[$i]}) {
668 push @exclude, $package;
672 if (@exclude eq @packages) {
673 # Command already done for all packages.
677 run($sequence[$i], \@packages, \@exclude, @options);
682 my @packages=@{shift()};
683 my @exclude=@{shift()};
686 # If some packages are excluded, add flags
687 # to prevent them from being acted on.
688 push @options, map { "-N$_" } @exclude;
690 # If the command has a rules: prefix, run debian/rules with
691 # the remainder as the target.
692 my $rules_target = undef;
693 if ($command =~ /^rules:(.*)/) {
697 # Check for override targets in debian/rules and
698 # run them instead of running the command directly.
699 my $override_command;
700 my $has_explicit_target = rules_explicit_target("override_".$command);
702 if (defined $rules_target) {
703 # Don't pass DH_ environment variables, since this is
704 # a fresh invocation of debian/rules and any sub-dh
706 $override_command=$command;
707 delete $ENV{DH_INTERNAL_OPTIONS};
708 delete $ENV{DH_INTERNAL_OVERRIDE};
709 $command="debian/rules";
710 @options=$rules_target;
712 elsif (defined $has_explicit_target) {
713 $override_command=$command;
714 # Check if target isn't noop
715 if ($has_explicit_target) {
716 # This passes the options through to commands called
718 $ENV{DH_INTERNAL_OPTIONS}=join("\x1e", @options);
719 $ENV{DH_INTERNAL_OVERRIDE}=$command;
720 $command="debian/rules";
721 @options="override_".$override_command;
728 # Pass additional command options if any
729 unshift @options, @{$command_opts{$command}} if exists $command_opts{$command};
732 if (defined $command) {
733 # 3 space indent lines the command being run up under the
734 # sequence name after "dh ".
735 print " ".escape_shell($command, @options)."\n";
738 print " ", "# Skipping ", $override_command, " - empty override", "\n";
742 if (defined $command) {
743 my $ret=system($command, @options);
745 if ($ret >> 8 != 0) {
753 if (defined $override_command) {
754 # Update log for overridden command now that it has
755 # finished successfully.
756 # (But avoid logging for dh_clean since it removes
758 if ($override_command ne 'dh_clean') {
759 my %packages=map { $_ => 1 } @packages;
760 map { delete $packages{$_} } @exclude;
761 write_log($override_command, keys %packages);
762 commit_override_log(keys %packages);
765 delete $ENV{DH_INTERNAL_OPTIONS};
766 delete $ENV{DH_INTERNAL_OVERRIDE};
775 sub rules_explicit_target {
776 # Checks if a specified target exists as an explicit target
778 # undef is returned if target does not exist, 0 if target is noop
779 # and 1 if target has dependencies or executes commands.
782 if (! $rules_parsed) {
783 my $processing_targets = 0;
784 my $not_a_target = 0;
786 open(MAKE, "LC_ALL=C make -Rrnpsf debian/rules debhelper-fail-me 2>/dev/null |");
788 if ($processing_targets) {
789 if (/^# Not a target:/) {
793 if (!$not_a_target && /^([^#:]+)::?\s*(.*)$/) {
794 # Target is defined. NOTE: if it is a depenency of
795 # .PHONY it will be defined too but that's ok.
796 # $2 contains target dependencies if any.
797 $current_target = $1;
798 $targets{$current_target} = ($2) ? 1 : 0;
801 if (defined $current_target) {
803 # Check if target has commands to execute
804 if (/^#\s*commands to execute/) {
805 $targets{$current_target} = 1;
810 $current_target = undef;
814 # "Not a target:" is always followed by
815 # a target name, so resetting this one
820 elsif (/^# Files$/) {
821 $processing_targets = 1;
828 return $targets{$target};
837 This program is a part of debhelper.
841 Joey Hess <joeyh@debian.org>