Skip to content

Commit ea3bda7

Browse files
committed
Add new build file to build the cython extension and update
pyproject.toml to use it. Run 'poetry install' to build and install rencode locally, then 'poetry run pytest' to run tests. Running 'poetry build' should build both sdist and wheel.
1 parent 14a0342 commit ea3bda7

3 files changed

Lines changed: 73 additions & 103 deletions

File tree

build.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from __future__ import annotations
2+
3+
import os
4+
import shutil
5+
6+
from pathlib import Path
7+
8+
from Cython.Build import cythonize
9+
from setuptools import Distribution
10+
from setuptools import Extension
11+
from setuptools.command.build_ext import build_ext
12+
13+
14+
COMPILE_ARGS = ["-march=native", "-O3", "-msse", "-msse2", "-mfma", "-mfpmath=sse"]
15+
LINK_ARGS: list[str] = []
16+
INCLUDE_DIRS: list[str] = []
17+
LIBRARIES: list[str] = []
18+
19+
20+
def build() -> None:
21+
extensions = [
22+
Extension(
23+
"*",
24+
["rencode/*.pyx"],
25+
extra_compile_args=COMPILE_ARGS,
26+
extra_link_args=LINK_ARGS,
27+
include_dirs=INCLUDE_DIRS,
28+
libraries=LIBRARIES,
29+
)
30+
]
31+
ext_modules = cythonize(
32+
extensions,
33+
include_path=INCLUDE_DIRS,
34+
compiler_directives={"binding": True, "language_level": 3},
35+
)
36+
37+
distribution = Distribution({
38+
"name": "rencode",
39+
"ext_modules": ext_modules
40+
})
41+
42+
cmd = build_ext(distribution)
43+
cmd.ensure_finalized()
44+
cmd.run()
45+
46+
# Copy built extensions back to the project
47+
for output in cmd.get_outputs():
48+
output = Path(output)
49+
relative_extension = output.relative_to(cmd.build_lib)
50+
51+
shutil.copyfile(output, relative_extension)
52+
mode = os.stat(relative_extension).st_mode
53+
mode |= (mode & 0o444) >> 2
54+
os.chmod(relative_extension, mode)
55+
56+
57+
if __name__ == "__main__":
58+
build()

pyproject.toml

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,29 @@ version = "1.0.7"
44
description = "rencode is an object serialization library similar to bencode from the Bittorrent project."
55
authors = ["Andrew Resch <andrewresch@gmail.com>"]
66
license = "GPLv3"
7+
include = [{path = "rencode/*.so", format = "wheel"}, {path = "rencode/*.c", format = "sdist"}]
8+
packages = [{include = "rencode"}]
9+
exclude = ['rencode/*.c']
10+
11+
[tool.poetry.build]
12+
script = "build.py"
713

814
[tool.poetry.dependencies]
915
python = "^3.9"
10-
Cython = ">=3"
16+
cython = ">=3"
1117
setuptools = "^80.4.0"
1218

1319
[tool.poetry.group.dev.dependencies]
1420
black = "^21.9b0"
1521
cython = "^3.1.0"
1622
pytest = "^8.3.5"
1723

24+
25+
[tool.poetry.group.build.dependencies]
26+
setuptools = "^80.9.0"
27+
cython = "^3.1.1"
28+
poetry-core = "^2.1.3"
29+
1830
[build-system]
19-
requires = ["setuptools>=42", "wheel", "Cython"]
20-
build-backend = "setuptools.build_meta"
31+
requires = ["poetry-core", "setuptools", "cython"]
32+
build-backend = "poetry.core.masonry.api"

setup.py

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)