#!/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)

%:
	dh $@ --buildsystem=pybuild

_pydevd_bundle/pydevd_runpy.py:
	libdir=/usr/lib/$$(debian/py3versions_min.py); \
	    cp $$libdir/runpy.py _pydevd_bundle/pydevd_runpy.py
	patch -p1 < debian/runpy.patch

execute_before_dh_auto_clean: _pydevd_bundle/pydevd_runpy.py
	# setup.py clean requires pydevd_runpy.py to be present

execute_after_dh_clean:
	rm -f _pydevd_bundle/pydevd_runpy.py

execute_before_dh_auto_configure: _pydevd_bundle/pydevd_runpy.py
	# setup.py config requires pydevd_runpy.py to be present

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.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

execute_after_dh_auto_install:
	# usr/bin needs to be moved from debian/python3-pydevd to debian/pydevd
	# dh_install will move it from debian/tmp
	-mkdir -p debian/tmp/usr
	mv debian/python3-pydevd/usr/bin debian/tmp/usr

override_dh_python3:
	dh_python3 --no-ext-rename

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)
	# * Some tests fail on mips64el, but I'm not sure why.  So we skip
	# them; see README.Debian for some more comments on this.
	# One test fails on the ppc64el autopkgtest, so we skip this too.
	#
	# * 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.
	arch=$$(dpkg --print-architecture); \
	if [ $$arch = mips64el ]; then \
	    EXCLUDE="\
	        --deselect=tests_python/test_debugger.py::test_case_flask \
	        --deselect=tests_python/test_debugger.py::test_case_django_a \
	        --deselect=tests_python/test_debugger.py::test_case_django_template_inherits_no_exception \
	        --deselect=tests_python/test_debugger.py::test_case_django_no_var_error \
	        --deselect=tests_python/test_debugger.py::test_case_django_no_attribute_exception_breakpoint \
	        --deselect=tests_python/test_debugger.py::test_case_django_no_attribute_exception_breakpoint_and_regular_exceptions \
	        --deselect=tests_python/test_debugger.py::test_case_django_invalid_template_exception_breakpoint \
	        --deselect=tests_python/test_debugger_json.py::test_case_django_no_attribute_exception_breakpoint \
	        --deselect=tests_python/test_debugger_json.py::test_case_flask_line_validation"; \
	elif [ $arch = ppc64el ]; then \
	    EXCLUDE="\
	        --deselect=tests_python/test_debugger_json.py::test_code_reload"; \
	else \
	    EXCLUDE=""; \
	fi; \
	export PYDEVD_USE_CYTHON=YES; \
	export http_proxy=""; \
	export PYBUILD_TEST_ARGS="\
		--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 \
	        $$EXCLUDE"; \
		xvfb-run -a -s "-screen 0 1024x1024x24" dh_auto_test || \
		xvfb-run -a -s "-screen 0 1024x1024x24" dh_auto_test
endif
