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