-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathsetup.py
More file actions
116 lines (97 loc) · 3.28 KB
/
setup.py
File metadata and controls
116 lines (97 loc) · 3.28 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright (C) 2008-2025 The scikit-sparse developers:
#
# 2008 David Cournapeau <[email protected]>
# 2009-2015 Nathaniel Smith <[email protected]>
# 2010 Dag Sverre Seljebotn <[email protected]>
# 2014 Leon Barrett <[email protected]>
# 2015 Yuri <[email protected]>
# 2016-2017 Antony Lee <[email protected]>
# 2016 Alex Grigorievskiy <[email protected]>
# 2016-2017 Joscha Reimer <[email protected]>
# 2021- Justin Ellis <[email protected]>
# 2022- Aaron Johnson <[email protected]>
# 2025- Bernard Roesler <[email protected]>
import os
import subprocess
import sys
from pathlib import Path
from setuptools import Extension, setup
def get_numpy_include():
"""Get the include directory for NumPy."""
try:
import numpy as np # noqa: PLC0415
return np.get_include()
except ImportError:
return []
INCLUDE_DIRS = []
LIBRARY_DIRS = []
numpy_include = get_numpy_include()
if numpy_include:
INCLUDE_DIRS.append(numpy_include)
# Check user SuiteSparse directories first
user_include_dir = os.getenv("SUITESPARSE_INCLUDE_DIR")
user_library_dir = os.getenv("SUITESPARSE_LIBRARY_DIR")
if user_include_dir:
INCLUDE_DIRS.append(user_include_dir)
if user_library_dir:
LIBRARY_DIRS.append(user_library_dir)
# Check if suitesparse is installed via conda
conda_prefix = os.getenv("CONDA_PREFIX")
if conda_prefix:
if os.name == "nt": # Windows
conda_include = Path(conda_prefix) / "Library" / "include" / "suitesparse"
conda_lib = Path(conda_prefix) / "Library" / "lib"
else:
conda_include = Path(conda_prefix) / "include" / "suitesparse"
conda_lib = Path(conda_prefix) / "lib"
if conda_include.is_dir():
INCLUDE_DIRS.append(str(conda_include))
if conda_lib.is_dir():
LIBRARY_DIRS.append(str(conda_lib))
# Check if suitesparse is installed via homebrew
try:
homebrew_prefix = (
subprocess.run(
"readlink -f $(brew --prefix suitesparse)",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
check=True, # raise an error if command fails
)
.stdout.decode()
.strip()
)
brew_include = Path(homebrew_prefix) / "include" / "suitesparse"
brew_lib = Path(homebrew_prefix) / "lib"
if brew_include.is_dir():
INCLUDE_DIRS.append(str(brew_include))
if brew_lib.is_dir():
LIBRARY_DIRS.append(str(brew_lib))
except Exception:
pass
# Check system-wide directories
INCLUDE_DIRS.append(str(Path(sys.prefix) / "include"))
INCLUDE_DIRS.append("/usr/include/suitesparse") # Linux default path
extension_names = [
"cholmod",
"amd",
"btf",
"camd",
"colamd",
"ccolamd",
"klu",
"spqr",
"umfpack",
]
extensions = [
Extension(
f"sksparse.{name}",
[f"src/sksparse/{name}.pyx"],
include_dirs=INCLUDE_DIRS,
library_dirs=LIBRARY_DIRS,
libraries=[name] if name != "spqr" else ["cholmod", "spqr"],
)
for name in extension_names
]
# No need to call "cythonize" here. Rely on pyproject.toml.
setup(ext_modules=extensions)