#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
DH_VERBOSE = 1

# Hardening flags.
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# Build options common to arch and indep targets.
COMMON_BUILD_OPTIONS = \
	--disable-applications \
	--disable-examples \
	--disable-static \
	--enable-all \
	--enable-openmp

# Environment information.
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Detect whether the long-double version of the FFTW library is available for
# the host architecture. If positive, support for the long-double precision of
# the NFFT library will be enabled.
SYSTEM_FFTWL ?= $(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/libfftw3l.so)

%:
	dh $@ --parallel --with autoreconf \
		--dbg-package=libnfft3-dbg

override_dh_auto_clean-arch:
	dh_auto_clean --arch --builddirectory=build-nfft
	dh_auto_clean --arch --builddirectory=build-nfftf
ifneq "" "$(SYSTEM_FFTWL)"
	dh_auto_clean --arch --builddirectory=build-nfftl
endif

override_dh_auto_clean-indep:
	dh_auto_clean --indep --builddirectory=build-indep

override_dh_auto_configure-arch:
	dh_auto_configure --builddirectory=build-nfft -- \
		$(COMMON_BUILD_OPTIONS) \
		--disable-doxygen-doc
	dh_auto_configure --builddirectory=build-nfftf -- \
		$(COMMON_BUILD_OPTIONS) \
		--disable-doxygen-doc \
		--enable-single \
		--program-suffix=f
ifneq "" "$(SYSTEM_FFTWL)"
	dh_auto_configure --builddirectory=build-nfftl -- \
		$(COMMON_BUILD_OPTIONS) \
		--disable-doxygen-doc \
		--enable-long-double \
		--program-suffix=l
endif

override_dh_auto_configure-indep:
	dh_auto_configure --builddirectory=build-nfft -- \
		$(COMMON_BUILD_OPTIONS) \
		--enable-doxygen-doc

override_dh_auto_build-arch:
	dh_auto_build --arch --builddirectory=build-nfft
	dh_auto_build --arch --builddirectory=build-nfftf
ifneq "" "$(SYSTEM_FFTWL)"
	dh_auto_build --arch --builddirectory=build-nfftl
endif

override_dh_auto_build-indep:
	cd $(CURDIR)/build-nfft && make doc

override_dh_auto_install-arch:
	dh_auto_install --arch --builddirectory=build-nfft \
		--package=libnfft3-double2
	dh_auto_install --arch --builddirectory=build-nfftf \
		--package=libnfft3-single2
ifneq "" "$(SYSTEM_FFTWL)"
	dh_auto_install --arch --builddirectory=build-nfftl \
		--package=libnfft3-long2
endif

override_dh_auto_test-arch:
ifeq (,$(filter nocheck, $(DEB_BUILD_OPTIONS)))
	dh_auto_test --builddirectory=build-nfft
# Testing for the single and long-double precisions should be disabled until
# a proper fix is found upstream:
#
# https://github.com/NFFT/nfft/issues/17.
	#dh_auto_test --builddirectory=build-nfftf
#ifneq "" "$(SYSTEM_FFTWL)"
#	dh_auto_test --builddirectory=build-nfftl
#endif
endif

override_dh_installdocs-indep:
	dh_installdocs --indep
	dh_doxygen --indep
