# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2003 Colin Walters <walters@debian.org>
# Copyright © 2006 Marc Dequènes (Duck) <Duck@DuckCorp.org>
# Copyright © 2003,2006-2011, 2014, 2016 Jonas Smedegaard <dr@jones.dk>
# Description: manage Python modules using the 'distutils' build system
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

#PATH_RULES#

ifndef _cdbs_class_python_distutils
_cdbs_class_python_distutils = 1

include $(_cdbs_class_path)/python-module.mk$(_cdbs_makefile_suffix)

#DEB_PYTHON_DISTUTILS_SRCDIR = $(DEB_SRCDIR)

DEB_PYTHON_SETUP_CMD ?= setup.py
DEB_PYTHON_BUILD_ARGS ?= --build-base="$(CURDIR)/$(cdbs_curbuilddir)/build"
DEB_PYTHON_INSTALL_ARGS_ALL ?= --no-compile -O0
DEB_PYTHON_CLEAN_ARGS ?= -a
#DEB_PYTHON_DISTUTILS_INSTALLDIR_SKEL = /usr/lib/@PYTHONFLAVOR@/dist-packages/

_cdbs_deprecated_vars += DEB_PYTHON_SETUP_CMD,0.4.89
_cdbs_deprecated_DEB_PYTHON_SETUP_CMD_default := $(DEB_PYTHON_SETUP_CMD)
DEB_PYTHON_SETUP_CMD_DEFAULT ?= $(DEB_PYTHON_SETUP_CMD)

_cdbs_deprecated_vars += DEB_PYTHON_BUILD_ARGS,0.4.89
_cdbs_deprecated_DEB_PYTHON_BUILD_ARGS_default := $(DEB_PYTHON_BUILD_ARGS)
DEB_PYTHON_BUILD_ARGS_DEFAULT ?= $(DEB_PYTHON_BUILD_ARGS)

_cdbs_deprecated_vars += DEB_PYTHON_CLEAN_ARGS,0.4.89
_cdbs_deprecated_DEB_PYTHON_CLEAN_ARGS_default := $(DEB_PYTHON_CLEAN_ARGS)
DEB_PYTHON_CLEAN_ARGS_DEFAULT ?= $(DEB_PYTHON_CLEAN_ARGS)

cdbs_python_distutils_srcdir = $(or $(strip \
	$(DEB_PYTHON_DISTUTILS_SRCDIR)),$(strip\
	$(DEB_SRCDIR)))
cdbs_python_setup_cmd = $(call cdbs_expand_curvar,DEB_PYTHON_SETUP_CMD)
cdbs_python_build_args = $(call cdbs_expand_curvar,DEB_PYTHON_BUILD_ARGS)
cdbs_python_install_args = $(call cdbs_expand_curvar,DEB_PYTHON_INSTALL_ARGS)
cdbs_python_clean_args = $(call cdbs_expand_curvar,DEB_PYTHON_CLEAN_ARGS)
_cdbs_expand_python_distutils_skeldir = $(strip \
	$(subst @PYTHONBINARY@,$(strip \
		$1 \
		$(call cdbs_warn_deprecated,\
			@PYTHONBINARY@,0.4.144,@PYTHONFLAVOR@)),\
		$(subst @PYTHONFLAVOR@,$1,\
			$(DEB_PYTHON_DISTUTILS_INSTALLDIR_SKEL))))

CDBS_BUILD_DEPENDS_class_python-distutils ?= $(cdbs_python_builddeps_cdbs)
CDBS_BUILD_DEPENDS +=, $(CDBS_BUILD_DEPENDS_class_python-distutils)

CDBS_BUILD_DEPENDS_cdbs_python-distutils_srcdir ?= cdbs (>= 0.4.97~)
CDBS_BUILD_DEPENDS += $(if $(DEB_PYTHON_DISTUTILS_SRCDIR),\
	$(comma) $(CDBS_BUILD_DEPENDS_cdbs_python-distutils_srcdir))

CDBS_BUILD_DEPENDS_cdbs_python-distutils_installdir ?= cdbs (>= 0.4.91~)
CDBS_BUILD_DEPENDS += $(if $(DEB_PYTHON_DISTUTILS_INSTALLDIR_SKEL),\
	$(comma) $(CDBS_BUILD_DEPENDS_cdbs_python-distutils_installdir))

# Python-related dependencies according to Python policy, appendix A
CDBS_BUILD_DEPENDS_class_python-distutils_python ?= $(cdbs_python_builddeps)
CDBS_BUILD_DEPENDS +=, $(CDBS_BUILD_DEPENDS_class_python-distutils_python)

