Kotlin: working standalone extractor built with bazel

This commit is contained in:
Paolo Tranquilli
2024-04-04 10:23:26 +02:00
parent c242466d31
commit 47ff1c1ee6
6 changed files with 100 additions and 58 deletions

View File

@@ -57,22 +57,52 @@ node.toolchain(
use_repo(node, "nodejs", "nodejs_toolchains")
kotlin_extractor_deps = use_extension("//java/kotlin-extractor:extension.bzl", "kotlin_extractor_deps")
# following list can be kept in sync by running `bazel mod tidy` in `codeql`
use_repo(
kotlin_extractor_deps,
"kotlin_extractor_dep_1.4.32",
"kotlin_extractor_dep_1.5.0",
"kotlin_extractor_dep_1.5.10",
"kotlin_extractor_dep_1.5.20",
"kotlin_extractor_dep_1.5.30",
"kotlin_extractor_dep_1.6.0",
"kotlin_extractor_dep_1.6.20",
"kotlin_extractor_dep_1.7.0",
"kotlin_extractor_dep_1.7.20",
"kotlin_extractor_dep_1.8.0",
"kotlin_extractor_dep_1.9.0-Beta",
"kotlin_extractor_dep_1.9.20-Beta",
"kotlin_extractor_dep_2.0.0-Beta4",
"kotlin_extractor_dep_2.0.255-SNAPSHOT",
"kotlin-compiler-1.4.32",
"kotlin-compiler-1.5.0",
"kotlin-compiler-1.5.10",
"kotlin-compiler-1.5.20",
"kotlin-compiler-1.5.30",
"kotlin-compiler-1.6.0",
"kotlin-compiler-1.6.20",
"kotlin-compiler-1.7.0",
"kotlin-compiler-1.7.20",
"kotlin-compiler-1.8.0",
"kotlin-compiler-1.9.0-Beta",
"kotlin-compiler-1.9.20-Beta",
"kotlin-compiler-2.0.0-Beta4",
"kotlin-compiler-2.0.255-SNAPSHOT",
"kotlin-compiler-embeddable-1.4.32",
"kotlin-compiler-embeddable-1.5.0",
"kotlin-compiler-embeddable-1.5.10",
"kotlin-compiler-embeddable-1.5.20",
"kotlin-compiler-embeddable-1.5.30",
"kotlin-compiler-embeddable-1.6.0",
"kotlin-compiler-embeddable-1.6.20",
"kotlin-compiler-embeddable-1.7.0",
"kotlin-compiler-embeddable-1.7.20",
"kotlin-compiler-embeddable-1.8.0",
"kotlin-compiler-embeddable-1.9.0-Beta",
"kotlin-compiler-embeddable-1.9.20-Beta",
"kotlin-compiler-embeddable-2.0.0-Beta4",
"kotlin-compiler-embeddable-2.0.255-SNAPSHOT",
"kotlin-stdlib-1.4.32",
"kotlin-stdlib-1.5.0",
"kotlin-stdlib-1.5.10",
"kotlin-stdlib-1.5.20",
"kotlin-stdlib-1.5.30",
"kotlin-stdlib-1.6.0",
"kotlin-stdlib-1.6.20",
"kotlin-stdlib-1.7.0",
"kotlin-stdlib-1.7.20",
"kotlin-stdlib-1.8.0",
"kotlin-stdlib-1.9.0-Beta",
"kotlin-stdlib-1.9.20-Beta",
"kotlin-stdlib-2.0.0-Beta4",
"kotlin-stdlib-2.0.255-SNAPSHOT",
)
register_toolchains(

View File

@@ -5,7 +5,7 @@ load(
"version_less",
)
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_javac_options", "kt_kotlinc_options")
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options")
py_binary(
name = "generate_dbscheme",
@@ -20,10 +20,13 @@ genrule(
tools = [":generate_dbscheme"],
)
kt_javac_options(
name = "javac-options",
warn = "off",
)
_resources = [
(
r,
r[len("src/main/resources/"):],
)
for r in glob(["src/main/resources/**"])
]
[
(
@@ -40,8 +43,27 @@ kt_javac_options(
],
x_suppress_version_warnings = True,
),
# * extractor.name is different for each version, so we need to put it in different output dirs
# * in order to put it in `resources`, we need to define `resource_strip_prefix` to strip this version
# * `resource_strip_prefix` is unique per jar, so we must also put other resources under the same version prefix
genrule(
name = "resources-%s" % v,
srcs = [src for src, _ in _resources],
outs = [
"%s/com/github/codeql/extractor.name" % v,
] + [
"%s/%s" % (v, tgt)
for _, tgt in _resources
],
cmd = "\n".join([
"echo codeql-extractor-kotlin-standalone-%s > $(RULEDIR)/%s/com/github/codeql/extractor.name" % (v, v),
] + [
"cp $(execpath %s) $(RULEDIR)/%s/%s" % (src, v, tgt)
for src, tgt in _resources
]),
),
kt_jvm_library(
name = "kotlin-extractor-%s" % v,
name = "codeql-extractor-kotlin-standalone-%s" % v,
srcs =
[":generated-dbscheme"] +
glob(
@@ -51,12 +73,18 @@ kt_javac_options(
],
exclude = ["src/main/kotlin/utils/versions/**"],
) + get_compatilibity_sources(v, "src/main/kotlin/utils/versions"),
javac_opts = ":javac-options",
kotlinc_opts = ":kotlinc-options-%s" % v,
module_name = "codeql-kotlin-extractor",
resource_strip_prefix = "%s/%s" % (
package_name(),
v,
),
resources = [
":resources-%s" % v,
],
deps = [
"@kotlin_extractor_dep_%s//:kotlin-compiler" % v,
"@kotlin_extractor_dep_%s//:kotlin-stdlib" % v,
"@kotlin-compiler-%s" % v,
"@kotlin-stdlib-%s" % v,
],
),
)

BIN
java/kotlin-extractor/deps/empty.jar (Stored with Git LFS)

Binary file not shown.

Binary file not shown.

View File

@@ -4,39 +4,22 @@ load("//misc/bazel:lfs.bzl", "lfs_smudge")
_kotlin_dep_build = """
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_import")
package(default_visibility = ["//visibility:public"])
kt_jvm_import(
name = "kotlin-compiler",
jar = "kotlin-compiler-{version}.jar",
)
kt_jvm_import(
name = "kotlin-compiler-embeddable",
jar = "kotlin-compiler-embeddable-{version}.jar",
)
kt_jvm_import(
name = "kotlin-stdlib",
jar = "kotlin-stdlib-{version}.jar",
name = "{name}",
jar = "{name}.jar",
visibility = ["//visibility:public"],
)
"""
def _kotlin_dep_impl(repository_ctx):
_, sep, version = repository_ctx.name.rpartition("_")
if not sep:
fail("rule @%s malformed, name should be <prefix>_<kotlin version>")
def _get_dep(repository_ctx, name):
return repository_ctx.path(Label("//java/kotlin-extractor/deps:%s" % name))
sources = [
# "empty.jar",
"kotlin-compiler-%s.jar" % version,
"kotlin-compiler-embeddable-%s.jar" % version,
"kotlin-stdlib-%s.jar" % version,
]
sources = [repository_ctx.path(Label("//java/kotlin-extractor/deps:%s" % p)) for p in sources]
lfs_smudge(repository_ctx, sources)
def _kotlin_dep_impl(repository_ctx):
_, _, name = repository_ctx.name.rpartition("~")
lfs_smudge(repository_ctx, [_get_dep(repository_ctx, name + ".jar")])
# for some reason rules_kotlin warns about these jars missing, this is to silence those warnings
empty = _get_dep(repository_ctx, "empty.zip")
for jar in (
"annotations-13.0.jar",
"kotlin-stdlib.jar",
@@ -44,17 +27,20 @@ def _kotlin_dep_impl(repository_ctx):
"kotlin-script-runtime.jar",
"trove4j.jar",
):
repository_ctx.symlink("empty.jar", jar)
repository_ctx.file("BUILD.bazel", _kotlin_dep_build.format(version = version))
repository_ctx.symlink(empty, jar)
repository_ctx.file("BUILD.bazel", _kotlin_dep_build.format(name = name))
_kotlin_dep = repository_rule(implementation = _kotlin_dep_impl)
_kotlin_dep = repository_rule(
implementation = _kotlin_dep_impl,
)
def _kotlin_deps_impl(module_ctx):
deps = []
for v in VERSIONS:
dep = "kotlin_extractor_dep_%s" % v
_kotlin_dep(name = dep)
deps.append(dep)
for lib in ("compiler", "compiler-embeddable", "stdlib"):
dep = "kotlin-%s-%s" % (lib, v)
_kotlin_dep(name = dep)
deps.append(dep)
return module_ctx.extension_metadata(
root_module_direct_deps = deps,
root_module_direct_dev_deps = [],

View File

@@ -1,3 +1,4 @@
# when updating this list, `bazel mod tidy` should be run from `codeql` to update `MODULE.bazel`
VERSIONS = [
"1.4.32",
"1.5.0",