#!/usr/bin/make -f
#export DH_VERBOSE = 1

include /usr/share/dpkg/pkg-info.mk
export PYBUILD_NAME=pydevd

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)
HOST_TRIPLET:=$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@ --buildsystem=pybuild

override_dh_auto_build:
	# This shared library is Python-version agnostic
	cd pydevd_attach_to_process/linux_and_mac && \
	    g++ $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -shared \
	      -o ../attach.$(HOST_TRIPLET).so -fPIC -nostartfiles attach.cpp
	# Regenerate the Cython C files (though ignore the generated
	# .so files; they will be compiled for each Python version in
	# dh_auto_build)
	python3 setup_pydevd_cython.py build_ext --inplace --force-cython
	dh_auto_build --buildsystem=pybuild
	txt2man -t pydevd -P pname -r "pydevd-$(DEB_VERSION_UPSTREAM)" -s 1 -v "" -d "July 2022" debian/manpage/pydevd.txt > debian/manpage/pydevd.1

override_dh_auto_install:
	dh_auto_install
	# dh_install doesn't remove the original version, so we use dh_movefiles
	dh_movefiles \
		--package=pydevd \
		--sourcedir=debian/python3-pydevd \
		usr/bin

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# * Some of the tests depend on the uninstalled pycompletionserver.py
	# or pydev_coverage.py, so we exclude those
	# * Two bytecode tests fail on Python 3.10, so we exclude those too.
	# Reported upstream to
	# https://github.com/fabioz/PyDev.Debugger/issues/222
	# * test_case_flask_exceptions fails, and is not tested by upstream,
	# so we just skip it.
	# Reported upstream to
	# https://github.com/fabioz/PyDev.Debugger/issues/223
	# * test_py_37_breakpoint_remote_no_import does not work on the built
	# or installed version; reported upstream to
	# https://github.com/fabioz/PyDev.Debugger/issues/224
	# * test_matplotlib_activation and test_pandas cause failures due
	# to a matplotlib warning (that does seem to be easily silenced)
	# if the build machine has too many fonts on it, so we skip then here
	# (but not in the autopkgtest suite)
	# * The http_proxy settings are to disable the proxies that pybuild
	# introduces
	#
	# We also run the tests a second time if the first fails, as
	# there are occasionally intermittent errors in the tests.
	export PYDEVD_USE_CYTHON=YES; \
	export http_proxy=""; \
	export PYBUILD_TEST_ARGS="-n auto \
		--ignore=tests/test_jyserver.py \
		--ignore=tests/test_pyserver.py \
		--ignore=tests_python/test_pydevcoverage.py \
	        --deselect=tests_python/test_debugger_json.py::test_case_flask_exceptions \
	        --deselect=tests_python/test_bytecode_manipulation.py::test_set_pydevd_break_01 \
	        --deselect=tests_python/test_bytecode_manipulation.py::test_set_pydevd_break_01a \
	        --deselect=tests_python/test_debugger.py::test_py_37_breakpoint_remote_no_import \
	        --deselect=tests_python/test_debugger.py::test_matplotlib_activation \
	        --deselect=tests_python/test_debugger_json.py::test_pandas"; \
		xvfb-run -a -s "-screen 0 1024x1024x24" dh_auto_test || \
		xvfb-run -a -s "-screen 0 1024x1024x24" dh_auto_test
endif
