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

# Hardening flags.
export DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

# Duplicate flags as CMake is ignoring CPPFLAGS.
CFLAGS+=$(CPPFLAGS)
CXXFLAGS+=$(CPPFLAGS)

# Retrieve environment information.
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)


# Configura package and enable dependencies available on all platforms.
# dc1394: off (obsolete)
# libav (ffmpeg): available on all platforms
# opencv: available on all platforms
# coin3: available on all platforms
# lapack: available on all platforms
# ogre: available on all platforms
# xml2: available on all platforms
# libjpeg6b: available on all platforms
# libpng: available on all platforms
# libx11: available on all platforms
CONFIGURE_ARGS=	\
 -DCMAKE_INSTALL_LIBDIR:STRING="lib/$(DEB_HOST_MULTIARCH)"	\
 -DBUILD_DEMOS=ON -DBUILD_TESTING=ON -DBUILD_EXAMPLES=ON	\
 -DBUILD_SHARED_LIBS=ON						\
 -DUSE_DC1394_1=OFF -DUSE_FFMPEG=ON				\
 -DUSE_OPENCV=ON 						\
 -DUSE_COIN=ON -DUSE_GSL=OFF					\
 -DUSE_LAPACK=ON -DUSE_OGRE=ON					\
 -DUSE_XML2=ON -DUSE_GTK2=ON -DUSE_LIBJPEG=ON			\
 -DUSE_LIBPNG=ON -DUSE_X11=ON

# dc1394, v4l and ois are only available on Linux.
ifneq ($(DEB_HOST_ARCH_OS),linux)
  CONFIGURE_ARGS += -DUSE_DC1394_2=OFF -DUSE_V4L2:BOOL=OFF -DUSE_OIS=OFF
# OGRE support triggers ICE on FreeBSD, disable for now.
  CONFIGURE_ARGS += -DUSE_OGRE=OFF
else
  CONFIGURE_ARGS += -DUSE_DC1394_2=ON -DUSE_V4L2:BOOL=ON -DUSE_OIS=ON
endif

# freenect is not available on hurd.
ifeq ($(DEB_HOST_ARCH_OS),hurd)
  CONFIGURE_ARGS += -DUSE_LIBFREENECT=OFF
else
  CONFIGURE_ARGS += -DUSE_LIBFREENECT=ON
endif


.PHONY: override_dh_auto_configure	\
	override_dh_auto_build-indep	\
	override_dh_strip		\
	override_dh_auto_test

%:
	dh  $@ --parallel

override_dh_auto_clean:
	dh_auto_clean
	rm -f doc/mainpage.doc
	rm -rf .pc

override_dh_auto_configure:
	dh_auto_configure -- $(CONFIGURE_ARGS)

override_dh_auto_build-indep:
	dh_auto_build -- html-doc # Generate Doxygen HTML documentation.

override_dh_strip:
	dh_strip -a --dbg-package=libvisp2.8-dbg

# Parallelism is disabled in tests as some cannot run at the same
# time
#
# Due to numerical imprecision, some tests are failing on ia64.
# This is not a critical issue so we let the testing fail on this
# architecture for now.
# See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=723803
ifeq ($(DEB_HOST_ARCH),ia64)
DH_AUTOTEST_CAN_FAIL=true
else
DH_AUTOTEST_CAN_FAIL=false
endif

override_dh_auto_test:
	export VISP_INPUT_IMAGE_PATH=/usr/share/visp-images-data/ \
	 && dh_auto_test --max-parallel=1 || ${DH_AUTOTEST_CAN_FAIL}
