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
|
#!/usr/bin/perl
=head1 NAME
dh_golang - Generates Static-Built-Using substvar for Go programs
=cut
use strict;
use warnings;
use Cwd qw(realpath);
use Dpkg::Substvars;
use Debian::Debhelper::Dh_Lib; # not in core
use Debian::Debhelper::Dh_Buildsystems; # not in core
=head1 SYNOPSIS
B<dh_golang> [S<I<debhelper options>>]
=head1 DESCRIPTION
B<dh_golang> is a debhelper program which adds the misc:Static-Built-Using
substvar 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.
B<dh_golang> started out in 2013 by generating the misc:Built-Using substvar,
but it was clarified Debian Policy in 2017 that the Built-Using field was
intended only for license-required dependencies, hence the switch to the new
Static-Built-Using field in 2022.
For backward compatibility, B<dh_golang> will continue to generate the
misc:Built-Using substvar until all Go packages have transitioned to
misc:Static-Built-Using.
=head1 NOTES
The best way to invoke B<dh_golang> is by adding B<dh-sequence-golang> to the
package build dependencies and using B<dh> or by using B<dh --with=golang>.
=cut
sub uniq {
my %list = map { $_ => 1 } @_;
return sort keys %list;
}
sub exec_single {
my ($cmd, @args) = @_;
verbose_print(escape_shell(@_));
my @output = qx($cmd @args);
error_exitcode($cmd) if $? != 0;
chomp(@output);
return @output;
}
# Amount of chunking we are going to use for dpkg commands, which should speed
# up the execution by avoiding too many database loads.
use constant CHUNKSIZE => 200;
sub exec_chunked {
my ($cmd, @list) = @_;
my @result;
while (@list) {
push @result, exec_single($cmd, splice(@list, 0, CHUNKSIZE));
}
return @result;
}
############################################################################
# Generate misc:Static-Built-Using substvar.
############################################################################
buildsystems_init();
my $bs = load_buildsystem("golang");
$bs->set_go_env();
my @targets = $bs->get_targets();
my $tmpl = '{{ range .Deps }}{{.}}
{{ end }}';
my @godeps = exec_single(qq{go list -f '$tmpl'}, @targets);
my $gofiletmpl = '\
{{ .Dir }}/{{ index (or .GoFiles .CgoFiles .TestGoFiles .XTestGoFiles .IgnoredGoFiles) 0 }}';
# gccgo can't find files for standard libraries, see #907263
if (grep /gccgo/, exec_single(q{go version}) or grep /gcc/, exec_single(q{go env GOTOOLDIR})) {
$gofiletmpl = '{{if not .Standard}}' . $gofiletmpl . '{{end}}';
}
my @gofiles = exec_chunked(qq{go list -f '$gofiletmpl'}, uniq(@godeps));
my @realpath;
foreach my $pathname (@gofiles) {
my $realpath = realpath($pathname);
# gofiles will include packages being built, so exclude those.
if ($realpath !~ /^\Q$bs->{cwd}\E/) {
push @realpath, $realpath;
}
}
my @searchoutput = exec_chunked('dpkg-query --search', @realpath);
my @gopkgs = split /, */, join ', ', map { s/: .+$//r } @searchoutput;
my @srcdeps = exec_chunked(q{dpkg-query -f='${source:Package} (= ${source:Version})\n' -W}, uniq(@gopkgs));
my $static_built_using = join ', ', uniq(@srcdeps);
# If there is an easier way to have a universal misc:Static-Built-Using on all binary
# packages, I am happy to merge your patch :).
foreach my $package (@{$dh{DOPACKAGES}}) {
# Skip adding the misc:Static-Built-Using substvar if the package is
# architecture independent, as those should not end up embedding
# other Go modules.
next if package_arch($package) eq 'all';
addsubstvar($package, "misc:Static-Built-Using", $static_built_using);
# Add old misc:Built-Using substvar for backward compatibility
# until all Go packages have transitioned to misc:Static-Built-Using
my $ext = pkgext($package);
my $svfile = "debian/${ext}substvars";
my $sv = Dpkg::Substvars->new($svfile);
$sv->set_as_optional('misc:Built-Using', $static_built_using);
$sv->save($svfile);
}
=head1 SEE ALSO
L<Debian::Debhelper::Buildsystem::golang(3pm)>, L<dh-make-golang(1)>, L<dh(1)>, L<deb-control(5)>
=head1 AUTHORS
=over
=item Michael Stapelberg <stapelberg@debian.org>
=item Debian Go Packaging Team <team+pkg-go@tracker.debian.org>
=back
=cut
# vim:ts=4:sw=4:et
|