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