From e23985ee38a509962569b0b6909f37c4f64615f1 Mon Sep 17 00:00:00 2001
From: Roger Leigh <rleigh@debian.org>
Date: Tue, 23 Nov 2010 18:00:06 +0000
Subject: [PATCH 3/6] dh: Add sequence dependencies and satisfy dependencies prior to running sequence

Add %sequence_deps and invoke recursively prior to examining logs and
running commands in sequence.  The supplied dependencies are equivalent
to the following make rules:

build: build-arch build-indep
install: install-arch install-indep
install-arch: build-arch
install-indep: build-indep
binary: binary-arch binary-indep
binary-arch: install-arch
binary-indep: install-indep

In the existing dh command sequences, the binary sequences all included
the corresponding install sequence commands, and in turn the install
sequences all included the corresponding build commands.  While this
works, it has a major deficiency.  If the "binary" sequence is run, it
will not run the "build" target in debian/rules.  This leads to a
situation where building with dpkg-buildpackge, which would typically
invoke "debian/rules build" followed by "debian/rules binary-arch"
and/or "debian/rules debian-indep" may do something different than
just invoking "debian/rules binary" or "dh binary" because the build
target in debian/rules is effectively bypassed.  This applies equally
to the -arch and -indep sequence variants.

This change eliminates the duplicated sequence commands, and instead
invokes the appropriate target(s) in debian/rules, as specified in the
%sequence_deps hash.  In the common case, the dh sequence by the same
name will be called, so the behaviour is identical.  However, this
provides a means to utilise all of the policy-specified targets, plus
the install targets and extend them with additional dependencies and
commands, while still allowing full use of dh and giving identical
behaviour whether dh or debian/rules targets are used.

Signed-off-by: Roger Leigh <rleigh@debian.org>
---
 dh |   64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 54 insertions(+), 10 deletions(-)

diff --git a/dh b/dh
index 0b07973..f8628e8 100755
--- a/dh
+++ b/dh
@@ -270,6 +270,16 @@ that is in the specified sequence. It then continues with the next command
 in the sequence. The B<--until>, B<--before>, B<--after>, and B<--remaining>
 options can override this behavior.
 
+A sequence can also have dependencies.  For example, the "binary"
+sequence depends upon the "binary-arch" and "binary-indep" sequences,
+and the "binary-arch" sequence depends upon the "install-arch"
+sequence which in turn depends upon the "build-arch" sequence.  These
+will, by default, be run via "debian/rules <sequence>" and so may be
+overridden or extended there, or else will run dh again to execute the
+depending sequence.  For example, "dh binary-arch" will run
+"debian/rules install-arch" which will run "dh install-arch" unless a
+custom install-arch target replaces the default target.
+
 B<dh> uses the B<DH_INTERNAL_OPTIONS> environment variable to pass information
 through to debhelper commands that are run inside override targets. The
 contents (and indeed, existence) of this environment variable, as the name
@@ -318,14 +328,15 @@ if (is_make_jobserver_unavailable()) {
 
 # Definitions of sequences.
 my %sequences;
-$sequences{build} = [qw{
+my @bd = qw{
 	dh_testdir
 	dh_auto_configure
 	dh_auto_build
 	dh_auto_test
-}],
-$sequences{'build-indep'} = [@{$sequences{build}}];
-$sequences{'build-arch'} = [@{$sequences{build}}];
+};
+$sequences{build} = [@bd];
+$sequences{'build-indep'} = [@bd];
+$sequences{'build-arch'} = [@bd];
 $sequences{clean} = [qw{
 	dh_testdir
 	dh_auto_clean
@@ -371,9 +382,9 @@ my @i = qw{
 	dh_compress
 	dh_fixperms
 };
-$sequences{'install'} = [@{$sequences{build}}, @i];
-$sequences{'install-indep'} = [@{$sequences{'build-indep'}}, @i];
-$sequences{'install-arch'} = [@{$sequences{'build-arch'}}, @i];
+$sequences{'install'} = [@i];
+$sequences{'install-indep'} = [@i];
+$sequences{'install-arch'} = [@i];
 my @ba=qw{
 	dh_strip
 	dh_makeshlibs
@@ -385,9 +396,19 @@ my @b=qw{
 	dh_md5sums
 	dh_builddeb
 };
-$sequences{binary} = [@{$sequences{install}}, @ba, @b];
-$sequences{'binary-indep'} = [@{$sequences{'install-indep'}}, @b];
-$sequences{'binary-arch'} = [@{$sequences{'install-arch'}}, @ba, @b];
+$sequences{binary} = [@ba, @b];
+$sequences{'binary-indep'} = [@b];
+$sequences{'binary-arch'} = [@ba, @b];
+
+# Sequence dependencies
+my %sequence_deps;
+$sequence_deps{build} = ['build-arch', 'build-indep'];
+$sequence_deps{install} = ['install-arch', 'install-indep'];
+$sequence_deps{'install-arch'} = ['build-arch'];
+$sequence_deps{'install-indep'} = ['build-indep'];
+$sequence_deps{binary} = ['binary-arch', 'binary-indep'];
+$sequence_deps{'binary-arch'} = ['install-arch'];
+$sequence_deps{'binary-indep'} = ['install-indep'];
 
 # Additional command options
 my %command_opts;
@@ -516,6 +537,29 @@ elsif (! exists $sequences{$sequence}) {
 }
 my @sequence=@{$sequences{$sequence}};
 
+# Recursively invoke sequence dependencies before any further processing.
+# The dh options are not passed in the environment, to ensure that the
+# behaviour is the same if invoked directly.
+my $deps = undef;
+$deps = $sequence_deps{$sequence}
+	if (exists($sequence_deps{$sequence}));
+if (defined($deps)) {
+	foreach my $dep (@$deps) {
+		my $command = 'debian/rules';
+		my @dep_options = ($dep);
+		print "   ".escape_shell($command, @dep_options)."\n";
+		if (! $dh{NO_ACT}) {
+			my $ret=system($command, @dep_options);
+			if ($ret >> 8 != 0) {
+				exit $ret >> 8;
+			}
+			elsif ($ret) {
+				exit 1;
+			}
+		}
+	}
+}
+
 # The list of all packages that can be acted on.
 my @packages=@{$dh{DOPACKAGES}};
 
-- 
1.7.2.3

