11# A build script for poetry that adds the rust extension.
22
3+ import itertools
34import os
45from typing import Any , Dict
56
7+ from packaging .specifiers import SpecifierSet
68from setuptools_rust import Binding , RustExtension
79
810
@@ -14,10 +16,27 @@ def build(setup_kwargs: Dict[str, Any]) -> None:
1416 target = "synapse.synapse_rust" ,
1517 path = cargo_toml_path ,
1618 binding = Binding .PyO3 ,
19+ # This flag is a no-op in the latest versions. Instead, we need to
20+ # specify this in the `bdist_wheel` config below.
1721 py_limited_api = True ,
1822 # We force always building in release mode, as we can't tell the
1923 # difference between using `poetry` in development vs production.
2024 debug = False ,
2125 )
2226 setup_kwargs .setdefault ("rust_extensions" , []).append (extension )
2327 setup_kwargs ["zip_safe" ] = False
28+
29+ # We lookup the minimum supported python version by looking at
30+ # `python_requires` (e.g. ">=3.9.0,<4.0.0") and finding the first python
31+ # version that matches. We then convert that into the `py_limited_api` form,
32+ # e.g. cp39 for python 3.9.
33+ py_limited_api : str
34+ python_bounds = SpecifierSet (setup_kwargs ["python_requires" ])
35+ for minor_version in itertools .count (start = 8 ):
36+ if f"3.{ minor_version } .0" in python_bounds :
37+ py_limited_api = f"cp3{ minor_version } "
38+ break
39+
40+ setup_kwargs .setdefault ("options" , {}).setdefault ("bdist_wheel" , {})[
41+ "py_limited_api"
42+ ] = py_limited_api
0 commit comments