#!/usr/bin/make -f
# -*- makefile -*-

# 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_CXXFLAGS_MAINT_APPEND = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# Environment information.
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
BUILDDIR = $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)

# Build without AltiVec support on ppc64el.
ifeq ($(DEB_HOST_ARCH),ppc64el)
DEB_CFLAGS_MAINT_APPEND += -mno-altivec
DEB_CXXFLAGS_MAINT_APPEND += -mno-altivec
endif

# CMake options common to -arch and -indep builds.
CMAKE_BUILD_OPTIONS = \
	-DAF_INSTALL_CMAKE_DIR=/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/ArrayFire \
	-DAF_INSTALL_DOC_DIR=/usr/share/doc/libarrayfire-doc \
	-DAF_INSTALL_EXAMPLE_DIR=/usr/share/doc/libarrayfire-doc/examples \
	-DAF_INSTALL_INC_DIR=/usr/include \
	-DAF_INSTALL_LIB_DIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
	-DBUILD_CPU=ON \
	-DBUILD_CUDA=OFF \
	-DBUILD_GRAPHICS=OFF \
	-DBUILD_NONFREE=OFF \
	-DBUILD_OPENCL=ON \
	-DBUILD_UNIFIED=ON \
	-DUSE_SYSTEM_BOOST_COMPUTE=ON \
	-DUSE_SYSTEM_CLBLAS=ON \
	-DUSE_SYSTEM_CLFFT=ON \
	-DWITH_COVERAGE=OFF

# Disable build of examples and testsuite if no check requested.
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
CMAKE_BUILD_OPTIONS += \
	-DBUILD_EXAMPLES=ON \
	-DBUILD_GTEST=ON \
	-DBUILD_TEST=ON
else
CMAKE_BUILD_OPTIONS += \
	-DBUILD_EXAMPLES=OFF \
	-DBUILD_GTEST=OFF \
	-DBUILD_TEST=OFF
endif

# Prevent execution of slow tests.
CTEST_EXCLUDE_REGEX = large|dense

%:
	dh $@ --parallel --builddirectory=$(BUILDDIR)

override_dh_auto_clean:
	dh_auto_clean
	# Remove auto-generated files.
	find $(CURDIR) -name "version.h" -delete

override_dh_auto_configure-arch:
	dh_auto_configure -- $(CMAKE_BUILD_OPTIONS) -DBUILD_DOCS=OFF

override_dh_auto_configure-indep:
	dh_auto_configure -- $(CMAKE_BUILD_OPTIONS) -DBUILD_DOCS=ON

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd $(BUILDDIR) && \
	CTEST_OUTPUT_ON_FAILURE=1 \
	ctest --force-new-ctest-process -R "cpu" -E "$(CTEST_EXCLUDE_REGEX)"
	# NOTE: The testsuite for the unified backend is run on the CPU backend only.
	# Change the name regex to libaf*.so* to run on all backends.
	cd $(BUILDDIR) && \
	LD_LIBRARY_PATH=$(shell find $(BUILDDIR) -type f -name "libafcpu.so*" -exec dirname {} \;) \
	CTEST_OUTPUT_ON_FAILURE=1 \
	ctest --force-new-ctest-process -R "unified" -E "$(CTEST_EXCLUDE_REGEX)"
endif

override_dh_install-indep:
	dh_install --indep -X LICENSE

override_dh_installchangelogs:
	dh_installchangelogs docs/pages/release_notes.md

override_dh_compress:
	dh_compress --exclude=examples --exclude=html

override_dh_strip-arch:
	dh_strip --package=libarrayfire-cpu3 \
		--dbg-package=libarrayfire-cpu3-dbg
	dh_strip --package=libarrayfire-opencl3 \
		--dbg-package=libarrayfire-opencl3-dbg
	dh_strip --package=libarrayfire-unified3 \
		--dbg-package=libarrayfire-unified3-dbg
