#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export 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.
BUILD_OPTIONS = \
	--disable-applications \
	--disable-examples \
	--disable-static \
	--enable-all \
	--enable-openmp

# Detect whether the long-double version of the FFTW library is available for
# the host architecture.
HAVE_FFTWL ?= $(shell pkg-config --exists fftw3l && echo 'yes')

DEB_HOST_ARCH_CPU := $(shell dpkg-architecture --query DEB_HOST_ARCH_CPU)

%:
	dh $@ --parallel --with autoreconf

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

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

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

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

override_dh_auto_build-indep:
	dh_auto_build --builddirectory=build-nfft -- doc

# Note: long-double testing is temporarily disabled for powerpc and ppc64el
# to allow the package to transition whilst upstream is working on a fix.
override_dh_auto_test-arch:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test --builddirectory=build-nfft
	dh_auto_test --builddirectory=build-nfftf
ifneq (,$(HAVE_FFTWL))
ifeq (,$(filter powerpc ppc64el,$(DEB_HOST_ARCH_CPU)))
	dh_auto_test --builddirectory=build-nfftl
endif
endif
endif

override_dh_auto_test-indep:
	# No-op.

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

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