#!/usr/bin/make -f

# Disable ccache, which causes strange build failures with the module.
# See Bug#582748.
export CCACHE_DISABLE := 1

CFLAGS = -Wall -g
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else
    CFLAGS += -O2
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

# Find kernel build information.
ifndef KSRC
    KSRC := /lib/modules/$(shell uname -r)/build
endif
ifndef KPKG_DEST_DIR
    KPKG_DEST_DIR = ..
endif
KVERS := $(shell perl debian/kernel-version $(KSRC))
export KSRC
export KVERS
export KPKG_DEST_DIR

configure: configure-stamp
configure-stamp:
	@if [ x"$(KVERS)" = x ] ; then \
	    echo 'No version in $(KSRC)/include/linux/version.h' >&2; \
	    exit 1; \
	fi
ifneq (,$(wildcard makefile))
	@echo "ERROR: unclean build directory from older version found"
	@echo "   Please run: module-assistant clean nvidia"
	@exit 1
endif
ifneq (,$(MA_SOURCE_PKG))
# module-assistant extracts the current source over any existing old build
# directory which may cause all sorts of strange build failures after
# updates, so create a stamp file (but don't remove it in the clean target)
# and try to catch this problem
	test -f debian/m-a.stamp || cp debian/changelog debian/m-a.stamp
	@if ! cmp -s debian/changelog debian/m-a.stamp ; then \
		echo "ERROR: build directory from older version found" ; \
		echo "   Please run: module-assistant clean $(MA_SOURCE_PKG:-source=)" ; \
		exit 1 ; \
	fi
endif
	sh debian/prep-modules $(KSRC)
	sed 's/#KVERS#/$(KVERS)/g' debian/install.template > debian/install
	touch $@

build: build-arch build-indep
build-arch: build-stamp
build-indep:
build-stamp: configure-stamp
	dh_prep
	dh_quilt_patch
	$(MAKE) LINUXDIR=$(KSRC)
	touch $@

get_built_using	?= $(filter-out (=),$(shell dpkg-query -f='$${source:Package} (=$${source:Version})' -W $1))

binary: binary-arch binary-indep
binary-indep:
binary-arch: build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_install
	dh_installdocs $(wildcard debian/changelog.nvidia-kernel*-source*)
	dh_installchangelogs NVIDIA_Changelog
	dh_installmodules
	dh_bugfiles
	dh_lintian
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v`cat debian/VERSION` \
		-V'built:using:kernel=$(call get_built_using,linux-headers-$(KVERS))' \
		-V'built:using:nvidia=$(call get_built_using,$(shell dpkg-parsechangelog | sed -nr '/^Source:/s/Source: //p')-source)'
	dh_md5sums
	dh_builddeb --destdir=$(KPKG_DEST_DIR)

clean:
	test -f debian/control || cp debian/control.template debian/control
	dh_testdir
	dh_testroot
	$(MAKE) LINUXDIR=$(KSRC) $(KPKG_EXTRAV_ARG) clean
	dh_quilt_unpatch || quilt --quiltrc /dev/null pop -af
	dh_clean debian/control debian/install

# The kdist_configure target is called by make-kpkg modules_config. It
# should configure the module so it is ready for compilation (mostly useful
# for calling configure).
kdist_configure: configure-stamp

# The kdist_image target is called by make-kpkg modules_image.  It is
# responsible for building the module, but doesn't generate a *.changes
# file.
kdist_image: build-stamp
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-arch
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

# The kdist target is called by make-kpkg modules.  It is responsible for
# building the module and a *.changes file.  Normally it should also sign
# the *.changes file, but we don't do that to avoid a dependency on
# devscripts.
kdist: build-stamp
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules binary-arch
	KMAINT="$(KMAINT)" KEMAIL="$(KEMAIL)" sh -v debian/genchanges
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

# The kdist_clean target is called by make-kpkg modules_clean.  It is
# responsible for cleaning up any changes that have been made by the other
# kdist_commands (except for the .deb files created).
kdist_clean:
	$(ROOT_CMD) $(MAKE) $(MFLAGS) -f debian/rules clean

# Legacy targets supported for backward compatibility.
binary-modules binary_modules: binary-arch

.PHONY: binary binary-arch binary-indep build build-arch build-indep clean
.PHONY: configure kdist kdist_clean kdist_configure kdist_image
