This file is indexed.

/usr/bin/dh_golang is in dh-golang 1.34.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
#!/usr/bin/perl -w

=head1 NAME

dh_golang - Generates Built-Using substvar

=cut

use strict;
use Cwd qw(realpath);
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: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.

=head1 NOTES

The best way to invoke B<dh_golang> is 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;
    for (my $i = 0; $i < @list; $i += CHUNKSIZE) {
        push @result, exec_single($cmd, splice(@list, $i, CHUNKSIZE));
    }

    return @result;
}

############################################################################
# Generate misc:Built-Using substvar.
############################################################################

buildsystems_init();
my $bs = load_buildsystem("golang");

$bs->_set_gopath();

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 }}';
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 $built_using = join ', ', uniq(@srcdeps);

# If there is an easier way to have a universal misc:Built-Using on all binary
# packages, I am happy to merge your patch :).
foreach my $package (@{$dh{DOPACKAGES}}) {
    # Skip adding the misc:Built-Using substvar if the package is
    # architecture independent, as those should not end up embeddeding
    # other Go modules.
    next if package_arch($package) eq 'all';

    addsubstvar($package, "misc:Built-Using", $built_using);
}

=head1 SEE ALSO

dh(1)

=head1 AUTHORS

Michael Stapelberg <stapelberg@debian.org>

=cut

# vim:ts=4:sw=4:et