#!/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 noopt,$(DEB_BUILD_OPTIONS)))
  OPT := 0
else
  OPT := $(shell dpkg-architecture -iany-i386 || dpkg-architecture -iany-amd64 && echo jit || echo 3)
endif

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

%:
	dh $@ --with sphinxdoc --without python-support

override_dh_auto_configure:
	set -e; \
	mem=$(shell sed -rne 's/^MemTotal: *([0-9]+) kB$$/\1/p' /proc/meminfo); \
	req_mem=1900; \
	bits=$(shell dpkg-architecture -qDEB_HOST_ARCH_BITS); \
	[ $$bits -ge 64 ] && req_mem=$$(($$req_mem * 2)) || true; \
	if [ $$(($$req_mem * 1024)) -gt $$mem ]; then \
		echo "You don't have enough RAM to build PyPy."; \
		echo "It's known to require >= $$req_mem MiB on $$bits bit archs."; \
		exit 1; \
	fi

override_dh_auto_build-arch: export TMPDIR = $(CURDIR)/build-tmp
override_dh_auto_build-arch: C_SRC_DIR = $(TMPDIR)/usession-$(PYPY_USESSION_BASENAME)-0/testing_1
override_dh_auto_build-arch:
	rm -rf $(TMPDIR)
	mkdir $(TMPDIR)
	cd pypy/translator/goal && $(PYTHON) translate.py --batch --opt=$(OPT) \
	                                     --source targetpypystandalone
	# 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 pypy/translator/goal

	# https://bugs.pypy.org/issue610:
	if [ -x /usr/sbin/execstack ]; then \
		/usr/sbin/execstack --clear-execstack pypy/translator/goal/pypy-c; \
	fi
	rst2man debian/pypy.1.rst > build-tmp/pypy.1

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

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) testrunner/runner.py \
	                       --logfile=build-tests/pytest-A.log \
	                       --config=pypy/pytest-A.cfg \
	                       --root=pypy || true
	$(PYTHON) pypy/test_all.py \
	          --pypy=pypy/translator/goal/pypy-c \
	          --resultlog=build-tests/lib-python.log \
	          lib-python || true
	pypy/translator/goal/pypy-c 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_auto_clean:
	rm -rf build-*
	# Created by our PEP3147 patch:
	rm -f pypy/doc/config/objspace.magic_tag.txt
	find . -name '*.pyc' -delete
	rm -f pypy/translator/goal/pypy-c
	rm -f pypy/translator/cli/src/*.exe pypy/translator/cli/src/*.dll
	rm -f include/*.h include/*.inl
	rm -rf pypy/_cache
	rm -f lib_pypy/ctypes_config_cache/_*_cache.py
	rm -f $(filter-out %__init__.py, $(wildcard lib_pypy/ctypes_config_cache/_*_.py))
	rm -f lib-python/modified-2.7/lib2to3/*Grammar*.pickle \
	      pypy/module/test_lib_pypy/ctypes_tests/_ctypes_test.o \
	      pypy/doc/config/commandline*.rst \
	      pypy/doc/config/objspace*.rst \
	      pypy/doc/config/translation*.rst

override_dh_install-arch:
	dh_install -a
	dh_install -p pypy pypy/translator/goal/pypy-c /usr/bin
	cd debian/pypy/usr/bin && mv pypy-c pypy
	# Architecture-dependant bits of the stdlib (shipped in pypy)
	dh_install -p pypy 'include/pypy_*.h' /usr/lib/pypy/include-arch
	dh_install -p pypy 'lib_pypy/ctypes_config_cache/_*_.py' \
	                   /usr/lib/pypy/lib_pypy/ctypes_config_cache
	# __init__.py gets included in our glob
	rm -f debian/pypy/usr/lib/pypy/lib_pypy/ctypes_config_cache/__init__.py

override_dh_install-indep:
	dh_install -i
	# Architecture-dependant bits of the stdlib (shipped in pypy)
	find debian/pypy-lib/usr/lib/pypy/lib_pypy/ctypes_config_cache \
	     \( -name '_*_.py' -a ! -name '__init__.py' \) -delete
	set -e; \
	for header in debian/pypy-dev/usr/include/pypy/pypy_*.h; do \
		header=$$(basename $$header); \
		dh_link -p pypy-dev /usr/lib/pypy/include-arch/$$header \
		                    /usr/include/pypy/$$header; \
	done

override_dh_fixperms-indep:
	find debian/pypy-lib -name '*.pyc' -delete
	find debian/pypy-lib -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 bytecompile-stdlib 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 -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_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
	rm pypy-$(VER).orig/pypy/translator/jvm/src/*.jar
	tar -cJ --owner root --group root --mode a+rX \
	    -f pypy_$(VER)+dfsg.orig.tar.xz pypy-$(VER).orig
	rm -rf pypy-$(VER).orig
