#!/usr/bin/make -f

DEB_BUILD_OPTION_PARALLEL = 1
include /usr/share/dpkg/buildopts.mk

# PyPy doesn't harden well, and handles opt and debug itself
BUILDFLAGS_ENV := DEB_BUILD_MAINT_OPTIONS=hardening=-stackprotector,-fortify
BUILDFLAGS_ENV += DEB_CFLAGS_MAINT_STRIP="-O0 -O1 -O2 -O3 -Os -Ofast -g"
export CFLAGS = $(shell $(BUILDFLAGS_ENV) dpkg-buildflags --get CFLAGS)
export LDFLAGS = $(shell $(BUILDFLAGS_ENV) dpkg-buildflags --get LDFLAGS)

%:
	dh $@ --with sphinxdoc

override_dh_auto_configure:
	# This rule left intentionally empty

override_dh_auto_build-arch: pypy/goal/pypy3.9-c
	$(MAKE) -C pypy/doc man BUILDDIR=$(CURDIR)/build-docs

	# Build cffi modules
	pypy/goal/pypy3.9-c lib_pypy/pypy_tools/build_cffi_imports.py
	debian/scripts/verify-all-headers-present.py

override_dh_auto_build-indep:
	$(MAKE) -C pypy/doc html BUILDDIR=$(CURDIR)/build-docs

cpython27/python:
	dh_autoreconf -D cpython27
	cd cpython27 && ./configure --with-system-ffi --with-system-expat
	cd cpython27 && $(MAKE) -j$(DEB_BUILD_OPTION_PARALLEL)

pypy/goal/pypy3.9-c: cpython27/python
	debian/scripts/translate.sh --python $(CURDIR)/cpython27/python

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
override_dh_auto_test-arch: export TMPDIR = $(CURDIR)/build-tests-tmp
override_dh_auto_test-arch: export HOME = $(TMPDIR)
override_dh_auto_test-arch:
	rm -rf $(TMPDIR) build-tests
	mkdir $(TMPDIR) build-tests
	ln -sf lib_pypy/cffi/_pycparser/ pycparser
	# Test failures currently ignored
	PYTHONPATH=. cpython27/python -u \
		testrunner/runner.py \
		--logfile=build-tests/pytest-A.log \
		--config=pypy/pytest-A.cfg \
		--root=pypy || true
	rm pycparser
	TERM=$${TERM:-dumb} cpython27/python -u \
		pypy/test_all.py \
		--pypy=pypy/goal/pypy3.9-c \
		--resultlog=build-tests/lib-python.log \
		lib-python || true
	cpython27/python -u pypy/test_all.py \
		--pypy=pypy/goal/pypy3.9-c \
		--resultlog=build-tests/pypy-c.log \
		pypy/module/pypyjit/test_pypy_c || true
endif

override_dh_auto_install:
	debian/scripts/gen-backend-versions.py

override_dh_fixperms-arch:
	debian/scripts/cleanup-lib.sh pypy3-lib
	find debian/pypy3-tk \( -name '*.pyc' -o -name '__pycache__' \) -delete
	# Fix interpreters
	find debian/pypy3-tk \
	     -name '*.py' -print0 \
	     | xargs -0 sed -i -e '1s|^#!.*python.*|#!/usr/bin/pypy3|'
	dh_fixperms -a

override_dh_fixperms-indep:
	debian/scripts/cleanup-lib.sh pypy3-lib-testsuite
	dh_fixperms -i

override_dh_sphinxdoc-arch:
	# dh_sphinxdoc doesn't fail silently when there are no docs

override_dh_compress:
	dh_compress -X.inv -X.txt

override_dh_shlibdeps-arch:
	dh_shlibdeps --package=pypy3 -- -dPre-Depends
	dh_shlibdeps --arch --remaining-packages

override_dh_installdeb:
	debian/scripts/maintscript.sh pypy3.preinst
	debian/scripts/maintscript.sh pypy3.postinst
	debian/scripts/maintscript.sh pypy3.prerm
	debian/scripts/maintscript.sh pypy3-lib.prerm
	dh_installdeb

HG_REPO ?= https://bitbucket.org/pypy/pypy
REV = $(shell dpkg-parsechangelog | sed -rne 's,^Version: .*hg([0-9]+).*,\1,p')
DVER = $(shell dpkg-parsechangelog | sed -rne 's,^Version: (.+)\+dfsg-.*,\1,p')
UVER = $(shell echo $(DVER) | sed -e 's/~//')
get-packaged-orig-source:
	@echo "You can build this from a local repo by supplying the HG_REPO variable"
	rm -rf pypy3-$(DVER).orig
	hg clone $(HG_REPO) pypy3-$(DVER).orig
ifeq (,$(REV))
	cd pypy3-$(DVER).orig && hg up -r release-pypy3.9-v$(UVER)
else
	cd pypy3-$(DVER).orig && hg up -r $(REV)
endif
	rm -rf pypy3-$(DVER).orig/.hg*
	rm pypy3-$(DVER).orig/lib-python/*/distutils/command/*.exe
	rm pypy3-$(DVER).orig/rpython/rlib/test/loadtest/loadtest*.dll
	rm -rf pypy3-$(DVER).orig/lib-python/*/ensurepip/_bundled/
	tar -cJ --owner root --group root --mode a+rX \
	    -f pypy3_$(DVER)+dfsg.orig.tar.xz pypy3-$(DVER).orig
	rm -rf pypy3-$(DVER).orig
