#!/usr/bin/make -f

%:
	dh $@ --with python2 --with python3 --buildsystem=cmake --parallel

# Python versions (default is first)
PYVERSIONS=$(shell pyversions -v -r)
DEFAULT_PY=$(shell pyversions -v -d)
PY3VERSIONS=$(shell py3versions -v -r)
DEFAULT_PY3=$(shell py3versions -v -d)
LIBSHIBOKEN_SOVERSION=1.2

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# G++ versions
g++-version := $(shell readlink /usr/bin/g++ | sed -e 's/g++-\(.*\)\.\(.*\)/\1\2/')
g++-version-ge-4.5 := $(shell if test $(g++-version) -ge 45; then echo "yes"; else echo "no"; fi)

CMAKE_COMMON_OPTIONS := -DCMAKE_SKIP_RPATH=true \
			-DLIB_SUFFIX=/$(DEB_HOST_MULTIARCH)

#### CONFIGURE ####

override_dh_auto_configure: configure_2 configure_3

configure_2: $(PYVERSIONS:%=override_dh_auto_configure_dbg-%) $(PYVERSIONS:%=override_dh_auto_configure-%)
	@echo "Python2 versions configured."

configure_3: $(PY3VERSIONS:%=override_dh_auto_configure_3_dbg-%) $(PY3VERSIONS:%=override_dh_auto_configure_3-%)
	@echo "Python3 versions configured."

override_dh_auto_configure_dbg-%:
	mkdir -p build-$*-dbg
	dh_auto_configure -a --builddirectory=build-$*-dbg -- \
	$(CMAKE_COMMON_OPTIONS) \
	-DCMAKE_BUILD_TYPE:STRING="Debug" \
	-DPYTHON_SUFFIX=-python$*-dbg \
	-DCMAKE_USE_PYTHON_VERSION="$*-dbg;$*_d;$*" \
	-DPython_ADDITIONAL_VERSIONS="$*-dbg;$*_d;$*" \
	-DPYTHON_EXECUTABLE=/usr/bin/python$*-dbg

override_dh_auto_configure-%:
	mkdir -p build-$*
	dh_auto_configure --builddirectory=build-$* -- \
	$(CMAKE_COMMON_OPTIONS) \
	-DCMAKE_BUILD_TYPE:STRING="Release" \
	-DPYTHON_SUFFIX=-python$* \
	-DCMAKE_USE_PYTHON_VERSION=$* \
	-DPython_ADDITIONAL_VERSIONS=$* \
	-DPYTHON_EXECUTABLE=/usr/bin/python$* \
	-DDEFAULT_PYTHON_SUFFIX=-python${DEFAULT_PY}

override_dh_auto_configure_3_dbg-%:
	mkdir -p build-$*-dbg
	dh_auto_configure -a --builddirectory=build-$*-dbg -- \
	$(CMAKE_COMMON_OPTIONS) \
	-DCMAKE_BUILD_TYPE:STRING="Debug" \
	-DUSE_PYTHON3=ON \
	-DPYTHON_SUFFIX=-python$*-dbg \
	-DCMAKE_USE_PYTHON_VERSION="$*-dbg;$*_d;$*" \
	-DCMAKE_USE_PYTHON_COMPILATION_FLAGS="dmu;dm" \
	-DPython_ADDITIONAL_VERSIONS="$*-dbg;$*_d;$*" \
	-DPYTHON3_EXECUTABLE=/usr/bin/python$*m \
	-DPYTHON3_DBG_EXECUTABLE=/usr/bin/python$*dm

