#!/usr/bin/make -f
#
# $(DEBIAN)/rules for Ubuntu linux
#
# Use this however you want, just give credit where credit is due.
#
# Copyright (c) 2007 Ben Collins <bcollins@ubuntu.com>
#

# Do not use make's built-in rules and variables
# (this increases performance and avoids hard-to-debug behaviour)
MAKEFLAGS += -rR

# Allow to run debian/rules directly without root
export DEB_RULES_REQUIRES_ROOT := no

.NOTPARALLEL:

DEBIAN=$(shell awk -F= '($$1 == "DEBIAN") { print $$2 }' <debian/debian.env)

# dpkg-buildpackage passes options that are incomptatible
# with the kernel build.
unexport CFLAGS
unexport LDFLAGS

export LC_ALL=C
export SHELL=/bin/bash -e

# Temporary workaround to fix build issues if only python2 runtime is installed
# (without python3-dev).
export PYTHON=python3

# Common variables for all architectures
include debian/rules.d/0-common-vars.mk

# Pull in some arch specific stuff
-include $(DEBIAN)/rules.d/$(arch).mk

# Pull in some branch specific stuff
-include $(DEBIAN)/rules.d/hooks.mk

# Maintainer targets
include debian/rules.d/1-maintainer.mk

do_linux_tools=$(sort $(filter-out false,$(do_tools_usbip) $(do_tools_cpupower) $(do_tools_perf) $(do_tools_bpftool) $(do_tools_x86)))
do_cloud_tools=$(sort $(filter-out false,$(do_tools_hyperv)))
do_tools_perf_jvmti?=false
do_tools_perf_python?=false

# Indep tools packages may only be built for the main linux source package
ifeq ($(DEB_SOURCE),linux)
	do_tools_common = true
	do_tools_host = true
endif

# The unversioned and unflavoured bpftools and linux-perf packages should only be built for the
# linux source package.
ifneq ($(DEB_SOURCE),linux)
	do_tools_bpftool = false
	do_tools_perf = false
endif

# The linux-source package may only be built for the linux source package
# We need to explicitly turn this off here to override any (incorrect)
# statement in any of the above included makefile snippets
ifneq ($(DEB_SOURCE),linux)
	do_source_package = false
endif

# Default settings for DKMS modules.
$(foreach _m,$(all_dkms_modules), \
  $(eval do_$(_m) ?= $(if $(filter $(_m),$(filter-out $(dkms_exclude),$(dkms_include) $(subst any,$(_m),$(subst $(arch),$(_m),$(dkms_$(_m)_archs))))),true,false)) \
)

# Are any of the kernel signing options enabled.
any_signed=$(sort $(filter-out false,$(uefi_signed) $(opal_signed) $(sipl_signed)))
ifeq ($(any_signed),true)
bin_pkg_name=$(bin_pkg_name_unsigned)
else
bin_pkg_name=$(bin_pkg_name_signed)
endif

# Stages -- support both DEB_STAGE=stage1 and DEB_BUILD_PROFILE=bootstrap
ifeq ($(DEB_STAGE),stage1)
    DEB_BUILD_PROFILES=stage1
endif
ifneq ($(DEB_BUILD_PROFILE),)
    DEB_BUILD_PROFILES=$(DEB_BUILD_PROFILE)
endif
ifneq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
    do_tools=false
    do_source_package=false
    do_flavour_image_package=false
    do_flavour_header_package=false
endif

# autopkgtest -- rebuild support
#  - only build the first flavour on the assumption it is representative
#  - disable dkms builds as the versions used may have been deleted
ifneq ($(filter autopkgtest,$(DEB_BUILD_PROFILES)),)
	flavours := $(firstword $(flavours))
$(foreach _m,$(all_dkms_modules),$(eval do_$(_m) = false))
endif

# Being used to build a mainline build -- turn off things which do not work.
ifeq ($(do_mainline_build),true)
	do_tools=false
	no_dumpfile=1
