From 99ad2a71e45b1d015bd4539ba093a9fae9fbf53b Mon Sep 17 00:00:00 2001
From: Roger Leigh <rleigh@debian.org>
Date: Sun, 12 Jun 2011 20:22:43 +0100
Subject: [PATCH] dh: Use minimal sequences if delegating work

The build and install rules run a minimal sequence if the build-arch or
build-indep, or install-arch or install-indep targets, respectively,
are present in debian/rules.  The purpose is to not do work ahead of
time, such as building before the build-arch or build-indep targets are
built, which could potentially lead to misbuilds.  If the targets are
not defined, the sequences may be run directly which is faster due to
being able to run the arch and indep commands together.
---
 dh | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/dh b/dh
index 75070ea..8af408c 100755
--- a/dh
+++ b/dh
@@ -338,6 +338,10 @@ if (is_make_jobserver_unavailable()) {
 
 # Definitions of sequences.
 my %sequences;
+my @bd_minimal = qw{
+	dh_testdir
+	dh_auto_configure
+};
 my @bd = qw{
 	dh_testdir
 	dh_auto_configure
@@ -357,6 +361,9 @@ $sequences{clean} = [qw{
 	dh_auto_clean
 	dh_clean
 }];
+my @i_minimal = qw{
+	dh_testroot
+};
 my @i = qw{
 	dh_testroot
 	dh_prep
@@ -550,6 +557,23 @@ elsif (! exists $sequences{$sequence}) {
 	error "Unknown sequence $sequence (choose from: ".
 		join(" ", sort keys %sequences).")";
 }
+
+# Note: it's not safe to run rules_explicit_target before this point
+# due to dh being recursively invoked with debhelper-fail-me as the
+# sequence
+# If debian/rules defines build-arch or build-indep, run sequences
+# separately.
+if (rules_explicit_target('build-arch') ||
+    rules_explicit_target('build-indep')) {
+    $sequences{build} = [@bd_minimal, 'rules:build-arch', 'rules:build-indep'];
+}
+# If debian/rules defines install-arch or install-indep, run sequences
+# separately.
+if (rules_explicit_target('install-arch') ||
+    rules_explicit_target('install-indep')) {
+	$sequences{'install'} = ['rules:build', @i_minimal, 'rules:install-arch', 'rules:install-indep'];
+}
+
 my @sequence=@{$sequences{$sequence}};
 
 # The list of all packages that can be acted on.
-- 
2.39.5