|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" Driver script to run all code generation """ |
| 3 | + |
| 4 | +import argparse |
| 5 | +import logging |
| 6 | +import os |
| 7 | +import sys |
| 8 | +import pathlib |
| 9 | +import typing |
| 10 | +import shlex |
| 11 | + |
| 12 | +if 'BUILD_WORKSPACE_DIRECTORY' not in os.environ: |
| 13 | + # we are not running with `bazel run`, set up module search path |
| 14 | + _repo_root = pathlib.Path(__file__).resolve().parents[2] |
| 15 | + sys.path.append(str(_repo_root)) |
| 16 | + |
| 17 | +from misc.codegen.lib import render, paths |
| 18 | +from misc.codegen.generators import generate |
| 19 | + |
| 20 | + |
| 21 | +def _parse_args() -> argparse.Namespace: |
| 22 | + dirs = [pathlib.Path().resolve()] |
| 23 | + dirs.extend(dirs[0].parents) |
| 24 | + for dir in dirs: |
| 25 | + conf = dir / "codegen.conf" |
| 26 | + if conf.exists(): |
| 27 | + break |
| 28 | + else: |
| 29 | + conf = None |
| 30 | + |
| 31 | + p = argparse.ArgumentParser(description="Code generation suite") |
| 32 | + p.add_argument("--generate", type=lambda x: x.split(","), |
| 33 | + help="specify what targets to generate as a comma separated list, choosing among dbscheme, ql, trap " |
| 34 | + "and cpp") |
| 35 | + p.add_argument("--verbose", "-v", action="store_true", help="print more information") |
| 36 | + p.add_argument("--quiet", "-q", action="store_true", help="only print errors") |
| 37 | + p.add_argument("--configuration-file", "-c", type=_abspath, default=conf, |
| 38 | + help="A configuration file to load options from. By default, the first codegen.conf file found by " |
| 39 | + "going up directories from the current location. If present all paths provided in options are " |
| 40 | + "considered relative to its directory") |
| 41 | + p.add_argument("--root-dir", type=_abspath, |
| 42 | + help="the directory that should be regarded as the root of the language pack codebase. Used to " |
| 43 | + "compute QL imports and in some comments and as root for relative paths provided as options. " |
| 44 | + "If not provided it defaults to the directory of the configuration file, if any") |
| 45 | + path_arguments = [ |
| 46 | + p.add_argument("--schema", default="schema.py", |
| 47 | + help="input schema file (default %(default)s)"), |
| 48 | + p.add_argument("--dbscheme", |
| 49 | + help="output file for dbscheme generation, input file for trap generation"), |
| 50 | + p.add_argument("--ql-output", |
| 51 | + help="output directory for generated QL files"), |
| 52 | + p.add_argument("--ql-stub-output", |
| 53 | + help="output directory for QL stub/customization files. Defines also the " |
| 54 | + "generated qll file importing every class file"), |
| 55 | + p.add_argument("--ql-test-output", |
| 56 | + help="output directory for QL generated extractor test files"), |
| 57 | + p.add_argument("--cpp-output", |
| 58 | + help="output directory for generated C++ files, required if trap or cpp is provided to " |
| 59 | + "--generate"), |
| 60 | + p.add_argument("--generated-registry", |
| 61 | + help="registry file containing information about checked-in generated code"), |
| 62 | + ] |
| 63 | + p.add_argument("--script-name", |
| 64 | + help="script name to put in header comments of generated files. By default, the path of this " |
| 65 | + "script relative to the root directory") |
| 66 | + p.add_argument("--trap-library", |
| 67 | + help="path to the trap library from an include directory, required if generating C++ trap bindings"), |
| 68 | + p.add_argument("--ql-format", action="store_true", default=True, |
| 69 | + help="use codeql to autoformat QL files (which is the default)") |
| 70 | + p.add_argument("--no-ql-format", action="store_false", dest="ql_format", help="do not format QL files") |
| 71 | + p.add_argument("--codeql-binary", default="codeql", help="command to use for QL formatting (default %(default)s)") |
| 72 | + p.add_argument("--force", "-f", action="store_true", |
| 73 | + help="generate all files without skipping unchanged files and overwriting modified ones") |
| 74 | + p.add_argument("--use-current-directory", action="store_true", |
| 75 | + help="do not consider paths as relative to --root-dir or the configuration directory") |
| 76 | + opts = p.parse_args() |
| 77 | + if opts.configuration_file is not None: |
| 78 | + with open(opts.configuration_file) as config: |
| 79 | + defaults = p.parse_args(shlex.split(config.read(), comments=True)) |
| 80 | + for flag, value in opts._get_kwargs(): |
| 81 | + if value is None: |
| 82 | + setattr(opts, flag, getattr(defaults, flag)) |
| 83 | + if opts.root_dir is None: |
| 84 | + opts.root_dir = opts.configuration_file.parent |
| 85 | + if not opts.generate: |
| 86 | + p.error("Nothing to do, specify --generate") |
| 87 | + # absolutize all paths |
| 88 | + for arg in path_arguments: |
| 89 | + path = getattr(opts, arg.dest) |
| 90 | + if path is not None: |
| 91 | + setattr(opts, arg.dest, _abspath(path) if opts.use_current_directory else (opts.root_dir / path)) |
| 92 | + if not opts.script_name: |
| 93 | + opts.script_name = paths.exe_file.relative_to(opts.root_dir) |
| 94 | + return opts |
| 95 | + |
| 96 | + |
| 97 | +def _abspath(x: str) -> typing.Optional[pathlib.Path]: |
| 98 | + return pathlib.Path(x).resolve() if x else None |
| 99 | + |
| 100 | + |
| 101 | +def run(): |
| 102 | + opts = _parse_args() |
| 103 | + if opts.verbose: |
| 104 | + log_level = logging.DEBUG |
| 105 | + elif opts.quiet: |
| 106 | + log_level = logging.ERROR |
| 107 | + else: |
| 108 | + log_level = logging.INFO |
| 109 | + logging.basicConfig(format="{levelname} {message}", style='{', level=log_level) |
| 110 | + for target in opts.generate: |
| 111 | + generate(target, opts, render.Renderer(opts.script_name, opts.root_dir)) |
| 112 | + |
| 113 | + |
| 114 | +if __name__ == "__main__": |
| 115 | + run() |
0 commit comments