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 Another common case is wanting to do something manually before or
155 after a particular debhelper command is run.
161 override_dh_fixperms:
163 chmod 4755 debian/foo/usr/bin/foo
165 If your package is a Python package, B<dh> will use B<dh_pysupport> by
166 default. This is how to use B<dh_pycentral> instead.
170 dh $@ --with python-central
172 If your package uses autotools and you want to freshen F<config.sub> and
173 F<config.guess> with newer versions from the B<autotools-dev> package
174 at build time, you can use some commands provided in B<autotools-dev>
175 that automate it, like this.
179 dh $@ --with autotools_dev
181 Here is how to force use of Perl's B<Module::Build> build system,
182 which can be necessary if debhelper wrongly detects that the package
187 dh $@ --buildsystem=perl_build
189 To patch your package using quilt, you can tell B<dh> to use quilt's B<dh>
190 sequence addons like this:
196 Here is an example of overriding where the B<dh_auto_>I<*> commands find
197 the package's source, for a package where the source is located in a
202 dh $@ --sourcedirectory=src
204 And here is an example of how to tell the B<dh_auto_>I<*> commands to build
205 in a subdirectory, which will be removed on B<clean>.
209 dh $@ --builddirectory=build
211 If your package can be built in parallel, you can support parallel building
212 as follows. Then B<dpkg-buildpackage -j> will work.
218 Here is a way to prevent B<dh> from running several commands that you don't
219 want it to run, by defining empty override targets for each command.
225 # Commands not to run:
226 override_dh_auto_test override_dh_compress override_dh_fixperms:
228 Sometimes, you may need to make an override target only run commands when a
229 particular package is being built. This can be accomplished using
230 L<dh_listpackages(1)> to test what is being built. For example:
236 override_dh_fixperms:
238 ifneq (,$(filter foo, $(shell dh_listpackages)))
239 chmod 4755 debian/foo/usr/bin/foo
242 Finally, remember that you are not limited to using override targets in the
243 rules file when using B<dh>. You can also explicitly define any of the regular
244 rules file targets when it makes sense to do so. A common reason to do this
245 is if your package needs different B<build-arch> and B<build-indep> targets. For
246 example, a package with a long document build process can put it in
247 B<build-indep> to avoid build daemons redundantly building the documentation.
253 build: build-arch build-indep ;
261 If you're curious about B<dh>'s internals, here's how it works under the hood.
263 Each debhelper command will record when it's successfully run in
264 F<debian/package.debhelper.log>. (Which B<dh_clean> deletes.) So B<dh> can tell
265 which commands have already been run, for which packages, and skip running
266 those commands again.
268 Each time B<dh> is run, it examines the log, and finds the last logged command
269 that is in the specified sequence. It then continues with the next command
270 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
271 options can override this behavior.
273 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
274 through to debhelper commands that are run inside override targets. The
275 contents (and indeed, existence) of this environment variable, as the name
276 might suggest, is subject to change at any time.
280 # Stash this away before init modifies it.
283 # python-support is enabled by default, at least for now
284 # (and comes first so python-central loads later and can disable it).
285 unshift @ARGV, "--with=python-support";
288 "until=s" => \$dh{UNTIL},
289 "after=s" => \$dh{AFTER},
290 "before=s" => \$dh{BEFORE},
291 "remaining" => \$dh{REMAINING},
293 my ($option,$value)=@_;
294 push @{$dh{WITH}},split(",", $value);
297 my ($option,$value)=@_;
298 @{$dh{WITH}} = grep { $_ ne $value } @{$dh{WITH}};
300 "l" => \&list_addons,
301 "list" => \&list_addons,
303 # Disable complaints about unknown options; they are passed on to
304 # the debhelper commands.
305 ignore_unknown_options => 1,
306 # Bundling does not work well since there are unknown options.
312 # If make is using a jobserver, but it is not available
313 # to this process, clean out MAKEFLAGS. This avoids
314 # ugly warnings when calling make.
315 if (is_make_jobserver_unavailable()) {
316 clean_jobserver_makeflags();
319 # Definitions of sequences.
321 $sequences{build} = [qw{
327 $sequences{'build-indep'} = [@{$sequences{build}}];
328 $sequences{'build-arch'} = [@{$sequences{build}}];
329 $sequences{clean} = [qw{
375 $sequences{'install'} = [@{$sequences{build}}, @i];
376 $sequences{'install-indep'} = [@{$sequences{'build-indep'}}, @i];
377 $sequences{'install-arch'} = [@{$sequences{'build-arch'}}, @i];
389 $sequences{binary} = [@{$sequences{install}}, @ba, @b];
390 $sequences{'binary-indep'} = [@{$sequences{'install-indep'}}, @b];
391 $sequences{'binary-arch'} = [@{$sequences{'install-arch'}}, @ba, @b];
393 # Additional command options
396 # sequence addon interface
401 foreach my $sequence (keys %sequences) {
402 my @list=@{$sequences{$sequence}};
403 next unless grep $existing, @list;
405 foreach my $command (@list) {
406 if ($command eq $existing) {
407 push @new, $new if $offset < 0;
409 push @new, $new if $offset > 0;
415 $sequences{$sequence}=\@new;
426 foreach my $sequence (keys %sequences) {
427 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
434 unshift @{$sequences{$sequence}}, $command;
436 sub add_command_options {
438 push @{$command_opts{$command}}, @_;
440 sub remove_command_options {
443 # Remove only specified options
444 if (my $opts = $command_opts{$command}) {
445 foreach my $opt (@_) {
446 $opts = [ grep { $_ ne $opt } @$opts ];
448 $command_opts{$command} = $opts;
452 # Clear all additional options
453 delete $command_opts{$command};
461 eval q{use File::Spec};
462 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Sequence");
464 for my $module_path (glob "$path/*.pm") {
465 my $name = basename($module_path);
473 for my $name (sort keys %addons) {
480 foreach my $addon (@{$dh{WITH}}) {
481 my $mod="Debian::Debhelper::Sequence::$addon";
485 error("unable to load addon $addon: $@");
491 # From v8, the sequence is the very first parameter.
492 $sequence=shift @ARGV_orig;
493 if ($sequence=~/^-/) {
494 error "Unknown sequence $sequence (options should not come before the sequence)";
498 # Before v8, the sequence could be at any position in the parameters,
499 # so was what was left after parsing.
501 if (defined $sequence) {
502 @ARGV_orig=grep { $_ ne $sequence } @ARGV_orig;
505 if (! defined $sequence) {
506 error "specify a sequence to run";
508 if ($sequence eq 'debian/rules' ||
509 $sequence =~ /^override_dh_/) {
510 # make -B causes the rules file to be run as a target.
511 # Also support completly empty override targets.
514 elsif (! exists $sequences{$sequence}) {
515 error "Unknown sequence $sequence (choose from: ".
516 join(" ", sort keys %sequences).")";
518 my @sequence=@{$sequences{$sequence}};
520 # The list of all packages that can be acted on.
521 my @packages=@{$dh{DOPACKAGES}};
523 # Get the options to pass to commands in the sequence.
524 # Filter out options intended only for this program.
526 if ($sequence eq 'build-arch' ||
527 $sequence eq 'install-arch' ||
528 $sequence eq 'binary-arch') {
530 # as an optimisation, remove from the list any packages
531 # that are not arch dependent
532 my %arch_packages = map { $_ => 1 } getpackages("arch");
533 @packages = grep { $arch_packages{$_} } @packages;
535 elsif ($sequence eq 'build-indep' ||
536 $sequence eq 'install-indep' ||
537 $sequence eq 'binary-indep') {
539 # ditto optimisation for arch indep
540 my %indep_packages = map { $_ => 1 } getpackages("indep");
541 @packages = grep { $indep_packages{$_} } @packages;
544 my $opt=shift @ARGV_orig;
545 if ($opt =~ /^--?(after|until|before|with|without)$/) {
549 elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with|without)=)/) {
553 push @options, "-O".$opt;
556 if ($options[$#options]=~/^-O--/) {
557 $options[$#options].="=".$opt;
560 $options[$#options].=$opt;
565 # Figure out at what point in the sequence to start for each package.
568 foreach my $package (@packages) {
569 my @log=load_log($package, \%logged);
571 # Run commands in the sequence that come after the
573 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
574 # Write a dummy log entry indicating that the specified
575 # command was, in fact, run. This handles the case where
576 # no commands remain to run after it, communicating to
577 # future dh instances that the specified command should not
579 write_log($sequence[$startpoint{$package}-1], $package);
581 elsif ($dh{REMAINING}) {
582 # Start at the beginning so all remaining commands will get
584 $startpoint{$package}=0;
587 # Find the last logged command that is in the sequence, and
588 # continue with the next command after it. If no logged
589 # command is in the sequence, we're starting at the beginning..
590 $startpoint{$package}=0;
591 COMMAND: foreach my $command (reverse @log) {
592 foreach my $i (0..$#sequence) {
593 if ($command eq $sequence[$i]) {
594 $startpoint{$package}=$i+1;
602 # Figure out what point in the sequence to go to.
603 my $stoppoint=$#sequence;
605 $stoppoint=command_pos($dh{UNTIL}, @sequence);
607 elsif ($dh{BEFORE}) {
608 $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
611 # Now run the commands in the sequence.
612 foreach my $i (0..$stoppoint) {
613 # Figure out which packages need to run this command.
615 foreach my $package (@packages) {
616 if ($startpoint{$package} > $i ||
617 $logged{$package}{$sequence[$i]}) {
618 push @exclude, $package;
622 if (@exclude eq @packages) {
623 # Command already done for all packages.
627 run($sequence[$i], \@packages, \@exclude, @options);
632 my @packages=@{shift()};
633 my @exclude=@{shift()};
636 # If some packages are excluded, add flags
637 # to prevent them from being acted on.
638 push @options, map { "-N$_" } @exclude;
640 # Check for override targets in debian/rules and
641 # run them instead of running the command directly.
642 my $override_command;
643 my $has_explicit_target = rules_explicit_target("override_".$command);
644 if (defined $has_explicit_target) {
645 $override_command=$command;
646 # Check if target isn't noop
647 if ($has_explicit_target) {
648 # This passes the options through to commands called
650 $ENV{DH_INTERNAL_OPTIONS}=join("\x1e", @options);
651 $ENV{DH_INTERNAL_OVERRIDE}=$command;
652 $command="debian/rules";
653 @options="override_".$override_command;
660 # Pass additional command options if any
661 unshift @options, @{$command_opts{$command}} if exists $command_opts{$command};
664 if (defined $command) {
665 # 3 space indent lines the command being run up under the
666 # sequence name after "dh ".
667 print " ".escape_shell($command, @options)."\n";
670 print " ", "# Skipping ", $override_command, " - empty override", "\n";
674 if (defined $command) {
675 my $ret=system($command, @options);
677 if ($ret >> 8 != 0) {
685 if (defined $override_command) {
686 # Update log for overridden command now that it has
687 # finished successfully.
688 # (But avoid logging for dh_clean since it removes
690 if ($override_command ne 'dh_clean') {
691 my %packages=map { $_ => 1 } @packages;
692 map { delete $packages{$_} } @exclude;
693 write_log($override_command, keys %packages);
694 commit_override_log(keys %packages);
697 delete $ENV{DH_INTERNAL_OPTIONS};
698 delete $ENV{DH_INTERNAL_OVERRIDE};
707 sub rules_explicit_target {
708 # Checks if a specified target exists as an explicit target
710 # undef is returned if target does not exist, 0 if target is noop
711 # and 1 if target has dependencies or executes commands.
714 if (! $rules_parsed) {
715 my $processing_targets = 0;
716 my $not_a_target = 0;
718 open(MAKE, "LC_ALL=C make -Rrnpsf debian/rules debhelper-fail-me 2>/dev/null |");
720 if ($processing_targets) {
721 if (/^# Not a target:/) {
725 if (!$not_a_target && /^([^#:]+)::?\s*(.*)$/) {
726 # Target is defined. NOTE: if it is a depenency of
727 # .PHONY it will be defined too but that's ok.
728 # $2 contains target dependencies if any.
729 $current_target = $1;
730 $targets{$current_target} = ($2) ? 1 : 0;
733 if (defined $current_target) {
735 # Check if target has commands to execute
736 if (/^#\s*commands to execute/) {
737 $targets{$current_target} = 1;
742 $current_target = undef;
746 # "Not a target:" is always followed by
747 # a target name, so resetting this one
752 elsif (/^# Files$/) {
753 $processing_targets = 1;
760 return $targets{$target};
769 This program is a part of debhelper.
773 Joey Hess <joeyh@debian.org>