Kotlin: support embeddable build in bazel

This commit is contained in:
Paolo Tranquilli
2024-04-04 11:26:54 +02:00
parent 5313288b8e
commit 55ff7109fa
3 changed files with 74 additions and 10 deletions

View File

@@ -56,11 +56,12 @@ node.toolchain(
)
use_repo(node, "nodejs", "nodejs_toolchains")
kotlin_extractor_deps = use_extension("//java/kotlin-extractor:extension.bzl", "kotlin_extractor_deps")
kotlin_extractor_deps = use_extension("//java/kotlin-extractor:deps.bzl", "kotlin_extractor_deps")
# following list can be kept in sync by running `bazel mod tidy` in `codeql`
use_repo(
kotlin_extractor_deps,
"codeql_kotlin_embeddable",
"kotlin-compiler-1.4.32",
"kotlin-compiler-1.5.0",
"kotlin-compiler-1.5.10",

View File

@@ -1,5 +1,7 @@
# notice that this is used in the `@codeql_koblin_embeddable` external repo, which means we need to
# reference explicitly @codeql
load(
"//java/kotlin-extractor:versions.bzl",
"@codeql//java/kotlin-extractor:versions.bzl",
"VERSIONS",
"get_compatilibity_sources",
"version_less",
@@ -7,6 +9,15 @@ load(
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options")
_for_embeddable = repo_name().endswith("codeql_kotlin_embeddable")
_common_extractor_name_prefix = "codeql-extractor-kotlin"
_extractor_name_prefix = "%s-%s" % (
_common_extractor_name_prefix,
"embeddable" if _for_embeddable else "standalone",
)
py_binary(
name = "generate_dbscheme",
srcs = ["generate_dbscheme.py"],
@@ -14,7 +25,7 @@ py_binary(
genrule(
name = "generated-dbscheme",
srcs = ["//java:dbscheme"],
srcs = ["@codeql//java:dbscheme"],
outs = ["KotlinExtractorDbScheme.kt"],
cmd = "$(execpath :generate_dbscheme) $< $@",
tools = [":generate_dbscheme"],
@@ -56,14 +67,14 @@ _resources = [
for _, tgt in _resources
],
cmd = "\n".join([
"echo codeql-extractor-kotlin-standalone-%s > $(RULEDIR)/%s/com/github/codeql/extractor.name" % (v, v),
"echo %s-%s > $(RULEDIR)/%s/com/github/codeql/extractor.name" % (_extractor_name_prefix, v, v),
] + [
"cp $(execpath %s) $(RULEDIR)/%s/%s" % (src, v, tgt)
for src, tgt in _resources
]),
),
kt_jvm_library(
name = "codeql-extractor-kotlin-standalone-%s" % v,
name = "%s-%s" % (_extractor_name_prefix, v),
srcs =
[":generated-dbscheme"] +
glob(
@@ -75,18 +86,31 @@ _resources = [
) + get_compatilibity_sources(v, "src/main/kotlin/utils/versions"),
kotlinc_opts = ":kotlinc-options-%s" % v,
module_name = "codeql-kotlin-extractor",
resource_strip_prefix = "%s/%s" % (
resource_strip_prefix = "../%s/%s" % (
repo_name(),
v,
) if _for_embeddable else "%s/%s" % (
package_name(),
v,
),
resources = [
":resources-%s" % v,
],
visibility = ["//visibility:public"],
deps = [
"@kotlin-compiler-%s" % v,
"@kotlin-compiler%s-%s" % (
"-embeddable" if _for_embeddable else "",
v,
),
"@kotlin-stdlib-%s" % v,
],
),
# if in main repository, alias the embeddable versions from the modified @codeql_kotlin_embeddable repo
alias(
name = "%s-embeddable-%s" % (_common_extractor_name_prefix, v),
actual = "@codeql_kotlin_embeddable//:%s-embeddable-%s" % (_common_extractor_name_prefix, v),
visibility = ["//visibility:public"],
) if not _for_embeddable else None,
)
for v in VERSIONS
]

View File

@@ -34,13 +34,52 @@ _kotlin_dep = repository_rule(
implementation = _kotlin_dep_impl,
)
def _walk(dir):
res = []
next_dirs = [dir]
# loops must be bounded in starlark
for i in range(100):
current_dirs = next_dirs
next_dirs = []
for d in current_dirs:
children = d.readdir()
next_dirs.extend([c for c in children if c.is_dir])
res.extend([c for c in children if not c.is_dir])
if not next_dirs:
break
return res
def _embeddable_source_impl(repository_ctx):
src_dir = repository_ctx.path(Label("//java/kotlin-extractor:src"))
for src in _walk(src_dir):
contents = repository_ctx.read(src)
contents = contents.replace(
"import com.intellij",
"import org.jetbrains.kotlin.com.intellij",
)
repository_ctx.file(str(src).replace(str(src_dir), "src"), contents)
repository_ctx.symlink(
Label("//java/kotlin-extractor:generate_dbscheme.py"),
"generate_dbscheme.py",
)
repository_ctx.symlink(
Label("//java/kotlin-extractor:BUILD.bazel"),
"BUILD.bazel",
)
_embeddable_source = repository_rule(implementation = _embeddable_source_impl)
def _add_rule(rules, rule, *, name, **kwargs):
rule(name = name, **kwargs)
rules.append(name)
def _kotlin_deps_impl(module_ctx):
deps = []
for v in VERSIONS:
for lib in ("compiler", "compiler-embeddable", "stdlib"):
dep = "kotlin-%s-%s" % (lib, v)
_kotlin_dep(name = dep)
deps.append(dep)
_add_rule(deps, _kotlin_dep, name = "kotlin-%s-%s" % (lib, v))
_add_rule(deps, _embeddable_source, name = "codeql_kotlin_embeddable")
return module_ctx.extension_metadata(
root_module_direct_deps = deps,
root_module_direct_dev_deps = [],