]> git.donarmstrong.com Git - debhelper.git/blob - dh
Always respect DEB_${flag}_{APPEND,SET} envvars.
[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 In order to override standard build flags, export appropriate environment
197 variables as documented in the L<dpkg-buildflags(1)> manual page. They will be
198 preferred over directly exported their counterparts (CFLAGS, CXXFLAGS, LDFLAGS
199 etc.). For example, to append -Wall to the standard CFLAGS and CXXFLAGS, use:
200
201         #!/usr/bin/make -f
202         export DEB_CFLAGS_APPEND = -Wall
203         export DEB_CXXFLAGS_APPEND = -Wall
204         
205         %:
206                 dh $@
207
208 Here is an example of overriding where the B<dh_auto_>I<*> commands find
209 the package's source, for a package where the source is located in a
210 subdirectory.
211
212         #!/usr/bin/make -f
213         %:
214                 dh $@ --sourcedirectory=src
215
216 And here is an example of how to tell the B<dh_auto_>I<*> commands to build
217 in a subdirectory, which will be removed on B<clean>.
218
219         #!/usr/bin/make -f
220         %:
221                 dh $@ --builddirectory=build
222
223 If your package can be built in parallel, you can support parallel building
224 as follows. Then B<dpkg-buildpackage -j> will work.
225
226         #!/usr/bin/make -f
227         %:
228                 dh $@ --parallel
229
230 Here is a way to prevent B<dh> from running several commands that you don't
231 want it to run, by defining empty override targets for each command.
232
233         #!/usr/bin/make -f
234         %:
235                 dh $@
236         
237         # Commands not to run:
238         override_dh_auto_test override_dh_compress override_dh_fixperms:
239
240 Sometimes, you may need to make an override target only run commands when a
241 particular package is being built. This can be accomplished using
242 L<dh_listpackages(1)> to test what is being built. For example:
243
244         #!/usr/bin/make -f
245         %:
246                 dh $@
247         
248         override_dh_fixperms:
249                 dh_fixperms
250         ifneq (,$(filter foo, $(shell dh_listpackages)))
251                 chmod 4755 debian/foo/usr/bin/foo
252         endif
253
254 Finally, remember that you are not limited to using override targets in the
255 rules file when using B<dh>. You can also explicitly define any of the regular
256 rules file targets when it makes sense to do so. A common reason to do this
257 is when your package needs different B<build-arch> and B<build-indep> targets.
258 For example, a package with a long document build process can put it in
259 B<build-indep>.
260
261         #!/usr/bin/make -f
262         %:
263                 dh $@
264         
265         build-indep:
266                 $(MAKE) docs
267         build-arch:
268                 $(MAKE) bins
269
270 Note that in the example above, dh will arrange for "debian/rules build"
271 to call your build-indep and build-arch targets. You do not need to
272 explicitly define the dependencies in the rules file when using dh with
273 compatability level v9. This example would be more complicated with
274 earlier compatability levels.
275
276 =head1 INTERNALS
277
278 If you're curious about B<dh>'s internals, here's how it works under the hood.
279
280 Each debhelper command will record when it's successfully run in
281 F<debian/package.debhelper.log>. (Which B<dh_clean> deletes.) So B<dh> can tell
282 which commands have already been run, for which packages, and skip running
283 those commands again.
284
285 Each time B<dh> is run, it examines the log, and finds the last logged command
286 that is in the specified sequence. It then continues with the next command
287 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
288 options can override this behavior.
289
290 A sequence can also run dependent targets in debian/rules.  For
291 example, the "binary" sequence runs the "install" target.
292
293 B<dh> sets environment variables listed by B<dpkg-buildflags>, unless
294 they are already set. It supports DEB_BUILD_OPTIONS=noopt too.
295
296 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
297 through to debhelper commands that are run inside override targets. The
298 contents (and indeed, existence) of this environment variable, as the name
299 might suggest, is subject to change at any time.
300
301 =cut
302
303 # Stash this away before init modifies it.
304 my @ARGV_orig=@ARGV;
305
306 # python-support is enabled by default, at least for now
307 # (and comes first so python-central loads later and can disable it).
308 unshift @ARGV, "--with=python-support";
309                 
310 init(options => {
311                 "until=s" => \$dh{UNTIL},
312                 "after=s" => \$dh{AFTER},
313                 "before=s" => \$dh{BEFORE},
314                 "remaining" => \$dh{REMAINING},
315                 "with=s" => sub {
316                         my ($option,$value)=@_;
317                         push @{$dh{WITH}},split(",", $value);
318                 },
319                 "without=s" => sub {
320                         my ($option,$value)=@_;
321                         @{$dh{WITH}} = grep { $_ ne $value } @{$dh{WITH}};
322                 },
323                 "l" => \&list_addons,
324                 "list" => \&list_addons,
325         },
326         # Disable complaints about unknown options; they are passed on to 
327         # the debhelper commands.
328         ignore_unknown_options => 1,
329         # Bundling does not work well since there are unknown options.
330         bundling => 0,
331 );
332 inhibit_log();
333 set_buildflags();
334
335 # If make is using a jobserver, but it is not available
336 # to this process, clean out MAKEFLAGS. This avoids
337 # ugly warnings when calling make.
338 if (is_make_jobserver_unavailable()) {
339         clean_jobserver_makeflags();
340 }
341
342 # Process the sequence parameter.
343 my $sequence;
344 if (! compat(7)) {
345         # From v8, the sequence is the very first parameter.
346         $sequence=shift @ARGV_orig;
347         if ($sequence=~/^-/) {
348                 error "Unknown sequence $sequence (options should not come before the sequence)";
349         }
350 }
351 else {
352         # Before v8, the sequence could be at any position in the parameters,
353         # so was what was left after parsing.
354         $sequence=shift;
355         if (defined $sequence) {
356                 @ARGV_orig=grep { $_ ne $sequence } @ARGV_orig;
357         }
358 }
359 if (! defined $sequence) {
360         error "specify a sequence to run";
361 }
362 # make -B causes the rules file to be run as a target.
363 # Also support completly empty override targets.
364 # Note: it's not safe to use rules_explicit_target before this check,
365 # since it causes dh to be run.
366 my $dummy_target="debhelper-fail-me";
367 if ($sequence eq 'debian/rules' ||
368     $sequence =~ /^override_dh_/ ||
369     $sequence eq $dummy_target) {
370         exit 0;
371 }
372
373
374 # Definitions of sequences.
375 my %sequences;
376 my @bd_minimal = qw{
377         dh_testdir
378 };
379 my @bd = qw{
380         dh_testdir
381         dh_auto_configure
382         dh_auto_build
383         dh_auto_test
384 };
385 my @i_minimal = qw{
386         dh_testroot
387 };
388 my @i = qw{
389         dh_testroot
390         dh_prep
391         dh_installdirs
392         dh_auto_install
393
394         dh_install
395         dh_installdocs
396         dh_installchangelogs
397         dh_installexamples
398         dh_installman
399
400         dh_installcatalogs
401         dh_installcron
402         dh_installdebconf
403         dh_installemacsen
404         dh_installifupdown
405         dh_installinfo
406         dh_installinit
407         dh_installmenu
408         dh_installmime
409         dh_installmodules
410         dh_installlogcheck
411         dh_installlogrotate
412         dh_installpam
413         dh_installppp
414         dh_installudev
415         dh_installwm
416         dh_installxfonts
417         dh_installgsettings
418         dh_bugfiles
419         dh_ucf
420         dh_lintian
421         dh_gconf
422         dh_icons
423         dh_perl
424         dh_usrlocal
425
426         dh_link
427         dh_compress
428         dh_fixperms
429 };
430 my @ba=qw{
431         dh_strip
432         dh_makeshlibs
433         dh_shlibdeps
434 };
435 my @b=qw{
436         dh_installdeb
437         dh_gencontrol
438         dh_md5sums
439         dh_builddeb
440 };
441 $sequences{clean} = [qw{
442         dh_testdir
443         dh_auto_clean
444         dh_clean
445 }];
446 $sequences{'build-indep'} = [@bd];
447 $sequences{'build-arch'} = [@bd];
448 if (! compat(8)) {
449         # From v9, sequences take standard rules targets into account.
450         if (rules_explicit_target('build-arch') ||
451             rules_explicit_target('build-indep')) {
452                 # run sequences separately
453                 $sequences{build} = [@bd_minimal, rules("build-arch"), rules("build-indep")];
454         }
455         else {
456                 # run standard sequence (this is faster)
457                 $sequences{build} = [@bd];
458         }
459         $sequences{'install-indep'} = [rules("build-indep"), @i];
460         $sequences{'install-arch'} = [rules("build-arch"), @i];
461         if (rules_explicit_target('install-arch') ||
462             rules_explicit_target('install-indep')) {
463                 # run sequences separately
464                 $sequences{'install'} = [rules("build"), @i_minimal, rules("install-arch"), rules("install-indep")];
465         }
466         else {
467                 # run standard sequence (this is faster)
468                 $sequences{'install'} = [rules("build"), @i, rules("install-arch"), rules("install-indep")];
469         }
470         $sequences{'binary-indep'} = [rules("install-indep"), @b];
471         $sequences{'binary-arch'} = [rules("install-arch"), @ba, @b];
472         $sequences{binary} = [rules("install"), rules("binary-arch"), rules("binary-indep")];
473 }
474 else {
475         $sequences{build} = [@bd];
476         $sequences{'install'} = [@{$sequences{build}}, @i];
477         $sequences{'install-indep'} = [@{$sequences{'build-indep'}}, @i];
478         $sequences{'install-arch'} = [@{$sequences{'build-arch'}}, @i];
479         $sequences{binary} = [@{$sequences{install}}, @ba, @b];
480         $sequences{'binary-indep'} = [@{$sequences{'install-indep'}}, @b];
481         $sequences{'binary-arch'} = [@{$sequences{'install-arch'}}, @ba, @b];
482 }
483
484 # Additional command options
485 my %command_opts;
486
487 # sequence addon interface
488 sub _insert {
489         my $offset=shift;
490         my $existing=shift;
491         my $new=shift;
492         foreach my $sequence (keys %sequences) {
493                 my @list=@{$sequences{$sequence}};
494                 next unless grep $existing, @list;
495                 my @new;
496                 foreach my $command (@list) {
497                         if ($command eq $existing) {
498                                 push @new, $new if $offset < 0;
499                                 push @new, $command;
500                                 push @new, $new if $offset > 0;
501                         }
502                         else {
503                                 push @new, $command;
504                         }
505                 }
506                 $sequences{$sequence}=\@new;
507         }
508 }
509 sub insert_before {
510         _insert(-1, @_);
511 }
512 sub insert_after {
513         _insert(1, @_);
514 }
515 sub remove_command {
516         my $command=shift;
517         foreach my $sequence (keys %sequences) {
518                 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
519         }
520         
521 }
522 sub add_command {
523         my $command=shift;
524         my $sequence=shift;
525         unshift @{$sequences{$sequence}}, $command;
526 }
527 sub add_command_options {
528         my $command=shift;
529         push @{$command_opts{$command}}, @_;
530 }
531 sub remove_command_options {
532         my $command=shift;
533         if (@_) {
534                 # Remove only specified options
535                 if (my $opts = $command_opts{$command}) {
536                         foreach my $opt (@_) {
537                                 $opts = [ grep { $_ ne $opt } @$opts ];
538                         }
539                         $command_opts{$command} = $opts;
540                 }
541         }
542         else {
543                 # Clear all additional options
544                 delete $command_opts{$command};
545         }
546 }
547
548 sub list_addons {
549         my %addons;
550
551         for my $inc (@INC) {
552                 eval q{use File::Spec};
553                 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Sequence");
554                 if (-d $path) {
555                         for my $module_path (glob "$path/*.pm") {
556                                 my $name = basename($module_path);
557                                 $name =~ s/\.pm$//;
558                                 $name =~ s/_/-/g;
559                                 $addons{$name} = 1;
560                         }
561                 }
562         }
563
564         for my $name (sort keys %addons) {
565                 print "$name\n";
566         }
567
568         exit 0;
569 }
570
571 # Load addons, which can modify sequences.
572 foreach my $addon (@{$dh{WITH}}) {
573         my $mod="Debian::Debhelper::Sequence::$addon";
574         $mod=~s/-/_/g;
575         eval "use $mod";
576         if ($@) {
577                 error("unable to load addon $addon: $@");
578         }
579 }
580
581 if (! exists $sequences{$sequence}) {
582         error "Unknown sequence $sequence (choose from: ".
583                 join(" ", sort keys %sequences).")";
584 }
585 my @sequence=optimize_sequence(@{$sequences{$sequence}});
586
587 # The list of all packages that can be acted on.
588 my @packages=@{$dh{DOPACKAGES}};
589
590 # Get the options to pass to commands in the sequence.
591 # Filter out options intended only for this program.
592 my @options;
593 if ($sequence eq 'build-arch' ||
594     $sequence eq 'install-arch' ||
595     $sequence eq 'binary-arch') {
596         push @options, "-a";
597         # as an optimisation, remove from the list any packages
598         # that are not arch dependent
599         my %arch_packages = map { $_ => 1 } getpackages("arch");
600         @packages = grep { $arch_packages{$_} } @packages;
601 }
602 elsif ($sequence eq 'build-indep' ||
603        $sequence eq 'install-indep' ||
604        $sequence eq 'binary-indep') {
605         push @options, "-i";
606         # ditto optimisation for arch indep
607         my %indep_packages = map { $_ => 1 } getpackages("indep");
608         @packages = grep { $indep_packages{$_} } @packages;
609 }
610 while (@ARGV_orig) {
611         my $opt=shift @ARGV_orig;
612         if ($opt =~ /^--?(after|until|before|with|without)$/) {
613                 shift @ARGV_orig;
614                 next;
615         }
616         elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with|without)=)/) {
617                 next;
618         }
619         elsif ($opt=~/^-/) {
620                 push @options, "-O".$opt;
621         }
622         elsif (@options) {
623                 if ($options[$#options]=~/^-O--/) {
624                         $options[$#options].="=".$opt;
625                 }
626                 else {
627                         $options[$#options].=$opt;
628                 }
629         }
630 }
631
632 # Figure out at what point in the sequence to start for each package.
633 my %logged;
634 my %startpoint;
635 foreach my $package (@packages) {
636         my @log=load_log($package, \%logged);
637         if ($dh{AFTER}) {
638                 # Run commands in the sequence that come after the
639                 # specified command.
640                 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
641                 # Write a dummy log entry indicating that the specified
642                 # command was, in fact, run. This handles the case where
643                 # no commands remain to run after it, communicating to
644                 # future dh instances that the specified command should not
645                 # be run again.
646                 write_log($sequence[$startpoint{$package}-1], $package);
647         }
648         elsif ($dh{REMAINING}) {
649                 # Start at the beginning so all remaining commands will get
650                 # run.
651                 $startpoint{$package}=0;
652         }
653         else {
654                 # Find the last logged command that is in the sequence, and
655                 # continue with the next command after it. If no logged
656                 # command is in the sequence, we're starting at the beginning..                         
657                 $startpoint{$package}=0;
658 COMMAND:        foreach my $command (reverse @log) {
659                         foreach my $i (0..$#sequence) {
660                                 if ($command eq $sequence[$i]) {
661                                         $startpoint{$package}=$i+1;
662                                         last COMMAND;
663                                 }
664                         }
665                 }
666         }
667 }
668
669 # Figure out what point in the sequence to go to.
670 my $stoppoint=$#sequence;
671 if ($dh{UNTIL}) {
672         $stoppoint=command_pos($dh{UNTIL}, @sequence);
673 }
674 elsif ($dh{BEFORE}) {
675         $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
676 }
677
678 # Now run the commands in the sequence.
679 foreach my $i (0..$stoppoint) {
680         # Figure out which packages need to run this command.
681         my @exclude;
682         foreach my $package (@packages) {
683                 if ($startpoint{$package} > $i ||
684                     $logged{$package}{$sequence[$i]}) {
685                         push @exclude, $package;
686                 }
687         }
688         
689         if (@exclude eq @packages) {
690                 # Command already done for all packages.
691                 next;
692         }
693
694         run($sequence[$i], \@packages, \@exclude, @options);
695 }
696
697 sub run {
698         my $command=shift;
699         my @packages=@{shift()};
700         my @exclude=@{shift()};
701         my @options=@_;
702         
703         # If some packages are excluded, add flags
704         # to prevent them from being acted on.
705         push @options, map { "-N$_" } @exclude;
706
707         # Check for override targets in debian/rules and
708         # run them instead of running the command directly.
709         my $override_command;
710         my $has_explicit_target = rules_explicit_target("override_".$command);
711
712         my $rules_target = rules_target($command);
713         if (defined $rules_target) {
714                 # Don't pass DH_ environment variables, since this is
715                 # a fresh invocation of debian/rules and any sub-dh
716                 # commands.
717                 $override_command=$command;
718                 delete $ENV{DH_INTERNAL_OPTIONS};
719                 delete $ENV{DH_INTERNAL_OVERRIDE};
720                 $command="debian/rules";
721                 @options=$rules_target;
722         }
723         elsif (defined $has_explicit_target) {
724                 $override_command=$command;
725                 # Check if target isn't noop
726                 if ($has_explicit_target) {
727                         # This passes the options through to commands called
728                         # inside the target.
729                         $ENV{DH_INTERNAL_OPTIONS}=join("\x1e", @options);
730                         $ENV{DH_INTERNAL_OVERRIDE}=$command;
731                         $command="debian/rules";
732                         @options="override_".$override_command;
733                 }
734                 else {
735                         $command = undef;
736                 }
737         }
738         else {
739                 # Pass additional command options if any
740                 unshift @options, @{$command_opts{$command}} if exists $command_opts{$command};
741         }
742
743         if (defined $command) {
744                 # 3 space indent lines the command being run up under the
745                 # sequence name after "dh ".
746                 print "   ".escape_shell($command, @options)."\n";
747         }
748         else {
749                 print "   ", "# Skipping ", $override_command, " - empty override", "\n";
750         }
751                                 
752         if (! $dh{NO_ACT}) {
753                 if (defined $command) {
754                         my $ret=system($command, @options);
755                         
756                         if ($ret >> 8 != 0) {
757                                 exit $ret >> 8;
758                         }
759                         elsif ($ret) {
760                                 exit 1;
761                         }
762                 }
763
764                 if (defined $override_command) {
765                         # Update log for overridden command now that it has
766                         # finished successfully.
767                         # (But avoid logging for dh_clean since it removes
768                         # the log earlier.)
769                         if ($override_command ne 'dh_clean') {
770                                 my %packages=map { $_ => 1 } @packages;
771                                 map { delete $packages{$_} } @exclude;
772                                 write_log($override_command, keys %packages);
773                                 commit_override_log(keys %packages);
774                         }
775
776                         delete $ENV{DH_INTERNAL_OPTIONS};
777                         delete $ENV{DH_INTERNAL_OVERRIDE};
778                 }
779         }
780 }
781
782 sub optimize_sequence {
783         my @sequence;
784         my %seen;
785         my $add=sub {
786                 # commands can appear multiple times when sequences are
787                 # inlined together; only the first should be needed
788                 my $command=shift;
789                 if (! $seen{$command}) {
790                         $seen{$command}=1;
791                         push @sequence, $command;
792                 }
793         };
794         foreach my $command (@_) {
795                 my $rules_target=rules_target($command);
796                 if (defined $rules_target &&
797                     ! defined rules_explicit_target($rules_target)) {
798                         # inline the sequence for this implicit target
799                         $add->($_) foreach optimize_sequence(@{$sequences{$rules_target}});
800                 }
801                 else {
802                         $add->($command);
803                 }
804         }
805         return @sequence;
806 }
807
808 sub rules_target {
809         my $command=shift;
810         if ($command =~ /^debian\/rules\s+(.*)/) {
811                 return $1
812         }
813         else {
814                 return undef;
815         }
816 }
817
818 sub rules {
819         return "debian/rules ".join(" ", @_);
820 }
821
822 {
823 my %targets;
824 my $rules_parsed;
825
826 sub rules_explicit_target {
827         # Checks if a specified target exists as an explicit target
828         # in debian/rules.
829         # undef is returned if target does not exist, 0 if target is noop
830         # and 1 if target has dependencies or executes commands.
831         my $target=shift;
832
833         if (! $rules_parsed) {
834                 my $processing_targets = 0;
835                 my $not_a_target = 0;
836                 my $current_target;
837                 open(MAKE, "LC_ALL=C make -Rrnpsf debian/rules $dummy_target 2>/dev/null |");
838                 while (<MAKE>) {
839                         if ($processing_targets) {
840                                 if (/^# Not a target:/) {
841                                         $not_a_target = 1;
842                                 }
843                                 else {
844                                         if (!$not_a_target && /^([^#:]+)::?\s*(.*)$/) {
845                                                 # Target is defined. NOTE: if it is a depenency of
846                                                 # .PHONY it will be defined too but that's ok.
847                                                 # $2 contains target dependencies if any.
848                                                 $current_target = $1;
849                                                 $targets{$current_target} = ($2) ? 1 : 0;
850                                         }
851                                         else {
852                                                 if (defined $current_target) {
853                                                         if (/^#/) {
854                                                                 # Check if target has commands to execute
855                                                                 if (/^#\s*commands to execute/) {
856                                                                         $targets{$current_target} = 1;
857                                                                 }
858                                                         }
859                                                         else {
860                                                                 # Target parsed.
861                                                                 $current_target = undef;
862                                                         }
863                                                 }
864                                         }
865                                         # "Not a target:" is always followed by
866                                         # a target name, so resetting this one
867                                         # here is safe.
868                                         $not_a_target = 0;
869                                 }
870                         }
871                         elsif (/^# Files$/) {
872                                 $processing_targets = 1;
873                         }
874                 }
875                 close MAKE;
876                 $rules_parsed = 1;
877         }
878
879         return $targets{$target};
880 }
881
882 }
883
884 =head1 SEE ALSO
885
886 L<debhelper(7)>
887
888 This program is a part of debhelper.
889
890 =head1 AUTHOR
891
892 Joey Hess <joeyh@debian.org>
893
894 =cut