]> git.donarmstrong.com Git - debhelper.git/blob - dh
dpkg-buildflags support
[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 the regular
244 rules file targets when it makes sense to do so. A common reason to do this
245 is if 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         binary: binary-arch binary-indep ;
254         binary-arch:: build-arch
255         binary-indep:: build-indep
256         build: build-arch build-indep ;
257         build-indep:
258                 $(MAKE) docs
259         build-arch:
260                 $(MAKE) bins
261
262 =head1 INTERNALS
263
264 If you're curious about B<dh>'s internals, here's how it works under the hood.
265
266 Each debhelper command will record when it's successfully run in
267 F<debian/package.debhelper.log>. (Which B<dh_clean> deletes.) So B<dh> can tell
268 which commands have already been run, for which packages, and skip running
269 those commands again.
270
271 Each time B<dh> is run, it examines the log, and finds the last logged command
272 that is in the specified sequence. It then continues with the next command
273 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
274 options can override this behavior.
275
276 B<dh> sets environment variables listed by B<dpkg-buildflags>, unless
277 they are already set. It supports DEB_BUILD_OPTIONS=noopt too.
278
279 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
280 through to debhelper commands that are run inside override targets. The
281 contents (and indeed, existence) of this environment variable, as the name
282 might suggest, is subject to change at any time.
283
284 =cut
285
286 # Stash this away before init modifies it.
287 my @ARGV_orig=@ARGV;
288
289 # python-support is enabled by default, at least for now
290 # (and comes first so python-central loads later and can disable it).
291 unshift @ARGV, "--with=python-support";
292                 
293 init(options => {
294                 "until=s" => \$dh{UNTIL},
295                 "after=s" => \$dh{AFTER},
296                 "before=s" => \$dh{BEFORE},
297                 "remaining" => \$dh{REMAINING},
298                 "with=s" => sub {
299                         my ($option,$value)=@_;
300                         push @{$dh{WITH}},split(",", $value);
301                 },
302                 "without=s" => sub {
303                         my ($option,$value)=@_;
304                         @{$dh{WITH}} = grep { $_ ne $value } @{$dh{WITH}};
305                 },
306                 "l" => \&list_addons,
307                 "list" => \&list_addons,
308         },
309         # Disable complaints about unknown options; they are passed on to 
310         # the debhelper commands.
311         ignore_unknown_options => 1,
312         # Bundling does not work well since there are unknown options.
313         bundling => 0,
314 );
315 inhibit_log();
316 set_buildflags();
317
318 # If make is using a jobserver, but it is not available
319 # to this process, clean out MAKEFLAGS. This avoids
320 # ugly warnings when calling make.
321 if (is_make_jobserver_unavailable()) {
322         clean_jobserver_makeflags();
323 }
324
325 # Definitions of sequences.
326 my %sequences;
327 $sequences{build} = [qw{
328         dh_testdir
329         dh_auto_configure
330         dh_auto_build
331         dh_auto_test
332 }],
333 $sequences{'build-indep'} = [@{$sequences{build}}];
334 $sequences{'build-arch'} = [@{$sequences{build}}];
335 $sequences{clean} = [qw{
336         dh_testdir
337         dh_auto_clean
338         dh_clean
339 }];
340 my @i = qw{
341         dh_testroot
342         dh_prep
343         dh_installdirs
344         dh_auto_install
345
346         dh_install
347         dh_installdocs
348         dh_installchangelogs
349         dh_installexamples
350         dh_installman
351
352         dh_installcatalogs
353         dh_installcron
354         dh_installdebconf
355         dh_installemacsen
356         dh_installifupdown
357         dh_installinfo
358         dh_installinit
359         dh_installmenu
360         dh_installmime
361         dh_installmodules
362         dh_installlogcheck
363         dh_installlogrotate
364         dh_installpam
365         dh_installppp
366         dh_installudev
367         dh_installwm
368         dh_installxfonts
369         dh_installgsettings
370         dh_bugfiles
371         dh_ucf
372         dh_lintian
373         dh_gconf
374         dh_icons
375         dh_perl
376         dh_usrlocal
377
378         dh_link
379         dh_compress
380         dh_fixperms
381 };
382 $sequences{'install'} = [@{$sequences{build}}, @i];
383 $sequences{'install-indep'} = [@{$sequences{'build-indep'}}, @i];
384 $sequences{'install-arch'} = [@{$sequences{'build-arch'}}, @i];
385 my @ba=qw{
386         dh_strip
387         dh_makeshlibs
388         dh_shlibdeps
389 };
390 my @b=qw{
391         dh_installdeb
392         dh_gencontrol
393         dh_md5sums
394         dh_builddeb
395 };
396 $sequences{binary} = [@{$sequences{install}}, @ba, @b];
397 $sequences{'binary-indep'} = [@{$sequences{'install-indep'}}, @b];
398 $sequences{'binary-arch'} = [@{$sequences{'install-arch'}}, @ba, @b];
399
400 # Additional command options
401 my %command_opts;
402
403 # sequence addon interface
404 sub _insert {
405         my $offset=shift;
406         my $existing=shift;
407         my $new=shift;
408         foreach my $sequence (keys %sequences) {
409                 my @list=@{$sequences{$sequence}};
410                 next unless grep $existing, @list;
411                 my @new;
412                 foreach my $command (@list) {
413                         if ($command eq $existing) {
414                                 push @new, $new if $offset < 0;
415                                 push @new, $command;
416                                 push @new, $new if $offset > 0;
417                         }
418                         else {
419                                 push @new, $command;
420                         }
421                 }
422                 $sequences{$sequence}=\@new;
423         }
424 }
425 sub insert_before {
426         _insert(-1, @_);
427 }
428 sub insert_after {
429         _insert(1, @_);
430 }
431 sub remove_command {
432         my $command=shift;
433         foreach my $sequence (keys %sequences) {
434                 $sequences{$sequence}=[grep { $_ ne $command } @{$sequences{$sequence}}];
435         }
436         
437 }
438 sub add_command {
439         my $command=shift;
440         my $sequence=shift;
441         unshift @{$sequences{$sequence}}, $command;
442 }
443 sub add_command_options {
444         my $command=shift;
445         push @{$command_opts{$command}}, @_;
446 }
447 sub remove_command_options {
448         my $command=shift;
449         if (@_) {
450                 # Remove only specified options
451                 if (my $opts = $command_opts{$command}) {
452                         foreach my $opt (@_) {
453                                 $opts = [ grep { $_ ne $opt } @$opts ];
454                         }
455                         $command_opts{$command} = $opts;
456                 }
457         }
458         else {
459                 # Clear all additional options
460                 delete $command_opts{$command};
461         }
462 }
463
464 sub list_addons {
465         my %addons;
466
467         for my $inc (@INC) {
468                 eval q{use File::Spec};
469                 my $path = File::Spec->catdir($inc, "Debian/Debhelper/Sequence");
470                 if (-d $path) {
471                         for my $module_path (glob "$path/*.pm") {
472                                 my $name = basename($module_path);
473                                 $name =~ s/\.pm$//;
474                                 $name =~ s/_/-/g;
475                                 $addons{$name} = 1;
476                         }
477                 }
478         }
479
480         for my $name (sort keys %addons) {
481                 print "$name\n";
482         }
483
484         exit 0;
485 }
486
487 foreach my $addon (@{$dh{WITH}}) {
488         my $mod="Debian::Debhelper::Sequence::$addon";
489         $mod=~s/-/_/g;
490         eval "use $mod";
491         if ($@) {
492                 error("unable to load addon $addon: $@");
493         }
494 }
495
496 my $sequence;
497 if (! compat(7)) {
498         # From v8, the sequence is the very first parameter.
499         $sequence=shift @ARGV_orig;
500         if ($sequence=~/^-/) {
501                 error "Unknown sequence $sequence (options should not come before the sequence)";
502         }
503 }
504 else {
505         # Before v8, the sequence could be at any position in the parameters,
506         # so was what was left after parsing.
507         $sequence=shift;
508         if (defined $sequence) {
509                 @ARGV_orig=grep { $_ ne $sequence } @ARGV_orig;
510         }
511 }
512 if (! defined $sequence) {
513         error "specify a sequence to run";
514 }
515 if ($sequence eq 'debian/rules' ||
516     $sequence =~ /^override_dh_/) {
517         # make -B causes the rules file to be run as a target.
518         # Also support completly empty override targets.
519         exit 0;
520 }
521 elsif (! exists $sequences{$sequence}) {
522         error "Unknown sequence $sequence (choose from: ".
523                 join(" ", sort keys %sequences).")";
524 }
525 my @sequence=@{$sequences{$sequence}};
526
527 # The list of all packages that can be acted on.
528 my @packages=@{$dh{DOPACKAGES}};
529
530 # Get the options to pass to commands in the sequence.
531 # Filter out options intended only for this program.
532 my @options;
533 if ($sequence eq 'build-arch' ||
534     $sequence eq 'install-arch' ||
535     $sequence eq 'binary-arch') {
536         push @options, "-a";
537         # as an optimisation, remove from the list any packages
538         # that are not arch dependent
539         my %arch_packages = map { $_ => 1 } getpackages("arch");
540         @packages = grep { $arch_packages{$_} } @packages;
541 }
542 elsif ($sequence eq 'build-indep' ||
543        $sequence eq 'install-indep' ||
544        $sequence eq 'binary-indep') {
545         push @options, "-i";
546         # ditto optimisation for arch indep
547         my %indep_packages = map { $_ => 1 } getpackages("indep");
548         @packages = grep { $indep_packages{$_} } @packages;
549 }
550 while (@ARGV_orig) {
551         my $opt=shift @ARGV_orig;
552         if ($opt =~ /^--?(after|until|before|with|without)$/) {
553                 shift @ARGV_orig;
554                 next;
555         }
556         elsif ($opt =~ /^--?(no-act|remaining|(after|until|before|with|without)=)/) {
557                 next;
558         }
559         elsif ($opt=~/^-/) {
560                 push @options, "-O".$opt;
561         }
562         elsif (@options) {
563                 if ($options[$#options]=~/^-O--/) {
564                         $options[$#options].="=".$opt;
565                 }
566                 else {
567                         $options[$#options].=$opt;
568                 }
569         }
570 }
571
572 # Figure out at what point in the sequence to start for each package.
573 my %logged;
574 my %startpoint;
575 foreach my $package (@packages) {
576         my @log=load_log($package, \%logged);
577         if ($dh{AFTER}) {
578                 # Run commands in the sequence that come after the
579                 # specified command.
580                 $startpoint{$package}=command_pos($dh{AFTER}, @sequence) + 1;
581                 # Write a dummy log entry indicating that the specified
582                 # command was, in fact, run. This handles the case where
583                 # no commands remain to run after it, communicating to
584                 # future dh instances that the specified command should not
585                 # be run again.
586                 write_log($sequence[$startpoint{$package}-1], $package);
587         }
588         elsif ($dh{REMAINING}) {
589                 # Start at the beginning so all remaining commands will get
590                 # run.
591                 $startpoint{$package}=0;
592         }
593         else {
594                 # Find the last logged command that is in the sequence, and
595                 # continue with the next command after it. If no logged
596                 # command is in the sequence, we're starting at the beginning..                         
597                 $startpoint{$package}=0;
598 COMMAND:        foreach my $command (reverse @log) {
599                         foreach my $i (0..$#sequence) {
600                                 if ($command eq $sequence[$i]) {
601                                         $startpoint{$package}=$i+1;
602                                         last COMMAND;
603                                 }
604                         }
605                 }
606         }
607 }
608
609 # Figure out what point in the sequence to go to.
610 my $stoppoint=$#sequence;
611 if ($dh{UNTIL}) {
612         $stoppoint=command_pos($dh{UNTIL}, @sequence);
613 }
614 elsif ($dh{BEFORE}) {
615         $stoppoint=command_pos($dh{BEFORE}, @sequence) - 1;
616 }
617
618 # Now run the commands in the sequence.
619 foreach my $i (0..$stoppoint) {
620         # Figure out which packages need to run this command.
621         my @exclude;
622         foreach my $package (@packages) {
623                 if ($startpoint{$package} > $i ||
624                     $logged{$package}{$sequence[$i]}) {
625                         push @exclude, $package;
626                 }
627         }
628         
629         if (@exclude eq @packages) {
630                 # Command already done for all packages.
631                 next;
632         }
633
634         run($sequence[$i], \@packages, \@exclude, @options);
635 }
636
637 sub run {
638         my $command=shift;
639         my @packages=@{shift()};
640         my @exclude=@{shift()};
641         my @options=@_;
642         
643         # If some packages are excluded, add flags
644         # to prevent them from being acted on.
645         push @options, map { "-N$_" } @exclude;
646
647         # Check for override targets in debian/rules and
648         # run them instead of running the command directly.
649         my $override_command;
650         my $has_explicit_target = rules_explicit_target("override_".$command);
651         if (defined $has_explicit_target) {
652                 $override_command=$command;
653                 # Check if target isn't noop
654                 if ($has_explicit_target) {
655                         # This passes the options through to commands called
656                         # inside the target.
657                         $ENV{DH_INTERNAL_OPTIONS}=join("\x1e", @options);
658                         $ENV{DH_INTERNAL_OVERRIDE}=$command;
659                         $command="debian/rules";
660                         @options="override_".$override_command;
661                 }
662                 else {
663                         $command = undef;
664                 }
665         }
666         else {
667                 # Pass additional command options if any
668                 unshift @options, @{$command_opts{$command}} if exists $command_opts{$command};
669         }
670
671         if (defined $command) {
672                 # 3 space indent lines the command being run up under the
673                 # sequence name after "dh ".
674                 print "   ".escape_shell($command, @options)."\n";
675         }
676         else {
677                 print "   ", "# Skipping ", $override_command, " - empty override", "\n";
678         }
679                                 
680         if (! $dh{NO_ACT}) {
681                 if (defined $command) {
682                         my $ret=system($command, @options);
683                         
684                         if ($ret >> 8 != 0) {
685                                 exit $ret >> 8;
686                         }
687                         elsif ($ret) {
688                                 exit 1;
689                         }
690                 }
691
692                 if (defined $override_command) {
693                         # Update log for overridden command now that it has
694                         # finished successfully.
695                         # (But avoid logging for dh_clean since it removes
696                         # the log earlier.)
697                         if ($override_command ne 'dh_clean') {
698                                 my %packages=map { $_ => 1 } @packages;
699                                 map { delete $packages{$_} } @exclude;
700                                 write_log($override_command, keys %packages);
701                                 commit_override_log(keys %packages);
702                         }
703
704                         delete $ENV{DH_INTERNAL_OPTIONS};
705                         delete $ENV{DH_INTERNAL_OVERRIDE};
706                 }
707         }
708 }
709
710 {
711 my %targets;
712 my $rules_parsed;
713
714 sub rules_explicit_target {
715         # Checks if a specified target exists as an explicit target
716         # in debian/rules.
717         # undef is returned if target does not exist, 0 if target is noop
718         # and 1 if target has dependencies or executes commands.
719         my $target=shift;
720
721         if (! $rules_parsed) {
722                 my $processing_targets = 0;
723                 my $not_a_target = 0;
724                 my $current_target;
725                 open(MAKE, "LC_ALL=C make -Rrnpsf debian/rules debhelper-fail-me 2>/dev/null |");
726                 while (<MAKE>) {
727                         if ($processing_targets) {
728                                 if (/^# Not a target:/) {
729                                         $not_a_target = 1;
730                                 }
731                                 else {
732                                         if (!$not_a_target && /^([^#:]+)::?\s*(.*)$/) {
733                                                 # Target is defined. NOTE: if it is a depenency of
734                                                 # .PHONY it will be defined too but that's ok.
735                                                 # $2 contains target dependencies if any.
736                                                 $current_target = $1;
737                                                 $targets{$current_target} = ($2) ? 1 : 0;
738                                         }
739                                         else {
740                                                 if (defined $current_target) {
741                                                         if (/^#/) {
742                                                                 # Check if target has commands to execute
743                                                                 if (/^#\s*commands to execute/) {
744                                                                         $targets{$current_target} = 1;
745                                                                 }
746                                                         }
747                                                         else {
748                                                                 # Target parsed.
749                                                                 $current_target = undef;
750                                                         }
751                                                 }
752                                         }
753                                         # "Not a target:" is always followed by
754                                         # a target name, so resetting this one
755                                         # here is safe.
756                                         $not_a_target = 0;
757                                 }
758                         }
759                         elsif (/^# Files$/) {
760                                 $processing_targets = 1;
761                         }
762                 }
763                 close MAKE;
764                 $rules_parsed = 1;
765         }
766
767         return $targets{$target};
768 }
769
770 }
771
772 =head1 SEE ALSO
773
774 L<debhelper(7)>
775
776 This program is a part of debhelper.
777
778 =head1 AUTHOR
779
780 Joey Hess <joeyh@debian.org>
781
782 =cut