From: Daniele Varrazzo Date: Mon, 2 Jan 2023 01:59:26 +0000 (+0000) Subject: ci: add in-tree build backend to avoid installing Cython if not needed X-Git-Tag: pool-3.2.0~144^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97e729ebf4e75655563039f7088f35236c526bf4;p=thirdparty%2Fpsycopg.git ci: add in-tree build backend to avoid installing Cython if not needed Only install Cython when there are pyx files to build. If there aren't, it means that the c files are already available and Cython is not required. Close #471. --- diff --git a/psycopg_c/MANIFEST.in b/psycopg_c/MANIFEST.in new file mode 100644 index 000000000..117298e0d --- /dev/null +++ b/psycopg_c/MANIFEST.in @@ -0,0 +1,3 @@ +# Include the build backend in the distributed files. +# It doesn't seem it can be specified in setup.cfg +include build_backend/*.py diff --git a/psycopg_c/build_backend/cython_backend.py b/psycopg_c/build_backend/cython_backend.py new file mode 100644 index 000000000..97bd2af88 --- /dev/null +++ b/psycopg_c/build_backend/cython_backend.py @@ -0,0 +1,38 @@ +""" +Build backend to build a Cython-based project only if needed. + +This backend adds a build dependency on Cython if pxd files are available, +otherwise it only relies on the c files to have been precompiled. +""" + +# Copyright (C) 2023 The Psycopg Team + +import os +from typing import Any, List + +import tomli +from setuptools import build_meta + + +def get_requires_for_build_wheel(config_settings: Any = None) -> List[str]: + if not os.path.exists("psycopg_c/_psycopg.pyx"): + # Cython files don't exist: we must be in a sdist and we can trust + # that the .c files we have packaged exist. + return [] + + # Cython files exists: we must be in a git checkout and we need Cython + # to build. Get the version from the pyproject itself to keep things in the + # same place. + with open("pyproject.toml", "rb") as f: + pyprj = tomli.load(f) + + rv: List[str] = pyprj["cython-backend"]["cython-requires"] + return rv + + +get_requires_for_build_sdist = get_requires_for_build_wheel + +# For the rest, behave like the rest of setuptoos.build_meta +prepare_metadata_for_build_wheel = build_meta.prepare_metadata_for_build_wheel +build_wheel = build_meta.build_wheel +build_sdist = build_meta.build_sdist diff --git a/psycopg_c/pyproject.toml b/psycopg_c/pyproject.toml index f0d7a3fe4..48958cef1 100644 --- a/psycopg_c/pyproject.toml +++ b/psycopg_c/pyproject.toml @@ -1,3 +1,13 @@ [build-system] -requires = ["setuptools>=49.2.0", "wheel>=0.37", "Cython>=3.0.0a11"] -build-backend = "setuptools.build_meta" +requires = ["setuptools >= 49.2.0", "wheel >= 0.37", "tomli >= 2.0.1"] + +# The cython_backend is a build backend adding a Cython dependency if the c +# source must be build from pxd files (when building from git checkout), and +# doesn't require Cython when needing to build from c files (when building +# from the sdist bundle). +build-backend = "cython_backend" +backend-path = ["build_backend"] + +[cython-backend] +# These packages are only installed if there are pyx files to compile. +cython-requires = ["Cython >= 3.0.0a11"] diff --git a/psycopg_c/setup.cfg b/psycopg_c/setup.cfg index 2c26ae675..9ee68c4e6 100644 --- a/psycopg_c/setup.cfg +++ b/psycopg_c/setup.cfg @@ -39,7 +39,6 @@ license_files = LICENSE.txt [options] python_requires = >= 3.7 -setup_requires = Cython >= 3.0.0a11 packages = find: zip_safe = False install_requires = diff --git a/tests/constraints.txt b/tests/constraints.txt index 5f03c00df..5c86d5a3a 100644 --- a/tests/constraints.txt +++ b/tests/constraints.txt @@ -33,6 +33,7 @@ sphinx-autodoc-typehints == 1.12.0 setuptools == 49.2.0 wheel == 0.37 Cython == 3.0.0a11 +tomli == 2.0.1 # Undeclared extras to "unblock" extra features shapely == 1.7.0