python: Add proper support for pyproject.toml-based builds
authorJeffery To <jeffery.to@gmail.com>
Fri, 17 Feb 2023 17:13:22 +0000 (01:13 +0800)
committerJosef Schlehofer <pepe.schlehofer@gmail.com>
Tue, 31 Oct 2023 09:03:43 +0000 (10:03 +0100)
This removes the changes made in
61f202c0170785addbbc449e4de61cc5886f0833 and adds actual support for
pyproject.toml-based (PEP 517) builds of Python packages.

Packages can force the use of the old build process by setting
PYTHON3_PKG_FORCE_DISTUTILS_SETUP:=1; this should only be a temporary
workaround until the package can be updated/fixed to use the new build
process.

Signed-off-by: Jeffery To <jeffery.to@gmail.com>
(cherry picked from commit 5156c0c82bcb18481bf588ba4b1ff67c12422a80)

lang/python/python3-host.mk
lang/python/python3-package.mk
lang/python/python3/Makefile
lang/python/setup.py.shim [deleted file]

index 54f9bde1e1a44f8706bffbe956e91b45b6682a2b..b272c28ca1ab23d928c42480260c1a58b9a376bc 100644 (file)
@@ -98,12 +98,6 @@ HOST_PYTHON3_PIP_VARS:= \
        PIP_CONFIG_FILE=/dev/null \
        PIP_DISABLE_PIP_VERSION_CHECK=1
 
-define SetupPyShim
-       if [ -f $(1)/pyproject.toml ] && [ ! -f $(1)/setup.py ] ; then \
-               $(CP) $(python3_mk_path)setup.py.shim $(1)setup.py ; \
-       fi
-endef
-
 # Multiple concurrent pip processes can lead to errors or unexpected results: https://github.com/pypa/pip/issues/2361
 # $(1) => packages to install
 define HostPython3/PipInstall
index f2336b8fcf1569c26a12bbfde188c3b390c4af04..d5d4f8a0f8fc204b041a151d3c1bf1904fac48f1 100644 (file)
@@ -56,18 +56,6 @@ define Python3/Run
        $(HOST_PYTHON3_BIN) $(2)
 endef
 
-# $(1) => build subdir
-# $(2) => additional arguments to setup.py
-# $(3) => additional variables
-define Python3/ModSetup
-       $(INSTALL_DIR) $(PKG_INSTALL_DIR)/$(PYTHON3_PKG_DIR)
-       $(call SetupPyShim,$(PKG_BUILD_DIR)/$(strip $(1)))
-       $(call Python3/Run, \
-               $(PKG_BUILD_DIR)/$(strip $(1)), \
-               setup.py $(2), \
-               $(3) PY_PKG_VERSION=$(PKG_VERSION))
-endef
-
 define Python3/FixShebang
        $(SED) "1"'!'"b;s,^#"'!'".*python.*,#"'!'"/usr/bin/python3," -i --follow-symlinks $(1)
 endef
@@ -189,10 +177,26 @@ endef
 
 # Py3Build
 
-PYTHON3_PKG_SETUP_DIR ?=
-PYTHON3_PKG_SETUP_GLOBAL_ARGS ?=
-PYTHON3_PKG_SETUP_ARGS ?= --single-version-externally-managed
-PYTHON3_PKG_SETUP_VARS ?=
+PYTHON3_PKG_BUILD?=1
+PYTHON3_PKG_FORCE_DISTUTILS_SETUP?=
+
+PYTHON3_PKG_SETUP_DIR?=
+PYTHON3_PKG_SETUP_GLOBAL_ARGS?=
+PYTHON3_PKG_SETUP_ARGS?=--single-version-externally-managed
+PYTHON3_PKG_SETUP_VARS?=
+
+PYTHON3_PKG_BUILD_CONFIG_SETTINGS?=
+PYTHON3_PKG_BUILD_VARS?=$(PYTHON3_PKG_SETUP_VARS)
+PYTHON3_PKG_BUILD_ARGS?=
+PYTHON3_PKG_BUILD_PATH?=$(PYTHON3_PKG_SETUP_DIR)
+
+PYTHON3_PKG_INSTALL_VARS?=
+
+PYTHON3_PKG_WHEEL_NAME?=$(subst -,_,$(if $(PYPI_SOURCE_NAME),$(PYPI_SOURCE_NAME),$(PKG_NAME)))
+PYTHON3_PKG_WHEEL_VERSION?=$(PKG_VERSION)
+
+PYTHON3_PKG_BUILD_DIR?=$(PKG_BUILD_DIR)/$(PYTHON3_PKG_BUILD_PATH)
+
 
 PYTHON3_PKG_HOST_PIP_INSTALL_ARGS = \
        $(foreach req,$(HOST_PYTHON3_PACKAGE_BUILD_DEPENDS), \
@@ -224,21 +228,58 @@ define Py3Build/InstallBuildDepends
        )
 endef
 
