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