#!/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)

# 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.
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_CPU_ASYNC=OFF \
	-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)))
BUILD_OPTIONS += -DBUILD_EXAMPLES=ON -DBUILD_TEST=ON
else
BUILD_OPTIONS += -DBUILD_EXAMPLES=OFF -DBUILD_TEST=OFF
endif

%:
	dh $@ --parallel

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 -- $(BUILD_OPTIONS) -DBUILD_DOCS=OFF

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

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test -- ARGS+="-R cpu -E large -E dense"
	# 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.
	LD_LIBRARY_PATH=$(shell find $(CURDIR) -type f -name "libafcpu.so*" -exec dirname {} \;) \
	dh_auto_test -- ARGS+="-R unified"
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
