* Raphael Hertzog <hert...@debian.org> [120316 12:07]:
> ┗(583)$ sed -ne '/^START/,/^END/ p' ~/tmp/test.txt
>
> Really it's not difficult.

It is not impossible and might not be difficult, but it complicates
things a lot without any need.

> So if you don't want to go for the above, you should at least use
> “print report("status", …)” so that the output ends up always starting
> with "dpkg-buildflags: status: " and this can be reasonably extracted
> too.

Is that required? As this is something to have some fixed format to
allow automatic extraction, I'd really prefer to not make it use
something for another purpose that might change the output in the
future.
(the environment stuff might use it, but then the output would get
inconsistent).

> > +    my @envvars = $build_flags->get_used_environment();
> > +    for my $envvar (@envvars) {
> > +   if (exists $ENV{$envvar}) {
> > +       printf "dpkg-buildflags: environment variable %s=%s\n",
> > +                           $envvar, $ENV{$envvar};
> > +   }
> > +    }
>
> BTW, I don't really like the usage of plain english in the output.
> Plain english ought to be translated... maybe we can use somewhat
> meaningful prefixes ?
>
> E: for environment (or ENV:?)
> V: for vendor (or VENDOR: ?)
> F: for feature (or FEATURE:?)
> R: for result or (or RES:? or FLAG:?)
>
> It also makes it easier to reliably extract a sub-part of the output...

as long as the output format is fixed, it does not make it easier to parse,
only harder to understand for humans.

everything else incorporated.

> > As flags modified by DEB_*_MAINT_* are not reflected by its origin, add
> > a new flag to describe flags modified that way.
>
> Do you really see some value to this? You already have the *_MAINT_* variables
> in the output.
>
> I don't believe that this is required.

It's not required but it might help people a lot to understand it.
Maintainers are humans, too, and getting "vendor" here after you
successfully changed it is a bit confusing (as logical as it is when
going one step back and looking at it).

        Bernhard R. Link
>From 95c1642a44b60e74ce8e2a96abff1cec71e65dac Mon Sep 17 00:00:00 2001
From: "Bernhard R. Link" <brl...@debian.org>
Date: Fri, 16 Mar 2012 10:23:38 +0100
Subject: [PATCH 2/4] Dpkg::BuildFlags: record environment variables looked at

Record environment variables looked at by Dpkg::BuildFlags and
the vendor hooks and make them available via the new
get_used_environment().

Signed-off-by: Bernhard R. Link <brl...@debian.org>
---
 scripts/Dpkg/BuildFlags.pm    |   39 ++++++++++++++++++++++++++++++++++++++-
 scripts/Dpkg/Vendor/Debian.pm |    1 +
 scripts/Dpkg/Vendor/Ubuntu.pm |    1 +
 3 files changed, 40 insertions(+), 1 deletions(-)

diff --git a/scripts/Dpkg/BuildFlags.pm b/scripts/Dpkg/BuildFlags.pm
index 31a54d9..489e4e6 100644
--- a/scripts/Dpkg/BuildFlags.pm
+++ b/scripts/Dpkg/BuildFlags.pm
@@ -69,7 +69,9 @@ sub load_vendor_defaults {
     $self->{'options'} = {};
     $self->{'source'} = {};
     $self->{'features'} = {};
+    $self->{'used_envs'} = {};
     my $build_opts = Dpkg::BuildOptions->new();
+    $self->mark_used_envvar("DEB_BUILD_OPTIONS");
     my $default_flags = $build_opts->has("noopt") ? "-g -O0" : "-g -O2";
     $self->{flags} = {
 	CPPFLAGS => '',
@@ -87,6 +89,8 @@ sub load_vendor_defaults {
     };
     # The Debian vendor hook will add hardening build flags
     run_vendor_hook("update-buildflags", $self);
+    # run_vendor_hook looked at DEB_VENDOR
+    $self->mark_used_envvar("DEB_VENDOR");
 }
 
 =item $bf->load_system_config()
@@ -126,18 +130,22 @@ sub load_environment_config {
     my ($self) = @_;
     foreach my $flag (keys %{$self->{flags}}) {
 	my $envvar = "DEB_" . $flag . "_SET";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->set($flag, $ENV{$envvar}, "env");
 	}
 	$envvar = "DEB_" . $flag . "_STRIP";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->strip($flag, $ENV{$envvar}, "env");
 	}
 	$envvar = "DEB_" . $flag . "_APPEND";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->append($flag, $ENV{$envvar}, "env");
 	}
 	$envvar = "DEB_" . $flag . "_PREPEND";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->prepend($flag, $ENV{$envvar}, "env");
 	}
