#!/usr/bin/make -f

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS) -DDEB_HOST_MULTIARCH=\"$(DEB_HOST_MULTIARCH)\"
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) -fopenmp-simd -DSIMDE_ENABLE_OPENMP
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS) -fopenmp-simd -DSIMDE_ENABLE_OPENMP
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

export OPENMM_INCLUDE_PATH := $(CURDIR)/openmmapi/include;$(CURDIR)/olla/include;$(CURDIR)/serialization/include;$(CURDIR)/plugins/amoeba/openmmapi/include;$(CURDIR)/plugins/rpmd/openmmapi/include;$(CURDIR)/plugins/drude/openmmapi/include
export OPENMM_LIB_PATH     := $(CURDIR)/obj-$(DEB_HOST_GNU_TYPE)

# CUDA compiler is non-free thus not available on buildds, nevertheless should not fail if present.
export OPENMM_CUDA_COMPILER := /usr/bin/nvcc

empty :=
space := $(empty)$(empty)

ifneq (,$(findstring i386, $(DEB_HOST_ARCH)))
    CTEST_EXTRA_ARGS = -E 'TestReferenceCustomIntegrator|TestSystem'
endif

SKIP_TEST_LIST += testMTSLangevinIntegrator

SKIP_REF_SERIAL_TEST_ARCH_LIST = s390x hppa sparc64
ifneq (,$(findstring $(space)$(DEB_HOST_ARCH)$(space), $(space)$(SKIP_REF_SERIAL_TEST_ARCH_LIST)$(space)))
    CTEST_EXTRA_ARGS = -E 'TestReferenceATMForce|TestReferenceCompoundIntegrator|TestReferenceCustomCVForce|TestReferenceCustomIntegrator|TestReferenceNoseHooverIntegrator|TestSerializ*'
    # python tests time out
    SKIP_TEST_LIST += TestATMForce test_Constraints_charmm test_1 test_DispersionCorrection  test_Disulfides  test_IdenticalTemplates \
	test_LennardJonesGenerator  test_NBFix  test_ProgrammaticForceField  testDisulfidePatch  testCopyForce \
	test_deepcopy  testLoadFromXML  testRunForClockTime  testSaveState
    # python tests fail
    SKIP_TEST_LIST += test_ImplicitSolventForces test_Forces testCopyIntegrator
    PYTEST_EXTRA_ARGS = -v
endif

SKIP_TESTS := $(shell SKIP_TESTS=""; \
        list_initialised=0; \
        for t in $(SKIP_TEST_LIST); do \
            if [ $${list_initialised} = 0 ]; then \
                SKIP_TESTS=$$t; \
                list_initialised=1; \
            else \
                SKIP_TESTS="$${SKIP_TESTS} or $$t"; \
            fi; \
        done; \
        if [ "x$${SKIP_TESTS}" != "x" ]; then \
            SKIP_TESTS="not ( $${SKIP_TESTS} )"; \
        fi; \
        echo $${SKIP_TESTS})

export PYBUILD_TEST_PYTEST=1
export PYBUILD_BEFORE_TEST := cp -r {dir}/tests/systems {build_dir}
export PYBUILD_TEST_ARGS   := $(PYTEST_EXTRA_ARGS) -k '$(SKIP_TESTS)' tests

# TODO: TEST_JOBS is not used in rules
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    TEST_JOBS = -n $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

CMAKE_FLAGS = \
        -DCMAKE_VERBOSE_MAKEFILE=ON \
        -DCMAKE_C_FLAGS_RELEASE="$(CFLAGS)" \
        -DCMAKE_CXX_FLAGS_RELEASE="$(CXXFLAGS)" \
        -DCMAKE_SHARED_LINKER_FLAGS_RELEASE="$(LDFLAGS)" \
        -DCMAKE_BUILD_RPATH_USE_ORIGIN=ON \
        -DCMAKE_BUILD_TYPE=Release      \
        $(CMAKE_ARCH_FLAGS) \
        -DOPENMM_BUILD_SERIALIZATION_SUPPORT=ON \
        -DOPENMM_BUILD_C_AND_FORTRAN_WRAPPERS=OFF \
        -DOPENMM_BUILD_OPENCL_TESTS=OFF \

%:
	dh $@ --buildsystem cmake --with numpy3,python3

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_FLAGS)

execute_after_dh_auto_build:
	pybuild --dir obj-$(DEB_HOST_GNU_TYPE)/python --build

override_dh_auto_test:
	dh_auto_test -- "ARGS+=--timeout 3600 $(CTEST_EXTRA_ARGS)"
	pybuild --dir obj-$(DEB_HOST_GNU_TYPE)/python --test # TODO: Run tests in parallel

override_dh_auto_install:
	dh_auto_install -O--buildsystem=cmake
	dh_install -plibopenmm8.1      debian/tmp/usr/lib/libOpenMM.so.* usr/lib/${DEB_HOST_MULTIARCH}
	dh_install -plibopenmm-dev     debian/tmp/usr/lib/libOpenMMAmoeba.so usr/lib/${DEB_HOST_MULTIARCH}/openmm
	dh_install -plibopenmm-dev     debian/tmp/usr/lib/libOpenMMDrude.so  usr/lib/${DEB_HOST_MULTIARCH}/openmm
	dh_install -plibopenmm-dev     debian/tmp/usr/lib/libOpenMMRPMD.so   usr/lib/${DEB_HOST_MULTIARCH}/openmm
	dh_install -plibopenmm-dev     debian/tmp/usr/lib/libOpenMM.so   usr/lib/${DEB_HOST_MULTIARCH}
	dh_install -plibopenmm-plugins debian/tmp/usr/lib/plugins        usr/lib/${DEB_HOST_MULTIARCH}/openmm
	pybuild --dir obj-$(DEB_HOST_GNU_TYPE)/python --install

override_dh_python3:
	dh_python3
	find debian/python3-openmm -name '*.py' \
		| xargs sed -i 's/^#!\/bin\/env python/#!\/usr\/bin\/python3/'
	find debian/python3-openmm -name __pycache__ \
		| xargs rm -rf
	find debian/python3-openmm -name '_openmm.*.so' \
		| xargs chrpath --replace /usr/lib/${DEB_HOST_MULTIARCH}/openmm

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

execute_after_dh_auto_clean:
	rm -rf .pybuild
