-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_build.py
352 lines (274 loc) · 9.12 KB
/
post_build.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import re
import json
import fileinput
import subprocess
from pathlib import Path
from tempfile import TemporaryDirectory
from os.path import commonprefix
# PATCH 1: replace relative includes in header files
def post_build(builder, ext):
patch_include(builder, ext)
generate_enums(builder, ext)
generate_structs(builder, ext)
generate_config(builder, ext)
def patch_include(builder, ext):
'Replaces #include instances in header files that use <> with "" for relative includes'
install_dir = builder.get_install_dir(ext) + "/include"
for path in Path(install_dir).rglob("*.h"):
with fileinput.FileInput(str(path), inplace=True, backup=".bak") as fp:
for fline in fp:
line = str(fline)
if line.strip().startswith("#include"):
include = line.split()[1]
if include[0] == "<" and include[-1] == ">":
include = include[1:-1]
if (path.parents[0] / include).exists():
print(line.replace(f"<{include}>", f'"{include}"'), end="")
continue
print(line, end="")
# PATCH 2: generates enums.py
ENUM_OUTPUT = """
"List of QUDA enumerations"
# NOTE: This file is automathically generated by setup.py
# DO NOT CHANGE MANUALLY but reinstall the package if needed
from .enum import Enum
"""
ENUM_CLASS = """
class {name}(Enum):
\"""
{docs}
\"""
_prefix = "{prefix}"
_suffix = "{suffix}"
_values = {values}
"""
REPLACE = {
"{": " { ",
"}": " } ",
",": " , ",
"=": " = ",
";": " ; ",
"[": " [ ",
"]": " ] ",
"*": " * ",
}
PATTERN = re.compile("|".join((re.escape(k) for k in REPLACE.keys())), flags=re.DOTALL)
get_words = lambda line: PATTERN.sub(lambda x: REPLACE[x.group(0)], line).split()
def commonsuffix(words):
"Finds common suffix between list of words"
reverse = lambda word: word[::-1]
words = list(map(reverse, words))
return commonprefix(words)[::-1]
def parse_enum(lines):
from cppyy import gbl
assert lines[0].startswith("typedef enum")
# getting comments:
comments = {}
for i, line in enumerate(lines):
if "//" in line:
line, comment = line.split("//")
line = line.strip()
comment = comment.strip()
lines[i] = line
key = get_words(line)[0]
comments[key] = comment
words = get_words(" ".join(lines))
assert words[-1] == ";"
name = words[-2]
enums = []
for i in range(len(words)):
if words[i] in ["{", ","]:
enums.append(words[i + 1])
prefix = commonprefix(enums).lower()
suffix = commonsuffix(enums).lower()
clean = (
lambda word: word[len(prefix) : -len(suffix)] if suffix else word[len(prefix) :]
)
comments = {
clean(key.lower()): " # " + val
for key, val in comments.items()
if key in enums
}
# Generating values
values = {clean(key.lower()): int(getattr(gbl, key)) for key in enums}
# Generating docs
docs = [f"{key} = {val}{comments.get(key,'')}" for key, val in values.items()]
docs = "\n ".join(docs)
return ENUM_CLASS.format(
name=name, prefix=prefix, suffix=suffix, values=values, docs=docs
)
def generate_enums(builder, ext):
"Reads enum_quda.h and returns the content of enums.py"
from cppyy import include
package_dir = builder.get_install_dir(ext)
header = package_dir + "/include/enum_quda.h"
include(header)
header = open(header)
lines = header.readlines()
header.close()
# packing groups of enums and then calling parse_enum
pack = []
out = ENUM_OUTPUT
for line in lines:
line = line.strip()
if line.startswith("typedef enum"):
pack.append(line)
elif pack:
pack.append(line)
if pack and line.endswith(";"):
out += parse_enum(pack)
pack = []
filename = package_dir + "/enums.py"
with open(filename, "w") as fp:
fp.write(out)
try:
from black import format_file_in_place, Mode, Path, WriteBack
format_file_in_place(Path(filename), False, Mode(), write_back=WriteBack.YES)
except ImportError:
pass
# PATCH 3: generates structs.py
STRUCT_OUTPUT = """
"List of QUDA parameter structures"
# NOTE: This file is automathically generated by setup.py
# DO NOT CHANGE MANUALLY but reinstall the package if needed
from .enums import *
from .struct import Struct
"""
STRUCT_CLASS = """
class {name}(Struct):
\"""{docs}
\"""
_types = {types}
"""
def get_name_type(line):
words = get_words(line)
assert words[-1] == ";"
if "[" in words:
par = words.index("[")
key = words[par - 1]
typ = words[: par - 1] + words[par:-1]
assert typ[-1] == "]"
else:
key = words[-2]
typ = words[:-2]
return " ".join(typ), key
def parse_struct(lines):
assert lines[0].startswith("typedef struct")
assert "}" in lines[-1]
types = {}
comment = ""
comments = {}
comment_region = False
for i, line in enumerate(lines[1:]):
line = line.strip()
if comment_region:
if "*/" in line:
tmp, line = line.split("*/")
comment_region = False
else:
tmp = line
comment += "\n" + tmp.strip()
if comment_region:
continue
if "//" in line:
line, tmp = line.split("//")
comment += tmp
elif "/*" in line:
line, tmp = line.split("/*")
comment += tmp
if "*/" not in comment:
comment_region = True
if "}" in line:
words = get_words(line)
assert ";" == words[-1]
assert "}" == words[0]
assert len(words) == 3
name = words[1]
elif ";" in line:
comment = comment.strip("/*<- \n")
line = line.strip()
typ, key = get_name_type(line)
types[key] = typ
comments[key] = comment.replace("\n", "\n ")
comment = ""
docs = f"\n {name} struct:\n" + "\n".join(
f" - {key} : {val}" for key, val in comments.items()
)
return STRUCT_CLASS.format(name=name, types=types, docs=docs)
def generate_structs(builder, ext):
"Reads enum_quda.h and returns the content of enums.py"
package_dir = builder.get_install_dir(ext)
header = open(package_dir + "/include/quda.h")
lines = header.readlines()
header.close()
# packing groups of enums and then calling parse_enum
pack = []
out = STRUCT_OUTPUT
for line in lines:
line = line.strip()
if line.startswith("typedef struct"):
pack.append(line)
elif pack:
pack.append(line)
if pack and "}" in line and line.endswith(";"):
out += parse_struct(pack)
pack = []
filename = package_dir + "/structs.py"
with open(filename, "w") as fp:
fp.write(out)
try:
from black import format_file_in_place, Mode, Path, WriteBack
format_file_in_place(Path(filename), False, Mode(), write_back=WriteBack.YES)
except ImportError:
pass
# Patch 4: Generate config.py
CONFIG_OUTPUT = """
"List of QUDA CMake Config Options"
# NOTE: This file is automathically generated by setup.py
# DO NOT CHANGE MANUALLY but reinstall the package if needed
"""
CMAKE = """
cmake_minimum_required(VERSION ${CMAKE_VERSION})
project(QUDA)
get_cmake_property(_before VARIABLES)
include(%s/lib/cmake/QUDA/QUDAConfig.cmake)
get_cmake_property(_after VARIABLES)
foreach (_name ${_after})
if((NOT _name IN_LIST _before) AND (NOT _name STREQUAL "_before"))
message(STATUS "VAR ${_name} = ${${_name}}")
endif()
endforeach()
"""
def generate_config(builder, ext):
"Generates the config.py file based on QUDAConfig.cmake"
package_dir = builder.get_install_dir(ext)
filename = package_dir + "/config.py"
with TemporaryDirectory() as temp_dir:
with open(temp_dir + "/CMakeLists.txt", "w") as cmake_file:
cmake_file.write(CMAKE % package_dir)
out = subprocess.check_output(
["cmake", "."], cwd=temp_dir, stderr=subprocess.DEVNULL
)
lines = tuple(
line.split()[2:]
for line in out.decode().split("\n")
if line.startswith("-- VAR")
)
assert all((len(line) >= 2 and line[1] == "=" for line in lines))
def parse(lines):
if not lines:
return None
if len(lines) == 1:
line = lines[0]
if line in ["TRUE", "1", "ON"]:
return True
if line in ["FALSE", "0", "OFF"]:
return False
return json.dumps(" ".join(lines))
text = "\n".join(f"{line[0]} = {parse(line[2:])}" for line in lines)
open(filename, "w").write(CONFIG_OUTPUT + text)
try:
from black import format_file_in_place, Mode, Path, WriteBack
format_file_in_place(Path(filename), False, Mode(), write_back=WriteBack.YES)
except ImportError:
pass