override_dh_auto_configure_3-%:
	mkdir -p build-$*
	dh_auto_configure --builddirectory=build-$* -- \
	$(CMAKE_COMMON_OPTIONS) \
	-DCMAKE_BUILD_TYPE:STRING="Release" \
	-DUSE_PYTHON3=ON \
	-DPYTHON_SUFFIX=-python$* \
	-DCMAKE_USE_PYTHON_VERSION=$* \
	-DCMAKE_USE_PYTHON_COMPILATION_FLAGS="mu;m" \
	-DPython_ADDITIONAL_VERSIONS=$* \
	-DPYTHON3_EXECUTABLE=/usr/bin/python$*m \
	-DPYTHON3_DBG_EXECUTABLE=/usr/bin/python$*dm \
	-DDEFAULT_PYTHON_SUFFIX=.cpython-$(subst .,,${DEFAULT_PY3})m

#### BUILD ####

override_dh_auto_build: build_2 build_3
	dh_auto_build -i --builddirectory=build-$(DEFAULT_PY) -- doc

build_2: $(PYVERSIONS:%=override_dh_auto_build_dbg-%) $(PYVERSIONS:%=override_dh_auto_build-%)
	@echo "Python2 versions built."

build_3: $(PY3VERSIONS:%=override_dh_auto_build_dbg-%) $(PY3VERSIONS:%=override_dh_auto_build-%)
	@echo "Python3 versions built."

override_dh_auto_build_dbg-%:
	dh_auto_build -a --builddirectory=build-$*-dbg

override_dh_auto_build-%:
	dh_auto_build -a --builddirectory=build-$*

#### INSTALL ####

override_dh_auto_install: install_2 install_3

install_2: $(PYVERSIONS:%=override_dh_auto_install_dbg-%) $(PYVERSIONS:%=override_dh_auto_install-%)
	@echo "Python2 versions installed."

install_3: $(PY3VERSIONS:%=override_dh_auto_install_dbg-%) $(PY3VERSIONS:%=override_dh_auto_install-%)
	@echo "Python3 versions installed."

override_dh_auto_install_dbg-%:
	dh_auto_install -a --builddirectory=build-$*-dbg --destdir=debian/tmp-dbg

override_dh_auto_install-%:
	dh_auto_install -a --builddirectory=build-$*

#### STRIP ####
override_dh_strip:
	dh_strip -a --dbg-package=shiboken-dbg --exclude=-dbg.so

#### TEST ####

override_dh_auto_test: test_2 test_3

test_2: $(PYVERSIONS:%=override_dh_auto_test_dbg-%) $(PYVERSIONS:%=override_dh_auto_test-%)
	@echo "Python2 versions tested."

test_3: $(PY3VERSIONS:%=override_dh_auto_test_dbg-%) $(PY3VERSIONS:%=override_dh_auto_test-%)
	@echo "Python3 versions tested."

override_dh_auto_test_dbg-%:
	dh_auto_test -a --builddirectory=build-$*-dbg

override_dh_auto_test_dbg-3.4:
	# The tests for the other and sample libraries segfault. The same goes for the shiboken_module.
	dh_auto_test -a --builddirectory=build-3.4-dbg -- ARGS="-E '^(other|sample|shiboken_module)'"

override_dh_auto_test_dbg-3.5:
	# The tests for the other and sample libraries segfault. The same goes for the shiboken_module.
	dh_auto_test -a --builddirectory=build-3.4-dbg -- ARGS="-E '^(other|sample|shiboken_module)'"

override_dh_auto_test-%:
	dh_auto_test -a --builddirectory=build-$*

#### CLEAN ####
override_dh_auto_clean:
	rm -rf build-*
	rm -rf debian/tmp-dbg
	rm -f debian/libshiboken*.symbols
	rm -f debian/libshiboken*.symbols-py*
	rm -f debian/libshiboken-py3-*.symbols
	rm -f debian/libshiboken-py3-*.symbols-py*
	- rm -f tests/py3kcompat.pyc
	- rm -f tests/__pycache__/py3k*.cpython-*.pyc

#### DEBIAN INSTALL ####

override_dh_install: override_dh_install_done
	dh_install -i

override_dh_install_real:
	# Install the debug variant
	dh_install --sourcedir=debian/tmp-dbg -pshiboken-dbg
	
	# Install the other variants
	dh_install -a --list-missing --remaining-packages

