#!/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 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 embeddeding
    # 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
    addsubstvar($package, "misc:Built-Using", $static_built_using);
}

=head1 SEE ALSO

dh(1), Debian::Debhelper::Buildsystem::golang(1), 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
