]> git.donarmstrong.com Git - debhelper.git/blob - dh
Apply manual pages conventions
[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> 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>>]
15
16 =head1 DESCRIPTION
17
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>, B<clean>,
20 B<install>, B<binary-arch>, B<binary-indep>, and B<binary>.
21
22 Commands in the B<binary-indep> sequence are passed the B<-i> option to ensure
23 they only work on binary independent packages, and commands in the
24 B<binary-arch> sequences are passed the B<-a> option to ensure they only work
25 on architecture dependent packages.
26
27 If F<debian/rules> contains a target with a name like B<override_>I<dh_command>,
28 then when it would normally run I<dh_command>, B<dh> will instead call that
29 target. The override target can then run the command with additional options,
30 or run entirely different commands instead. See examples below. (Note that to
31 use this feature, you should Build-Depend on debhelper 7.0.50 or above.)
32
33 =head1 OPTIONS
34
35 =over 4
36
37 =item B<--with> I<addon>[B<,>I<addon> ...]
38
39 Add the debhelper commands specified by the given addon to appropriate places
40 in the sequence of commands that is run. This option can be repeated more
41 than once, or multiple addons can be listed, separated by commas.
42 This is used when there is a third-party package that provides
43 debhelper commands. See the F<PROGRAMMING> file for documentation about
44 the sequence addon interface.
45
46 =item B<--without> I<addon>
47
48 The inverse of B<--with>, disables using the given addon.
49
50 =item B<--list>, B<-l>
51
52 List all available addons.
53
54 =item B<--until> I<cmd>
55
56 Run commands in the sequence until and including I<cmd>, then stop.
57
58 =item B<--before> I<cmd>
59
60 Run commands in the sequence before I<cmd>, then stop.
61
62 =item B<--after> I<cmd>
63
64 Run commands in the sequence that come after I<cmd>.
65
66 =item B<--remaining>
67
68 Run all commands in the sequence that have yet to be run.
69
70 =item B<--no-act>
71
72 Prints commands that would run for a given sequence, but does not run them.
73
74 =back
75
76 All other options passed to B<dh> are passed on to each command it runs. This
77 can be used to set an option like B<-v> or B<-X> or B<-N>, as well as for more
78 specialised options.
79
80 In the above options, I<cmd> can be a full name of a debhelper command, or
81 a substring. It'll first search for a command in the sequence exactly
82 matching the name, to avoid any ambiguity. If there are multiple substring
83 matches, the last one in the sequence will be used.
84
85 =cut
86
87 sub command_pos {
88         my $command=shift;
89         my @sequence=@_;
90
91         foreach my $i (0..$#sequence) {
92                 if ($command eq $sequence[$i]) {
93                         return $i;
94                 }
95         }
96
97         my @matches;
98         foreach my $i (0..$#sequence) {
99                 if ($sequence[$i] =~ /\Q$command\E/) {
100                         push @matches, $i;
101                 }
102         }
103         if (! @matches) {
104                 error "command specification \"$command\" does not match any command in the sequence"
105         }
106         else {
107                 return pop @matches;
108         }
109 }
110
111 =head1 EXAMPLES
112
113 To see what commands are included in a sequence, without actually doing
114 anything:
115
116         dh binary-arch --no-act
117
118 This is a very simple rules file, for packages where the default sequences of
119 commands work with no additional options.
120
121         #!/usr/bin/make -f
122         %:
123                 dh $@
124
125 Often you'll want to pass an option to a specific debhelper command. The
126 easy way to do with is by adding an override target for that command.
127         
128         #!/usr/bin/make -f
129         %:
130                 dh $@
131
132         override_dh_strip:
133                 dh_strip -Xfoo
134                 
135         override_dh_installdocs:
136                 dh_installdocs README TODO
137
138 Sometimes the automated L<dh_auto_configure(1)> and L<dh_auto_build(1)>
139 can't guess what to do for a strange package. Here's how to avoid running
140 either and instead run your own commands.
141
142         #!/usr/bin/make -f
143         %:
144                 dh $@
145
146         override_dh_auto_configure:
147                 ./mondoconfig
148
149         override_dh_auto_build:
150                 make universe-explode-in-delight
151
152 Another common case is wanting to do something manually before or
153 after a particular debhelper command is run.
154
155         #!/usr/bin/make -f
156         %:
157                 dh $@
158
159         override_dh_fixperms:
160                 dh_fixperms
161                 chmod 4755 debian/foo/usr/bin/foo
162
163 If your package is a Python package, B<dh> will use B<dh_pysupport> by
164 default. This is how to use B<dh_pycentral> instead.
165
166         #!/usr/bin/make -f
167         %:
168                 dh $@ --with python-central
169
170 If your package uses autotools and you want to freshen F<config.sub> and
171 F<config.guess> with newer versions from the B<autotools-dev> package
172 at build time, you can use some commands provided in B<autotools-dev>
173 that automate it, like this.
174
175         #!/usr/bin/make -f
176         %:
177                 dh $@ --with autotools_dev
178
179 Here is how to force use of Perl's B<Module::Build> build system,
180 which can be necessary if debhelper wrongly detects that the package
181 uses MakeMaker.
182
183         #!/usr/bin/make -f
184         %:
185                 dh $@ --buildsystem=perl_build
186
187 To patch your package using quilt, you can tell B<dh> to use quilt's B<dh>
188 sequence addons like this:
189         
190         #!/usr/bin/make -f
191         %:
192                 dh $@ --with quilt
193
194 Here is an example of overriding where the B<dh_auto_>I<*> commands find
195 the package's source, for a package where the source is located in a
196 subdirectory.
197
198         #!/usr/bin/make -f
199         %:
200                 dh $@ --sourcedirectory=src
201
202 And here is an example of how to tell the B<dh_auto_>I<*> commands to build
203 in a subdirectory, which will be removed on B<clean>.
204
205         #!/usr/bin/make -f
206         %:
207                 dh $@ --builddirectory=build
208
209 If your package can be built in parallel, you can support parallel building
210 as follows. Then B<dpkg-buildpackage -j> will work.
211
212         #!/usr/bin/make -f
213         %:
214                 dh $@ --parallel
215
216 Here is a way to prevent B<dh> from running several commands that you don't
217 want it to run, by defining empty override targets for each command.
218
219         #!/usr/bin/make -f
220         %:
221                 dh $@
222         
223         # Commands not to run:
224         override_dh_auto_test override_dh_compress override_dh_fixperms:
225
226 Sometimes, you may need to make an override target only run commands when a
227 particular package is being built. This can be accomplished using
228 L<dh_listpackages(1)> to test what is being built. For example:
229
230         #!/usr/bin/make -f
231         %:
232                 dh $@
233         
234         override_dh_fixperms:
235                 dh_fixperms
236         ifneq (,$(findstring foo, $(shell dh_listpackages)))
237                 chmod 4755 debian/foo/usr/bin/foo
238         endif
239
240 Finally, remember that you are not limited to using override targets in the
241 rules file when using B<dh>. You can also explicitly define any of the regular
242 rules file targets when it makes sense to do so. A common reason to do this
243 is if your package needs different B<build-arch> and B<build-indep> targets. For
244 example, a package with a long document build process can put it in
245 B<build-indep> to avoid build daemons redundantly building the documentation.
246
247         #!/usr/bin/make -f
248         %:
249                 dh $@
250         
251         build: build-arch build-indep ;
252         build-indep:
253                 $(MAKE) docs
254         build-arch:
255                 $(MAKE) bins
256
257 =head1 INTERNALS
258
259 If you're curious about B<dh>'s internals, here's how it works under the hood.
260
261 Each debhelper command will record when it's successfully run in
262 F<debian/package.debhelper.log>. (Which B<dh_clean> deletes.) So B<dh> can tell
263 which commands have already been run, for which packages, and skip running
264 those commands again.
265
266 Each time B<dh> is run, it examines the log, and finds the last logged command
267 that is in the specified sequence. It then continues with the next command
268 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
269 options can override this behavior.
270
271 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
272 through to debhelper commands that are run inside override targets. The
273 contents (and indeed, existence) of this environment variable, as the name
274 might suggest, is subject to change at any time.
275
276 =cut
277
278 # Stash this away before init modifies it.
279 my @ARGV_orig=@ARGV;
280
281 # python-support is enabled by default, at least for now
282 # (and comes first so python-central loads later and can disable it).
283 unshift @ARGV, "--with=python-support";
284                 
285 init(options => {
286                 "until=s" => \$dh{UNTIL},
287                 "after=s" => \$dh{AFTER},
288                 "before=s" => \$dh{BEFORE},
289                 "remaining" => \$dh{REMAINING},
290                 "with=s" => sub {
291                         my ($option,$value)=@_;
292                         push @{$dh{WITH}},split(",", $value);
293                 },
294                 "without=s" => sub {
295                         my ($option,$value)=@_;
296                         @{$dh{WITH}} = grep { $_ ne $value } @{$dh{WITH}};
297                 },
298                 "l" => \&list_addons,
299                 "list" => \&list_addons,
300         },
301         # Disable complaints about unknown options; they are passed on to 
302         # the debhelper commands.
303         ignore_unknown_options => 1,
304         # Bundling does not work well since there are unknown options.
305         bundling => 0,
306 );
307 inhibit_log();
308
309
310 # If make is using a jobserver, but it is not available
311 # to this process, clean out MAKEFLAGS. This avoids
312 # ugly warnings when calling make.
313 if (is_make_jobserver_unavailable()) {
314         clean_jobserver_makeflags();
315 }
316
317 # Definitions of sequences.
318 my %sequences;
319 $sequences{build} = [qw{
320         dh_testdir
321         dh_auto_configure
322         dh_auto_build
323         dh_auto_test
324 }],
325 $sequences{clean} = [qw{
326         dh_testdir
327         dh_auto_clean
328         dh_clean
329 }];
330 $sequences{install} = [@{$sequences{build}}, qw{
331         dh_testroot
332         dh_prep
333         dh_installdirs
334         dh_auto_install
335
336         dh_install
337         dh_installdocs
338         dh_installchangelogs
339         dh_installexamples
340         dh_installman
341
342         dh_installcatalogs
343         dh_installcron
344         dh_installdebconf
345         dh_installemacsen
346         dh_installifupdown
347         dh_installinfo
348         dh_installinit
349         dh_installmenu
350         dh_installmime
351         dh_installmodules
352         dh_installlogcheck
353         dh_installlogrotate
354         dh_installpam
355         dh_installppp
356         dh_installudev
357         dh_installwm
358         dh_installxfonts
359         dh_bugfiles
360         dh_lintian
361         dh_gconf
362         dh_icons
363         dh_perl
364         dh_usrlocal
365
366         dh_link
367         dh_compress
368         dh_fixperms
369 }];
370 my @b=qw{
371         dh_installdeb
372         dh_gencontrol
373         dh_md5sums
374         dh_builddeb
375 };
376 $sequences{'binary-indep'} = [@{$sequences{install}}, @b];
377 $sequences{binary} = [@{$sequences{install}}, qw{
378         dh_strip
379         dh_makeshlibs
380         dh_shlibdeps
381 }, @b];
382 $sequences{'binary-arch'} = [@{$sequences{binary}}];
383
384 # Additional command options
385 my %command_opts;
386
387 # sequence addon interface
388 sub _insert {
389         my $offset=shift;
390         my $existing=shift;
391         my $new=shift;
392         foreach my $sequence (keys %sequences) {
393                 my @list=@{$sequences{$sequence}};
394                 next unless grep $existing, @list;
395                 my @new;
396                 foreach my $command (@list) {
397                         if ($command eq $existing) {
398                                 push @new, $new if $offset < 0;
399                                 push @new, $command;
400                                 push @new, $new if $offset > 0;
401                         }
402                         else {
403                                 push @new, $command;
404                         }
405                 }
406                 $sequences{$sequence}=\@new;
407         }
408 }
409 sub insert_before {
410         _insert(-1, @_);
411 }
412 sub insert_after {
413         _insert(1, @_);
414 }
415 sub remove_command {
416         my $command=shift;
417         foreach my $sequence (keys %sequences) {
418                 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
419         }
420         
421 }
422 sub add_command {
423         my $command=shift;
424         my $sequence=shift;
425         unshift @{$sequences{$sequence}}, $command;
426 }
427 sub add_command_options {
428         my $command=shift;
429         push @{$command_opts{$command}}, @_;
430 }
431 sub remove_command_options {
432         my $command=shift;
433         if (@_) {
434                 # Remove only specified options
435                 if (my $opts = $command_opts{$command}) {
436                         foreach my $opt (@_) {
437                                 $opts = [ grep { $_ ne $opt } @$opts ];
438                         }
439                         $command_opts{$command} = $opts;
440                 }
441         }
442         else {
443                 # Clear all additional options
444                 delete $command_opts{$command};
445         }
446 }
447
448 sub list_addons {
449         my %addons;
450
451         for my $inc (@INC) {
452                 eval q{use File::Spec};
453                 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Sequence");
454                 if (-d $path) {
455                         for my $module_path (glob "$path/*.pm") {
456                                 my $name = basename($module_path);
457                                 $name =~ s/\.pm$//;
458                                 $name =~ s/_/-/g;
459                                 $addons{$name} = 1;
460                         }
461                 }
462         }
463
464         for my $name (sort keys %addons) {
465                 print "$name\n";
466         }
467
468         exit 0;
469 }
470
471 foreach my $addon (@{$dh{WITH}}) {
472         my $mod="Debian::Debhelper::Sequence::$addon";
473         $mod=~s/-/_/g;
474         eval "use $mod";
475         if ($@) {
476                 error("unable to load addon $addon: $@");
477         }
478 }
479
480 my $sequence;
481 if (! compat(7)) {
482         # From v8, the sequence is the very first parameter.
483         $sequence=shift @ARGV_orig;
484         if ($sequence=~/^-/) {
485                 error "Unknown sequence $sequence (options should not come before the sequence)";
486         }
487 }
488 else {
489         # Before v8, the sequence could be at any position in the parameters,
490         # so was what was left after parsing.
491         $sequence=shift;
492         if (defined $sequence) {
493                 @ARGV_orig=grep { $_ ne $sequence } @ARGV_orig;
494         }
495 }
496 if (! defined $sequence) {
497         error "specify a sequence to run";
498 }
499 if ($sequence eq 'debian/rules' ||
500     $sequence =~ /^override_dh_/) {
501         # make -B causes the rules file to be run as a target.
502         # Also support completly empty override targets.
503         exit 0;
504 }
505 elsif (! exists $sequences{$sequence}) {
506         error "Unknown sequence $sequence (choose from: ".
507                 join(" ", sort keys %sequences).")";
508 }
509 my @sequence=@{$sequences{$sequence}};
510
511 # The list of all packages that can be acted on.
512 my @packages=@{$dh{DOPACKAGES}};
513
514 # Get the options to pass to commands in the sequence.
515 # Filter out options intended only for this program.
516 my @options;
517 if ($sequence eq 'binary-arch') {
518         push @options, "-a";
519         # as an optimisation, remove from the list any packages
520         # that are not arch dependent
521         my %arch_packages = map { $_ => 1 } getpackages("arch");
522         @packages = grep { $arch_packages{$_} } @packages;
523 }
524 elsif ($sequence eq 'binary-indep') {
525         push @options, "-i";
526         # ditto optimisation for arch indep
527         my %indep_packages = map { $_ => 1 } getpackages("indep");
528         @packages = grep { $indep_packages{$_} } @packages;
529 }
530 while (@ARGV_orig) {
531         my $opt=shift @ARGV_orig;
532         if ($opt =~ /^--?(after|until|before|with|without)$/) {
533                 shift @ARGV_orig;
534                 next;
535         }
536         elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with|without)=)/) {
537                 next;
538         }
539         elsif ($opt=~/^-/) {
540                 push @options, "-O".$opt;
541         }
542         elsif (@options) {
543                 if ($options[$#options]=~/^-O--/) {
544                         $options[$#options].="=".$opt;
545                 }
546                 else {
547                         $options[$#options].=$opt;
548                 }
549         }
550 }
551
552 # Figure out at what point in the sequence to start for each package.
553 my %logged;
554 my %startpoint;
555 foreach my $package (@packages) {
556         my @log=load_log($package, \%logged);
557         if ($dh{AFTER}) {
558                 # Run commands in the sequence that come after the
559                 # specified command.
560                 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
561                 # Write a dummy log entry indicating that the specified
562                 # command was, in fact, run. This handles the case where
563                 # no commands remain to run after it, communicating to
564                 # future dh instances that the specified command should not
565                 # be run again.
566                 write_log($sequence[$startpoint{$package}-1], $package);
567         }
568         elsif ($dh{REMAINING}) {
569                 # Start at the beginning so all remaining commands will get
570                 # run.
571                 $startpoint{$package}=0;
572         }
573         else {
574                 # Find the last logged command that is in the sequence, and
575                 # continue with the next command after it. If no logged
576                 # command is in the sequence, we're starting at the beginning..                         
577                 $startpoint{$package}=0;
578 COMMAND:        foreach my $command (reverse @log) {
579                         foreach my $i (0..$#sequence) {
580                                 if ($command eq $sequence[$i]) {
581                                         $startpoint{$package}=$i+1;
582                                         last COMMAND;
583                                 }
584                         }
585                 }
586         }
587 }
588
589 # Figure out what point in the sequence to go to.
590 my $stoppoint=$#sequence;
591 if ($dh{UNTIL}) {
592         $stoppoint=command_pos($dh{UNTIL}, @sequence);
593 }
594 elsif ($dh{BEFORE}) {
595         $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
596 }
597
598 # Now run the commands in the sequence.
599 foreach my $i (0..$stoppoint) {
600         # Figure out which packages need to run this command.
601         my @exclude;
602         foreach my $package (@packages) {
603                 if ($startpoint{$package} > $i ||
604                     $logged{$package}{$sequence[$i]}) {
605                         push @exclude, $package;
606                 }
607         }
608         
609         if (@exclude eq @packages) {
610                 # Command already done for all packages.
611                 next;
612         }
613
614         run($sequence[$i], \@packages, \@exclude, @options);
615 }
616
617 sub run {
618         my $command=shift;
619         my @packages=@{shift()};
620         my @exclude=@{shift()};
621         my @options=@_;
622         
623         # If some packages are excluded, add flags
624         # to prevent them from being acted on.
625         push @options, map { "-N$_" } @exclude;
626
627         # Check for override targets in debian/rules and
628         # run them instead of running the command directly.
629         my $override_command;
630         my $has_explicit_target = rules_explicit_target("override_".$command);
631         if (defined $has_explicit_target) {
632                 $override_command=$command;
633                 # Check if target isn't noop
634                 if ($has_explicit_target) {
635                         # This passes the options through to commands called
636                         # inside the target.
637                         $ENV{DH_INTERNAL_OPTIONS}=join("\x1e", @options);
638                         $command="debian/rules";
639                         @options="override_".$override_command;
640                 }
641                 else {
642                         $command = undef;
643                 }
644         }
645         else {
646                 # Pass additional command options if any
647                 unshift @options, @{$command_opts{$command}} if exists $command_opts{$command};
648         }
649
650         if (defined $command) {
651                 # 3 space indent lines the command being run up under the
652                 # sequence name after "dh ".
653                 print "   ".escape_shell($command, @options)."\n";
654         }
655         else {
656                 print "   ", "# Skipping ", $override_command, " - empty override", "\n";
657         }
658
659         if (! $dh{NO_ACT}) {
660                 if (defined $command) {
661                         my $ret=system($command, @options);
662                         if ($ret >> 8 != 0) {
663                                 exit $ret >> 8;
664                         }
665                         elsif ($ret) {
666                                 exit 1;
667                         }
668                 }
669
670                 if (defined $override_command) {
671                         delete $ENV{DH_INTERNAL_OPTIONS};
672                         # Need to handle logging for overriden commands here,
673                         # because the actual debhelper command may not have
674                         # been run by the rules file target.
675                         # (But avoid logging for dh_clean since it removes
676                         # the log earlier.)
677                         if ($override_command ne 'dh_clean') {
678                                 my %packages=map { $_ => 1 } @packages;
679                                 map { delete $packages{$_} } @exclude;
680                                 write_log($override_command, keys %packages);
681                         }
682                 }
683         }
684 }
685
686 {
687 my %targets;
688 my $rules_parsed;
689
690 sub rules_explicit_target {
691         # Checks if a specified target exists as an explicit target
692         # in debian/rules.
693         # undef is returned if target does not exist, 0 if target is noop
694         # and 1 if target has dependencies or executes commands.
695         my $target=shift;
696
697         if (! $rules_parsed) {
698                 my $processing_targets = 0;
699                 my $not_a_target = 0;
700                 my $current_target;
701                 open(MAKE, "LC_ALL=C make -Rrnpsf debian/rules debhelper-fail-me 2>/dev/null |");
702                 while (<MAKE>) {
703                         if ($processing_targets) {
704                                 if (/^# Not a target:/) {
705                                         $not_a_target = 1;
706                                 }
707                                 else {
708                                         if (!$not_a_target && /^([^#:]+)::?\s*(.*)$/) {
709                                                 # Target is defined. NOTE: if it is a depenency of
710                                                 # .PHONY it will be defined too but that's ok.
711                                                 # $2 contains target dependencies if any.
712                                                 $current_target = $1;
713                                                 $targets{$current_target} = ($2) ? 1 : 0;
714                                         }
715                                         else {
716                                                 if (defined $current_target) {
717                                                         if (/^#/) {
718                                                                 # Check if target has commands to execute
719                                                                 if (/^#\s*commands to execute/) {
720                                                                         $targets{$current_target} = 1;
721                                                                 }
722                                                         }
723                                                         else {
724                                                                 # Target parsed.
725                                                                 $current_target = undef;
726                                                         }
727                                                 }
728                                         }
729                                         # "Not a target:" is always followed by
730                                         # a target name, so resetting this one
731                                         # here is safe.
732                                         $not_a_target = 0;
733                                 }
734                         }
735                         elsif (/^# Files$/) {
736                                 $processing_targets = 1;
737                         }
738                 }
739                 close MAKE;
740                 $rules_parsed = 1;
741         }
742
743         return $targets{$target};
744 }
745
746 }
747
748 =head1 SEE ALSO
749
750 L<debhelper(7)>
751
752 This program is a part of debhelper.
753
754 =head1 AUTHOR
755
756 Joey Hess <joeyh@debian.org>
757
758 =cut