-
Notifications
You must be signed in to change notification settings - Fork 10
/
setup.py
53 lines (40 loc) · 1.73 KB
/
setup.py
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
"""Register the Cython extension."""
from __future__ import annotations
from pathlib import Path
from typing import ClassVar
from Cython.Build import cythonize
import numpy as np
from setuptools import Command, Extension, setup
BASE_DIR = Path(__file__).resolve().parent
PACKAGE_NAME = "mo_pack"
SRC_DIR = BASE_DIR / "src"
PACKAGE_DIR = SRC_DIR / PACKAGE_NAME
class CleanCython(Command): # type: ignore[misc]
"""Command for purging artifacts built by Cython."""
description = "Purge artifacts built by Cython"
user_options: ClassVar[list[tuple[str, str, str]]] = []
def initialize_options(self: CleanCython) -> None:
"""Set options/attributes/caches used by the command to default values."""
def finalize_options(self: CleanCython) -> None:
"""Set final values for all options/attributes used by the command."""
def run(self: CleanCython) -> None:
"""Execute the actions intended by the command."""
for path in PACKAGE_DIR.rglob("*"):
if path.suffix in (".pyc", ".pyo", ".c", ".so"):
msg = f"clean: removing file {path}"
print(msg) # noqa: T201
path.unlink()
# https://setuptools.pypa.io/en/latest/userguide/ext_modules.html
extension = Extension(
f"{PACKAGE_NAME}._packing",
[f"src/{PACKAGE_NAME}/_packing.pyx"],
include_dirs=[np.get_include()],
libraries=["mo_unpack"],
# https://cython.readthedocs.io/en/latest/src/userguide/migrating_to_cy30.html?highlight=NPY_NO_DEPRECATED_API#numpy-c-api
define_macros=[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")],
extra_compile_args=["-std=c99"],
)
setup(
cmdclass={"clean_cython": CleanCython},
ext_modules=cythonize(extension, language_level="3str"),
)