Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
165 lines (140 loc) · 5.66 KB
/
Copy path__init__.py
File metadata and controls
165 lines (140 loc) · 5.66 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
"""
import ctypes
import os
from pathlib import Path
import re
import sys
from textwrap import dedent
import platform
_HERE = Path(__file__).resolve().parent
__all__ = ["get_include_dir", "get_lib_dir", "get_library", "get_pkg_config",
"get_openblas_config"]
# Use importlib.metadata to single-source the version
try:
# importlib.metadata is present in Python 3.8 and later
import importlib.metadata as importlib_metadata
except ImportError:
# use the shim package importlib-metadata pre-3.8
import importlib_metadata as importlib_metadata
try:
# __package__ allows for the case where __name__ is "__main__"
__version__ = importlib_metadata.version(__package__ or __name__)
except importlib_metadata.PackageNotFoundError:
__version__ = "0.0.0"
def get_include_dir():
"""Return the include directory needed for compilation
"""
return os.path.join(_HERE, "include").replace("\\", "/")
def get_lib_dir():
"""Return the lib directory needed for linking
"""
return os.path.join(_HERE, "lib").replace("\\", "/")
def get_library(fullname=False):
"""Return the lib name needed for linking. If `fullname` then return the
filename of the library, otherwise return the link name.
"""
if sys.platform == "win32":
libs = [x for x in os.listdir(get_lib_dir()) if x.endswith(".lib")]
if fullname:
return libs[0]
else:
return os.path.splitext(libs[0])[0]
else:
libs = [x for x in os.listdir(get_lib_dir()) if x.startswith("libscipy_openblas")]
if fullname:
return libs[0]
else:
# remove the leading lib from libscipy_openblas*
return os.path.splitext(libs[0])[0][3:]
def get_pkg_config(use_preloading=False, use_prefix=False):
"""Return a multi-line string that, when saved to a file, can be used with
pkg-config for build systems like meson
If use_preloading is True then on posix the ``Libs:`` directive will not add
``f"-L{get_library()}" so that at runtime this module must be imported before
the target module
If use_prefix is True, then `pcfiledir` will be used and the file should be
stored in `dirname(__file__)/lib/pkgconfig` for use with pkgconf-pypi
"""
extralib = ""
if sys.platform == "win32":
machine = platform.machine().lower()
if machine != "arm64":
extralib = "-defaultlib:advapi32 -lgfortran -lquadmath"
libs_flags = f"-L${{libdir}} -l{get_library()}"
else:
extralib = f"-lm -lpthread -lgfortran -lquadmath -L${{libdir}} -l{get_library()}"
if use_preloading:
libs_flags = ""
else:
libs_flags = f"${{libdir}}/{get_library(fullname=True)} -Wl,-rpath,${{libdir}}"
cflags = "-DBLAS_SYMBOL_PREFIX=scipy_ -DBLAS_SYMBOL_SUFFIX=64_ -DHAVE_BLAS_ILP64 -DOPENBLAS_ILP64_NAMING_SCHEME"
if use_prefix:
libdir = "${pcfiledir}/../../lib"
incdir = "${pcfiledir}/../../include"
else:
# Don't use `$prefix` since the file may be located outside the package tree
libdir = get_lib_dir()
incdir = get_include_dir()
variables = {}
with open(f"{get_lib_dir()}/pkgconfig/scipy-openblas.pc") as pc_file:
for line in pc_file:
# The CMake build used by Windows ARM64 produces a Version: keyword but not
# a version= variable, so parse both kinds of line.
if match := re.fullmatch(r"(.+?)[:=](.*)", line.strip()):
variables[match[1].strip()] = match[2].strip()
return dedent(f"""\
libdir={libdir}
includedir={incdir}
openblas_config= {variables["openblas_config"]}
version={variables.get("version") or variables["Version"]}
extralib={extralib}
Name: openblas
Description: OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version
Version: ${{version}}
URL: https://github.com/xianyi/OpenBLAS
Libs: {libs_flags}
Libs.private: ${{extralib}}
Cflags: -I${{includedir}} {cflags}
""").replace("\\", "/")
if sys.platform == "win32":
os.add_dll_directory(get_lib_dir())
def write__distributor_init(target):
"""Accepts a Pathlib or string of a directory.
Write a pre-import file that will import scipy_openblas64 before
continuing to import the library. This will load OpenBLAS into the
executable's namespace and make the functions available for use.
"""
fname = os.path.join(target, "_distributor_init.py")
with open(fname, "wt", encoding="utf8") as fid:
fid.write(dedent(f"""\
'''
Helper to preload OpenBLAS from scipy_openblas64
'''
import scipy_openblas64
"""))
dll = None
def get_openblas_config():
"""Use ctypes to pull out the config string from the OpenBLAS library.
"""
# Keep the dll alive
global dll
if not dll:
lib_dir = get_lib_dir()
if sys.platform == "win32":
# Get libopenblas*.lib
libnames = [x for x in os.listdir(lib_dir) if x.endswith(".dll")]
else:
# Get openblas*
libnames = [x for x in os.listdir(lib_dir) if x.startswith("libscipy")]
dll = ctypes.CDLL(os.path.join(lib_dir, libnames[0]), ctypes.RTLD_GLOBAL)
openblas_config = dll.scipy_openblas_get_config64_
openblas_config.restype = ctypes.c_char_p
bytes = openblas_config()
return bytes.decode("utf8")
if "CIBUILDWHEEL" in os.environ and sys.platform == "android":
# We're cross-compiling, so we can't load the library.
pass
else:
# Import the DLL which will make the namespace available to NumPy/SciPy
get_openblas_config()