#!/usr/bin/make -f

# Disable tests which are not working
# for cross builds at the moment
# Although we override dh_auto_test we
# set this ENV to silence some warnings
export DEB_BUILD_OPTIONS=nocheck

#export DH_VERBOSE=1
export LDFLAGS=

export WINDOW_SYSTEM=lws-generic
export BUILD=release
export PVR_BUILD_DIR=j722s_linux
export KERNELDIR=/usr/src/linux-headers-balluff-am67a-6.12-ti/

# Construct the resulting build directory where the compiled kernel module
# and install_km.sh helper is located.
BUILD_DIR:=binary_$(PVR_BUILD_DIR)_$(WINDOW_SYSTEM)_$(BUILD)/target_aarch64

# Get the kernel version from the kernel headers directory.
# This is used to place the kernel module in the correct location.
KERNELVERSION:=$(shell cat $(KERNELDIR)/include/config/kernel.release)

override_dh_auto_clean:
	ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make clobber 

override_dh_auto_build:
	ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -j $(shell nproc)

# Don't use install.sh to avoid need of shipping unecessary files.
# Package contains only the kernel modules which is directly copied
# to the correct location by using override_dh_auto_install
# and depmod and ldconfig are called in postinst.
# Just for documentation pupose the way using the install script:
# DISCIMAGE=/ sudo -E ./install.sh
override_dh_auto_install:
	dh_install $(BUILD_DIR)/pvrsrvkm.ko /lib/modules/$(KERNELVERSION)/extra

# Override the default dh_builddeb target to use xz compression (otherwise when using Ubuntu for packaging .zst will be used which isn't supported in bullseye)
override_dh_builddeb:
	dh_builddeb -- -Zxz

%:
	dh $@
