1
1
# A build script for poetry that adds the rust extension.
2
2
3
+ import itertools
3
4
import os
4
5
from typing import Any , Dict
5
6
7
+ from packaging .specifiers import SpecifierSet
6
8
from setuptools_rust import Binding , RustExtension
7
9
8
10
@@ -14,10 +16,27 @@ def build(setup_kwargs: Dict[str, Any]) -> None:
14
16
target = "synapse.synapse_rust" ,
15
17
path = cargo_toml_path ,
16
18
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.
17
21
py_limited_api = True ,
18
22
# We force always building in release mode, as we can't tell the
19
23
# difference between using `poetry` in development vs production.
20
24
debug = False ,
21
25
)
22
26
setup_kwargs .setdefault ("rust_extensions" , []).append (extension )
23
27
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