#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk

# Non-Go files needed at build or test time. dh-go copies only *.go, go.mod,
# go.sum and testdata/ contents into the build tree. explain_cve.tmpl is a
# go:embed target; the __snapshots__ files are go-snaps golden files, without
# which go-snaps would write fresh snapshots and pass without comparing.
export DH_GOLANG_INSTALL_EXTRA := \
	grype/presenter/explain/explain_cve.tmpl \
	$(wildcard cmd/grype/cli/ui/__snapshots__/*.snap) \
	$(wildcard cmd/grype/internal/ui/__snapshots__/*.snap) \
	$(wildcard grype/presenter/explain/__snapshots__/*.snap) \
	$(wildcard grype/presenter/table/__snapshots__/*.snap)

# Code generators, which are main packages with no tests, and test suites that
# need docker, network access or a pre-built grype binary. Each entry is
# justified in debian/README.source.
export DH_GOLANG_EXCLUDES := \
	cmd/grype/cli/commands/internal/jsonschema \
	grype/db/internal/codename/generate \
	grype/db/internal/provider/unmarshal/osvmodel/generate \
	grype/db/v6/build/transformers/os/generate \
	grype/db/v6/schema$$ \
	grype/internal/packagemetadata/generate \
	internal/dbtest/cmd/manager \
	test/cli \
	test/integration

# Individual tests that need docker or download Go modules; see
# debian/README.source.
SKIP_TESTS := TestSyftLocationExcludes|TestMatcherGolang_GoSymbols

%:
	dh $@ --with=golang --buildsystem=golang

execute_after_dh_auto_configure:
	# https://bugs.debian.org/1149023: dh-go writes "replace M => ./src/M" for
	# any directory under _build/src, without checking that it holds a go.mod.
	# Point those at a stub of our own (go 1.21, so generics compile) instead
	# of writing into the replace target, which may be a symlink into /usr.
	# Nothing imports those modules; Go only reads them to resolve the module
	# graph. Same workaround as syft.
	sed -n 's|^replace \([^ ]*\) => \./\(src/.*\)$$|\1 \2|p' _build/go.work \
	| while read -r mod dir; do \
		if [ ! -e "_build/$$dir/go.mod" ]; then \
			mkdir -p "_build/stubs/$$mod"; \
			printf 'module %s\n\ngo 1.21\n' "$$mod" > "_build/stubs/$$mod/go.mod"; \
			sed -i "s|^replace $$mod => \./$$dir$$|replace $$mod => ./stubs/$$mod|" _build/go.work; \
			echo "stub module for $$mod"; \
		fi; \
	done

execute_after_dh_golang:
	# dh_golang also sets misc:Built-Using, marked optional, for packages that
	# have not moved to Static-Built-Using yet; at compat 14 dh_gencontrol
	# applies every relationship substvar whether or not debian/control uses
	# it. With 262 sources the field exceeds lintian's 5000-character limit
	# and says nothing Static-Built-Using does not.
	sed -i '/^misc:Built-Using?\?=/d' debian/grype.substvars

override_dh_auto_build:
	# PIE is not the default for go install; enable it for ASLR hardening.
	dh_auto_build -- -buildmode=pie \
		-ldflags="-X main.version=$(DEB_VERSION_UPSTREAM:+ds=) \
			-X main.gitDescription=v$(DEB_VERSION_UPSTREAM:+ds=)"

override_dh_auto_test:
	dh_auto_test -- -short -skip='$(SKIP_TESTS)'

override_dh_auto_install:
	# Binary-only package: grype is not imported by anything in Debian.
	# At compat 14 an explicit dh_auto_install call installs into debian/tmp;
	# point it at the only binary package.
	dh_auto_install --destdir=debian/grype -- --no-source