$(foreach _m,$(all_dkms_modules),$(eval do_$(_m) = false))
	do_skip_checks=true
endif

# Disable tools build and packaging if do_tools != true
ifneq ($(do_tools),true)
	do_linux_tools = false
	do_cloud_tools = false
	do_tools_common = false
	do_tools_host = false
endif

# Either tools package needs the common source preparation
do_any_tools=$(sort $(filter-out false,$(do_linux_tools) $(do_cloud_tools)))

# Debian Build System targets
.PHONY: binary
binary: binary-indep binary-arch

.PHONY: build
build: build-arch build-indep

.PHONY: clean
clean: debian/control debian/canonical-certs.pem debian/canonical-revoked-certs.pem
	dh_testdir
	dh_testroot
	dh_clean

	# normal build junk
	rm -rf $(DEBIAN)/abi
	rm -rf $(builddir) $(stampdir)
	rm -rf debian/linux-*/
	rm -rf debian/tmp-headers

	cp $(DEBIAN)/changelog debian/changelog

	# If we have a reconstruct script use it.
	[ -f $(DEBIAN)/reconstruct ] && bash $(DEBIAN)/reconstruct || true

	rm -f debian/scripts/fix-filenames

.PHONY: distclean
distclean: clean
	rm -rf debian/control debian/changelog

# Builds the image, arch headers and debug packages
include debian/rules.d/2-binary-arch.mk

# Builds the source and linux-headers indep packages
include debian/rules.d/3-binary-indep.mk

# Calculate Ubuntu Compatible Signing levels
UBUNTU_COMPATIBLE_SIGNING=$(shell grep -qx ' *Subject: C = GB, ST = Isle of Man, O = Canonical Ltd., OU = Secure Boot, CN = Canonical Ltd. Secure Boot Signing (2021 v3)' debian/canonical-revoked-certs.pem && echo ubuntu/4 pro/3)

# Misc stuff
debian/control: debian/canonical-revoked-certs.pem $(DEBIAN)/changelog FORCE
	$(SHELL) debian/scripts/control-create $(DEB_SOURCE) > $@
	sed -i -e 's/PKGVER/$(DEB_VERSION_UPSTREAM)/g'                          \
	        -e 's/ABINUM/$(abinum)/g'                                       \
		-e 's/SRCPKGNAME/$(DEB_SOURCE)/g'                               \
		-e 's/=SERIES=/$(DEB_DISTRIBUTION)/g'                           \
		-e 's|\(^Maintainer:.*\)|\1\nXSC-Ubuntu-Compatible-Signing: $(UBUNTU_COMPATIBLE_SIGNING)|g' \
		-e 's/\(^Build-Depends:$$\)/\1\n$(GCC_BUILD_DEPENDS)/g'         \
	$@

debian/canonical-certs.pem: $(wildcard debian/certs/*.pem $(DEBIAN)/certs/*.pem)
	for cert in $(sort $(notdir $^));					\
	do									\
		for dir in $(DEBIAN) debian;					\
		do								\
			if [ -f "$$dir/certs/$$cert" ]; then			\
				cat "$$dir/certs/$$cert";			\
				break;						\
			fi;							\
		done;								\
	done >"$@"

debian/canonical-revoked-certs.pem: $(wildcard debian/revoked-certs/*.pem $(DEBIAN)/revoked-certs/*.pem)
	for cert in $(sort $(notdir $^));					\
	do									\
		for dir in $(DEBIAN) debian;					\
		do								\
			if [ -f "$$dir/revoked-certs/$$cert" ]; then		\
				cat "$$dir/revoked-certs/$$cert";		\
				break;						\
			fi;							\
		done;								\
	done >"$@"

# delete partially updated (i.e. corrupted) files on error
.DELETE_ON_ERROR:

# Add FORCE to the prerequisites of a target to force it to be always rebuilt.
.PHONY: FORCE
