Debian Codegraph @

Tools/jit/build.py

raw ◀ ▶
1 Debian patch touch this file
1 """Build an experimental just-in-time compiler for CPython."""
2
3 import argparse
4 import pathlib
5 import shlex
6 import sys
7
8 import _targets
9
10 if __name__ == "__main__":
11 comment = f"$ {shlex.join([pathlib.Path(sys.executable).name] + sys.argv)}"
12 parser = argparse.ArgumentParser(description=__doc__)
13 parser.add_argument(
14 "target",
15 nargs="+",
16 type=_targets.get_target,
17 help="a PEP 11 target triple to compile for",
18 )
19 parser.add_argument(
20 "-d", "--debug", action="store_true", help="compile for a debug build of Python"
21 )
22 parser.add_argument(
23 "-f", "--force", action="store_true", help="force the entire JIT to be rebuilt"
24 )
25 parser.add_argument(
26 "-o",
27 "--output-dir",
28 help="where to output generated files",
29 required=True,
30 type=lambda p: pathlib.Path(p).resolve(),
31 )
32 parser.add_argument(
33 "-p",
34 "--pyconfig-dir",
35 help="where to find pyconfig.h",
36 required=True,
37 type=lambda p: pathlib.Path(p).resolve(),
38 )
39 parser.add_argument(
40 "-v", "--verbose", action="store_true", help="echo commands as they are run"
41 )
42 parser.add_argument(
43 "--cflags", help="additional flags to pass to the compiler", default=""
44 )
•45 parser.add_argument("--llvm-version", help="LLVM version to use")
46 args = parser.parse_args()
47 for target in args.target:
48 target.debug = args.debug
49 target.force = args.force
50 target.verbose = args.verbose
51 target.cflags = args.cflags
52 target.pyconfig_dir = args.pyconfig_dir
•53 if args.llvm_version:
54 target.llvm_version = args.llvm_version
55 target.build(
56 comment=comment,
57 force=args.force,
58 jit_stencils=args.output_dir / f"jit_stencils-{target.triple}.h",
59 )
60 jit_stencils_h = args.output_dir / "jit_stencils.h"
61 lines = [f"// {comment}\n"]
62 guard = "#if"
63 for target in args.target:
64 lines.append(f"{guard} {target.condition}\n")
65 lines.append(f'#include "jit_stencils-{target.triple}.h"\n')
66 guard = "#elif"
67 lines.append("#else\n")
68 lines.append('#error "unexpected target"\n')
69 lines.append("#endif\n")
70 body = "".join(lines)
71 # Don't touch the file if it hasn't changed (so we don't trigger a rebuild):
72 if not jit_stencils_h.is_file() or jit_stencils_h.read_text() != body:
73 jit_stencils_h.write_text(body)