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