override_dh_install_done: override_dh_install_real
ifneq (,$(filter libshiboken-dev, $(shell dh_listpackages)))
	# Setup the default version symbolic links
	ln -sf libshiboken-python${DEFAULT_PY}.so \
	               debian/libshiboken-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libshiboken.so
	# Make sure the pkg-config is the default's
	install -D -m 644 build-${DEFAULT_PY}/data/shiboken.pc \
			debian/libshiboken-dev/usr/lib/$(DEB_HOST_MULTIARCH)/pkgconfig/shiboken.pc
	# Make sure the cmake file is Python2's default
	install -D -m 644 build-${DEFAULT_PY}/data/ShibokenConfig.cmake debian/libshiboken-dev/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Shiboken-$(shell grep 'set(PACKAGE_VERSION ' build-${DEFAULT_PY}/data/ShibokenConfigVersion.cmake | sed -e 's/.*PACKAGE_VERSION \(.*\))/\1/g')/ShibokenConfig.cmake
endif
ifneq (,$(filter shiboken-dbg, $(shell dh_listpackages)))
	ln -sf libshiboken-python${DEFAULT_PY}-dbg.so \
	               debian/shiboken-dbg/usr/lib/$(DEB_HOST_MULTIARCH)/libshiboken-dbg.so
endif

#### Shared libs ####
override_dh_makeshlibs: $(PYVERSIONS:%=override_dh_makeshlibs-%) $(PY3VERSIONS:%=override_dh_makeshlibs_py3-%)
	cat debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols-py* > debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols
	cat debian/libshiboken-py3-${LIBSHIBOKEN_SOVERSION}.symbols-py* > debian/libshiboken-py3-${LIBSHIBOKEN_SOVERSION}.symbols
	dh_makeshlibs -a

override_dh_makeshlibs-%:
	echo "libshiboken-python$*.so.${LIBSHIBOKEN_SOVERSION} libshiboken${LIBSHIBOKEN_SOVERSION} #MINVER#" > debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols-py$*
	cat debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols.pristine >> debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols-py$*
ifeq ($(g++-version-ge-4.5),yes)
	# g++ version is >= 4.5, add related symbols
	sed -e 's|^ _| (ge-g++4.5)_|g' debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols.g++-ge-4.5 >> debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols-py$*
else
	# g++ version is < 4.5, add related symbols
	sed -e 's|^ _| (lt-g++4.5)_|g' debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols.g++-lt-4.5 >> debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols-py$*
endif

override_dh_makeshlibs_py3-%:
	echo "libshiboken.cpython-$(subst .,,$*)m.so.${LIBSHIBOKEN_SOVERSION} libshiboken-py3-${LIBSHIBOKEN_SOVERSION} #MINVER#" > debian/libshiboken-py3-${LIBSHIBOKEN_SOVERSION}.symbols-py$*
	cat debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols.pristine >> debian/libshiboken-py3-${LIBSHIBOKEN_SOVERSION}.symbols-py$*
ifeq ($(g++-version-ge-4.5),yes)
	# g++ version is >= 4.5, add related symbols
	sed -e 's|^ _| (ge-g++4.5)_|g' debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols.g++-ge-4.5 >> debian/libshiboken-py3-${LIBSHIBOKEN_SOVERSION}.symbols-py$*
else
	# g++ version is < 4.5, add related symbols
	sed -e 's|^ _| (lt-g++4.5)_|g' debian/libshiboken${LIBSHIBOKEN_SOVERSION}.symbols.g++-lt-4.5 >> debian/libshiboken-py3-${LIBSHIBOKEN_SOVERSION}.symbols-py$*
endif


override_dh_installdocs:
	dh_installdocs
ifneq (,$(filter shiboken-doc, $(shell dh_listpackages)))
	dh_sphinxdoc -pshiboken-doc
endif