-define Py3Build/Compile/Default
+define Py3Build/Compile/Distutils
        $(call Py3Build/InstallBuildDepends)
-       $(call Python3/ModSetup, \
-               $(PYTHON3_PKG_SETUP_DIR), \
-               $(PYTHON3_PKG_SETUP_GLOBAL_ARGS) \
-               install --prefix="/usr" --root="$(PKG_INSTALL_DIR)" \
-               $(PYTHON3_PKG_SETUP_ARGS), \
+       $(INSTALL_DIR) $(PKG_INSTALL_DIR)/$(PYTHON3_PKG_DIR)
+       $(call Python3/Run, \
+               $(PKG_BUILD_DIR)/$(strip $(PYTHON3_PKG_SETUP_DIR)), \
+               setup.py \
+                       $(PYTHON3_PKG_SETUP_GLOBAL_ARGS) \
+                       install \
+                       --prefix="/usr" \
+                       --root="$(PKG_INSTALL_DIR)" \
+                       $(PYTHON3_PKG_SETUP_ARGS) \
+                       , \
                $(PYTHON3_PKG_SETUP_VARS) \
        )
 endef
 
-Py3Build/Configure=$(Py3Build/Configure/Default)
+define Py3Build/Compile/Default
+       $(call Py3Build/InstallBuildDepends)
+       $(call Python3/Run, \
+               $(PKG_BUILD_DIR), \
+               -m build \
+                       --no-isolation \
+                       --outdir "$(PYTHON3_PKG_BUILD_DIR)"/openwrt-build \
+                       --wheel \
+                       $(foreach setting,$(PYTHON3_PKG_BUILD_CONFIG_SETTINGS),--config-setting=$(setting)) \
+                       $(PYTHON3_PKG_BUILD_ARGS) \
+                       "$(PYTHON3_PKG_BUILD_DIR)" \
+                       , \
+               $(PYTHON3_PKG_BUILD_VARS) \
+       )
+endef
+
+define Py3Build/Install/Default
+       $(call Python3/Run, \
+               $(PKG_BUILD_DIR), \
+               -m installer \
+                       --destdir "$(PKG_INSTALL_DIR)" \
+                       --no-compile-bytecode \
+                       --prefix /usr \
+                       "$(PYTHON3_PKG_BUILD_DIR)"/openwrt-build/$(PYTHON3_PKG_WHEEL_NAME)-$(PYTHON3_PKG_WHEEL_VERSION)-*.whl \
+                       , \
+               $(PYTHON3_PKG_INSTALL_VARS) \
+       )
+endef
+
 Py3Build/Compile=$(Py3Build/Compile/Default)
+Py3Build/Install=$(Py3Build/Install/Default)
 
-PYTHON3_PKG_BUILD ?= 1
+ifeq ($(strip $(PYTHON3_PKG_FORCE_DISTUTILS_SETUP)),1)
+  Py3Build/Compile=$(Py3Build/Compile/Distutils)
+  Py3Build/Install:=:
+endif
 
 ifeq ($(strip $(PYTHON3_PKG_BUILD)),1)
   ifeq ($(PY3),stdlib)
@@ -246,4 +287,5 @@ ifeq ($(strip $(PYTHON3_PKG_BUILD)),1)
   endif
   Hooks/Configure/Post+=Py3Build/CheckHostPipVersionMatch
   Build/Compile=$(Py3Build/Compile)
+  Build/Install=$(Py3Build/Install)
 endif
index 12cd70037d77da39947b4da68fbd177a2cec7a53..903e548b901f9db2db15e291627e495354b7e3bc 100644 (file)
@@ -11,7 +11,7 @@ include $(TOPDIR)/rules.mk
 include ../python3-version.mk
 
 PKG_NAME:=python3
-PKG_RELEASE:=1
+PKG_RELEASE:=2
 PKG_VERSION:=$(PYTHON3_VERSION).$(PYTHON3_VERSION_MICRO)
 
 PKG_SOURCE:=Python-$(PKG_VERSION).tar.xz
@@ -42,7 +42,7 @@ PKG_CONFIG_DEPENDS:= \
        CONFIG_PACKAGE_python3-pkg-resources \
        CONFIG_PACKAGE_python3-setuptools CONFIG_PACKAGE_python3-pip
 
-PKG_BUILD_DEPENDS:=bluez python3/host
+PKG_BUILD_DEPENDS:=bluez python3/host python-build/host python-installer/host python-wheel/host
 HOST_BUILD_DEPENDS:=bzip2/host libffi/host
 
 include $(INCLUDE_DIR)/host-build.mk
diff --git a/lang/python/setup.py.shim b/lang/python/setup.py.shim
deleted file mode 100644 (file)
index b309abc..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-import os
-import setuptools
-
-# FIXME: see about getting rid of PY_PKG_VERSION asap when setuptools handles this correctly
-if __name__ == "__main__":
-    setuptools.setup(version=os.environ['PY_PKG_VERSION'])