-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfigure_file.py
More file actions
392 lines (344 loc) · 13.5 KB
/
Copy pathconfigure_file.py
File metadata and controls
392 lines (344 loc) · 13.5 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
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import logging
import os
import re
import shlex
from dataclasses import dataclass
from typing import Dict, Iterable, List, Optional, Set
PLACEHOLDER_RE = re.compile(r"@([A-Za-z_][A-Za-z0-9_]*)@|\$\{([A-Za-z_][A-Za-z0-9_]*)\}")
CMAKE_DEFINE_RE = re.compile(r"^\s*#\s*cmakedefine(?:01)?\s+([A-Za-z_][A-Za-z0-9_]*)", re.MULTILINE)
SET_RE_TEMPLATE = r"(?:^|[^A-Za-z0-9_])(?:env_set|set)\s*\(\s*{name}(?:\s|\))"
@dataclass(frozen=True)
class ConfigureFile:
source: str
output: str
value_files: tuple[str, ...]
variables: Dict[str, str]
def parse_configure_vars(configure_vars: Optional[List[str]]) -> Dict[str, str]:
ret: Dict[str, str] = {}
for configure_var in configure_vars or []:
if "=" not in configure_var:
logging.fatal(
f"Configure variable {configure_var} is not in the form key=value"
)
raise SystemExit(-1)
key, value = configure_var.split("=", 1)
if not key:
logging.fatal(f"Configure variable {configure_var} has an empty key")
raise SystemExit(-1)
ret[key] = value
return ret
def _normalize_path(path: str) -> str:
return os.path.normpath(path).replace(os.path.sep, "/")
def _resolve_cmake_path(path: str, source_dir: str, binary_dir: str) -> str:
replacements = {
"${CMAKE_CURRENT_SOURCE_DIR}": source_dir,
"${CMAKE_SOURCE_DIR}": source_dir,
"${PROJECT_SOURCE_DIR}": source_dir,
"${CMAKE_CURRENT_BINARY_DIR}": binary_dir,
"${CMAKE_BINARY_DIR}": binary_dir,
"${PROJECT_BINARY_DIR}": binary_dir,
}
for key, value in replacements.items():
path = path.replace(key, value)
if not os.path.isabs(path):
path = os.path.join(source_dir, path)
return os.path.normpath(path)
def _path_tail(path: str) -> str:
match = re.search(r"\$\{[A-Za-z_][A-Za-z0-9_]*\}/(.+)", path)
if match:
return match.group(1)
return path
def _resolve_existing_source(path: str, source_dir: str, resolved: str) -> str:
if os.path.exists(resolved):
return resolved
tail = _path_tail(path)
matches = []
for current, dirs, files in os.walk(source_dir):
dirs[:] = [d for d in dirs if d != ".git"]
for filename in files:
candidate = os.path.join(current, filename)
if _normalize_path(candidate).endswith(_normalize_path(tail)):
matches.append(candidate)
if len(matches) == 1:
return os.path.normpath(matches[0])
if len(matches) > 1:
logging.fatal(
f"Could not resolve configure_file source {path}; "
f"found multiple matches: {', '.join(sorted(matches))}"
)
raise SystemExit(-1)
return resolved
def _parse_configure_file_args(line: str) -> Optional[List[str]]:
match = re.search(r"configure_file\s*\((.*)\)", line)
if not match:
return None
lexer = shlex.shlex(match.group(1), posix=True)
lexer.whitespace_split = True
lexer.commenters = "#"
args = list(lexer)
if len(args) < 2:
logging.fatal(f"Invalid configure_file entry: {line.rstrip()}")
raise SystemExit(-1)
return args
def _find_placeholders(template_path: str) -> tuple[Set[str], Set[str]]:
try:
with open(template_path, "r") as f:
contents = f.read()
except OSError as exc:
logging.fatal(f"Cannot read configure_file template {template_path}: {exc}")
raise SystemExit(-1)
required = {match.group(1) or match.group(2) for match in PLACEHOLDER_RE.finditer(contents)}
optional = {match.group(1) for match in CMAKE_DEFINE_RE.finditer(contents)}
return required, optional
def _iter_candidate_files(rootdir: str) -> Iterable[str]:
ignored_dirs = {".git", "bazel-bin", "bazel-out", "bazel-testlogs"}
for current, dirs, files in os.walk(rootdir):
dirs[:] = [d for d in dirs if d not in ignored_dirs]
for filename in files:
if filename.endswith((".cmake", ".txt", ".in", ".h.cmake")) or filename == "CMakeLists.txt":
yield os.path.join(current, filename)
def _find_value_files(
rootdir: str,
placeholders: Set[str],
template_path: str,
fail_on_missing: bool = True,
) -> tuple[str, ...]:
if not placeholders:
return ()
found: Dict[str, Set[str]] = {placeholder: set() for placeholder in placeholders}
for path in _iter_candidate_files(rootdir):
if os.path.normpath(path) == os.path.normpath(template_path):
continue
try:
with open(path, "r", errors="ignore") as f:
contents = f.read()
except OSError:
continue
for placeholder in placeholders:
if re.search(SET_RE_TEMPLATE.format(name=re.escape(placeholder)), contents):
found[placeholder].add(path)
missing = sorted([placeholder for placeholder, paths in found.items() if not paths])
if missing and fail_on_missing:
logging.fatal(
"Missing CMake definitions for configure_file placeholders "
f"{', '.join(missing)} in {template_path}"
)
raise SystemExit(-1)
files = set()
for paths in found.values():
files.update(paths)
return tuple(sorted(files))
def _current_cmake_dirs(filename: str, source_dir: str, binary_dir: str) -> tuple[str, str]:
source_dir_abs = os.path.abspath(source_dir)
binary_dir_abs = os.path.abspath(binary_dir)
cmake_dir = os.path.dirname(os.path.abspath(filename))
try:
if os.path.commonpath([source_dir_abs, cmake_dir]) != source_dir_abs:
return source_dir, binary_dir
except ValueError:
return source_dir, binary_dir
rel_dir = os.path.relpath(cmake_dir, source_dir_abs)
if rel_dir == ".":
return source_dir, binary_dir
return cmake_dir, os.path.normpath(os.path.join(binary_dir_abs, rel_dir))
def _configure_output_keys(output: str, binary_dir: str) -> Set[str]:
keys = {_normalize_path(output)}
rel_output = output
if os.path.isabs(output):
rel_output = os.path.relpath(output, binary_dir)
keys.add(_normalize_path(rel_output))
normalized_rel_output = _normalize_path(rel_output)
if normalized_rel_output.startswith("pregenerated/"):
keys.add(normalized_rel_output[len("pregenerated/") :])
else:
keys.add(f"pregenerated/{normalized_rel_output}")
return keys
def _uses_cmake_current_dir(path: str) -> bool:
return (
"${CMAKE_CURRENT_SOURCE_DIR}" in path
or "${CMAKE_CURRENT_BINARY_DIR}" in path
)
def _infer_current_dirs_from_source(
source_arg: str,
source: str,
source_dir: str,
binary_dir: str,
) -> tuple[str, str]:
source_abs = _normalize_path(os.path.abspath(source))
tail = _normalize_path(_path_tail(source_arg))
if tail and source_abs.endswith(f"/{tail}"):
source_parent = os.path.normpath(source_abs[: -len(tail)].rstrip("/"))
else:
source_parent = os.path.dirname(os.path.abspath(source))
source_dir_abs = os.path.abspath(source_dir)
binary_dir_abs = os.path.abspath(binary_dir)
try:
if os.path.commonpath([source_dir_abs, source_parent]) != source_dir_abs:
return source_dir, binary_dir
except ValueError:
return source_dir, binary_dir
rel_dir = os.path.relpath(source_parent, source_dir_abs)
if rel_dir == ".":
return source_dir, binary_dir
return source_parent, os.path.normpath(os.path.join(binary_dir_abs, rel_dir))
def _describe_configure_file(key: str, entry: ConfigureFile, binary_dir: str) -> str:
return (
f"key={key}, output={_normalize_path(entry.output)}, "
f"rel_output={_normalize_path(os.path.relpath(entry.output, binary_dir))}, "
f"source={_normalize_path(entry.source)}, "
f"value_files={[ _normalize_path(path) for path in entry.value_files ]}"
)
def parse_configure_files_list(
filename: Optional[str],
source_dir: str,
binary_dir: str,
configure_vars: Optional[Dict[str, str]] = None,
needed_outputs: Optional[Set[str]] = None,
) -> Dict[str, ConfigureFile]:
if not filename:
return {}
if not os.path.exists(filename):
logging.fatal(f"Configure files list {filename} does not exist")
raise SystemExit(-1)
ret: Dict[str, ConfigureFile] = {}
normalized_needed_outputs = (
{_normalize_path(output) for output in needed_outputs}
if needed_outputs is not None
else None
)
with open(filename, "r") as f:
lines = f.readlines()
for line in lines:
args = _parse_configure_file_args(line)
if args is None:
continue
current_source_dir, current_binary_dir = _current_cmake_dirs(
filename,
source_dir,
binary_dir,
)
source = _resolve_cmake_path(args[0], current_source_dir, current_binary_dir)
source = _resolve_existing_source(args[0], source_dir, source)
if _uses_cmake_current_dir(args[0]) and _uses_cmake_current_dir(args[1]):
current_source_dir, current_binary_dir = _infer_current_dirs_from_source(
args[0],
source,
source_dir,
binary_dir,
)
output = _resolve_cmake_path(args[1], current_source_dir, current_binary_dir)
output_keys = _configure_output_keys(output, binary_dir)
if normalized_needed_outputs is not None and not (
output_keys & normalized_needed_outputs
):
logging.info(
"Skipping configure_file entry for output %s because none of its "
"keys %s are needed; needed output count=%d",
_normalize_path(output),
sorted(output_keys),
len(normalized_needed_outputs),
)
continue
variables = configure_vars or {}
required_placeholders, optional_placeholders = _find_placeholders(source)
configured_variable_names = set(variables.keys())
required_placeholders -= configured_variable_names
optional_placeholders -= configured_variable_names
value_files = tuple(
sorted(
set(_find_value_files(source_dir, required_placeholders, source))
| set(
_find_value_files(
source_dir,
optional_placeholders,
source,
fail_on_missing=False,
)
)
)
)
entry = ConfigureFile(
source=source,
output=output,
value_files=value_files,
variables=variables,
)
ret[_normalize_path(output)] = entry
ret[_normalize_path(os.path.relpath(output, binary_dir))] = entry
logging.info(
"Registered configure_file output %s from source %s with keys %s",
_normalize_path(output),
_normalize_path(source),
sorted(output_keys),
)
if filename:
logging.info(
"Configured %d configure_file entries from %s",
len({entry.output for entry in ret.values()}),
filename,
)
return ret
def _log_configure_file_miss(
configure_files: Dict[str, ConfigureFile],
output: str,
binary_dir: str,
normalized: List[str],
) -> None:
logging.info(
"No configure_file matched requested output %s. Tried candidates: %s. "
"binary_dir=%s. configured entries=%d",
_normalize_path(output),
normalized,
_normalize_path(binary_dir),
len({entry.output for entry in configure_files.values()}),
)
for key, entry in sorted(configure_files.items()):
logging.info(
"Configured configure_file did not match %s: %s",
_normalize_path(output),
_describe_configure_file(key, entry, binary_dir),
)
def find_configure_file(
configure_files: Dict[str, ConfigureFile],
output: str,
binary_dir: str,
) -> Optional[ConfigureFile]:
if not configure_files:
logging.info(
"No configure_file entries are configured while looking for %s",
_normalize_path(output),
)
return None
candidates = [
output,
output.replace("<pregenerated>/", ""),
output.replace("pregenerated/", "", 1),
]
if not os.path.isabs(output):
candidates.append(os.path.join(binary_dir, output.replace("pregenerated/", "", 1)))
normalized = [_normalize_path(candidate) for candidate in candidates]
logging.info(
"Looking for configure_file match for %s using candidates %s",
_normalize_path(output),
normalized,
)
for candidate in normalized:
if candidate in configure_files:
logging.info(
"Matched configure_file for %s by exact candidate %s: %s",
_normalize_path(output),
candidate,
_describe_configure_file(candidate, configure_files[candidate], binary_dir),
)
return configure_files[candidate]
for key, entry in configure_files.items():
if any(key.endswith(candidate) or candidate.endswith(key) for candidate in normalized):
logging.info(
"Matched configure_file for %s by suffix key %s: %s",
_normalize_path(output),
key,
_describe_configure_file(key, entry, binary_dir),
)
return entry
_log_configure_file_miss(configure_files, output, binary_dir, normalized)
return None