#!/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_MULTIARCH := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# Detect whether the long-double version of FFTW is available for the host
# architecture. If not, the long-double build of NFFT will be discarded.
ifeq "" "$(wildcard /usr/lib/$(DEB_HOST_MULTIARCH)/libfftw3l.so)"
DH_SKIP_NFFTL = --no-package=libnfft3-long2
endif

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

override_dh_auto_clean-arch:
	dh_auto_clean --arch --builddirectory=build-nfft
	dh_auto_clean --arch --builddirectory=build-nfftf
ifeq "" "$(DH_SKIP_NFFTL)"
	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
ifeq "" "$(DH_SKIP_NFFTL)"
	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-indep -- \
		$(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
ifeq "" "$(DH_SKIP_NFFTL)"
	dh_auto_build --arch --builddirectory=build-nfftl
endif

override_dh_auto_build-indep:
	cd $(CURDIR)/build-indep && 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
ifeq "" "$(DH_SKIP_NFFTL)"
	dh_auto_install --arch --builddirectory=build-nfftl \
		--package=libnfft3-long2
endif

override_dh_auto_install-indep:
	# Nothing to do here.

override_dh_installdocs-arch:
	dh_installdocs --arch

override_dh_installdocs-indep:
	dh_installdocs --indep $(CURDIR)/build-indep/doc/html
	dh_doxygen --indep

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test-arch:
	# only test double, otherwise too long
	dh_auto_test --builddirectory=build-nfft
endif
