| 1 |
import glob |
| 2 |
import os |
| 3 |
import platform |
| 4 |
import shutil |
| 5 |
import sys |
| 6 |
import sysconfig |
| 7 |
import tarfile |
| 8 |
import textwrap |
| 9 |
import time |
| 10 |
import zipfile |
| 11 |
from tempfile import gettempdir, mkstemp |
| 12 |
from urllib.error import HTTPError |
| 13 |
from urllib.request import Request, urlopen |
| 14 |
|
| 15 |
OPENBLAS_V = '0.3.21.dev' |
| 16 |
OPENBLAS_LONG = 'v0.3.20-571-g3dec11c6' |
| 17 |
BASE_LOC = 'https://anaconda.org/multibuild-wheels-staging/openblas-libs' |
| 18 |
BASEURL = f'{BASE_LOC}/{OPENBLAS_LONG}/download' |
| 19 |
SUPPORTED_PLATFORMS = [ |
| 20 |
'linux-aarch64', |
| 21 |
'linux-x86_64', |
| 22 |
'musllinux-x86_64', |
| 23 |
'linux-i686', |
| 24 |
'linux-ppc64le', |
| 25 |
'linux-s390x', |
| 26 |
'win-amd64', |
| 27 |
'win-32', |
| 28 |
'macosx-x86_64', |
| 29 |
'macosx-arm64', |
| 30 |
] |
| 31 |
IS_32BIT = sys.maxsize < 2**32 |
| 32 |
|
| 33 |
|
| 34 |
def get_plat(): |
| 35 |
plat = sysconfig.get_platform() |
| 36 |
plat_split = plat.split("-") |
| 37 |
arch = plat_split[-1] |
| 38 |
if arch == "win32": |
| 39 |
plat = "win-32" |
| 40 |
elif arch in ["universal2", "intel"]: |
| 41 |
plat = f"macosx-{platform.uname().machine}" |
| 42 |
elif len(plat_split) > 2: |
| 43 |
plat = f"{plat_split[0]}-{arch}" |
| 44 |
assert plat in SUPPORTED_PLATFORMS, f'invalid platform {plat}' |
| 45 |
return plat |
| 46 |
|
| 47 |
|
| 48 |
def get_ilp64(): |
| 49 |
if os.environ.get("NPY_USE_BLAS_ILP64", "0") == "0": |
| 50 |
return None |
| 51 |
if IS_32BIT: |
| 52 |
raise RuntimeError("NPY_USE_BLAS_ILP64 set on 32-bit arch") |
| 53 |
return "64_" |
| 54 |
|
| 55 |
|
| 56 |
def get_manylinux(arch): |
| 57 |
if arch in ('i686'): |
| 58 |
default = '2010' |
| 59 |
else: |
| 60 |
default = '2014' |
| 61 |
ml_ver = os.environ.get("MB_ML_VER", default) |
| 62 |
|
| 63 |
assert ml_ver in ('2010', '2014', '_2_24'), f'invalid MB_ML_VER {ml_ver}' |
| 64 |
suffix = f'manylinux{ml_ver}_{arch}.tar.gz' |
| 65 |
return suffix |
| 66 |
|
| 67 |
|
| 68 |
def get_musllinux(arch): |
| 69 |
musl_ver = "1_1" |
| 70 |
suffix = f'musllinux_{musl_ver}_{arch}.tar.gz' |
| 71 |
return suffix |
| 72 |
|
| 73 |
|
| 74 |
def get_linux(arch): |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
try: |
| 79 |
from packaging.tags import sys_tags |
| 80 |
tags = list(sys_tags()) |
| 81 |
plat = tags[0].platform |
| 82 |
except ImportError: |
| 83 |
|
| 84 |
plat = 'manylinux' |
| 85 |
|
| 86 |
v = sysconfig.get_config_var('HOST_GNU_TYPE') or '' |
| 87 |
if 'musl' in v: |
| 88 |
plat = 'musllinux' |
| 89 |
|
| 90 |
if 'manylinux' in plat: |
| 91 |
return get_manylinux(arch) |
| 92 |
elif 'musllinux' in plat: |
| 93 |
return get_musllinux(arch) |
| 94 |
|
| 95 |
|
| 96 |
def download_openblas(target, plat, ilp64): |
| 97 |
osname, arch = plat.split("-") |
| 98 |
fnsuffix = {None: "", "64_": "64_"}[ilp64] |
| 99 |
filename = '' |
| 100 |
headers = {'User-Agent': |
| 101 |
('Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 ; ' |
| 102 |
'(KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.3')} |
| 103 |
suffix = None |
| 104 |
if osname == "linux": |
| 105 |
suffix = get_linux(arch) |
| 106 |
typ = 'tar.gz' |
| 107 |
elif plat == 'macosx-x86_64': |
| 108 |
suffix = 'macosx_10_9_x86_64-gf_c469a42.tar.gz' |
| 109 |
typ = 'tar.gz' |
| 110 |
elif plat == 'macosx-arm64': |
| 111 |
suffix = 'macosx_11_0_arm64-gf_5272328.tar.gz' |
| 112 |
typ = 'tar.gz' |
| 113 |
elif osname == 'win': |
| 114 |
if plat == "win-32": |
| 115 |
suffix = 'win32-gcc_8_3_0.zip' |
| 116 |
else: |
| 117 |
suffix = 'win_amd64-gcc_10_3_0.zip' |
| 118 |
typ = 'zip' |
| 119 |
|
| 120 |
if not suffix: |
| 121 |
return None |
| 122 |
filename = f'{BASEURL}/openblas{fnsuffix}-{OPENBLAS_LONG}-{suffix}' |
| 123 |
req = Request(url=filename, headers=headers) |
| 124 |
|
| 125 |
for _ in range(3): |
| 126 |
try: |
| 127 |
time.sleep(1) |
| 128 |
response = urlopen(req) |
| 129 |
break |
| 130 |
except HTTPError: |
| 131 |
print(f'Could not download "{filename}"', file=sys.stderr) |
| 132 |
raise |
| 133 |
|
| 134 |
length = response.getheader('content-length') |
| 135 |
if response.status != 200: |
| 136 |
print(f'Could not download "{filename}"', file=sys.stderr) |
| 137 |
return None |
| 138 |
print(f"Downloading {length} from {filename}", file=sys.stderr) |
| 139 |
data = response.read() |
| 140 |
print("Saving to file", file=sys.stderr) |
| 141 |
with open(target, 'wb') as fid: |
| 142 |
fid.write(data) |
| 143 |
return typ |
| 144 |
|
| 145 |
|
| 146 |
def setup_openblas(plat=get_plat(), ilp64=get_ilp64()): |
| 147 |
''' |
| 148 |
Download and setup an openblas library for building. If successful, |
| 149 |
the configuration script will find it automatically. |
| 150 |
|
| 151 |
Returns |
| 152 |
------- |
| 153 |
msg : str |
| 154 |
path to extracted files on success, otherwise indicates what went wrong |
| 155 |
To determine success, do ``os.path.exists(msg)`` |
| 156 |
''' |
| 157 |
_, tmp = mkstemp() |
| 158 |
if not plat: |
| 159 |
raise ValueError('unknown platform') |
| 160 |
typ = download_openblas(tmp, plat, ilp64) |
| 161 |
if not typ: |
| 162 |
return '' |
| 163 |
osname, arch = plat.split("-") |
| 164 |
if osname == 'win': |
| 165 |
if not typ == 'zip': |
| 166 |
return f'expecting to download zipfile on windows, not {typ}' |
| 167 |
return unpack_windows_zip(tmp) |
| 168 |
else: |
| 169 |
if not typ == 'tar.gz': |
| 170 |
return 'expecting to download tar.gz, not %s' % str(typ) |
| 171 |
return unpack_targz(tmp) |
| 172 |
|
| 173 |
|
| 174 |
def unpack_windows_zip(fname): |
| 175 |
with zipfile.ZipFile(fname, 'r') as zf: |
| 176 |
|
| 177 |
lib = [x for x in zf.namelist() if OPENBLAS_LONG in x and |
| 178 |
x.endswith('a') and not x.endswith('dll.a') and |
| 179 |
not x.endswith('dev.a')] |
| 180 |
if not lib: |
| 181 |
return 'could not find libopenblas_%s*.a ' \ |
| 182 |
'in downloaded zipfile' % OPENBLAS_LONG |
| 183 |
if get_ilp64() is None: |
| 184 |
target = os.path.join(gettempdir(), 'openblas.a') |
| 185 |
else: |
| 186 |
target = os.path.join(gettempdir(), 'openblas64_.a') |
| 187 |
with open(target, 'wb') as fid: |
| 188 |
fid.write(zf.read(lib[0])) |
| 189 |
return target |
| 190 |
|
| 191 |
|
| 192 |
def unpack_targz(fname): |
| 193 |
target = os.path.join(gettempdir(), 'openblas') |
| 194 |
if not os.path.exists(target): |
| 195 |
os.mkdir(target) |
| 196 |
with tarfile.open(fname, 'r') as zf: |
| 197 |
|
| 198 |
prefix = os.path.commonpath(zf.getnames()) |
| 199 |
extract_tarfile_to(zf, target, prefix) |
| 200 |
return target |
| 201 |
|
| 202 |
|
| 203 |
def extract_tarfile_to(tarfileobj, target_path, archive_path): |
| 204 |
"""Extract TarFile contents under archive_path/ to target_path/""" |
| 205 |
|
| 206 |
target_path = os.path.abspath(target_path) |
| 207 |
|
| 208 |
def get_members(): |
| 209 |
for member in tarfileobj.getmembers(): |
| 210 |
if archive_path: |
| 211 |
norm_path = os.path.normpath(member.name) |
| 212 |
if norm_path.startswith(archive_path + os.path.sep): |
| 213 |
member.name = norm_path[len(archive_path)+1:] |
| 214 |
else: |
| 215 |
continue |
| 216 |
|
| 217 |
dst_path = os.path.abspath(os.path.join(target_path, member.name)) |
| 218 |
if os.path.commonpath([target_path, dst_path]) != target_path: |
| 219 |
|
| 220 |
continue |
| 221 |
|
| 222 |
yield member |
| 223 |
|
| 224 |
tarfileobj.extractall(target_path, members=get_members()) |
| 225 |
|
| 226 |
|
| 227 |
def make_init(dirname): |
| 228 |
''' |
| 229 |
Create a _distributor_init.py file for OpenBlas |
| 230 |
''' |
| 231 |
with open(os.path.join(dirname, '_distributor_init.py'), 'w') as fid: |
| 232 |
fid.write(textwrap.dedent(""" |
| 233 |
''' |
| 234 |
Helper to preload windows dlls to prevent dll not found errors. |
| 235 |
Once a DLL is preloaded, its namespace is made available to any |
| 236 |
subsequent DLL. This file originated in the numpy-wheels repo, |
| 237 |
and is created as part of the scripts that build the wheel. |
| 238 |
''' |
| 239 |
import os |
| 240 |
import glob |
| 241 |
if os.name == 'nt': |
| 242 |
# convention for storing / loading the DLL from |
| 243 |
# numpy/.libs/, if present |
| 244 |
try: |
| 245 |
from ctypes import WinDLL |
| 246 |
basedir = os.path.dirname(__file__) |
| 247 |
except: |
| 248 |
pass |
| 249 |
else: |
| 250 |
libs_dir = os.path.abspath(os.path.join(basedir, '.libs')) |
| 251 |
DLL_filenames = [] |
| 252 |
if os.path.isdir(libs_dir): |
| 253 |
for filename in glob.glob(os.path.join(libs_dir, |
| 254 |
'*openblas*dll')): |
| 255 |
# NOTE: would it change behavior to load ALL |
| 256 |
# DLLs at this path vs. the name restriction? |
| 257 |
WinDLL(os.path.abspath(filename)) |
| 258 |
DLL_filenames.append(filename) |
| 259 |
if len(DLL_filenames) > 1: |
| 260 |
import warnings |
| 261 |
warnings.warn("loaded more than 1 DLL from .libs:" |
| 262 |
"\\n%s" % "\\n".join(DLL_filenames), |
| 263 |
stacklevel=1) |
| 264 |
""")) |
| 265 |
|
| 266 |
|
| 267 |
def test_setup(plats): |
| 268 |
''' |
| 269 |
Make sure all the downloadable files exist and can be opened |
| 270 |
''' |
| 271 |
def items(): |
| 272 |
""" yields all combinations of arch, ilp64 |
| 273 |
""" |
| 274 |
for plat in plats: |
| 275 |
yield plat, None |
| 276 |
osname, arch = plat.split("-") |
| 277 |
if arch not in ('i686', 'arm64', '32'): |
| 278 |
yield plat, '64_' |
| 279 |
if osname == "linux" and arch in ('i686', 'x86_64'): |
| 280 |
oldval = os.environ.get('MB_ML_VER', None) |
| 281 |
os.environ['MB_ML_VER'] = '1' |
| 282 |
yield plat, None |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
if oldval: |
| 287 |
os.environ['MB_ML_VER'] = oldval |
| 288 |
else: |
| 289 |
os.environ.pop('MB_ML_VER') |
| 290 |
|
| 291 |
errs = [] |
| 292 |
for plat, ilp64 in items(): |
| 293 |
osname, _ = plat.split("-") |
| 294 |
if plat not in plats: |
| 295 |
continue |
| 296 |
target = None |
| 297 |
try: |
| 298 |
try: |
| 299 |
target = setup_openblas(plat, ilp64) |
| 300 |
except Exception as e: |
| 301 |
print(f'Could not setup {plat} with ilp64 {ilp64}, ') |
| 302 |
print(e) |
| 303 |
errs.append(e) |
| 304 |
continue |
| 305 |
if not target: |
| 306 |
raise RuntimeError(f'Could not setup {plat}') |
| 307 |
print(target) |
| 308 |
if osname == 'win': |
| 309 |
if not target.endswith('.a'): |
| 310 |
raise RuntimeError("Not .a extracted!") |
| 311 |
else: |
| 312 |
files = glob.glob(os.path.join(target, "lib", "*.a")) |
| 313 |
if not files: |
| 314 |
raise RuntimeError("No lib/*.a unpacked!") |
| 315 |
finally: |
| 316 |
if target is not None: |
| 317 |
if os.path.isfile(target): |
| 318 |
os.unlink(target) |
| 319 |
else: |
| 320 |
shutil.rmtree(target) |
| 321 |
if errs: |
| 322 |
raise errs[0] |
| 323 |
|
| 324 |
|
| 325 |
def test_version(expected_version, ilp64=get_ilp64()): |
| 326 |
""" |
| 327 |
Assert that expected OpenBLAS version is |
| 328 |
actually available via scikit-misc |
| 329 |
""" |
| 330 |
|
| 331 |
import ctypes |
| 332 |
|
| 333 |
import skmisc.loess |
| 334 |
|
| 335 |
dll = ctypes.CDLL(skmisc.loess._loess.__file__) |
| 336 |
if ilp64 == "64_": |
| 337 |
get_config = dll.openblas_get_config64_ |
| 338 |
else: |
| 339 |
get_config = dll.openblas_get_config |
| 340 |
get_config.restype = ctypes.c_char_p |
| 341 |
res = get_config() |
| 342 |
print('OpenBLAS get_config returned', str(res)) |
| 343 |
if not expected_version: |
| 344 |
expected_version = OPENBLAS_V |
| 345 |
check_str = b'OpenBLAS %s' % expected_version.encode() |
| 346 |
print(check_str) |
| 347 |
assert check_str in res, f'{expected_version} not found in {res}' |
| 348 |
if ilp64: |
| 349 |
assert b"USE64BITINT" in res |
| 350 |
else: |
| 351 |
assert b"USE64BITINT" not in res |
| 352 |
|
| 353 |
|
| 354 |
if __name__ == '__main__': |
| 355 |
import argparse |
| 356 |
parser = argparse.ArgumentParser( |
| 357 |
description='Download and expand an OpenBLAS archive for this ' |
| 358 |
'architecture') |
| 359 |
parser.add_argument('--test', nargs='*', default=None, |
| 360 |
help='Test different architectures. "all", or any of ' |
| 361 |
f'{SUPPORTED_PLATFORMS}') |
| 362 |
parser.add_argument('--write-init', nargs=1, |
| 363 |
metavar='OUT_SKMISC_DIR', |
| 364 |
help='Write distribution init to named dir') |
| 365 |
parser.add_argument('--check_version', nargs='?', default='', |
| 366 |
help='Check provided OpenBLAS version string ' |
| 367 |
'against available OpenBLAS') |
| 368 |
args = parser.parse_args() |
| 369 |
if args.check_version != '': |
| 370 |
test_version(args.check_version) |
| 371 |
elif args.write_init: |
| 372 |
make_init(args.write_init[0]) |
| 373 |
elif args.test is None: |
| 374 |
print(setup_openblas()) |
| 375 |
else: |
| 376 |
if len(args.test) == 0 or 'all' in args.test: |
| 377 |
test_setup(SUPPORTED_PLATFORMS) |
| 378 |
else: |
| 379 |
test_setup(args.test) |