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