Ubuntu Pastebin

Paste from mwhudson at Thu, 14 Apr 2016 07:30:27 +0000

Download as text
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
diff --git a/debian/changelog b/debian/changelog
index d92b257..e9dba09 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,8 +1,16 @@
-dh-golang (1.12ubuntu1) xenial; urgency=medium
+dh-golang (1.14) unstable; urgency=medium
 
-  * Follow /usr/bin/go symlink when computing Built-Using. (LP: #1564124)
+  [ Michael Hudson-Doyle ]
+  * Compute Built-Using with go list, not Build-Depends (Closes: #819473)
 
- -- Michael Hudson-Doyle <michael.hudson@canonical.com>  Thu, 31 Mar 2016 11:42:07 +1300
+ -- Michael Stapelberg <stapelberg@debian.org>  Thu, 14 Apr 2016 09:11:54 +0200
+
+dh-golang (1.13) unstable; urgency=medium
+
+  [ Stephen Gelman ]
+  * Set build_dep to 1 to support the “native” architecture (Closes: #819596)
+
+ -- Michael Stapelberg <stapelberg@debian.org>  Thu, 14 Apr 2016 08:51:20 +0200
 
 dh-golang (1.12) unstable; urgency=medium
 
diff --git a/lib/Debian/Debhelper/Buildsystem/golang.pm b/lib/Debian/Debhelper/Buildsystem/golang.pm
index bedafd0..fbdf510 100644
--- a/lib/Debian/Debhelper/Buildsystem/golang.pm
+++ b/lib/Debian/Debhelper/Buildsystem/golang.pm
@@ -35,6 +35,11 @@ sub _set_dh_gopkg {
     $ENV{DH_GOPKG} = $source->{"XS-Go-Import-Path"};
 }
 
+sub _set_gopath {
+    my $this = shift;
+    $ENV{GOPATH} = $this->{cwd} . '/' . $this->get_builddir();
+}
+
 sub _link_contents {
     my ($src, $dst) = @_;
 
@@ -156,7 +161,6 @@ sub get_targets {
 sub build {
     my $this = shift;
 
-    $ENV{GOPATH} = $this->{cwd} . '/' . $this->get_builddir();
     if (exists($ENV{DH_GOLANG_GO_GENERATE}) && $ENV{DH_GOLANG_GO_GENERATE} == 1) {
         $this->doit_in_builddir("go", "generate", "-v", @_, get_targets());
     }
@@ -166,7 +170,7 @@ sub build {
 sub test {
     my $this = shift;
 
-    $ENV{GOPATH} = $this->{cwd} . '/' . $this->get_builddir();
+    $this->_set_gopath();
     $this->doit_in_builddir("go", "test", "-v", @_, get_targets());
 }
 
diff --git a/script/dh_golang b/script/dh_golang
index 3070b61..5e1e71d 100755
--- a/script/dh_golang
+++ b/script/dh_golang
@@ -7,6 +7,7 @@ dh_golang - Generates Built-Using substvar
 =cut
 
 use strict;
+use Cwd qw(realpath);
 use Debian::Debhelper::Dh_Lib; # not in core
 use Dpkg; # not in core
 use Dpkg::Control; # not in core
@@ -15,6 +16,7 @@ use Dpkg::Deps; # not in core
 use Dpkg::Gettext; # not in core
 use Scalar::Util qw(blessed); # in core since v5.7.3
 use List::Util qw(first); # in core since v5.7.3
+use Debian::Debhelper::Buildsystem::golang;
 
 =head1 SYNOPSIS
 
@@ -23,14 +25,9 @@ B<dh_golang> [S<I<debhelper options>>]
 =head1 DESCRIPTION
 
 B<dh_golang> is a debhelper program which adds the misc:Built-Using substvar
-based on the Build-Dependencies of your packages. Every package starting with
-golang is queried for the precise version number.
-
-As an example, if you Build-Depend on golang-pq-dev, the resulting
-misc:Built-Using value (aside from the precise version number) will look like
-this:
-
-golang (= 2:1.1.1-1), golang-pq-dev (= 0.0~git20130606-1),
+based on the dependencies of the package being built. It uses go list to
+determine the packages imported and dpkg-query to find the source package and
+version that provided that package.
 
 =head1 NOTES
 
@@ -40,90 +37,32 @@ The best way to invoke B<dh_golang> is by using B<dh --with=golang>.
 
 init();
 
-# This code was copied from dpkg-checkbuilddeps, see
-# http://sources.debian.net/src/dpkg/1.18.1/scripts/dpkg-checkbuilddeps.pl/?hl=140#L140
-sub parse_status {
-    my $status = shift;
-
-    my $facts = Dpkg::Deps::KnownFacts->new();
-    local $/ = '';
-    open(my $status_fh, '<', $status)
-        or syserr(g_('cannot open %s'), $status);
-    while (<$status_fh>) {
-        next unless /^Status: .*ok installed$/m;
-
-        my ($package) = /^Package: (.*)$/m;
-        my ($version) = /^Version: (.*)$/m;
-        my ($arch) = /^Architecture: (.*)$/m;
-        my ($multiarch) = /^Multi-Arch: (.*)$/m;
-        $facts->add_installed_package($package, $version, $arch,
-                                      $multiarch);
-
-        if (/^Provides: (.*)$/m) {
-            my $provides = deps_parse($1, reduce_arch => 1, union => 1);
-            next if not defined $provides;
-            foreach (grep { $_->isa('Dpkg::Deps::Simple') }
-                                 $provides->get_deps())
-            {
-                $facts->add_provided_package($_->{package},
-                                    $_->{relation}, $_->{version},
-                                    $package);
-            }
-        }
-    }
-    close $status_fh;
-
-    return $facts;
-}
-
 ############################################################################
-# Generate misc:Built-Using substvar with the versions of all golang-*
-# build-dependency packages.
+# Generate misc:Built-Using substvar.
 ############################################################################
 
-my $built_using;
-
-my $control = Dpkg::Control::Info->new();
-my $source = $control->get_source();
-my $build_depends = $source->{"Build-Depends"};
-if (defined($build_depends) && $build_depends ne '') {
-    my $facts;
-    if ($Dpkg::VERSION >= 1.01) {
-        $facts = parse_status($Dpkg::ADMINDIR . "/status");
-    } else {
-        $facts = parse_status($Dpkg::admindir . "/status");
-    }
+my $bs = Debian::Debhelper::Buildsystem::golang->new();
 
-    sub flatten {
-        my ($dep) = @_;
-        if (blessed($dep) eq 'Dpkg::Deps::Simple') {
-            return $dep->get_evaluation($facts) ? $dep->{package} : undef;
-        }
-        if (blessed($dep) eq 'Dpkg::Deps::OR') {
-            # Return the first installed package.
-            return first { defined($_) } map { flatten($_) } $dep->get_deps();
-        }
-        die 'Unexpected object (of type ' . blessed($dep) . '), has the Dpkg::Deps API changed?';
-    }
+$bs->_set_dh_gopkg();
+$bs->_set_gopath();
 
-    my $deps = deps_parse($build_depends, reduce_restrictions => 1);
-    my $golang_deps = join(' ', grep { /^golang-/ }
-                                map { flatten($_) }
-                                $deps->get_deps());
-    # If the build depends contains a package that comes from golang-defaults
-    # (usually 'golang-go' but possibly just 'golang'), report the owner of
-    # where /usr/bin/go in Built-Using as well.
-    my $source_deps = `dpkg-query -f='\${source:Package} ' -W $golang_deps`;
-    if ($source_deps =~ /\bgolang-defaults\b/) {
-        my $golang_go_dep = `dpkg-query --search \$(readlink -f /usr/bin/go) 2>/dev/null | cut -d: -f1`;
-        if ($? == 0) {
-            chomp($golang_deps .= " $golang_go_dep");
-        }
-    }
-    if ($golang_deps ne '') {
-        $built_using = `dpkg-query -f='\${source:Package} (= \${source:Version}), ' -W $golang_deps`;
+my @targets = $bs->get_targets();
+
+my $godeps = `go list -f '{{ range .Deps }}{{.}} {{ end }}' @targets`;
+$godeps =~ s/\n/ /g;
+my @godirs = split /\n/, `go list -f '{{ .Dir }}' $godeps`;
+my $realgodirs;
+my $cwd = $bs->{cwd};
+for my $godir (@godirs) {
+    my $realpath = realpath($godir);
+    # @godirs will include the directories of the package being built, so exclude them.
+    if ($realpath !~ /^$bs->{cwd}/) {
+        $realgodirs .= realpath($godir) . " ";
     }
 }
+my $pkgs = `dpkg-query --search $realgodirs | cut -d: -f1`;
+$pkgs =~ s/\n/ /g;
+my $built_using = `dpkg-query -f='\${source:Package} (= \${source:Version}), ' -W $pkgs`;
 
 # If there is an easier way to have a universal misc:Built-Using on all binary
 # packages, I am happy to merge your patch :).
Download as text