-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_hdf5_win.py
217 lines (185 loc) · 7.64 KB
/
get_hdf5_win.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Copied from h5py. Licensed under the BSD 3-Clause license.
# Copyright (c) 2008 Andrew Collette and contributors
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the
# distribution.
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""
Script for downloading and building HDF5 on Windows
This does not support MPI, nor non-Windows OSes
This script may not completely clean up after itself, it is designed to run in a
CI environment which thrown away each time
"""
from os import environ, makedirs, walk, getcwd, chdir
from os.path import join as pjoin, exists
from tempfile import TemporaryFile, TemporaryDirectory
from sys import exit, stderr
from shutil import copy
from glob import glob
from subprocess import run
from zipfile import ZipFile
import requests
HDF5_URL = "https://github.com/HDFGroup/hdf5/releases/download/hdf5_{dotted_version}/hdf5-{dashed_version}.zip"
ZLIB_ROOT = environ.get("ZLIB_ROOT")
CMAKE_CONFIGURE_CMD = [
"cmake",
"-DBUILD_SHARED_LIBS:BOOL=ON",
"-DCMAKE_BUILD_TYPE:STRING=RELEASE",
"-DHDF5_BUILD_CPP_LIB=OFF",
"-DHDF5_BUILD_HL_LIB=ON",
"-DHDF5_BUILD_TOOLS:BOOL=OFF",
"-DBUILD_TESTING:BOOL=OFF",
]
if ZLIB_ROOT:
CMAKE_CONFIGURE_CMD += [
"-DHDF5_ENABLE_Z_LIB_SUPPORT=ON",
f"-DZLIB_INCLUDE_DIR={ZLIB_ROOT}\\include",
f"-DZLIB_LIBRARY_RELEASE={ZLIB_ROOT}\\lib_release\\zlib.lib",
f"-DZLIB_LIBRARY_DEBUG={ZLIB_ROOT}\\lib_debug\\zlibd.lib",
]
CMAKE_BUILD_CMD = ["cmake", "--build"]
CMAKE_INSTALL_ARG = ["--target", "install", "--config", "Release"]
CMAKE_INSTALL_PATH_ARG = "-DCMAKE_INSTALL_PREFIX={install_path}"
CMAKE_HDF5_LIBRARY_PREFIX = ["-DHDF5_EXTERNAL_LIB_PREFIX=h5py_"]
REL_PATH_TO_CMAKE_CFG = "hdf5-{version}"
DEFAULT_VERSION = "1.14.3.3"
VSVERSION_TO_GENERATOR = {
"9": "Visual Studio 9 2008",
"10": "Visual Studio 10 2010",
"14": "Visual Studio 14 2015",
"15": "Visual Studio 15 2017",
"16": "Visual Studio 16 2019",
"9-64": "Visual Studio 9 2008 Win64",
"10-64": "Visual Studio 10 2010 Win64",
"14-64": "Visual Studio 14 2015 Win64",
"15-64": "Visual Studio 15 2017 Win64",
"16-64": "Visual Studio 16 2019",
"17-64": "Visual Studio 17 2022",
}
def get_dashed_version(version):
dotted_version = version
if len(version.split(".")) > 3:
dashed_version = "-".join(version.rsplit(".", maxsplit=1))
else:
dashed_version = version
return {"dotted_version": dotted_version, "dashed_version": dashed_version}
def download_hdf5(version, outfile):
file = HDF5_URL.format(**get_dashed_version(version))
print("Downloading " + file, file=stderr)
r = requests.get(file, stream=True)
try:
r.raise_for_status()
except requests.HTTPError:
print(
"Failed to download hdf5 version {version}, exiting".format(
version=version
),
file=stderr,
)
exit(1)
else:
for chunk in r.iter_content(chunk_size=None):
outfile.write(chunk)
def build_hdf5(version, hdf5_file, install_path, cmake_generator, use_prefix):
versions = get_dashed_version(version)
try:
with TemporaryDirectory() as hdf5_extract_path:
generator_args = (
["-G", cmake_generator] if cmake_generator is not None else []
)
prefix_args = CMAKE_HDF5_LIBRARY_PREFIX if use_prefix else []
with ZipFile(hdf5_file) as z:
z.extractall(hdf5_extract_path)
old_dir = getcwd()
with TemporaryDirectory() as new_dir:
chdir(new_dir)
cfg_cmd = (
CMAKE_CONFIGURE_CMD
+ [
get_cmake_install_path(install_path),
get_cmake_config_path(
versions["dashed_version"], hdf5_extract_path
),
]
+ generator_args
+ prefix_args
)
print(f"Configuring HDF5 version {versions["dotted_version"]}...")
print(" ".join(cfg_cmd), file=stderr)
run(cfg_cmd, check=True)
build_cmd = (
CMAKE_BUILD_CMD
+ [
".",
]
+ CMAKE_INSTALL_ARG
)
print(f"Building HDF5 version {version}...")
print(" ".join(build_cmd), file=stderr)
run(build_cmd, check=True)
print(
f"Installed HDF5 version {version} to {install_path}", file=stderr
)
chdir(old_dir)
except OSError as e:
if e.winerror == 145:
print("Hit the rmtree race condition, continuing anyway...", file=stderr)
else:
raise
for f in glob(pjoin(install_path, "bin/*.dll")):
copy(f, pjoin(install_path, "lib"))
def get_cmake_config_path(version, extract_point):
return pjoin(extract_point, REL_PATH_TO_CMAKE_CFG.format(version=version))
def get_cmake_install_path(install_path):
if install_path is not None:
return CMAKE_INSTALL_PATH_ARG.format(install_path=install_path)
return " "
def hdf5_install_cached(install_path):
if exists(pjoin(install_path, "lib", "hdf5.dll")):
return True
return False
def main():
install_path = environ.get("HDF5_DIR")
version = environ.get("HDF5_VERSION", DEFAULT_VERSION)
vs_version = environ.get("HDF5_VSVERSION")
use_prefix = True if environ.get("H5PY_USE_PREFIX") is not None else False
if install_path is not None:
if not exists(install_path):
makedirs(install_path)
if vs_version is not None:
cmake_generator = VSVERSION_TO_GENERATOR[vs_version]
else:
cmake_generator = None
if not hdf5_install_cached(install_path):
with TemporaryFile() as f:
download_hdf5(version, f)
build_hdf5(version, f, install_path, cmake_generator, use_prefix)
else:
print("using cached hdf5", file=stderr)
if install_path is not None:
print("hdf5 files: ", file=stderr)
for dirpath, dirnames, filenames in walk(install_path):
for file in filenames:
print(" * " + pjoin(dirpath, file))
if __name__ == "__main__":
main()