@@ -155,18 +163,22 @@ sub load_maintainer_config {
     my ($self) = @_;
     foreach my $flag (keys %{$self->{flags}}) {
 	my $envvar = "DEB_" . $flag . "_MAINT_SET";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->set($flag, $ENV{$envvar}, undef);
 	}
 	$envvar = "DEB_" . $flag . "_MAINT_STRIP";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->strip($flag, $ENV{$envvar}, undef);
 	}
 	$envvar = "DEB_" . $flag . "_MAINT_APPEND";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->append($flag, $ENV{$envvar}, undef);
 	}
 	$envvar = "DEB_" . $flag . "_MAINT_PREPEND";
+	$self->mark_used_envvar($envvar);
 	if (exists $ENV{$envvar}) {
 	    $self->prepend($flag, $ENV{$envvar}, undef);
 	}
@@ -390,6 +402,31 @@ sub list {
     return sort keys %{$self->{'flags'}};
 }
 
+=item $bf->mark_used_envvar($envvar)
+
+Records that the given environment variable had influenced
+or could have influenced (if it had existed or had a different
+value) the calculated flags.
+
+=cut
+
+sub mark_used_envvar {
+    my ($self, $envvar) = @_;
+    $self->{'used_envs'}{$envvar} = 1;
+}
+
+=item my @list = $bf->list_used_envvars()
+
+Returns a list of all environment variables that had a
+possible influence.
+
+=cut
+
+sub list_used_envvars {
+    my ($self) = @_;
+    return sort keys $self->{'used_envs'};
+}
+
 =back
 
 =head1 CHANGES
@@ -405,7 +442,7 @@ based on the package maintainer directives.
 =head Version 1.02
 
 New methods: $bf->get_features(), $bf->has_features(), $bf->set_feature(),
-  $bf->get_feature_areas().
+  $bf->get_feature_areas(), $bf->mark_used_envvar(), $bf->list_used_envvars().
 
 =head1 AUTHOR
 
