Skip to content

Commit

Permalink
Move swift/codegen to misc/codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
redsun82 committed Feb 27, 2023
1 parent 6d192cd commit cdd4e80
Show file tree
Hide file tree
Showing 65 changed files with 116 additions and 86 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
paths:
- "swift/**"
- "misc/bazel/**"
- "misc/codegen/**"
- "*.bazel*"
- .github/workflows/swift.yml
- .github/actions/**
Expand All @@ -19,6 +20,7 @@ on:
paths:
- "swift/**"
- "misc/bazel/**"
- "misc/codegen/**"
- "*.bazel*"
- .github/workflows/swift.yml
- .github/actions/**
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions misc/bazel/workspace.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
6 changes: 4 additions & 2 deletions misc/bazel/workspace_deps.bzl
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.
14 changes: 14 additions & 0 deletions misc/codegen/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
8 changes: 4 additions & 4 deletions swift/codegen/README.md → misc/codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
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))
* C++ structured classes (see [`cppgen.py`](generators/cppgen.py))

## 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
Expand All @@ -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).
4 changes: 2 additions & 2 deletions swift/codegen/codegen.py → misc/codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
_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:
Expand Down
11 changes: 11 additions & 0 deletions misc/codegen/generators/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

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__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@

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__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
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__)

Expand Down
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions swift/codegen/lib/paths.py → misc/codegen/lib/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
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()
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pathlib
import re
from swift.codegen.lib import dbscheme
from misc.codegen.lib import dbscheme


class _Re:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from dataclasses import dataclass
from toposort import toposort_flatten

from swift.codegen.lib import schema, schemadefs
from misc.codegen.lib import schema, schemadefs


@dataclass
Expand Down Expand Up @@ -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):
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package(default_visibility = ["//swift:__subpackages__"])
package(default_visibility = ["//misc/codegen:__subpackages__"])

filegroup(
name = "trap",
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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"),
],
)
Expand All @@ -17,7 +17,7 @@ py_library(
srcs = [src],
deps = [
":utils",
"//swift/codegen/generators",
"//misc/codegen/generators",
],
)
for src in glob(["test_*.py"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import pytest

from swift.codegen.lib import cpp
from misc.codegen.lib import cpp


@pytest.mark.parametrize("keyword", cpp.cpp_keywords)
Expand Down
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
Original file line number Diff line number Diff line change
@@ -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"))

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from swift.codegen.test.utils import *
from misc.codegen.test.utils import *

import hashlib

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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():
Expand Down
Loading

0 comments on commit cdd4e80

Please sign in to comment.