From cdd4e8021bf0dddc214f48b77768bad0ac53ccdb Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Fri, 24 Feb 2023 13:44:29 +0100 Subject: [PATCH] Move `swift/codegen` to `misc/codegen` --- .github/workflows/swift.yml | 2 ++ .pre-commit-config.yaml | 2 +- CODEOWNERS | 1 + misc/bazel/workspace.bzl | 10 ++++++++++ misc/bazel/workspace_deps.bzl | 6 ++++-- {swift => misc}/codegen/.pep8 | 0 misc/codegen/BUILD.bazel | 14 ++++++++++++++ {swift => misc}/codegen/README.md | 8 ++++---- {swift => misc}/codegen/codegen.py | 4 ++-- misc/codegen/generators/BUILD.bazel | 11 +++++++++++ {swift => misc}/codegen/generators/__init__.py | 0 {swift => misc}/codegen/generators/cppgen.py | 4 ++-- {swift => misc}/codegen/generators/dbschemegen.py | 6 +++--- {swift => misc}/codegen/generators/qlgen.py | 4 ++-- {swift => misc}/codegen/generators/trapgen.py | 4 ++-- {swift => misc}/codegen/lib/BUILD.bazel | 4 ++-- {swift => misc}/codegen/lib/cpp.py | 0 {swift => misc}/codegen/lib/dbscheme.py | 0 {swift => misc}/codegen/lib/paths.py | 7 ++++--- {swift => misc}/codegen/lib/ql.py | 0 {swift => misc}/codegen/lib/render.py | 0 {swift => misc}/codegen/lib/schema.py | 0 {swift/codegen => misc/codegen/lib}/schemadefs.py | 2 +- {swift => misc}/codegen/loaders/BUILD.bazel | 4 ++-- {swift => misc}/codegen/loaders/dbschemeloader.py | 2 +- {swift => misc}/codegen/loaders/schemaloader.py | 4 ++-- {swift => misc}/codegen/requirements.txt | 0 {swift/codegen/lib => misc/codegen}/schemadefs.py | 2 +- {swift => misc}/codegen/templates/BUILD.bazel | 2 +- .../codegen/templates/cpp_classes_cpp.mustache | 0 .../codegen/templates/cpp_classes_h.mustache | 0 .../codegen/templates/dbscheme.mustache | 0 .../codegen/templates/ql_class.mustache | 0 {swift => misc}/codegen/templates/ql_db.mustache | 0 .../codegen/templates/ql_imports.mustache | 0 .../templates/ql_ipa_constructor_stub.mustache | 0 .../codegen/templates/ql_ipa_types.mustache | 0 .../codegen/templates/ql_parent.mustache | 0 .../codegen/templates/ql_property_doc.mustache | 0 {swift => misc}/codegen/templates/ql_stub.mustache | 0 .../codegen/templates/ql_test_class.mustache | 0 .../codegen/templates/ql_test_missing.mustache | 0 .../codegen/templates/ql_test_property.mustache | 0 .../codegen/templates/trap_tags_h.mustache | 0 .../codegen/templates/trap_traps_cpp.mustache | 0 .../codegen/templates/trap_traps_h.mustache | 0 {swift => misc}/codegen/test/BUILD.bazel | 6 +++--- {swift => misc}/codegen/test/test_cpp.py | 2 +- {swift => misc}/codegen/test/test_cppgen.py | 6 +++--- {swift => misc}/codegen/test/test_dbscheme.py | 4 ++-- {swift => misc}/codegen/test/test_dbschemegen.py | 6 +++--- .../codegen/test/test_dbschemelaoder.py | 6 +++--- {swift => misc}/codegen/test/test_ql.py | 4 ++-- {swift => misc}/codegen/test/test_qlgen.py | 6 +++--- {swift => misc}/codegen/test/test_render.py | 2 +- {swift => misc}/codegen/test/test_schemaloader.py | 6 +++--- {swift => misc}/codegen/test/test_trapgen.py | 6 +++--- {swift => misc}/codegen/test/utils.py | 10 +++++----- swift/README.md | 2 ++ swift/actions/build-and-test/action.yml | 2 +- swift/codegen.conf | 2 ++ swift/codegen/BUILD.bazel | 12 ++++-------- swift/codegen/generators/BUILD.bazel | 11 ----------- swift/extractor/trap/BUILD.bazel | 4 ++-- swift/schema.py | 2 +- 65 files changed, 116 insertions(+), 86 deletions(-) rename {swift => misc}/codegen/.pep8 (100%) create mode 100644 misc/codegen/BUILD.bazel rename {swift => misc}/codegen/README.md (84%) rename {swift => misc}/codegen/codegen.py (98%) create mode 100644 misc/codegen/generators/BUILD.bazel rename {swift => misc}/codegen/generators/__init__.py (100%) rename {swift => misc}/codegen/generators/cppgen.py (97%) rename {swift => misc}/codegen/generators/dbschemegen.py (97%) rename {swift => misc}/codegen/generators/qlgen.py (99%) rename {swift => misc}/codegen/generators/trapgen.py (96%) rename {swift => misc}/codegen/lib/BUILD.bazel (56%) rename {swift => misc}/codegen/lib/cpp.py (100%) rename {swift => misc}/codegen/lib/dbscheme.py (100%) rename {swift => misc}/codegen/lib/paths.py (70%) rename {swift => misc}/codegen/lib/ql.py (100%) rename {swift => misc}/codegen/lib/render.py (100%) rename {swift => misc}/codegen/lib/schema.py (100%) rename {swift/codegen => misc/codegen/lib}/schemadefs.py (98%) rename {swift => misc}/codegen/loaders/BUILD.bazel (56%) rename {swift => misc}/codegen/loaders/dbschemeloader.py (97%) rename {swift => misc}/codegen/loaders/schemaloader.py (98%) rename {swift => misc}/codegen/requirements.txt (100%) rename {swift/codegen/lib => misc/codegen}/schemadefs.py (98%) rename {swift => misc}/codegen/templates/BUILD.bazel (68%) rename {swift => misc}/codegen/templates/cpp_classes_cpp.mustache (100%) rename {swift => misc}/codegen/templates/cpp_classes_h.mustache (100%) rename {swift => misc}/codegen/templates/dbscheme.mustache (100%) rename {swift => misc}/codegen/templates/ql_class.mustache (100%) rename {swift => misc}/codegen/templates/ql_db.mustache (100%) rename {swift => misc}/codegen/templates/ql_imports.mustache (100%) rename {swift => misc}/codegen/templates/ql_ipa_constructor_stub.mustache (100%) rename {swift => misc}/codegen/templates/ql_ipa_types.mustache (100%) rename {swift => misc}/codegen/templates/ql_parent.mustache (100%) rename {swift => misc}/codegen/templates/ql_property_doc.mustache (100%) rename {swift => misc}/codegen/templates/ql_stub.mustache (100%) rename {swift => misc}/codegen/templates/ql_test_class.mustache (100%) rename {swift => misc}/codegen/templates/ql_test_missing.mustache (100%) rename {swift => misc}/codegen/templates/ql_test_property.mustache (100%) rename {swift => misc}/codegen/templates/trap_tags_h.mustache (100%) rename {swift => misc}/codegen/templates/trap_traps_cpp.mustache (100%) rename {swift => misc}/codegen/templates/trap_traps_h.mustache (100%) rename {swift => misc}/codegen/test/BUILD.bazel (72%) rename {swift => misc}/codegen/test/test_cpp.py (98%) rename {swift => misc}/codegen/test/test_cppgen.py (98%) rename {swift => misc}/codegen/test/test_dbscheme.py (95%) rename {swift => misc}/codegen/test/test_dbschemegen.py (99%) rename {swift => misc}/codegen/test/test_dbschemelaoder.py (95%) rename {swift => misc}/codegen/test/test_ql.py (98%) rename {swift => misc}/codegen/test/test_qlgen.py (99%) rename {swift => misc}/codegen/test/test_render.py (99%) rename {swift => misc}/codegen/test/test_schemaloader.py (99%) rename {swift => misc}/codegen/test/test_trapgen.py (97%) rename {swift => misc}/codegen/test/utils.py (83%) delete mode 100644 swift/codegen/generators/BUILD.bazel diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index de0f11f0521..806e04e6c68 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -5,6 +5,7 @@ on: paths: - "swift/**" - "misc/bazel/**" + - "misc/codegen/**" - "*.bazel*" - .github/workflows/swift.yml - .github/actions/** @@ -19,6 +20,7 @@ on: paths: - "swift/**" - "misc/bazel/**" + - "misc/codegen/**" - "*.bazel*" - .github/workflows/swift.yml - .github/actions/** diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5f35c2c183b..e612a423462 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -53,5 +53,5 @@ repos: name: Run Swift code generation unit tests files: ^swift/codegen/.*\.py$ language: system - entry: bazel test //swift/codegen/test + entry: bazel test //misc/codegen/test pass_filenames: false diff --git a/CODEOWNERS b/CODEOWNERS index 27ac245e959..8371dd768cd 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -6,6 +6,7 @@ /python/ @github/codeql-dynamic /ruby/ @github/codeql-dynamic /swift/ @github/codeql-swift +/misc/codegen/ @github/codeql-swift /java/kotlin-extractor/ @github/codeql-kotlin /java/kotlin-explorer/ @github/codeql-kotlin diff --git a/misc/bazel/workspace.bzl b/misc/bazel/workspace.bzl index 46426f48d5c..ef89ccfb666 100644 --- a/misc/bazel/workspace.bzl +++ b/misc/bazel/workspace.bzl @@ -33,3 +33,13 @@ def codeql_workspace(repository_name = "codeql"): "https://github.com/bazelbuild/rules_python/archive/refs/tags/0.8.1.tar.gz", ], ) + + maybe( + repo_rule = http_archive, + name = "bazel_skylib", + sha256 = "b8a1527901774180afc798aeb28c4634bdccf19c4d98e7bdd1ce79d1fe9aaad7", + urls = [ + "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.4.1/bazel-skylib-1.4.1.tar.gz", + "https://github.com/bazelbuild/bazel-skylib/releases/download/1.4.1/bazel-skylib-1.4.1.tar.gz", + ], + ) diff --git a/misc/bazel/workspace_deps.bzl b/misc/bazel/workspace_deps.bzl index 987fd144fd8..674be778d78 100644 --- a/misc/bazel/workspace_deps.bzl +++ b/misc/bazel/workspace_deps.bzl @@ -1,9 +1,11 @@ load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") load("@rules_python//python:pip.bzl", "pip_install") +load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace") def codeql_workspace_deps(repository_name = "codeql"): pip_install( - name = "swift_codegen_deps", - requirements = "@%s//swift/codegen:requirements.txt" % repository_name, + name = "codegen_deps", + requirements = "@%s//misc/codegen:requirements.txt" % repository_name, ) + bazel_skylib_workspace() rules_pkg_dependencies() diff --git a/swift/codegen/.pep8 b/misc/codegen/.pep8 similarity index 100% rename from swift/codegen/.pep8 rename to misc/codegen/.pep8 diff --git a/misc/codegen/BUILD.bazel b/misc/codegen/BUILD.bazel new file mode 100644 index 00000000000..102e52fb10b --- /dev/null +++ b/misc/codegen/BUILD.bazel @@ -0,0 +1,14 @@ +load("@codegen_deps//:requirements.bzl", "requirement") + +py_binary( + name = "codegen", + srcs = ["codegen.py"], + data = [ + "//misc/codegen/templates:cpp", + "//misc/codegen/templates:trap", + ], + visibility = ["//visibility:public"], + deps = [ + "//misc/codegen/generators", + ], +) diff --git a/swift/codegen/README.md b/misc/codegen/README.md similarity index 84% rename from swift/codegen/README.md rename to misc/codegen/README.md index db94d327da6..149c673dab5 100644 --- a/swift/codegen/README.md +++ b/misc/codegen/README.md @@ -3,7 +3,7 @@ This directory contains the code generation suite used by the Swift extractor and the QL library. This suite will use the abstract class specification of [`schema.yml`](schema.yml) to generate: -* [the `dbscheme` file](../ql/lib/swift.dbscheme) (see [`dbschemegen.py`](generators/dbschemegen.py)) +* [the `dbscheme` file](../ql/lib/misc.dbscheme) (see [`dbschemegen.py`](generators/dbschemegen.py)) * [the QL generated code](../ql/lib/codeql/swift/generated) and when appropriate [the corresponding stubs](../ql/lib/codeql/swift/elements) (see [`qlgen.py`](generators/qlgen.py)) * C++ tags and trap entries (see [`trapgen.py`](generators/trapgen.py)) @@ -11,9 +11,9 @@ the abstract class specification of [`schema.yml`](schema.yml) to generate: ## Usage -By default `bazel run //swift/codegen` will update all checked-in generated files (`dbscheme` and QL sources). You can +By default `bazel run //misc/codegen` will update all checked-in generated files (`dbscheme` and QL sources). You can append `--` followed by other options to tweak the behaviour, which is mainly intended for debugging. -See `bazel run //swift/codegen -- --help` for a list of all options. In particular `--generate` can be used with a comma +See `bazel run //misc/codegen -- --help` for a list of all options. In particular `--generate` can be used with a comma separated list to select what to generate (choosing among `dbscheme`, `ql`, `trap` and `cpp`). C++ code is generated during build (see [`swift/extractor/trap/BUILD.bazel`](../extractor/trap/BUILD.bazel)). After a @@ -38,7 +38,7 @@ specified as the `generate` function in the modules within [the `generators` dir Finally, [`codegen.py`](codegen.py) is the driver script gluing everything together and specifying the command line options. -Unit tests are in [the `test` directory](test) and can be run via `bazel test //swift/codegen/test`. +Unit tests are in [the `test` directory](test) and can be run via `bazel test //misc/codegen/test`. For more details about each specific generation target, please refer to the module docstrings in [the `generators` directory](generators). diff --git a/swift/codegen/codegen.py b/misc/codegen/codegen.py similarity index 98% rename from swift/codegen/codegen.py rename to misc/codegen/codegen.py index c65502088a7..11f6fbe4a7b 100755 --- a/swift/codegen/codegen.py +++ b/misc/codegen/codegen.py @@ -14,8 +14,8 @@ if 'BUILD_WORKSPACE_DIRECTORY' not in os.environ: _repo_root = pathlib.Path(__file__).resolve().parents[2] sys.path.append(str(_repo_root)) -from swift.codegen.lib import render, paths -from swift.codegen.generators import generate +from misc.codegen.lib import render, paths +from misc.codegen.generators import generate def _parse_args() -> argparse.Namespace: diff --git a/misc/codegen/generators/BUILD.bazel b/misc/codegen/generators/BUILD.bazel new file mode 100644 index 00000000000..df328a7df7f --- /dev/null +++ b/misc/codegen/generators/BUILD.bazel @@ -0,0 +1,11 @@ +load("@codegen_deps//:requirements.bzl", "requirement") + +py_library( + name = "generators", + srcs = glob(["*.py"]), + visibility = ["//misc/codegen:__subpackages__"], + deps = [ + "//misc/codegen/lib", + "//misc/codegen/loaders", + ], +) diff --git a/swift/codegen/generators/__init__.py b/misc/codegen/generators/__init__.py similarity index 100% rename from swift/codegen/generators/__init__.py rename to misc/codegen/generators/__init__.py diff --git a/swift/codegen/generators/cppgen.py b/misc/codegen/generators/cppgen.py similarity index 97% rename from swift/codegen/generators/cppgen.py rename to misc/codegen/generators/cppgen.py index 9de53818f71..f6a7f8d792c 100644 --- a/swift/codegen/generators/cppgen.py +++ b/misc/codegen/generators/cppgen.py @@ -16,8 +16,8 @@ import typing import inflection -from swift.codegen.lib import cpp, schema -from swift.codegen.loaders import schemaloader +from misc.codegen.lib import cpp, schema +from misc.codegen.loaders import schemaloader def _get_type(t: str, add_or_none_except: typing.Optional[str] = None) -> str: diff --git a/swift/codegen/generators/dbschemegen.py b/misc/codegen/generators/dbschemegen.py similarity index 97% rename from swift/codegen/generators/dbschemegen.py rename to misc/codegen/generators/dbschemegen.py index 2c6ebca87f3..05483748db5 100755 --- a/swift/codegen/generators/dbschemegen.py +++ b/misc/codegen/generators/dbschemegen.py @@ -17,9 +17,9 @@ import typing import inflection -from swift.codegen.lib import schema -from swift.codegen.loaders import schemaloader -from swift.codegen.lib.dbscheme import * +from misc.codegen.lib import schema +from misc.codegen.loaders import schemaloader +from misc.codegen.lib.dbscheme import * log = logging.getLogger(__name__) diff --git a/swift/codegen/generators/qlgen.py b/misc/codegen/generators/qlgen.py similarity index 99% rename from swift/codegen/generators/qlgen.py rename to misc/codegen/generators/qlgen.py index bb140ab4f82..aec610fe4cd 100755 --- a/swift/codegen/generators/qlgen.py +++ b/misc/codegen/generators/qlgen.py @@ -29,8 +29,8 @@ import itertools import inflection -from swift.codegen.lib import schema, ql -from swift.codegen.loaders import schemaloader +from misc.codegen.lib import schema, ql +from misc.codegen.loaders import schemaloader log = logging.getLogger(__name__) diff --git a/swift/codegen/generators/trapgen.py b/misc/codegen/generators/trapgen.py similarity index 96% rename from swift/codegen/generators/trapgen.py rename to misc/codegen/generators/trapgen.py index 67921486c86..e22b3e4e0e7 100755 --- a/swift/codegen/generators/trapgen.py +++ b/misc/codegen/generators/trapgen.py @@ -17,8 +17,8 @@ import pathlib import inflection from toposort import toposort_flatten -from swift.codegen.lib import dbscheme, cpp -from swift.codegen.loaders import dbschemeloader +from misc.codegen.lib import dbscheme, cpp +from misc.codegen.loaders import dbschemeloader log = logging.getLogger(__name__) diff --git a/swift/codegen/lib/BUILD.bazel b/misc/codegen/lib/BUILD.bazel similarity index 56% rename from swift/codegen/lib/BUILD.bazel rename to misc/codegen/lib/BUILD.bazel index fd2607448b4..482d1ac178f 100644 --- a/swift/codegen/lib/BUILD.bazel +++ b/misc/codegen/lib/BUILD.bazel @@ -1,9 +1,9 @@ -load("@swift_codegen_deps//:requirements.bzl", "requirement") +load("@codegen_deps//:requirements.bzl", "requirement") py_library( name = "lib", srcs = glob(["*.py"]), - visibility = ["//swift/codegen:__subpackages__"], + visibility = ["//misc/codegen:__subpackages__"], deps = [ requirement("pystache"), requirement("inflection"), diff --git a/swift/codegen/lib/cpp.py b/misc/codegen/lib/cpp.py similarity index 100% rename from swift/codegen/lib/cpp.py rename to misc/codegen/lib/cpp.py diff --git a/swift/codegen/lib/dbscheme.py b/misc/codegen/lib/dbscheme.py similarity index 100% rename from swift/codegen/lib/dbscheme.py rename to misc/codegen/lib/dbscheme.py diff --git a/swift/codegen/lib/paths.py b/misc/codegen/lib/paths.py similarity index 70% rename from swift/codegen/lib/paths.py rename to misc/codegen/lib/paths.py index eae5192c0d9..b102987a226 100644 --- a/swift/codegen/lib/paths.py +++ b/misc/codegen/lib/paths.py @@ -4,15 +4,16 @@ import pathlib import sys import os +_this_file = pathlib.Path(__file__).resolve() + try: workspace_dir = pathlib.Path(os.environ['BUILD_WORKSPACE_DIRECTORY']).resolve() # <- means we are using bazel run root_dir = workspace_dir / 'swift' except KeyError: - _this_file = pathlib.Path(__file__).resolve() root_dir = _this_file.parents[2] workspace_dir = root_dir.parent -lib_dir = root_dir / 'codegen' / 'lib' -templates_dir = root_dir / 'codegen' / 'templates' +lib_dir = _this_file.parents[2] / 'codegen' / 'lib' +templates_dir = _this_file.parents[2] / 'codegen' / 'templates' exe_file = pathlib.Path(sys.argv[0]).resolve() diff --git a/swift/codegen/lib/ql.py b/misc/codegen/lib/ql.py similarity index 100% rename from swift/codegen/lib/ql.py rename to misc/codegen/lib/ql.py diff --git a/swift/codegen/lib/render.py b/misc/codegen/lib/render.py similarity index 100% rename from swift/codegen/lib/render.py rename to misc/codegen/lib/render.py diff --git a/swift/codegen/lib/schema.py b/misc/codegen/lib/schema.py similarity index 100% rename from swift/codegen/lib/schema.py rename to misc/codegen/lib/schema.py diff --git a/swift/codegen/schemadefs.py b/misc/codegen/lib/schemadefs.py similarity index 98% rename from swift/codegen/schemadefs.py rename to misc/codegen/lib/schemadefs.py index d972e5a63e5..6c9457490e7 100644 --- a/swift/codegen/schemadefs.py +++ b/misc/codegen/lib/schemadefs.py @@ -1,5 +1,5 @@ from typing import Callable as _Callable -from swift.codegen.lib import schema as _schema +from misc.codegen.lib import schema as _schema import inspect as _inspect from dataclasses import dataclass as _dataclass diff --git a/swift/codegen/loaders/BUILD.bazel b/misc/codegen/loaders/BUILD.bazel similarity index 56% rename from swift/codegen/loaders/BUILD.bazel rename to misc/codegen/loaders/BUILD.bazel index de1d8dfe5db..be07c6d884b 100644 --- a/swift/codegen/loaders/BUILD.bazel +++ b/misc/codegen/loaders/BUILD.bazel @@ -1,9 +1,9 @@ -load("@swift_codegen_deps//:requirements.bzl", "requirement") +load("@codegen_deps//:requirements.bzl", "requirement") py_library( name = "loaders", srcs = glob(["*.py"]), - visibility = ["//swift/codegen:__subpackages__"], + visibility = ["//misc/codegen:__subpackages__"], deps = [ requirement("toposort"), requirement("inflection"), diff --git a/swift/codegen/loaders/dbschemeloader.py b/misc/codegen/loaders/dbschemeloader.py similarity index 97% rename from swift/codegen/loaders/dbschemeloader.py rename to misc/codegen/loaders/dbschemeloader.py index eda299c77b1..51e362362a7 100644 --- a/swift/codegen/loaders/dbschemeloader.py +++ b/misc/codegen/loaders/dbschemeloader.py @@ -1,6 +1,6 @@ import pathlib import re -from swift.codegen.lib import dbscheme +from misc.codegen.lib import dbscheme class _Re: diff --git a/swift/codegen/loaders/schemaloader.py b/misc/codegen/loaders/schemaloader.py similarity index 98% rename from swift/codegen/loaders/schemaloader.py rename to misc/codegen/loaders/schemaloader.py index cb276ed11c2..5fd392b112d 100644 --- a/swift/codegen/loaders/schemaloader.py +++ b/misc/codegen/loaders/schemaloader.py @@ -8,7 +8,7 @@ import importlib.util from dataclasses import dataclass from toposort import toposort_flatten -from swift.codegen.lib import schema, schemadefs +from misc.codegen.lib import schema, schemadefs @dataclass @@ -99,7 +99,7 @@ def load(m: types.ModuleType) -> schema.Schema: classes = {} known = {"int", "string", "boolean"} known.update(n for n in m.__dict__ if not n.startswith("__")) - import swift.codegen.lib.schemadefs as defs + import misc.codegen.lib.schemadefs as defs null = None for name, data in m.__dict__.items(): if hasattr(defs, name): diff --git a/swift/codegen/requirements.txt b/misc/codegen/requirements.txt similarity index 100% rename from swift/codegen/requirements.txt rename to misc/codegen/requirements.txt diff --git a/swift/codegen/lib/schemadefs.py b/misc/codegen/schemadefs.py similarity index 98% rename from swift/codegen/lib/schemadefs.py rename to misc/codegen/schemadefs.py index d972e5a63e5..6c9457490e7 100644 --- a/swift/codegen/lib/schemadefs.py +++ b/misc/codegen/schemadefs.py @@ -1,5 +1,5 @@ from typing import Callable as _Callable -from swift.codegen.lib import schema as _schema +from misc.codegen.lib import schema as _schema import inspect as _inspect from dataclasses import dataclass as _dataclass diff --git a/swift/codegen/templates/BUILD.bazel b/misc/codegen/templates/BUILD.bazel similarity index 68% rename from swift/codegen/templates/BUILD.bazel rename to misc/codegen/templates/BUILD.bazel index 7745e7ea2ac..a86346245af 100644 --- a/swift/codegen/templates/BUILD.bazel +++ b/misc/codegen/templates/BUILD.bazel @@ -1,4 +1,4 @@ -package(default_visibility = ["//swift:__subpackages__"]) +package(default_visibility = ["//misc/codegen:__subpackages__"]) filegroup( name = "trap", diff --git a/swift/codegen/templates/cpp_classes_cpp.mustache b/misc/codegen/templates/cpp_classes_cpp.mustache similarity index 100% rename from swift/codegen/templates/cpp_classes_cpp.mustache rename to misc/codegen/templates/cpp_classes_cpp.mustache diff --git a/swift/codegen/templates/cpp_classes_h.mustache b/misc/codegen/templates/cpp_classes_h.mustache similarity index 100% rename from swift/codegen/templates/cpp_classes_h.mustache rename to misc/codegen/templates/cpp_classes_h.mustache diff --git a/swift/codegen/templates/dbscheme.mustache b/misc/codegen/templates/dbscheme.mustache similarity index 100% rename from swift/codegen/templates/dbscheme.mustache rename to misc/codegen/templates/dbscheme.mustache diff --git a/swift/codegen/templates/ql_class.mustache b/misc/codegen/templates/ql_class.mustache similarity index 100% rename from swift/codegen/templates/ql_class.mustache rename to misc/codegen/templates/ql_class.mustache diff --git a/swift/codegen/templates/ql_db.mustache b/misc/codegen/templates/ql_db.mustache similarity index 100% rename from swift/codegen/templates/ql_db.mustache rename to misc/codegen/templates/ql_db.mustache diff --git a/swift/codegen/templates/ql_imports.mustache b/misc/codegen/templates/ql_imports.mustache similarity index 100% rename from swift/codegen/templates/ql_imports.mustache rename to misc/codegen/templates/ql_imports.mustache diff --git a/swift/codegen/templates/ql_ipa_constructor_stub.mustache b/misc/codegen/templates/ql_ipa_constructor_stub.mustache similarity index 100% rename from swift/codegen/templates/ql_ipa_constructor_stub.mustache rename to misc/codegen/templates/ql_ipa_constructor_stub.mustache diff --git a/swift/codegen/templates/ql_ipa_types.mustache b/misc/codegen/templates/ql_ipa_types.mustache similarity index 100% rename from swift/codegen/templates/ql_ipa_types.mustache rename to misc/codegen/templates/ql_ipa_types.mustache diff --git a/swift/codegen/templates/ql_parent.mustache b/misc/codegen/templates/ql_parent.mustache similarity index 100% rename from swift/codegen/templates/ql_parent.mustache rename to misc/codegen/templates/ql_parent.mustache diff --git a/swift/codegen/templates/ql_property_doc.mustache b/misc/codegen/templates/ql_property_doc.mustache similarity index 100% rename from swift/codegen/templates/ql_property_doc.mustache rename to misc/codegen/templates/ql_property_doc.mustache diff --git a/swift/codegen/templates/ql_stub.mustache b/misc/codegen/templates/ql_stub.mustache similarity index 100% rename from swift/codegen/templates/ql_stub.mustache rename to misc/codegen/templates/ql_stub.mustache diff --git a/swift/codegen/templates/ql_test_class.mustache b/misc/codegen/templates/ql_test_class.mustache similarity index 100% rename from swift/codegen/templates/ql_test_class.mustache rename to misc/codegen/templates/ql_test_class.mustache diff --git a/swift/codegen/templates/ql_test_missing.mustache b/misc/codegen/templates/ql_test_missing.mustache similarity index 100% rename from swift/codegen/templates/ql_test_missing.mustache rename to misc/codegen/templates/ql_test_missing.mustache diff --git a/swift/codegen/templates/ql_test_property.mustache b/misc/codegen/templates/ql_test_property.mustache similarity index 100% rename from swift/codegen/templates/ql_test_property.mustache rename to misc/codegen/templates/ql_test_property.mustache diff --git a/swift/codegen/templates/trap_tags_h.mustache b/misc/codegen/templates/trap_tags_h.mustache similarity index 100% rename from swift/codegen/templates/trap_tags_h.mustache rename to misc/codegen/templates/trap_tags_h.mustache diff --git a/swift/codegen/templates/trap_traps_cpp.mustache b/misc/codegen/templates/trap_traps_cpp.mustache similarity index 100% rename from swift/codegen/templates/trap_traps_cpp.mustache rename to misc/codegen/templates/trap_traps_cpp.mustache diff --git a/swift/codegen/templates/trap_traps_h.mustache b/misc/codegen/templates/trap_traps_h.mustache similarity index 100% rename from swift/codegen/templates/trap_traps_h.mustache rename to misc/codegen/templates/trap_traps_h.mustache diff --git a/swift/codegen/test/BUILD.bazel b/misc/codegen/test/BUILD.bazel similarity index 72% rename from swift/codegen/test/BUILD.bazel rename to misc/codegen/test/BUILD.bazel index 9ae41c14941..dde67283335 100644 --- a/swift/codegen/test/BUILD.bazel +++ b/misc/codegen/test/BUILD.bazel @@ -1,11 +1,11 @@ -load("@swift_codegen_deps//:requirements.bzl", "requirement") +load("@codegen_deps//:requirements.bzl", "requirement") py_library( name = "utils", testonly = True, srcs = ["utils.py"], deps = [ - "//swift/codegen/lib", + "//misc/codegen/lib", requirement("pytest"), ], ) @@ -17,7 +17,7 @@ py_library( srcs = [src], deps = [ ":utils", - "//swift/codegen/generators", + "//misc/codegen/generators", ], ) for src in glob(["test_*.py"]) diff --git a/swift/codegen/test/test_cpp.py b/misc/codegen/test/test_cpp.py similarity index 98% rename from swift/codegen/test/test_cpp.py rename to misc/codegen/test/test_cpp.py index d04877e38c4..c4bee337a4f 100644 --- a/swift/codegen/test/test_cpp.py +++ b/misc/codegen/test/test_cpp.py @@ -3,7 +3,7 @@ from copy import deepcopy import pytest -from swift.codegen.lib import cpp +from misc.codegen.lib import cpp @pytest.mark.parametrize("keyword", cpp.cpp_keywords) diff --git a/swift/codegen/test/test_cppgen.py b/misc/codegen/test/test_cppgen.py similarity index 98% rename from swift/codegen/test/test_cppgen.py rename to misc/codegen/test/test_cppgen.py index 3c62143e86a..278f184fcbc 100644 --- a/swift/codegen/test/test_cppgen.py +++ b/misc/codegen/test/test_cppgen.py @@ -1,8 +1,8 @@ import sys -from swift.codegen.generators import cppgen -from swift.codegen.lib import cpp -from swift.codegen.test.utils import * +from misc.codegen.generators import cppgen +from misc.codegen.lib import cpp +from misc.codegen.test.utils import * output_dir = pathlib.Path("path", "to", "output") diff --git a/swift/codegen/test/test_dbscheme.py b/misc/codegen/test/test_dbscheme.py similarity index 95% rename from swift/codegen/test/test_dbscheme.py rename to misc/codegen/test/test_dbscheme.py index a520cc2b66b..e2635ecee5a 100644 --- a/swift/codegen/test/test_dbscheme.py +++ b/misc/codegen/test/test_dbscheme.py @@ -1,8 +1,8 @@ import sys from copy import deepcopy -from swift.codegen.lib import dbscheme -from swift.codegen.test.utils import * +from misc.codegen.lib import dbscheme +from misc.codegen.test.utils import * def test_dbcolumn_name(): diff --git a/swift/codegen/test/test_dbschemegen.py b/misc/codegen/test/test_dbschemegen.py similarity index 99% rename from swift/codegen/test/test_dbschemegen.py rename to misc/codegen/test/test_dbschemegen.py index 74070967d1a..7a986538b06 100644 --- a/swift/codegen/test/test_dbschemegen.py +++ b/misc/codegen/test/test_dbschemegen.py @@ -1,9 +1,9 @@ import collections import sys -from swift.codegen.generators import dbschemegen -from swift.codegen.lib import dbscheme -from swift.codegen.test.utils import * +from misc.codegen.generators import dbschemegen +from misc.codegen.lib import dbscheme +from misc.codegen.test.utils import * InputExpectedPair = collections.namedtuple("InputExpectedPair", ("input", "expected")) diff --git a/swift/codegen/test/test_dbschemelaoder.py b/misc/codegen/test/test_dbschemelaoder.py similarity index 95% rename from swift/codegen/test/test_dbschemelaoder.py rename to misc/codegen/test/test_dbschemelaoder.py index 6212b860cbb..ab4efbff75a 100644 --- a/swift/codegen/test/test_dbschemelaoder.py +++ b/misc/codegen/test/test_dbschemelaoder.py @@ -1,9 +1,9 @@ import sys from copy import deepcopy -from swift.codegen.lib import dbscheme -from swift.codegen.loaders.dbschemeloader import iterload -from swift.codegen.test.utils import * +from misc.codegen.lib import dbscheme +from misc.codegen.loaders.dbschemeloader import iterload +from misc.codegen.test.utils import * @pytest.fixture diff --git a/swift/codegen/test/test_ql.py b/misc/codegen/test/test_ql.py similarity index 98% rename from swift/codegen/test/test_ql.py rename to misc/codegen/test/test_ql.py index f5b5218bd43..0006bbb96e4 100644 --- a/swift/codegen/test/test_ql.py +++ b/misc/codegen/test/test_ql.py @@ -1,8 +1,8 @@ import sys from copy import deepcopy -from swift.codegen.lib import ql -from swift.codegen.test.utils import * +from misc.codegen.lib import ql +from misc.codegen.test.utils import * def test_property_has_first_table_param_marked(): diff --git a/swift/codegen/test/test_qlgen.py b/misc/codegen/test/test_qlgen.py similarity index 99% rename from swift/codegen/test/test_qlgen.py rename to misc/codegen/test/test_qlgen.py index e543bd9c1a9..f16b1404c2e 100644 --- a/swift/codegen/test/test_qlgen.py +++ b/misc/codegen/test/test_qlgen.py @@ -4,9 +4,9 @@ import sys import pytest -from swift.codegen.generators import qlgen -from swift.codegen.lib import ql -from swift.codegen.test.utils import * +from misc.codegen.generators import qlgen +from misc.codegen.lib import ql +from misc.codegen.test.utils import * @pytest.fixture(autouse=True) diff --git a/swift/codegen/test/test_render.py b/misc/codegen/test/test_render.py similarity index 99% rename from swift/codegen/test/test_render.py rename to misc/codegen/test/test_render.py index ce9c5b10b04..f9129178f04 100644 --- a/swift/codegen/test/test_render.py +++ b/misc/codegen/test/test_render.py @@ -2,7 +2,7 @@ import sys import pytest -from swift.codegen.test.utils import * +from misc.codegen.test.utils import * import hashlib diff --git a/swift/codegen/test/test_schemaloader.py b/misc/codegen/test/test_schemaloader.py similarity index 99% rename from swift/codegen/test/test_schemaloader.py rename to misc/codegen/test/test_schemaloader.py index 2428636609e..9724a82da8f 100644 --- a/swift/codegen/test/test_schemaloader.py +++ b/misc/codegen/test/test_schemaloader.py @@ -2,9 +2,9 @@ import sys import pytest -from swift.codegen.test.utils import * -from swift.codegen.lib import schemadefs as defs -from swift.codegen.loaders.schemaloader import load +from misc.codegen.test.utils import * +from misc.codegen.lib import schemadefs as defs +from misc.codegen.loaders.schemaloader import load def test_empty_schema(): diff --git a/swift/codegen/test/test_trapgen.py b/misc/codegen/test/test_trapgen.py similarity index 97% rename from swift/codegen/test/test_trapgen.py rename to misc/codegen/test/test_trapgen.py index c8121ce48ba..a81f40e0dd8 100644 --- a/swift/codegen/test/test_trapgen.py +++ b/misc/codegen/test/test_trapgen.py @@ -1,8 +1,8 @@ import sys -from swift.codegen.generators import trapgen -from swift.codegen.lib import cpp, dbscheme -from swift.codegen.test.utils import * +from misc.codegen.generators import trapgen +from misc.codegen.lib import cpp, dbscheme +from misc.codegen.test.utils import * output_dir = pathlib.Path("path", "to", "output") diff --git a/swift/codegen/test/utils.py b/misc/codegen/test/utils.py similarity index 83% rename from swift/codegen/test/utils.py rename to misc/codegen/test/utils.py index 70db539f3f2..e33500711f2 100644 --- a/swift/codegen/test/utils.py +++ b/misc/codegen/test/utils.py @@ -3,7 +3,7 @@ from unittest import mock import pytest -from swift.codegen.lib import render, schema, paths +from misc.codegen.lib import render, schema, paths schema_dir = pathlib.Path("a", "dir") schema_file = schema_dir / "schema.py" @@ -39,15 +39,15 @@ def opts(): @pytest.fixture(autouse=True) def override_paths(tmp_path): - with mock.patch("swift.codegen.lib.paths.root_dir", tmp_path), \ - mock.patch("swift.codegen.lib.paths.exe_file", tmp_path / "exe"): + with mock.patch("misc.codegen.lib.paths.root_dir", tmp_path), \ + mock.patch("misc.codegen.lib.paths.exe_file", tmp_path / "exe"): yield @pytest.fixture def input(opts, tmp_path): opts.schema = tmp_path / schema_file - with mock.patch("swift.codegen.loaders.schemaloader.load_file") as load_mock: + with mock.patch("misc.codegen.loaders.schemaloader.load_file") as load_mock: load_mock.return_value = schema.Schema([]) yield load_mock.return_value assert load_mock.mock_calls == [ @@ -58,7 +58,7 @@ def input(opts, tmp_path): @pytest.fixture def dbscheme_input(opts, tmp_path): opts.dbscheme = tmp_path / dbscheme_file - with mock.patch("swift.codegen.loaders.dbschemeloader.iterload") as load_mock: + with mock.patch("misc.codegen.loaders.dbschemeloader.iterload") as load_mock: load_mock.entities = [] load_mock.side_effect = lambda _: load_mock.entities yield load_mock diff --git a/swift/README.md b/swift/README.md index b6d7f1d57d2..766c5ff1dce 100644 --- a/swift/README.md +++ b/swift/README.md @@ -39,6 +39,8 @@ bazel run //swift/codegen to update generated files. This can be shortened to `bazel run codegen` if you are in the `swift` directory. +You can also run `../misc/codegen/codegen.py`, as long as you are beneath the `swift` directory. + ## IDE setup ### CLion and the native bazel plugin diff --git a/swift/actions/build-and-test/action.yml b/swift/actions/build-and-test/action.yml index c0c1e7f7e31..2e084e9a97b 100644 --- a/swift/actions/build-and-test/action.yml +++ b/swift/actions/build-and-test/action.yml @@ -62,7 +62,7 @@ runs: if : ${{ github.event_name == 'pull_request' }} shell: bash run: | - bazel test //swift/codegen/test + bazel test //misc/codegen/test - name: Run qltest tests if : ${{ github.event_name == 'pull_request' }} shell: bash diff --git a/swift/codegen.conf b/swift/codegen.conf index 8f5965bef5c..b620e52d04f 100644 --- a/swift/codegen.conf +++ b/swift/codegen.conf @@ -5,3 +5,5 @@ --ql-stub-output=ql/lib/codeql/swift/elements --ql-test-output=ql/test/extractor-tests/generated --generated-registry=ql/.generated.list +--script-name=codegen/codegen.py +--trap-library=swift/extractor/trap diff --git a/swift/codegen/BUILD.bazel b/swift/codegen/BUILD.bazel index fcf72c0c2ae..bc807e9329a 100644 --- a/swift/codegen/BUILD.bazel +++ b/swift/codegen/BUILD.bazel @@ -1,19 +1,15 @@ -load("@swift_codegen_deps//:requirements.bzl", "requirement") +load("@bazel_skylib//rules:native_binary.bzl", "native_binary") -py_binary( +native_binary( name = "codegen", - srcs = ["codegen.py"], + out = "codegen", + src = "//misc/codegen", data = [ "//swift:schema", "//swift:codegen_conf", - "//swift/codegen/templates:cpp", - "//swift/codegen/templates:trap", ], args = [ "--configuration-file=$(location //swift:codegen_conf)", ], visibility = ["//swift:__subpackages__"], - deps = [ - "//swift/codegen/generators", - ], ) diff --git a/swift/codegen/generators/BUILD.bazel b/swift/codegen/generators/BUILD.bazel deleted file mode 100644 index dbcf529037c..00000000000 --- a/swift/codegen/generators/BUILD.bazel +++ /dev/null @@ -1,11 +0,0 @@ -load("@swift_codegen_deps//:requirements.bzl", "requirement") - -py_library( - name = "generators", - srcs = glob(["*.py"]), - visibility = ["//swift/codegen:__subpackages__"], - deps = [ - "//swift/codegen/lib", - "//swift/codegen/loaders", - ], -) diff --git a/swift/extractor/trap/BUILD.bazel b/swift/extractor/trap/BUILD.bazel index 75e0caede55..0db9b3b3248 100644 --- a/swift/extractor/trap/BUILD.bazel +++ b/swift/extractor/trap/BUILD.bazel @@ -14,7 +14,7 @@ genrule( for ext in ("h", "cpp") ], cmd = " ".join([ - "$(location //swift/codegen)", + "$(location //misc/codegen)", "--generate=dbscheme,trap,cpp", "--dbscheme=$(RULEDIR)/generated/swift.dbscheme", "--cpp-output=$(RULEDIR)/generated", @@ -23,7 +23,7 @@ genrule( "--schema=$(location //swift:schema)", "--script-name=codegen/codegen.py", ]), - exec_tools = ["//swift/codegen", "//swift:schema"], + exec_tools = ["//misc/codegen", "//swift:schema"], ) filegroup( diff --git a/swift/schema.py b/swift/schema.py index 82eab1b4119..9c94a642369 100644 --- a/swift/schema.py +++ b/swift/schema.py @@ -9,7 +9,7 @@ This file should be kept simple: For how documentation of generated QL code works, please read schema_documentation.md. """ -from swift.codegen.lib.schemadefs import * +from misc.codegen.lib.schemadefs import * include("prefix.dbscheme")