#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS = hardening=+all abi=+lfs

include /usr/share/dpkg/architecture.mk

BUILD_TESTING = $(if $(filter nocheck,$(DEB_BUILD_OPTIONS)),OFF,ON)
# skip some flaky tests
# TestMemoryLeak fails, MERGE reports "bus error" on buildd (but not debci)
GTEST_FILTER := -TestMemoryLeak.*:MERGE.*
ifeq ($(DEB_HOST_ARCH_BITS),32)
GTEST_FILTER := ${GTEST_FILTER}:testCppcontribSaDecode*
endif
ifeq ($(DEB_BUILD_ARCH), sparc64)
GTEST_FILTER := ${GTEST_FILTER}:testCppcontribSaDecode*:testCppcontribUintreader*
endif
ifeq ($(DEB_BUILD_ARCH), s390x)
	GTEST_FILTER := ${GTEST_FILTER}:testCppcontribSaDecode*:testCppcontribUintreader*:TestFastScan.*
endif

export PYBUILD_SYSTEM = cmake

COMMON_CONFIGURE_ARGS = \
	-DFAISS_ENABLE_GPU=OFF \
	-DFAISS_ENABLE_PYTHON=ON

ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
COMMON_CONFIGURE_ARGS += \
	-DFAISS_USE_SYSTEM_GTEST=ON \
	-DFAISS_GTEST_FILTER="$(GTEST_FILTER)"
endif

export PYBUILD_CONFIGURE_ARGS = \
	$(COMMON_CONFIGURE_ARGS) \
	-DBUILD_TESTING=OFF \
	-DFAISS_ENABLE_C_API=OFF

export PYBUILD_CONFIGURE_ARGS_$(shell py3versions -d) = \
	$(COMMON_CONFIGURE_ARGS) \
	-DBUILD_TESTING=$(BUILD_TESTING) \
	-DFAISS_ENABLE_C_API=ON

export PYBUILD_AFTER_INSTALL = \
	cd {build_dir}/faiss/python \
	&& {interpreter} setup.py install --root=$(CURDIR)/debian/python3-faiss --install-lib=/usr/lib/python{version}/site-packages

%:
	dh $@ --buildsystem=pybuild

