#!/usr/bin/make -f

# TODO MPI? test with GlobalFrictionalContact3D_SparseBlockStorage
# TODO doc package

export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow

# If SALSABUILD is set we want to drop debug info as well
ifneq ($(SALSABUILD),)
  export DEB_CXXFLAGS_MAINT_APPEND += -g0
endif

# GCC-10 promoted a warning to an error, creating a regression in hem5.f
export DEB_FCFLAGS_MAINT_APPEND=-fallow-argument-mismatch

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/default.mk

LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH)

ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	TESTING=OFF
else
	TESTING=ON
endif

ifneq (,$(filter terse,$(DEB_BUILD_OPTIONS)))
	CMAKE_VERBOSE_OFF=-DCMAKE_VERBOSE_MAKEFILE=OFF
endif

# Skip tests that consist of collections of tests, they take too long
# and time out.
TESTS_TO_SKIP=COLLECTION collection

# Skip this test as it depends on ql0001 which is not included due to
# copyright issues.
TESTS_TO_SKIP += python_test_lcp

# Skip tests that time out
TESTS_TO_SKIP += dr_iso1 ContactTest

BUILDDIR=$(CURDIR)/obj-$(DEB_HOST_MULTIARCH)

# We disable parallel build because an 8 GB laptop runs out of memory
# with 4 parallel processes
%:
	dh $@ --with python3 --with numpy3 --no-parallel --builddirectory=$(BUILDDIR)

# Notes: remove rpath due to Debian policy
override_dh_auto_configure:
	env DESTDIR=$(CURDIR)/debian/tmp                           \
	dh_auto_configure --                                       \
	-DCMAKE_SKIP_BUILD_RPATH=TRUE                              \
	-DFORCE_SKIP_RPATH=1                                       \
	-DCMAKE_SKIP_INSTALL_RPATH=TRUE                            \
	-DCMAKE_SKIP_RPATH=TRUE                                    \
	-DINSTALL_CMAKE_DIR=$(LIBDIR)/cmake/siconos                \
	-DPython3_EXECUTABLE=$(shell which python3)                \
	-DI_WANT_STATIC_LPSOLVE=ON                                 \
	-DLpSolve_LIBRARY=/usr/lib/liblpsolve55_pic.a              \
	-DCMAKE_Fortran_FLAGS="$(FCFLAGS)"                         \
	-DWITH_TESTING=$(TESTING)                                  \
	-DWITH_DOCUMENTATION=ON                                    \
	-DWITH_DOXY2SWIG=ON                                        \
	-DWITH_SYSTEM_SUITESPARSE=ON                               \
	-DWITH_SERIALIZATION=ON                                    \
	-DWITH_GENERATION=ON                                       \
	-DWITH_BULLET=ON                                           \
	-DWITH_FCLIB=OFF                                            \
	-DWITH_OCE=OFF                                             \
	-DIDONTWANTMPI=ON                                          \
	-DWITH_MUMPS=ON                                            \
	-Dpython_install_options="--install-layout=deb"            \
	$(CMAKE_VERBOSE_OFF)                                       \
	-Dtests_timeout=30

# Build ctest regular expression
null  :=
space := $(null) $(null)
or := |
TESTS_TO_SKIP := $(subst $(space),$(or),$(strip $(TESTS_TO_SKIP)))

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	dh_auto_test -- ARGS="-E '$(TESTS_TO_SKIP)'"
endif

# Install software, headers, docs.
override_dh_auto_install:
	dh_auto_install

# Problem: ALL targets are checked even though we split them into
# separate packages, therefore attempting to use only
# e.g. libsiconos-kernel-dev fails due to libsiconos-control6 missing.
# Solution: Modify CMake targets to only check libraries for requested
# components.
	test "$$(ls debian/tmp/usr/lib/cmake/siconos-*/siconosTargets-none.cmake | wc -l)" = 1
	echo "cmake_policy(PUSH)" >tmp.cmake
	echo "cmake_policy(SET CMP0057 NEW)" >>tmp.cmake
	sed -e 's/list(APPEND _IMPORT_CHECK_TARGETS Siconos::\(\w*\) )/if(\1 IN_LIST siconos_FIND_COMPONENTS)\nlist(APPEND _IMPORT_CHECK_TARGETS Siconos::\1)\nendif()/' debian/tmp/usr/lib/cmake/siconos-*/siconosTargets-none.cmake >>tmp.cmake
	echo "cmake_policy(POP)" >>tmp.cmake
	mv tmp.cmake debian/tmp/usr/lib/cmake/siconos-*/siconosTargets-none.cmake