]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
ci: add in-tree build backend to avoid installing Cython if not needed
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 2 Jan 2023 01:59:26 +0000 (01:59 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Mon, 2 Jan 2023 16:49:21 +0000 (16:49 +0000)
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.

psycopg_c/MANIFEST.in [new file with mode: 0644]
psycopg_c/build_backend/cython_backend.py [new file with mode: 0644]
psycopg_c/pyproject.toml
psycopg_c/setup.cfg
tests/constraints.txt

diff --git a/psycopg_c/MANIFEST.in b/psycopg_c/MANIFEST.in
new file mode 100644 (file)
index 0000000..117298e
--- /dev/null
@@ -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 (file)
index 0000000..97bd2af
--- /dev/null
@@ -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
index f0d7a3fe48c840ae1090549070e4def06ecf62f2..48958cef1af2d586033cf84c6f47a0f6bb3e39ce 100644 (file)
@@ -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"]
index 2c26ae675c1999e1294dfe6b6f6fe4ca2cd03a70..9ee68c4e61af9716125a5ebc53c631a287068528 100644 (file)
@@ -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 =
index 5f03c00df27c7f190721ff3539ed8f6b4dea9b08..5c86d5a3a0c0e08b27ae2c307d9ac1190ed0ae2b 100644 (file)
@@ -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