pre-build::
	mkdir -p debian/python-module-stampdir

# build stage
$(patsubst %,build/%,$(cdbs_python_indep_packages) $(cdbs_python_arch_packages)):: build/%: debian/python-module-stampdir/%

$(patsubst %,debian/python-module-stampdir/%,$(cdbs_python_indep_packages)):: debian/python-module-stampdir/%:
	$(strip cd $(cdbs_python_distutils_srcdir) && \
		$(cdbs_curpythonruntime) \
			$(cdbs_python_setup_cmd) build \
			$(cdbs_python_build_args))
	touch $@

$(patsubst %,debian/python-module-stampdir/%,$(cdbs_python_arch_packages)) :: debian/python-module-stampdir/%:
	$(strip set -e; \
		cd $(cdbs_python_distutils_srcdir) && \
		$(foreach flavor,$(cdbs_curpythonflavors),\
			$(call cdbs_expand_pythonruntime,$(cdbs_curpkg),$(flavor)) \
				$(cdbs_python_setup_cmd) build \
				$(cdbs_python_build_args); ))
	touch $@


# install stage
$(patsubst %,install/%,$(cdbs_python_indep_packages)) :: install/%:
	$(strip cd $(cdbs_python_distutils_srcdir) && \
		$(cdbs_curpythonruntime) \
			$(cdbs_python_setup_cmd) install \
			--root="$(cdbs_python_destdir)" \
			--install-layout=deb \
			$(if $(DEB_PYTHON_DISTUTILS_INSTALLDIR_SKEL),\
				--install-lib=$(call _cdbs_expand_python_distutils_skeldir,$(cdbs_curpythondefaultflavor))) \
			$(cdbs_python_install_args))

$(patsubst %,install/%,$(cdbs_python_arch_packages)) :: install/%:
	$(strip set -e; \
		cd $(cdbs_python_distutils_srcdir) && \
		$(foreach flavor,$(cdbs_curpythonflavors),\
			$(call cdbs_expand_pythonruntime,$(cdbs_curpkg),$(flavor)) \
				$(cdbs_python_setup_cmd) install \
				--root="$(cdbs_python_destdir)" \
				--install-layout=deb \
				$(if $(DEB_PYTHON_DISTUTILS_INSTALLDIR_SKEL),\
					--install-lib=$(call _cdbs_expand_python_distutils_skeldir,$(flavor))) \
				$(cdbs_python_install_args); ))


# clean stage
clean:: $(patsubst %,python-module-clean/%,$(cdbs_python_indep_packages) $(cdbs_python_arch_packages))

$(patsubst %,python-module-clean/%,$(cdbs_python_indep_packages)) :: python-module-clean/%:
	-$(strip cd $(cdbs_python_distutils_srcdir) && \
		$(cdbs_curpythonruntime) \
			$(cdbs_python_setup_cmd) clean \
			$(cdbs_python_clean_args))

$(patsubst %,python-module-clean/%,$(cdbs_python_arch_packages)) :: python-module-clean/%:
	-$(strip cd $(cdbs_python_distutils_srcdir) && \
		$(foreach flavor,$(cdbs_curpythonflavors),\
			$(call cdbs_expand_pythonruntime,$(cdbs_curpkg),$(flavor)) \
				$(cdbs_python_setup_cmd) clean \
				$(cdbs_python_clean_args); ))

# cleanup stamp dir
# (dh_clean choke on dirs named stamp, so need to happen before clean::)
clean:: clean-python-distutils
clean-python-distutils::
	rm -rf debian/python-module-stampdir

# Calling "setup.py clean" may create .pyc files and __pycache__
# directories, so we need a final cleanup pass here.
# Also clean up .egg-info files generated by setuptools
clean::
	find "$(CURDIR)" -name '*.py[co]' -delete
	find "$(CURDIR)" -name __pycache__ -type d -empty -delete
	find "$(CURDIR)" -prune -name '*.egg-info' -exec rm -rf '{}' ';'

.PHONY: $(patsubst %,python-module-clean/%,$(cdbs_python_indep_packages) $(cdbs_python_arch_packages))

# deprecated variables
cdbs_expand_python_distutils_installdir = $(strip $(call cdbs_warn_deprecated,\
	cdbs_expand_python_distutils_installdir,0.4.131)\
	$(subst @PYTHONBINARY@,$1,$(strip \
		$(subst @PYTHONFLAVOR@,$1,$(or $(strip \
			$(DEB_PYTHON_DISTUTILS_INSTALLDIR_SKEL)),$(strip \
			/usr/lib/@PYTHONFLAVOR@/dist-packages/))))))

endif
