#!/usr/bin/make -f

PYTHON=python2.7
# Take advantage of PyPy if it's installed
#PYTHON=$(shell command -v pypy > /dev/null && echo pypy || echo python2.7)

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

export PYPY_USESSION_BASENAME := debian

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  SUB_MAKEFLAGS = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# dh_python2 is used for pypy-sandbox
%:
	dh $@ --with python2,sphinxdoc

override_dh_auto_configure:
	debian/scripts/memory-check.sh

override_dh_auto_build-arch: pypy/goal/pypy-c pypy/goal/pypy-c-sandbox
	$(MAKE) -C pypy/doc man BUILDDIR=$(CURDIR)/build-docs
	# Build cffi modules
	pypy/goal/pypy-c -c 'import _sqlite3, _curses, _tkinter.tklib, syslog'

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

pypy/goal/pypy-%: export TMPDIR = $(CURDIR)/build-tmp
pypy/goal/pypy-%: C_SRC_DIR = $(TMPDIR)/usession-$(PYPY_USESSION_BASENAME)-0/testing_1
pypy/goal/pypy-%:
	rm -rf $(TMPDIR)
	mkdir $(TMPDIR)
	debian/scripts/translate.sh --python $(PYTHON) $$(echo $@ | grep -q sandbox && echo --sandbox)
	# PyPy captures all make output, so we call it ourselves, to avoid timeouts:
	$(MAKE) -C $(C_SRC_DIR) $(SUB_MAKEFLAGS)
	cp $(C_SRC_DIR)/pypy-c $@

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

override_dh_auto_install:
	# Upstream provides no install script

override_dh_fixperms-indep:
	find debian/pypy-lib debian/pypy-lib-testsuite debian/pypy-tk \
	     -name '*.pyc' -delete
	find debian/pypy-lib debian/pypy-lib-testsuite debian/pypy-tk \
	     -name '__pycache__' -delete
	# Don't need these, and lintian will make a noise about them
	find debian/pypy-lib \( -name 'regen' -o -name '*.bat' -o -name 'fetch_*' \) -delete
	# Remove empty directories, because pypyclean will
	find debian/pypy-lib*/usr/lib/pypy/lib-python -type d -empty -delete
	# Nothing in the stdlib should be executable
	chmod -R a-x+X debian/pypy-lib*/usr/lib/pypy/lib*
	# Fix interpreters
	find debian/pypy-lib debian/pypy-lib-testsuite debian/pypy-tk \
	     -name '*.py' -print0 \
	     | xargs -0 sed -i -e '1s|^#!.*python.*|#!/usr/bin/pypy|'
	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_python2:
	dh_python2 -p python-pypy.sandbox

override_dh_installdeb:
	set -e; for maintscript in preinst postinst; do \
		sed 's/#VERSION#/$(VER)/g' debian/pypy.$$maintscript.in \
		    > debian/pypy.$$maintscript; \
	done
	dh_installdeb

override_dh_builddeb:
	dh_builddeb -- -Zxz

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