-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsetup.py
More file actions
83 lines (68 loc) · 2.65 KB
/
setup.py
File metadata and controls
83 lines (68 loc) · 2.65 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
72
73
74
75
76
77
78
79
80
81
82
83
"""Setup script for building Cython extensions for the `animate` package.
The script performs PETSc detection and populates Extension objects based on the local
PETSc installation (via petsctools/petsc4py).
"""
import os
from dataclasses import dataclass, field
import numpy as np
import petsc4py
import petsctools
from Cython.Build import cythonize
from setuptools import Extension, find_packages, setup
@dataclass
class ExternalDependency:
"""Courtesy of Firedrake. See
https://github.com/firedrakeproject/firedrake/blob/main/setup.py
"""
include_dirs: list[str] = field(default_factory=list, init=True)
extra_compile_args: list[str] = field(default_factory=list, init=True)
libraries: list[str] = field(default_factory=list, init=True)
library_dirs: list[str] = field(default_factory=list, init=True)
extra_link_args: list[str] = field(default_factory=list, init=True)
runtime_library_dirs: list[str] = field(default_factory=list, init=True)
def __add__(self, other):
combined = {}
for f in self.__dataclass_fields__.keys():
combined[f] = getattr(self, f) + getattr(other, f)
return self.__class__(**combined)
def keys(self):
return self.__dataclass_fields__.keys()
def __getitem__(self, key):
try:
return getattr(self, key)
except AttributeError as attr_err:
raise KeyError(f"Key {key} not present") from attr_err
def extensions():
"""Returns a list of Cython extensions to be compiled."""
# Define external dependencies
mpi_ = ExternalDependency(
extra_compile_args=petsctools.get_petscvariables()["MPICC_SHOW"].split()[1:],
)
numpy_ = ExternalDependency(include_dirs=[np.get_include()])
petsc_dir = petsctools.get_petsc_dir()
petsc_dirs = [petsc_dir, os.path.join(petsc_dir, petsctools.get_petsc_arch())]
petsc_includes = [petsc4py.get_include()] + [
os.path.join(d, "include") for d in petsc_dirs
]
petsc_ = ExternalDependency(
libraries=["petsc"],
include_dirs=petsc_includes,
library_dirs=[os.path.join(petsc_dirs[-1], "lib")],
runtime_library_dirs=[os.path.join(petsc_dirs[-1], "lib")],
)
# Define Cython extensions
cython_list = [
Extension(
name="animate.cython.numbering",
language="c",
sources=[os.path.join("animate", "cython", "numbering.pyx")],
**(mpi_ + petsc_ + numpy_),
)
]
return cythonize(cython_list)
if __name__ == "__main__":
# Run the setup function to build the Cython extensions
setup(
packages=find_packages(),
ext_modules=extensions(),
)