From e8c799129672d5b7dbac58a06fadd175cffaa7ed Mon Sep 17 00:00:00 2001
From: Joey Hess <joey@kitenet.net>
Date: Tue, 14 Jun 2011 17:22:34 -0400
Subject: [PATCH] dpkg-buildflags support

* dh_auto_build, dh_auto_configure, dh: Set environment variables
  listed by dpkg-buildflags --export. Any environment variables that
  are already set to other values will not be changed.
  Closes: #544844
* Also, support DEB_BUILD_OPTIONS=noopt, by changing -O2 to -O0.
---
 Debian/Debhelper/Dh_Buildsystems.pm |  1 +
 Debian/Debhelper/Dh_Lib.pm          | 33 ++++++++++++++++++++++++++++-
 debhelper.pod                       |  4 ++++
 debian/changelog                    |  6 ++++++
 dh                                  |  5 ++++-
 5 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/Debian/Debhelper/Dh_Buildsystems.pm b/Debian/Debhelper/Dh_Buildsystems.pm
index 22f80f9..9c3fc05 100644
--- a/Debian/Debhelper/Dh_Buildsystems.pm
+++ b/Debian/Debhelper/Dh_Buildsystems.pm
@@ -159,6 +159,7 @@ sub buildsystems_init {
 	);
 	$args{options}{$_} = $options{$_} foreach keys(%options);
 	Debian::Debhelper::Dh_Lib::init(%args);
+	Debian::Debhelper::Dh_Lib::set_buildflags();
 	set_parallel($max_parallel);
 }
 
diff --git a/Debian/Debhelper/Dh_Lib.pm b/Debian/Debhelper/Dh_Lib.pm
index 51b53d3..51f16a6 100644
--- a/Debian/Debhelper/Dh_Lib.pm
+++ b/Debian/Debhelper/Dh_Lib.pm
@@ -18,7 +18,7 @@ use vars qw(@ISA @EXPORT %dh);
 	    &inhibit_log &load_log &write_log &commit_override_log
 	    &dpkg_architecture_value &sourcepackage
 	    &is_make_jobserver_unavailable &clean_jobserver_makeflags
-	    &cross_command);
+	    &cross_command &set_buildflags);
 
 my $max_compat=9;
 
@@ -900,4 +900,35 @@ sub cross_command {
 	}
 }
 
+# Sets environment variables from dpkg-buildflags. Avoids changing
+# any existing environment variables. Supports DEB_BUILD_OPTIONS=noopt.
+sub set_buildflags {
+	# optimisation
+	return if $ENV{DH_INTERNAL_BUILDFLAGS};
+	$ENV{DH_INTERNAL_BUILDFLAGS}=1;
+
+	my $noopt=$ENV{DEB_BUILD_OPTIONS}=~/noopt/;
+
+	my @shell=`dpkg-buildflags --export`;
+	foreach my $line (@shell) {
+		chomp $line;
+		if ($line=~/^export\s+([^=]+)=(["'])(.*)\2$/) {
+			my $var=$1;
+			my $val=$3;
+			if ($noopt) {
+				$val=$ENV{$var} if exists $ENV{$var};
+				$val=~s/-O\d+/-O0/;
+				$ENV{$var}=$val;
+				next;
+			}
+			elsif (! exists $ENV{$var}) {
+				$ENV{$var}=$val;
+			}
+		}
+		else {
+			warning "unparsable line from dpkg-buildflags: $line";
+		}
+	}
+}
+
 1
diff --git a/debhelper.pod b/debhelper.pod
index d61cc84..c076acf 100644
--- a/debhelper.pod
+++ b/debhelper.pod
@@ -191,6 +191,10 @@ in ALL packages acted on, not just the first.
 
 =head1 BUILD SYSTEM OPTIONS
 
+All of the B<dh_auto_>I<*> debhelper programs sets environment variables
+listed by B<dpkg-buildflags>, unless they are already set. They
+support DEB_BUILD_OPTIONS=noopt too.
+
 The following command line options are supported by all of the B<dh_auto_>I<*>
 debhelper programs. These programs support a variety of build systems,
 and normally heuristically determine which to use, and how to use them.
diff --git a/debian/changelog b/debian/changelog
index 35692da..fb01bc6 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -5,6 +5,12 @@ debhelper (8.1.7) UNRELEASED; urgency=low
   * dh_movefiles: Optimise use of xargs. Closes: #627737
   * dh_auto_configure: In v9, does not include the source package name
     in --libexecdir when using autoconf. Closes: #541458
+  * dh_auto_build, dh_auto_configure, dh: Set environment variables
+    listed by dpkg-buildflags --export. Any environment variables that
+    are already set to other values will not be changed.
+    Closes: #544844
+  * Also, support DEB_BUILD_OPTIONS=noopt, by changing -O2 to -O0
+    in any variables listed by dpkg-buildflags.
 
  -- Joey Hess <joeyh@debian.org>  Sat, 21 May 2011 15:24:19 -0400
 
diff --git a/dh b/dh
index 50e0f14..7c53611 100755
--- a/dh
+++ b/dh
@@ -273,6 +273,9 @@ 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.
 
+B<dh> sets environment variables listed by B<dpkg-buildflags>, unless
+they are already set. It supports DEB_BUILD_OPTIONS=noopt too.
+
 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
@@ -310,7 +313,7 @@ init(options => {
 	bundling => 0,
 );
 inhibit_log();
-
+set_buildflags();
 
 # If make is using a jobserver, but it is not available
 # to this process, clean out MAKEFLAGS. This avoids
-- 
2.39.5