Skip to content

Commit 6d520c3

Browse files
committed
feat: compile the callgrind wrapper at build time
1 parent 4f45b41 commit 6d520c3

File tree

6 files changed

+59
-41
lines changed

6 files changed

+59
-41
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ celerybeat.pid
122122
# Environments
123123
.env
124124
.venv
125+
.venv.*
125126
env/
126127
venv/
127128
ENV/

pyproject.toml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
[build-system]
2-
requires = ["hatchling"]
3-
build-backend = "hatchling.build"
4-
51
[project.urls]
62
Homepage = "https://codspeed.io/"
73
Documentation = "https://docs.codspeed.io/"
@@ -35,11 +31,7 @@ classifiers = [
3531
]
3632
dependencies = [
3733
"cffi >= 1.17.1",
38-
# cffi doesn't automatically install setuptools with python 3.12+
39-
# cf https://github.com/python-cffi/cffi/releases/tag/v1.16.0
40-
"setuptools; python_full_version >= '3.12.0'",
4134
"pytest>=3.8",
42-
"filelock >= 3.12.2",
4335
"rich>=13.8.1",
4436
"importlib-metadata>=8.5.0; python_version < '3.10'",
4537
]
@@ -53,9 +45,17 @@ compat = [
5345
# "pytest-speed>=0.3.5",
5446
]
5547
test = ["pytest ~= 7.0", "pytest-cov ~= 4.0.0"]
48+
5649
[project.entry-points]
5750
pytest11 = { codspeed = "pytest_codspeed.plugin" }
5851

52+
[build-system]
53+
requires = ["setuptools >= 61", "cffi >= 1.17.1"]
54+
build-backend = "setuptools.build_meta"
55+
56+
[tool.setuptools.dynamic]
57+
version = { attr = "pytest_codspeed.__version__" }
58+
5959
[tool.hatch.envs.default]
6060
python = "3.11"
6161
features = ["lint", "test", "compat"]
@@ -67,12 +67,6 @@ features = ["test"]
6767
python = ["3.9", "3.10", "3.11", "3.12", "3.13"]
6868
features = ["compat", "test"]
6969

70-
[tool.hatch.version]
71-
path = "src/pytest_codspeed/__init__.py"
72-
73-
[tool.hatch.build.targets.sdist]
74-
include = ["/src"]
75-
7670
[tool.mypy]
7771
python_version = "3.12"
7872

setup.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import os
2+
import platform
3+
4+
from setuptools import setup
5+
6+
system = platform.system()
7+
current_arch = platform.machine()
8+
9+
IS_EXTENSION_REQUIRED = (
10+
system == "Linux"
11+
and current_arch
12+
in [
13+
"x86_64",
14+
"arm64",
15+
]
16+
or os.environ.get("PYTEST_CODSPEED_FORCE_EXTENSION") is not None
17+
)
18+
19+
setup(
20+
package_data={
21+
"pytest_codspeed": [
22+
"instruments/valgrind/_wrapper/*.h",
23+
"instruments/valgrind/_wrapper/*.c",
24+
]
25+
},
26+
cffi_modules=[
27+
"src/pytest_codspeed/instruments/valgrind/_wrapper/build.py:ffibuilder"
28+
]
29+
if IS_EXTENSION_REQUIRED
30+
else [],
31+
)

src/pytest_codspeed/instruments/valgrind/_wrapper/.gitignore

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

src/pytest_codspeed/instruments/valgrind/_wrapper/__init__.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,13 @@
11
from __future__ import annotations
22

3-
import os
43
from typing import TYPE_CHECKING
54

6-
from cffi import FFI # type: ignore
7-
from filelock import FileLock
8-
95
if TYPE_CHECKING:
106
from .wrapper import lib as LibType
117

12-
_wrapper_dir = os.path.dirname(os.path.abspath(__file__))
13-
14-
15-
def _get_ffi():
16-
ffi = FFI()
17-
with open(f"{_wrapper_dir}/wrapper.h") as f:
18-
ffi.cdef(f.read())
19-
ffi.set_source(
20-
"dist_callgrind_wrapper",
21-
'#include "wrapper.h"',
22-
sources=["wrapper.c"],
23-
)
24-
return ffi
25-
268

279
def get_lib() -> LibType:
2810
try:
29-
ffi = _get_ffi()
30-
build_lock = FileLock(f"{_wrapper_dir}/build.lock")
31-
with build_lock:
32-
ffi.compile(
33-
target="dist_callgrind_wrapper.*",
34-
tmpdir=_wrapper_dir,
35-
)
3611
from .dist_callgrind_wrapper import lib # type: ignore
3712

3813
return lib
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from pathlib import Path
2+
3+
from cffi import FFI # type: ignore
4+
5+
wrapper_dir = Path(__file__).parent
6+
7+
ffibuilder = FFI()
8+
9+
ffibuilder.cdef((wrapper_dir / "wrapper.h").read_text())
10+
11+
ffibuilder.set_source(
12+
"pytest_codspeed.instruments.valgrind._wrapper.dist_callgrind_wrapper",
13+
'#include "wrapper.h"',
14+
sources=["src/pytest_codspeed/instruments/valgrind/_wrapper/wrapper.c"],
15+
include_dirs=[str(wrapper_dir)],
16+
)
17+
18+
if __name__ == "__main__":
19+
ffibuilder.compile(verbose=True)

0 commit comments

Comments
 (0)