Repository navigation
Expand file tree
/
Copy pathsetup.py
More file actions
71 lines (53 loc) · 2.56 KB
/
Copy pathsetup.py
File metadata and controls
71 lines (53 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Build the CFFI extension, with libphash built by its own CMake project first."""
import sys
import sysconfig
from pathlib import Path
from setuptools import setup
from setuptools.command.bdist_wheel import bdist_wheel
from setuptools.command.build_ext import build_ext
from setuptools.command.sdist import sdist
sys.path.insert(0, str(Path(__file__).resolve().parent / "tools"))
import sbom
import sdist as reproducible
from native import build_libphash, link_arguments
class BuildExt(build_ext):
"""Build libphash with CMake, then compile and link the CFFI extension against it.
The C build runs here rather than when setup.py is loaded, so that an sdist is made
without a compiler or CMake.
"""
def run(self) -> None:
"""Build libphash, then the extensions with its link arguments."""
args = link_arguments(build_libphash())
for ext in self.extensions:
ext.include_dirs.extend(args["include_dirs"])
ext.define_macros.extend(args["define_macros"])
ext.extra_objects.extend(args["extra_objects"])
ext.libraries.extend(args["libraries"])
ext.extra_link_args.extend(args["extra_link_args"])
super().run()
class BdistWheel(bdist_wheel):
"""Tag the wheel abi3 and add the SBOM of the linked libraries to it (PEP 770).
cffi compiles the module against the limited API wherever the interpreter has one,
so one wheel built on 3.10 serves every later CPython with the GIL. A free-threaded
interpreter has no stable ABI: its wheel keeps the tag of its version (cp314t).
"""
def finalize_options(self) -> None:
"""Ask for the abi3 tag, from the oldest supported Python, under the GIL."""
if not sysconfig.get_config_var("Py_GIL_DISABLED"):
self.py_limited_api = "cp310"
super().finalize_options()
def egg2dist(self, egginfo_path: str, distinfo_path: str) -> None:
"""Write the .dist-info directory, then its sboms/ entry."""
super().egg2dist(egginfo_path, distinfo_path)
sbom.write(Path(distinfo_path) / "sboms", self.distribution.get_version())
class Sdist(sdist):
"""Write the sdist so that the same sources give the same bytes."""
def make_distribution(self) -> None:
"""Make the archives, then rewrite each reproducibly."""
super().make_distribution()
for archive in self.archive_files:
reproducible.normalize(Path(archive))
setup(
cffi_modules=["tools/ffi.py:ffibuilder"],
cmdclass={"build_ext": BuildExt, "bdist_wheel": BdistWheel, "sdist": Sdist},
)