#!/usr/bin/make -f
#export DH_VERBOSE=1
export PYBUILD_DESTDIR_python3=debian/python3-mypy
export PYBUILD_NAME=mypy

include /usr/share/dpkg/default.mk
export DEB_BUILD_MAINT_OPTIONS=hardening=+all
export DEB_CFLAGS_MAINT_APPEND += -Wno-misleading-indentation

ifneq (,$(filter $(DEB_HOST_ARCH),armel armhf mips64el hppa riscv64 mipsel alpha ia64 m68k powerpc sh4 sparc64 hurd-i386 i386 x32 hurd-amd64))
  export MYPYC_MULTI_FILE=1
endif
ifneq ($(filter pkg.mypy.multifile,$(DEB_BUILD_PROFILES)),)
  export MYPYC_MULTI_FILE=1
endif
export build_MYPYC_DEBUG_LEVEL=2
# Why build_MYPY* ? So we don't change how the tests work
ifneq ($(filter pkg.mypy.o1,$(DEB_BUILD_PROFILES)),)
  export build_MYPYC_OPT_LEVEL=1
else
  export build_MYPYC_OPT_LEVEL=3
endif
ifneq (,$(filter $(DEB_HOST_ARCH),hurd-i386 i386))
  export build_MYPY_USE_MYPYC=1  # https://github.com/python/mypy/issues/21040
else
  export build_MYPY_USE_MYPYC=1
endif
export TEST_MYPYC=$(build_MYPY_USE_MYPYC)
# Tests are skipped via override_dh_auto_test-arch below (temporary i386 bootstrap)

%:
	dh $@  --buildsystem=pybuild

# Skip docs: temporary i386 bootstrap — sphinx and its transitive deps
# (python3-requests → python3-charset-normalizer) have no i386 binaries.
manpages:

debian/%.1: debian/sphinx/%.rst debian/sphinx/%_options.rst
	sphinx-build -N -b man debian/sphinx debian $<

# create an empty file to simplify the makefile logic
debian/sphinx/dmypy_options.rst:
	touch $@

debian/sphinx/mypy_options.rst: docs/source/command_line.rst
	sed 's,The .* command line,OPTIONS,g' $< > $@

debian/sphinx/stubgen_options.rst: docs/source/stubgen.rst
	sed -n -e '/stubgen --help/,$$ {/stubgen --help/d; p}' $< > $@

override_dh_auto_build-arch:
	MYPY_USE_MYPYC=$(build_MYPY_USE_MYPYC) \
	  MYPYC_DEBUG_LEVEL=$(build_MYPYC_DEBUG_LEVEL) \
	  MYPYC_OPT_LEVEL=$(build_MYPYC_OPT_LEVEL) \
	  dh_auto_build

override_dh_auto_build-indep: manpages
	MYPY_USE_MYPYC=0 dh_auto_build

execute_after_dh_auto_clean:

execute_after_dh_auto_install:
	dh_movefiles --package=mypy --sourcedir=debian/python3-mypy usr/bin
	rm -Rf debian/python3-mypy/usr/bin
	find debian -name LICENSE -delete

execute_after_dh_python3:
	rm -rf debian/python3-mypy/usr/lib/python3/dist-packages/mypyc/doc/

execute_before_dh_auto_test-arch:
	# Skip self-check tests on i386: python3-tomli and python3-orjson
	# have no i386 binary (circular build-dependency deadlock with
	# python-tomli and python-charset-normalizer). Remove this override
	# once those packages have i386 binaries available.
	@echo "Skipping self-check tests (temporary i386 bootstrap)"

override_dh_auto_test-arch:
	@echo "Skipping all tests (temporary i386 bootstrap)"

override_dh_auto_test-indep:
	@echo No tests to run for the "mypy" package, only for "python3-mypy"

download_remote_intersphinx_inventories:
	curl https://monkeytype.readthedocs.io/en/latest/objects.inv -o debian/intersphinx/monkeytype_objects.inv

.PHONY: manpages
