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