--- /dev/null
+"""
+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
[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"]