#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2022 Marcel Telka
#

include $(WS_MAKE_RULES)/setup.py.mk

ifeq ($(strip $(PYTHON_BOOTSTRAP)),yes)
# To bootstrap the bootstrapper we need to make sure it finds its own modules
ifeq ($(strip $(COMPONENT_NAME)),pyproject_installer)
PYTHON_ENV += PYTHONPATH=$(@D)/src
endif

COMPONENT_BUILD_CMD =		$(PYTHON) -m pyproject_installer build
COMPONENT_BUILD_ARGS =

COMPONENT_INSTALL_CMD =		$(PYTHON) -m pyproject_installer install
COMPONENT_INSTALL_ARGS =
COMPONENT_INSTALL_ARGS +=	--destdir $(PROTO_DIR)

# pyproject_installer does not bytecompile after the install.  Since we need
# pyc files we need to force that.
COMPONENT_POST_INSTALL_ACTION += \
	$(PYTHON) -m compileall $(PROTO_DIR)/$(PYTHON_DIR)/site-packages $(PROTO_DIR)/$(PYTHON_LIB) ;
else
COMPONENT_BUILD_CMD =		$(PYTHON) -m build
COMPONENT_BUILD_ARGS =
COMPONENT_BUILD_ARGS +=		--wheel
COMPONENT_BUILD_ARGS +=		--no-isolation

COMPONENT_INSTALL_CMD =		$(PYTHON) -m installer
COMPONENT_INSTALL_ARGS =
COMPONENT_INSTALL_ARGS +=	--destdir $(PROTO_DIR)
COMPONENT_INSTALL_ARGS +=	$(@D)/dist/*.whl

USERLAND_REQUIRED_PACKAGES.python += library/python/build
USERLAND_REQUIRED_PACKAGES.python += library/python/installer
endif

# Move all modules from default site-packages directory to vendor-packages
# directory where we place modules shipped by the OS but not included in the
# core Python distribution.
COMPONENT_POST_INSTALL_ACTION += \
	if [ -d $(PROTO_DIR)/$(PYTHON_DIR)/site-packages ] ; then \
		$(RM) -r $(PROTO_DIR)/$(PYTHON_LIB) ; \
		$(MV) $(PROTO_DIR)/$(PYTHON_DIR)/site-packages $(PROTO_DIR)/$(PYTHON_LIB) ; \
	fi ;

# Generate raw list of hatch, pdm, pep735, pipenv, and poetry test dependencies
# per Python version.
#
# Please note we set PATH below for tox to workaround
# https://github.com/tox-dev/tox/issues/2538
COMPONENT_POST_INSTALL_ACTION += \
	cd $(@D)$(COMPONENT_SUBDIR:%=/%) ; \
	cfg=$(BUILD_DIR)/pyproject_deps-$(PYTHON_VERSION).json ; \
	$(RM) $$cfg ; \
	for p in $(TEST_REQUIREMENTS_HATCH) ; do \
		$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg add hatch_$$p hatch pyproject.toml $$p ; \
	done ; \
	for p in $(TEST_REQUIREMENTS_PDM) ; do \
		$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg add pdm_$$p pdm $$p ; \
	done ; \
	for p in $(TEST_REQUIREMENTS_PEP735) ; do \
		$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg add pep735_$$p pep735 $$p ; \
	done ; \
	if [ "$(strip $(TEST_STYLE))" == "tox" -a -x "$(TOX)" ] ; then \
		for p in $$(PATH=$(PATH) PYTHONPATH=$(PROTO_DIR)/$(PYTHON_DIR)/site-packages:$(PROTO_DIR)/$(PYTHON_LIB) \
			$(TOX) -qq --no-provision --print-dependency-groups-to=- $(TOX_TESTENV)) ; do \
				$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg add pep735_$$p pep735 $$p ; \
		done ; \
	fi ; \
	for p in $(TEST_REQUIREMENTS_PIPENV) ; do \
		$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg add pipenv_$$p pipenv Pipfile $$p ; \
	done ; \
	for p in $(TEST_REQUIREMENTS_POETRY) ; do \
		$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg add poetry_$$p poetry $$p ; \
	done ; \
	if [ -f $$cfg ] ; then \
		$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg sync ; \
		$(PYTHON) -m pyproject_installer deps --depsconfig $$cfg eval \
			| $(PYTHON) $(WS_TOOLS)/python-requires - >> $(@D)/.depend-test ; \
	fi ;

# Add build dependencies from project metadata to REQUIRED_PACKAGES
REQUIRED_PACKAGES_RESOLVED += $(BUILD_DIR)/META.depend.res
$(BUILD_DIR)/META.depend.res: $(SOURCE_DIR)/.prep
	$(MKDIR) $(BUILD_DIR)
	# PYTHON_ENV is needed four times here to have the PYTHONPATH set
	# properly when we bootstrap the pyproject_installer bootstrapper.
	$(PYTHON_ENV) $(PYTHON) -m pyproject_installer deps --depsconfig $(BUILD_DIR)/pyproject_deps.json add build_pep517 pep517
	$(PYTHON_ENV) $(PYTHON) -m pyproject_installer deps --depsconfig $(BUILD_DIR)/pyproject_deps.json add build_pep518 pep518
	cd $(SOURCE_DIR) ; $(PYTHON_ENV) $(PYTHON) -m pyproject_installer deps --depsconfig $(BUILD_DIR)/pyproject_deps.json sync
	$(PYTHON_ENV) $(PYTHON) -m pyproject_installer deps --depsconfig $(BUILD_DIR)/pyproject_deps.json eval --depformat '$$nname' \
		| $(GSED) -e 's/.*/depend type=require fmri=pkg:\/library\/python\/&-$$(PYV)/' \
		> $@
# PYTHON_ENV (see above) needs BITS
$(BUILD_DIR)/META.depend.res: BITS = $(PREFERRED_BITS)

# We need pyproject_installer for two purposes:
# - to detect build dependencies for all Python projects, and
# - to bootstrap some other Python projects.
# The pyproject_installer is not needed (and cannot be needed) for its own
# build.
ifneq ($(strip $(COMPONENT_NAME)),pyproject_installer)
USERLAND_REQUIRED_PACKAGES.python += library/python/pyproject-installer
endif