#!/usr/bin/make -f
# -*- makefile -*-
# Makefile for MRPT Debian package.

include /usr/share/dpkg/architecture.mk

export DEB_BUILD_MAINT_OPTIONS=hardening=+all,+fortify

# default: dont build expensive caller graphs with dot in the documentation pkg
export MRPT_genCALLERGRAPHS=NO

# For multiarch support:
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_TARGET_MULTIARCH ?= $(shell dpkg-architecture -qDEB_TARGET_MULTIARCH)
#DEB_HOST_ARCH_OS ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

# SSE options
ifeq ($(DEB_HOST_ARCH),amd64)
CMAKE_ARCH_FLAGS = -DMRPT_AUTODETECT_SIMD=OFF -DENABLE_SSE2=ON -DENABLE_SSE3=ON
else
CMAKE_ARCH_FLAGS = -DMRPT_AUTODETECT_SIMD=OFF
endif

# Unit tests can be run with target "test_legacy", or "test_gdb", which shows stack
# traces on failure:
MRPT_TEST_TARGET = test_legacy

# Debug info from the cmake scripts
export VERBOSE = 1

# Avoid running out of memory on mipsel,mips64el
ifneq (,$(findstring mips,$(DEB_HOST_ARCH)))
  export DEB_CXXFLAGS_MAINT_APPEND += -g1
endif

CMAKE_FLAGS = \
	-DCMAKE_INSTALL_PREFIX="$(CURDIR)/debian/usr/" \
	-DCMAKE_INSTALL_LIBDIR="lib/$(DEB_TARGET_MULTIARCH)" \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_BUILD_TYPE=Release  \
	-DMRPT_ALLOW_LGPLV3=ON \
	-DCMAKE_MRPT_USE_DEB_POSTFIXS="1" \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	$(CMAKE_ARCH_FLAGS) \
	

# Don't set: CMAKE_SKIP_RPATH=ON --> it makes unit tests to fail,
# and the rpath is removed anyway by cmake's install target.

%:
	dh $@ --buildsystem=cmake

override_dh_auto_build-arch:
	dh_auto_build -O--buildsystem=cmake

	# Python bindings:
#	dh_python3 --package=python3-mrpt


override_dh_auto_build-indep:
	# Build doc targets:
	dh_auto_build -O--buildsystem=cmake -- \
		man_pages_all \
		documentation_psgz_guides

	# clean useless doxygen files:
	find $(CURDIR)/doc/ -name '*.md5' | xargs -I FIL rm FIL

override_dh_auto_configure:
	# Show CPU flags, to help debugging unit test crashes related to
	# illegal instructions, etc.
	cat /proc/cpuinfo
	# Show env vars for debugging:
	env
	# Autoconfigure step:
	dh_auto_configure -- $(CMAKE_FLAGS)

override_dh_auto_install:
	# Do nothing?
	echo "[override_dh_auto_install]"

override_dh_auto_test-indep:
	echo "[override_dh_auto_test-indep] Do nothing."

override_dh_auto_test-arch:
	# Tests for arch-dependent :
	echo "[override_dh_auto_test-arch]"
	dh_auto_build -O--buildsystem=cmake -- $(MRPT_TEST_TARGET) -j1

override_dh_install:
	# Install:
	dh_auto_build -O--buildsystem=cmake -- install

	# Move all the ".so.x.y" files to the corresponding binary packages:
	# libmrpt-${name}${CMAKE_MRPT_VERSION_NUMBER_MAJOR}.${CMAKE_MRPT_VERSION_NUMBER_MINOR}/usr/...
	VERSION=$$(head version_prefix.txt -n1 | cut -f 1-2 -d . ); tmpfil=$$(mktemp); find $(CURDIR)/debian/ -name "*.so.?.*" > $$tmpfil; cat $$tmpfil | while read line; do f=$$(basename $$line | cut -f 1 -d '.'); echo "Processing so file: $$line, basename: $$f"; dir=$(CURDIR)/debian/$$f$${VERSION}/usr/lib/$(DEB_TARGET_MULTIARCH); mkdir -p $$dir; mv -f $$line $$dir; done

	# JLBC Nov 2018: Due to what is explained in MRPT/cmakemodules/DeclareMRPTLibs.cmake, we need to make some "mv" here:

	# Move all the ".so" files to the proper place: the -dev package:
	tmpfil=$$(mktemp); find $(CURDIR)/debian/ -name "*.so" | grep -v 'dev' | grep -v 'pymrpt' > $$tmpfil; cat $$tmpfil | while read line; do f=$$(basename $$line .so); echo "Processing so file: $$line, basename: $f"; mkdir -p $(CURDIR)/debian/$$f-dev/usr/lib/$(DEB_TARGET_MULTIARCH); mv -vf $$line $(CURDIR)/debian/$$f-dev/usr/lib/$(DEB_TARGET_MULTIARCH); done

	# Move cmake modules to the -dev packages:
	tmpfil=$$(mktemp); find $(CURDIR)/debian/usr/share/ -name '*-config.cmake' > $$tmpfil; cat $$tmpfil | while read line; do f=$$(dirname $$line); f=$$(basename $$f); echo "Processing cmake scripts: $$line, module name: $$f"; dir=$(CURDIR)/debian/lib$$f-dev/usr/share/; mkdir -p $$dir; mv -f $(CURDIR)/debian/usr/share/$$f $$dir; done

	# Move headers to -dev directories:
	cp -vr $(CURDIR)/debian/usr/libmrpt-*dev $(CURDIR)/debian/

	# Move python stuff:
#	mkdir -p $(CURDIR)/debian/python-mrpt/usr/lib
#	mv -v $(CURDIR)/debian/usr/lib/* $(CURDIR)/debian/python-mrpt/usr/lib

	# Move mrpt-doc:
	dir=$(CURDIR)/debian/mrpt-doc/usr/share/doc/mrpt-doc; mkdir -p $$dir; mv -v $(CURDIR)/debian/usr/mrpt-doc/usr/share/doc/mrpt-doc/* $$dir

	# Move mrpt-apps:
	dir=$(CURDIR)/debian/mrpt-apps/usr/; mkdir -p $$dir; cp -vr $(CURDIR)/debian/usr/mrpt-apps/* $$dir/..

	# Move mrpt-common:
	dir=$(CURDIR)/debian/mrpt-common/usr/; mkdir -p $$dir; cp -vr $(CURDIR)/debian/usr/mrpt-common/* $$dir/..

override_dh_missing:
	dh_missing --list-missing
