-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.py
More file actions
executable file
·344 lines (297 loc) · 10.9 KB
/
Copy pathparser.py
File metadata and controls
executable file
·344 lines (297 loc) · 10.9 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
#!/usr/bin/env python3
import argparse
import logging
import os
import shutil
import subprocess
import sys
import time
from typing import Dict, List, Optional, Set
from build import CONFIGURE_FILE_TOOL_PATH
from cc_import_parse import parseCCImports
from configure_file import parse_configure_files_list, parse_configure_vars
from ninjabuild import genBazelBuildFiles, getBuildTargets
def parse_manually_generated(manually_generated: List[str]) -> Dict[str, str]:
ret = {}
if not manually_generated:
return ret
for e in manually_generated:
if "=" not in e:
logging.fatal(
f"Manually generated dependency {e} is not in the form key=value"
)
sys.exit(-1)
v = e.split("=")
ret[v[0]] = v[1]
return ret
# FIXME: This should be a parameter
# if relative it's relative to the rootdir
BUILD_CUSTOMIZATION_DIRECTORY = "bazel/cpp"
TOOL_SOURCE_DIRECTORY = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
"tools",
)
def _build_post_treatment_command(script: str, build_file: str) -> List[str]:
if script.endswith(".py"):
return [sys.executable, script, build_file]
return [script, build_file]
def _top_level_build_dir(rootdir: str, prefix: str) -> str:
if prefix in ("", "."):
return rootdir
return os.path.join(rootdir, prefix)
def _configure_vars_from_cli_paths(
rootdir: str,
binary_dir: str,
prefix: str,
configure_vars: Optional[List[str]],
) -> Dict[str, str]:
ret = {
"CMAKE_SOURCE_DIR": os.path.abspath(_top_level_build_dir(rootdir, prefix)),
"CMAKE_BINARY_DIR": os.path.abspath(binary_dir),
}
ret.update(parse_configure_vars(configure_vars))
return ret
def _copy_if_different(source: str, destination: str) -> None:
source_abs = os.path.abspath(source)
destination_abs = os.path.abspath(destination)
if source_abs == destination_abs:
return
shutil.copy2(source_abs, destination_abs)
def install_configure_file_tool(rootdir: str, prefix: str) -> None:
tool_destination = os.path.join(
_top_level_build_dir(rootdir, prefix),
CONFIGURE_FILE_TOOL_PATH,
)
os.makedirs(os.path.dirname(tool_destination), exist_ok=True)
_copy_if_different(
os.path.join(TOOL_SOURCE_DIRECTORY, "render_configure_file.py"),
tool_destination,
)
def run_post_treatments(
build_file: str, post_treatments: Optional[List[str]]
) -> None:
if not post_treatments:
return
for script in post_treatments:
if not os.path.exists(script):
logging.fatal(f"Post-treatment script {script} does not exist")
sys.exit(-1)
cmd = _build_post_treatment_command(script, build_file)
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
continue
logging.error(f"Post-treatment failed for {build_file} with script {script}")
if result.stdout:
logging.error(result.stdout.rstrip())
if result.stderr:
logging.error(result.stderr.rstrip())
sys.exit(result.returncode or -1)
def _add_needed_configure_output(outputs: Set[str], path: Optional[str], binary_dir: str) -> None:
if not path:
return
normalized = os.path.normpath(path).replace(os.path.sep, "/")
outputs.add(normalized)
if os.path.isabs(path):
rel = os.path.relpath(path, binary_dir)
normalized = os.path.normpath(rel).replace(os.path.sep, "/")
outputs.add(normalized)
if normalized.startswith("pregenerated/"):
outputs.add(normalized[len("pregenerated/") :])
else:
outputs.add(f"pregenerated/{normalized}")
def collect_needed_configure_outputs(top_levels: List[object], binary_dir: str) -> Set[str]:
outputs: Set[str] = set()
seen: Set[str] = set()
def visit(target: object) -> None:
name = getattr(target, "name", None)
if name in seen:
return
if name is not None:
seen.add(name)
_add_needed_configure_output(outputs, name, binary_dir)
_add_needed_configure_output(outputs, getattr(target, "shortName", None), binary_dir)
for include, include_dir in getattr(target, "includes", set()):
_add_needed_configure_output(outputs, include, binary_dir)
if include_dir is not None:
_add_needed_configure_output(outputs, os.path.join(include_dir, include), binary_dir)
build = getattr(target, "producedby", None)
if build is None:
return
for dep in build.getInputs():
visit(dep)
for dep in build.depends:
if dep.depsAreVirtual():
continue
visit(dep)
for top_level in top_levels:
visit(top_level)
return outputs
def main(argv=None):
logging.basicConfig(
level=logging.INFO,
format="%(name)s - %(levelname)s - %(message)s - Line: %(lineno)d",
)
parser = argparse.ArgumentParser(description="Process Ninja build input file.")
parser.add_argument("filename", type=str, help="Ninja build input file")
parser.add_argument("rootdir", type=str, help="Root directory")
parser.add_argument(
"-m",
"--manually_generated",
action="append",
help="Manually generated dependencies",
)
parser.add_argument(
"--remap",
action="append",
help="Which path are remopped to which path",
)
parser.add_argument(
"-p",
"--prefix",
default="",
help="Initial directory prefix for generated Bazel BUILD files",
)
parser.add_argument(
"--imports",
action="append",
help="A file containing a list of cc_imports to be added to the BUILD files",
)
parser.add_argument(
"--top-level-target",
action="append",
help="The name of top level target(s) to be generated, if not specified all targets will be generated",
)
parser.add_argument(
"--post-treatment",
action="append",
help="Executable run after each generated BUILD.bazel file; receives the file path to rewrite",
)
parser.add_argument(
"--configure_files_list",
help="File containing CMake configure_file(...) lines used to generate pregenerated files",
)
parser.add_argument(
"--configure_var",
action="append",
help="CMake configure_file variable in the form key=value",
)
args = parser.parse_args(argv)
filename = args.filename
rootdir = args.rootdir
manually_generated = parse_manually_generated(args.manually_generated)
if not filename or not rootdir:
logging.fatal(
"Ninja build input file and/or folder where the code is located is/are missing"
)
sys.exit(-1)
with open(filename, "r") as f:
raw_ninja = f.readlines()
raw_imports = []
location = ""
if len(args.imports or []) > 0:
for i in args.imports:
if not os.path.exists(i):
logging.fatal(f"Imports file {i} does not exist")
sys.exit(-1)
p = os.path.dirname(i)
# Skip the trailing /
loc = p.replace(rootdir, "")[1:]
if location != "" and loc != location:
raise Exception(
"Not all the cc_imports files are in the same place current: {location} new: {loc}"
)
location = loc
with open(i, "r") as f:
raw_imports.extend(f.readlines())
start = time.time()
cc_imports = parseCCImports(raw_imports, location)
end = time.time()
print(f"Time to parse cc_imports: {end - start}", file=sys.stdout)
start = time.time()
compilerIncludes = getCompilerIncludesDir()
end = time.time()
print(f"Time to getCompilerIncludes: {end - start}", file=sys.stdout)
start = time.time()
prefix = ""
if args.prefix != "":
if not os.path.exists(f"{rootdir}{os.path.sep}{args.prefix}"):
logging.fatal(f"Prefix directory {args.prefix} does not exist in {rootdir}")
sys.exit(-1)
if not os.path.isdir(f"{rootdir}{os.path.sep}{args.prefix}"):
logging.fatal(f"Prefix directory {args.prefix} is not a directory")
sys.exit(-1)
# Should we have a special case for "." ?
prefix = f"{args.prefix}{os.path.sep}"
cur_dir = os.path.dirname(os.path.abspath(filename))
logging.info("Parsing ninja file and buildTargets")
if not rootdir.endswith(os.path.sep):
rootdir = f"{rootdir}{os.path.sep}"
remap = {}
if args.remap:
for e in args.remap:
(fromPath, toPath) = e.split("=")
remap[fromPath] = toPath
top_levels_targets = getBuildTargets(
raw_ninja,
cur_dir,
filename,
manually_generated,
rootdir,
prefix,
remap,
cc_imports,
compilerIncludes,
args.top_level_target or ["all"],
)
end = time.time()
print(f"Time to getBuildTargets: {end - start}", file=sys.stdout)
start = time.time()
needed_configure_outputs = collect_needed_configure_outputs(top_levels_targets, cur_dir)
configure_files = parse_configure_files_list(
args.configure_files_list,
rootdir,
cur_dir,
_configure_vars_from_cli_paths(
rootdir,
cur_dir,
args.prefix,
args.configure_var,
),
needed_configure_outputs,
)
end = time.time()
print(f"Time to parse configure_files: {end - start}", file=sys.stdout)
start = time.time()
if configure_files:
install_configure_file_tool(rootdir, args.prefix)
logging.info("Generating Bazel BUILD files from buildTargets")
logging.info(f"There are {len(top_levels_targets)} top level targets")
output = genBazelBuildFiles(
top_levels_targets,
rootdir,
prefix,
BUILD_CUSTOMIZATION_DIRECTORY,
configure_files,
cur_dir,
)
end = time.time()
print(f"Time to generate Bazel's BUILD files: {end - start}", file=sys.stdout)
logging.info("Done")
for name, content in output.items():
if len(content) > 1:
logging.info(
f"Wrote {rootdir}{name}{os.path.sep}BUILD.bazel len = {len(content)}"
)
build_file = f"{rootdir}{name}{os.path.sep}BUILD.bazel"
with open(build_file, "w") as f:
f.write(content)
run_post_treatments(build_file, args.post_treatment)
def getCompilerIncludesDir(compiler: str = "clang++") -> List[str]:
sed_cmd = "/^[[:space:]]\\{1,\\}/p"
cmd = f"""echo "" |{compiler} -Wp,-v -x c++ - -fsyntax-only 2>&1 |sed -n -e '{sed_cmd}' | sed 's/^[ \\t]*//'"""
ret: List[str] = []
result = subprocess.run(cmd, shell=True, capture_output=True, text=True)
ret.extend(result.stdout.split("\n"))
return ret
if __name__ == "__main__":
main()