-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild_utilities.py
215 lines (179 loc) · 6.46 KB
/
build_utilities.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
from contextlib import contextmanager as _contextmanager
from enum import Enum as _Enum
from typing import Any as _Any
from typing import Dict as _Dict
from typing import List as _List
from typing import Tuple as _Tuple
from typing import Union as _Union
import os as _os
import shutil as _shutil
import subprocess as _subprocess
def underscore_join(*args: _Any) -> str:
return "_".join(str(arg) for arg in args if arg)
def _quiet_print(quiet: bool, *msg: _Any) -> None:
if quiet:
pass
else:
print(*msg)
def create_directory(path: str, quiet: bool = False) -> None:
_quiet_print(quiet, "[LegionSolversBuild] Creating directory", path)
assert not _os.path.exists(path)
_os.mkdir(path)
def remove_file(path: str, quiet: bool = False) -> None:
if _os.path.exists(path):
_quiet_print(quiet, "[LegionSolversBuild] Removing file", path)
assert _os.path.isfile(path)
_os.remove(path)
else:
_quiet_print(quiet, "[LegionSolversBuild] File", path, "does not exist")
def remove_directory(path: str, quiet: bool = False) -> None:
if _os.path.exists(path):
_quiet_print(quiet, "[LegionSolversBuild] Removing directory", path)
assert _os.path.isdir(path)
_shutil.rmtree(path)
else:
_quiet_print(quiet, "[LegionSolversBuild] Directory", path, "does not exist")
@_contextmanager
def change_directory(path: str, quiet: bool = False):
prev_path: str = _os.getcwd()
_quiet_print(quiet, "[LegionSolversBuild] Changing directory to", path)
_os.chdir(path)
try:
yield
finally:
_quiet_print(quiet, "[LegionSolversBuild] Changing directory to", prev_path)
_os.chdir(prev_path)
def run(*command: str, check: bool = True, quiet: bool = False) -> None:
_quiet_print(quiet, "[LegionSolversBuild] Running command", " ".join(command))
_subprocess.run(command, check=check)
def download(url: str, quiet: bool = False) -> None:
_quiet_print(quiet, "[LegionSolversBuild] Downloading file", url)
run("wget", url)
def clone(
url: str,
branch: str = "",
path: str = "",
commit: str = "",
quiet: bool = False,
) -> None:
if branch:
if path:
_quiet_print(
quiet,
"[LegionSolversBuild] Cloning branch",
branch,
"of repository",
url,
"into directory",
path,
)
run("git", "clone", "--branch", branch, url, path)
if commit:
with change_directory(path):
_quiet_print(
quiet,
"[LegionSolversBuild] Checking out commit",
commit,
"in directory",
path,
)
run("git", "checkout", commit)
else:
_quiet_print(
quiet,
"[LegionSolversBuild] Cloning branch",
branch,
"of repository",
url,
)
run("git", "clone", "--branch", branch, url)
assert not commit
else:
if path:
_quiet_print(
quiet,
"[LegionSolversBuild] Cloning repository",
url,
"into directory",
path,
)
run("git", "clone", url, path)
if commit:
with change_directory(path):
_quiet_print(
quiet,
"[LegionSolversBuild] Checking out commit",
commit,
"in directory",
path,
)
run("git", "checkout", commit)
else:
_quiet_print(quiet, "[LegionSolversBuild] Cloning repository", url)
run("git", "clone", url)
assert not commit
CMakeDefines = _Dict[str, _Union[bool, int, str]]
def cmake(
build_path: str = "build",
defines: CMakeDefines = {},
build: bool = True,
test: bool = False,
install: bool = True,
cmake_cmd: _Tuple[str, ...] = ("cmake", ".."),
) -> None:
create_directory(build_path)
print("[LegionSolversBuild] Running CMake in directory", build_path)
with change_directory(build_path):
cmd: _List[str] = list(cmake_cmd)
for key, value in defines.items():
if isinstance(value, bool):
cmd.append("-D{0}={1}".format(key, "ON" if value else "OFF"))
else:
cmd.append("-D{0}={1}".format(key, value))
run(*cmd)
if build:
# CMake's automatic CPU core count detection is unreliable on
# some HPC clusters, launching an unreasonably large number of
# parallel build jobs. We use Python's os.cpu_count() instead.
cores = _os.cpu_count()
if cores is None:
print("[LegionSolversBuild] WARNING:",
"Could not automatically determine CPU core count.")
cores = 8 # reasonably conservative guess
print("[LegionSolversBuild] Building with", cores, "cores.")
run("cmake", "--build", ".", "--parallel", str(cores), check=True)
if test:
run("ctest", check=False)
if install:
run("make", "install", check=True)
################################################################################
class Machines(_Enum):
UNKNOWN = 0
SAPLING = 1
PIZDAINT = 2
LASSEN = 3
SUMMIT = 4
def _getenv(name: str) -> str:
result = _os.getenv(name)
if result is None:
raise RuntimeError("Environment variable " + name + " does not exist")
return result
MACHINE: Machines = {
"SAPLING": Machines.SAPLING,
"PIZDAINT": Machines.PIZDAINT,
"LASSEN": Machines.LASSEN,
"SUMMIT": Machines.SUMMIT,
}.get(_getenv("LEGION_SOLVERS_MACHINE").upper(), Machines.UNKNOWN)
if MACHINE == Machines.UNKNOWN:
print("[LegionSolversBuild] WARNING: Unknown machine")
GASNET_CONDUITS: _Dict[Machines, str] = {
Machines.UNKNOWN: "mpi",
Machines.SAPLING: "ibv",
Machines.PIZDAINT: "mpi", # TODO: don't remember which network Piz Daint uses
Machines.LASSEN: "ibv",
Machines.SUMMIT: "ibv",
}
SCRATCH_DIR: str = _getenv("LEGION_SOLVERS_SCRATCH_DIR")
LIB_PREFIX: str = _getenv("LEGION_SOLVERS_LIB_PREFIX")
assert _os.path.isdir(SCRATCH_DIR)
assert _os.path.isdir(LIB_PREFIX)