Skip to content

Commit 77f7fe8

Browse files
committed
Swift: merge codegen and cppcodegen
Python code was simplified, and now a `--generate` option can be used to drive what can be generated. The extractor pack creation now will use an internally generated dbscheme. This should be the same as the checked in one, but doing so allows `bazel run create-extractor-pack` and `bazel run codegen` to be run independently from one another, while previously the former had to follow the latter in case of a schema change. This is the change that triggered the above simplification, as in order for the two dbscheme files to be identical, the first `// generated` line had to state the same generator script.
1 parent 4b2b6fa commit 77f7fe8

File tree

13 files changed

+70
-149
lines changed

13 files changed

+70
-149
lines changed

.github/workflows/swift-codegen.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
git diff --exit-code --stat HEAD
2626
- name: Generate C++ files
2727
run: |
28-
bazel run //swift/codegen:cppcodegen -- --cpp-output=$PWD/swift-generated-headers
28+
bazel run //swift/codegen:codegen -- --generate=trap,cpp --cpp-output=$PWD/swift-generated-headers
2929
- uses: actions/upload-artifact@v3
3030
with:
3131
name: swift-generated-headers

swift/BUILD.bazel

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,11 @@ load("@rules_pkg//:install.bzl", "pkg_install")
33
load("//:defs.bzl", "codeql_platform")
44
load("//misc/bazel:pkg_runfiles.bzl", "pkg_runfiles")
55

6-
filegroup(
7-
name = "dbscheme",
8-
srcs = ["ql/lib/swift.dbscheme"],
9-
visibility = ["//visibility:public"],
10-
)
11-
126
pkg_files(
137
name = "dbscheme_files",
148
srcs = [
159
"ql/lib/swift.dbscheme.stats",
16-
":dbscheme",
10+
"//swift/extractor/trap:generated_dbscheme",
1711
],
1812
)
1913

swift/codegen/BUILD.bazel

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ filegroup(
1515
py_binary(
1616
name = "codegen",
1717
srcs = ["codegen.py"],
18-
visibility = ["//swift/codegen/test:__pkg__"],
19-
deps = ["//swift/codegen/generators"],
20-
)
21-
22-
# as opposed to the above, that is meant to only be run with bazel run,
23-
# we need to be precise with data dependencies of this which is meant be run during build
24-
py_binary(
25-
name = "cppcodegen",
26-
srcs = ["cppcodegen.py"],
2718
data = [
2819
":schema",
2920
":schema_includes",

swift/codegen/codegen.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,49 @@
11
#!/usr/bin/env python3
2-
""" Driver script to run all checked in code generation """
2+
""" Driver script to run all code generation """
3+
4+
import argparse
5+
import logging
6+
import pathlib
7+
import sys
8+
import importlib
9+
import types
10+
import typing
11+
12+
from swift.codegen.lib import render, paths
13+
from swift.codegen.generators import generate
14+
15+
16+
def _parse_args() -> argparse.Namespace:
17+
p = argparse.ArgumentParser()
18+
p.add_argument("--generate", type=lambda x: x.split(","), default=["dbscheme", "ql"])
19+
p.add_argument("--verbose", "-v", action="store_true")
20+
p.add_argument("--swift-dir", type=_abspath, default=paths.swift_dir)
21+
p.add_argument("--schema", type=_abspath, default=paths.swift_dir / "codegen/schema.yml")
22+
p.add_argument("--dbscheme", type=_abspath, default=paths.swift_dir / "ql/lib/swift.dbscheme")
23+
p.add_argument("--ql-output", type=_abspath, default=paths.swift_dir / "ql/lib/codeql/swift/generated")
24+
p.add_argument("--ql-stub-output", type=_abspath, default=paths.swift_dir / "ql/lib/codeql/swift/elements")
25+
p.add_argument("--ql-format", action="store_true", default=True)
26+
p.add_argument("--no-ql-format", action="store_false", dest="ql_format")
27+
p.add_argument("--codeql-binary", default="codeql")
28+
p.add_argument("--cpp-output", type=_abspath)
29+
p.add_argument("--cpp-namespace", default="codeql")
30+
p.add_argument("--trap-affix", default="Trap")
31+
p.add_argument("--cpp-include-dir", default="swift/extractor/trap")
32+
return p.parse_args()
33+
34+
35+
def _abspath(x: str) -> pathlib.Path:
36+
return pathlib.Path(x).resolve()
37+
38+
39+
def run():
40+
opts = _parse_args()
41+
log_level = logging.DEBUG if opts.verbose else logging.INFO
42+
logging.basicConfig(format="{levelname} {message}", style='{', level=log_level)
43+
exe_path = paths.exe_file.relative_to(opts.swift_dir)
44+
for target in opts.generate:
45+
generate(target, opts, render.Renderer(exe_path))
346

4-
from swift.codegen.generators import generator, dbschemegen, qlgen
547

648
if __name__ == "__main__":
7-
generator.run(dbschemegen, qlgen)
49+
run()

swift/codegen/cppcodegen.py

Lines changed: 0 additions & 7 deletions
This file was deleted.

swift/codegen/generators/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from . import dbschemegen, qlgen, trapgen, cppgen
2+
3+
4+
def generate(target, opts, renderer):
5+
module = globals()[f"{target}gen"]
6+
module.generate(opts, renderer)

swift/codegen/generators/cppgen.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from toposort import toposort_flatten
66

77
from swift.codegen.lib import cpp, schema
8-
from swift.codegen.generators import generator
98

109

1110
def _get_type(t: str, trap_affix: str) -> str:
@@ -64,13 +63,8 @@ def get_classes(self):
6463

6564

6665
def generate(opts, renderer):
66+
assert opts.cpp_output
6767
processor = Processor({cls.name: cls for cls in schema.load(opts.schema).classes}, opts.trap_affix)
6868
out = opts.cpp_output
6969
renderer.render(cpp.ClassList(processor.get_classes(), opts.cpp_namespace, opts.trap_affix,
7070
opts.cpp_include_dir, opts.schema), out / f"{opts.trap_affix}Classes.h")
71-
72-
73-
tags = ("cpp", "schema")
74-
75-
if __name__ == "__main__":
76-
generator.run()

swift/codegen/generators/dbschemegen.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import inflection
55

66
from swift.codegen.lib import schema
7-
from swift.codegen.generators import generator
87
from swift.codegen.lib.dbscheme import *
98

109
log = logging.getLogger(__name__)
@@ -93,9 +92,3 @@ def generate(opts, renderer):
9392
declarations=get_declarations(data))
9493

9594
renderer.render(dbscheme, out)
96-
97-
98-
tags = ("schema", "dbscheme")
99-
100-
if __name__ == "__main__":
101-
generator.run()

swift/codegen/generators/generator.py

Lines changed: 0 additions & 33 deletions
This file was deleted.

swift/codegen/generators/options.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

0 commit comments

Comments
 (0)