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