diff --git a/scripts/Dpkg/Vendor/Debian.pm b/scripts/Dpkg/Vendor/Debian.pm
index b4ce4cf..ab4ddf2 100644
--- a/scripts/Dpkg/Vendor/Debian.pm
+++ b/scripts/Dpkg/Vendor/Debian.pm
@@ -100,6 +100,7 @@ sub add_hardening_flags {
 
     # Adjust features based on Maintainer's desires.
     my $opts = Dpkg::BuildOptions->new(envvar => "DEB_BUILD_MAINT_OPTIONS");
+    $flags->mark_used_envvar("DEB_BUILD_MAINT_OPTIONS");
     foreach my $feature (split(",", $opts->get("hardening") // "")) {
 	$feature = lc($feature);
 	if ($feature =~ s/^([+-])//) {
diff --git a/scripts/Dpkg/Vendor/Ubuntu.pm b/scripts/Dpkg/Vendor/Ubuntu.pm
index 7e60fcf..17648bd 100644
--- a/scripts/Dpkg/Vendor/Ubuntu.pm
+++ b/scripts/Dpkg/Vendor/Ubuntu.pm
@@ -108,6 +108,7 @@ sub run_hook {
 	$self->SUPER::run_hook($hook, $flags);
 
 	# Allow control of hardening-wrapper via dpkg-buildpackage DEB_BUILD_OPTIONS
+	$flags->mark_used_envvar("DEB_BUILD_OPTIONS");
 	my $build_opts = Dpkg::BuildOptions->new();
 	my $hardening;
 	if ($build_opts->has("hardening")) {
-- 
1.7.9.1

>From c551a23df47eddca85c739fc4ccede6c7ea264b4 Mon Sep 17 00:00:00 2001
From: "Bernhard R. Link" <brl...@debian.org>
Date: Thu, 15 Mar 2012 11:25:39 +0100
Subject: [PATCH 3/4] dpkg-buildflags: add --status action to describe what is
 happening

It's hard to see from a build log file what values should have been
used and why. The new --status action added by this tries to output
all meaningful information in way useful for human consumption
and for automatic log parsers.

Signed-off-by: Bernhard R. Link <brl...@debian.org>
---
 man/dpkg-buildflags.1      |   10 +++++++++
 scripts/dpkg-buildflags.pl |   49 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/man/dpkg-buildflags.1 b/man/dpkg-buildflags.1
index 4244b82..234d1cf 100644
--- a/man/dpkg-buildflags.1
+++ b/man/dpkg-buildflags.1
@@ -72,6 +72,16 @@ Print the list of flags supported by the current vendor
 (one per line). See the \fBSUPPORTED FLAGS\fP section for more
 information about them.
 .TP
+.BI \-\-status
+Display any information that can be useful to explain the behaviour
+of dpkg-buildflags:
+relevant environment variables, current vendor, state of all feature flags.
+Also print the resulting compiler flags with their origin.
+
+This is intended to be run from \fBdebian/rules\fP, so that the
+build log keeps a clear trace of the build flags used. This can
+be useful to diagnose problems related to them.
+.TP
 .BI \-\-export= format
 Print to standard output shell (if \fIformat\fP is \fBsh\fP) or make
 (if \fIformat\fP is \fBmake\fP) commands that can be used to export
diff --git a/scripts/dpkg-buildflags.pl b/scripts/dpkg-buildflags.pl
index d0f9fa8..6a70c56 100755
--- a/scripts/dpkg-buildflags.pl
+++ b/scripts/dpkg-buildflags.pl
@@ -24,6 +24,7 @@ use Dpkg;
 use Dpkg::Gettext;
 use Dpkg::ErrorHandling;
 use Dpkg::BuildFlags;
+use Dpkg::Vendor qw(get_current_vendor);
 
 textdomain("dpkg-dev");
 
@@ -52,6 +53,9 @@ Actions:
                      compilation flags in a shell script, in make,
                      or on a ./configure command line.
   --dump             output all compilation flags with their values
+  --status           print a synopsis with all parameters affecting
+                     the behaviour of dpkg-buildflags and the resulting
+                     flags and their origin.
   --help             show this help message.
   --version          show the version.
 "), $progname;
@@ -72,14 +76,10 @@ while (@ARGV) {
             if defined($action);
         my $type = $1 || "sh";
         $action = "export-$type";
-    } elsif (m/^--dump$/) {
-        usageerr(_g("two commands specified: --%s and --%s"), "dump", $action)
-            if defined($action);
-        $action = "dump";
-    } elsif (m/^--list$/) {
-        usageerr(_g("two commands specified: --%s and --%s"), "list", $action)
+    } elsif (m/^--(list|status|dump)$/) {
+        usageerr(_g("two commands specified: --%s and --%s"), $1, $action)
             if defined($action);
-        $action = "list";
+        $action = $1;
     } elsif (m/^-(h|-help)$/) {
         usage();
         exit 0;
@@ -147,6 +147,41 @@ if ($action eq "get") {
 	print "$flag=$value\n";
     }
     exit(0);
+} elsif ($action eq "status") {
+    # prefix everything with "dpkg-buildflags: " to allow easy extraction
+    # from a buildd log. Thus also using hardcoded, non-translated strings
+    # instead of Dpkg:ErrorHandling functions.
+
+    # First print all environment variables that might have changed the
+    # results (only existing ones, might make sense to add a option to
+    # also show which could have set to modify it).
+    my @envvars = $build_flags->list_used_envvars();
+    for my $envvar (@envvars) {
+	if (exists $ENV{$envvar}) {
+	    printf "dpkg-buildflags: environment variable %s=%s\n",
+				$envvar, $ENV{$envvar};
+	}
+    }
+    my $vendor = Dpkg::Vendor::get_current_vendor();
+    $vendor = "undefined" unless defined($vendor);
+    print "dpkg-buildflags: vendor is $vendor\n";
+    # Then the resulting features:
+    foreach my $area (sort $build_flags->get_feature_areas()) {
+	print "dpkg-buildflags: $area features:";
+	my %features = $build_flags->get_features($area);
+	foreach my $feature (sort keys %features) {
+	    printf " %s=%s", $feature, $features{$feature} ? "yes" : "no";
+	}
+	print "\n";
+    }
+    # Then the resulting values (with their origin):
+    foreach my $flag ($build_flags->list()) {
+	my $value = $build_flags->get($flag);
+	my $origin = $build_flags->get_origin($flag);
+	# Note that DEB_*_MAINT_* does not effect $origin.
+	print "dpkg-buildflags: $flag [$origin]: $value\n";
+    }
+    exit(0);
 }
 
 exit(1);
-- 
1.7.9.1

Reply via email to