Package: dh-golang
Version: 1.48

It would be nice if dh-golang would support setting go buildtags.

My use-case is a relatively large go application (snapd). It has a
buildtag (nosecboot) to exclude parts that are only relevant for
embedded systems. Right now I workaround this by removing files but
that is a bit of a ugly workaround. Better support for buildtags would
solve this nicely. On fedora their rpm macro honors "BUILDTAGS" so I
was wondering if we should maybe support for a DH_GOLANG_BUILDTAGS
environment?

Attached is a very lightly tested patch that seems to be doing what I
want. But I'm really not a perl guy so someone should have a careful
look :)

Cheers,
 Michael
diff -Nru dh-golang-1.48/debian/changelog dh-golang-1.48+mvo1/debian/changelog
--- dh-golang-1.48/debian/changelog	2020-02-26 21:28:21.000000000 +0100
+++ dh-golang-1.48+mvo1/debian/changelog	2020-04-15 09:27:05.000000000 +0200
@@ -1,3 +1,9 @@
+dh-golang (1.48+mvo1) UNRELEASED; urgency=medium
+
+  * add support for DH_GOLANG_BUILDTAGS
+
+ -- Michael Vogt <michael.v...@ubuntu.com>  Wed, 15 Apr 2020 09:27:05 +0200
+
 dh-golang (1.48) unstable; urgency=medium
 
   * Prevent "no non-test Go files" error during build
diff -Nru dh-golang-1.48/lib/Debian/Debhelper/Buildsystem/golang.pm dh-golang-1.48+mvo1/lib/Debian/Debhelper/Buildsystem/golang.pm
--- dh-golang-1.48/lib/Debian/Debhelper/Buildsystem/golang.pm	2020-02-26 21:28:21.000000000 +0100
+++ dh-golang-1.48+mvo1/lib/Debian/Debhelper/Buildsystem/golang.pm	2020-04-15 09:27:05.000000000 +0200
@@ -213,6 +213,11 @@
 Depending on what the Go package in question uses C<go generate> for, you may
 want to enable C<DH_GOLANG_GO_GENERATE>:
 
+=item DH_GOLANG_BUILDTAGS
+
+C<DH_GOLANG_BUILDTAGS> (string, default empty) controls what build tags
+to use during the build.
+
 =over
 
 =item *
@@ -593,20 +598,20 @@
     my @targets = $this->get_targets();
 
     if (exists($ENV{DH_GOLANG_GO_GENERATE}) && $ENV{DH_GOLANG_GO_GENERATE} == 1) {
-        $this->doit_in_builddir("go", "generate", "-v", @_, @targets);
+        $this->doit_in_builddir("go", "generate","-tags", $ENV{DH_GOLANG_BUILDTAGS}, "-v", @_, @targets);
     }
     unshift @_, ('-p', $this->get_parallel());
 
     if (_go1_has_minor(13)) {
         # Go 1.13 officially supports reproducible build, adding new -trimpath option
         # https://github.com/golang/go/issues/16860
-        $this->doit_in_builddir("go", "install", "-trimpath", "-v", @_, @targets);
+        $this->doit_in_builddir("go", "install", "-tags", $ENV{DH_GOLANG_BUILDTAGS}, "-trimpath", "-v", @_, @targets);
     } elsif (_go1_has_minor(10)) {
         # Go 1.10 changed flag behaviour, -{gc,asm}flags=all= only works for Go >= 1.10.
         my $trimpath = "all=\"-trimpath=" . $ENV{GOPATH} . "/src\"";
-        $this->doit_in_builddir("go", "install", "-gcflags=$trimpath", "-asmflags=$trimpath", "-v", @_, @targets);
+        $this->doit_in_builddir("go", "install", "-tags", $ENV{DH_GOLANG_BUILDTAGS}, "-gcflags=$trimpath", "-asmflags=$trimpath", "-v", @_, @targets);
     } else {
-        $this->doit_in_builddir("go", "install", "-v", @_, @targets);
+        $this->doit_in_builddir("go", "install", "-tags", $ENV{DH_GOLANG_BUILDTAGS}, "-v", @_, @targets);
     }
 }
 
@@ -621,11 +626,11 @@
     # of many not-yet-fixed upstream packages, so we disable it for the time
     # being.
     if (_go1_has_minor(10)) {
-        $this->doit_in_builddir("go", "test", "-vet=off", "-v", @_, @targets);
+        $this->doit_in_builddir("go", "test", "-tags", $ENV{DH_GOLANG_BUILDTAGS}, "-vet=off", "-v", @_, @targets);
     } else {
         # For backwards-compatibility with Go < 1.10, which incorrectly
         # interprets the -vet=off flag as a target:
-        $this->doit_in_builddir("go", "test", "-v", @_, @targets);
+        $this->doit_in_builddir("go", "test", "-tags", $ENV{DH_GOLANG_BUILDTAGS}, "-v", @_, @targets);
     }
 }
 
diff -Nru dh-golang-1.48/script/dh_golang dh-golang-1.48+mvo1/script/dh_golang
--- dh-golang-1.48/script/dh_golang	2020-02-24 07:29:05.000000000 +0100
+++ dh-golang-1.48+mvo1/script/dh_golang	2020-04-15 09:27:05.000000000 +0200
@@ -74,7 +74,7 @@
 
 my $tmpl = '{{ range .Deps }}{{.}}
 {{ end }}';
-my @godeps = exec_single(qq{go list -f '$tmpl'}, @targets);
+my @godeps = exec_single(qq{go list -tags "$ENV{DH_GOLANG_BUILDTAGS}" -f '$tmpl'}, @targets);
 
 my $gofiletmpl = '\
 {{ .Dir }}/{{ index (or .GoFiles .CgoFiles .TestGoFiles .XTestGoFiles .IgnoredGoFiles) 0 }}';
@@ -84,7 +84,7 @@
     $gofiletmpl = '{{if not .Standard}}' . $gofiletmpl . '{{end}}';
 }
 
-my @gofiles = exec_chunked(qq{go list -f '$gofiletmpl'}, uniq(@godeps));
+my @gofiles = exec_chunked(qq{go list -tags "$ENV{DH_GOLANG_BUILDTAGS}" -f '$gofiletmpl'}, uniq(@godeps));
 
 my @realpath;
 foreach my $pathname (@gofiles) {

Reply via email to