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

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

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

# Build without AltiVec support on ppc64el.
ifeq ($(DEB_HOST_ARCH_CPU),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=lib/$(DEB_HOST_MULTIARCH)/cmake/ArrayFire \
	-DAF_INSTALL_DOC_DIR=share/doc/libarrayfire-doc \
	-DAF_INSTALL_EXAMPLE_DIR=share/doc/libarrayfire-doc/examples \
	-DAF_INSTALL_LIB_DIR=lib/$(DEB_HOST_MULTIARCH) \
	-DBUILD_CPU=ON \
	-DBUILD_CPU_ASYNC=OFF \
	-DBUILD_CUDA=OFF \
	-DBUILD_EXAMPLES=OFF \
	-DBUILD_GRAPHICS=OFF \
	-DBUILD_NONFREE=OFF \
	-DBUILD_OPENCL=ON \
	-DBUILD_UNIFIED=ON \
	-DUSE_SYSTEM_CLBLAS=ON \
	-DUSE_SYSTEM_CLFFT=ON \
	-DWITH_COVERAGE=OFF

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
BUILD_OPTIONS += -DBUILD_TEST=ON
else
BUILD_OPTIONS += -DBUILD_TEST=OFF
endif

%:
	dh $@ --parallel

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

# NOTE: Testcases labelled dense or large are currently disabled to lower the
# workload on the builders. Remove the exclusion regexes to enable them.
# NOTE: The testsuite for the unified backend is performed for the CPU backend
# only.
# NOTE: The imageio testcase fails on some architectures. It has been
# temporarily disabled whilst upstream is working on a fix.
override_dh_auto_test:
	dh_auto_test -- ARGS+="-R cpu -E 'large|dense|imageio'"
	LD_LIBRARY_PATH=$(shell find $(CURDIR) -type f -name "libafcpu.so*" -exec dirname {} \;) \
		dh_auto_test -- ARGS+="-R unified"

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
