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