Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 55ff710

Browse files
committed
Kotlin: support embeddable build in bazel
1 parent 5313288 commit 55ff710

3 files changed

Lines changed: 74 additions & 10 deletions

File tree

MODULE.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ node.toolchain(
5656
)
5757
use_repo(node, "nodejs", "nodejs_toolchains")
5858

59-
kotlin_extractor_deps = use_extension("//java/kotlin-extractor:extension.bzl", "kotlin_extractor_deps")
59+
kotlin_extractor_deps = use_extension("//java/kotlin-extractor:deps.bzl", "kotlin_extractor_deps")
6060

6161
# following list can be kept in sync by running `bazel mod tidy` in `codeql`
6262
use_repo(
6363
kotlin_extractor_deps,
64+
"codeql_kotlin_embeddable",
6465
"kotlin-compiler-1.4.32",
6566
"kotlin-compiler-1.5.0",
6667
"kotlin-compiler-1.5.10",

java/kotlin-extractor/BUILD.bazel

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,31 @@
1+
# notice that this is used in the `@codeql_koblin_embeddable` external repo, which means we need to
2+
# reference explicitly @codeql
13
load(
2-
"//java/kotlin-extractor:versions.bzl",
4+
"@codeql//java/kotlin-extractor:versions.bzl",
35
"VERSIONS",
46
"get_compatilibity_sources",
57
"version_less",
68
)
79
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
810
load("@rules_kotlin//kotlin:core.bzl", "kt_kotlinc_options")
911

12+
_for_embeddable = repo_name().endswith("codeql_kotlin_embeddable")
13+
14+
_common_extractor_name_prefix = "codeql-extractor-kotlin"
15+
16+
_extractor_name_prefix = "%s-%s" % (
17+
_common_extractor_name_prefix,
18+
"embeddable" if _for_embeddable else "standalone",
19+
)
20+
1021
py_binary(
1122
name = "generate_dbscheme",
1223
srcs = ["generate_dbscheme.py"],
1324
)
1425

1526
genrule(
1627
name = "generated-dbscheme",
17-
srcs = ["//java:dbscheme"],
28+
srcs = ["@codeql//java:dbscheme"],
1829
outs = ["KotlinExtractorDbScheme.kt"],
1930
cmd = "$(execpath :generate_dbscheme) $< $@",
2031
tools = [":generate_dbscheme"],
@@ -56,14 +67,14 @@ _resources = [
5667
for _, tgt in _resources
5768
],
5869
cmd = "\n".join([
59-
"echo codeql-extractor-kotlin-standalone-%s > $(RULEDIR)/%s/com/github/codeql/extractor.name" % (v, v),
70+
"echo %s-%s > $(RULEDIR)/%s/com/github/codeql/extractor.name" % (_extractor_name_prefix, v, v),
6071
] + [
6172
"cp $(execpath %s) $(RULEDIR)/%s/%s" % (src, v, tgt)
6273
for src, tgt in _resources
6374
]),
6475
),
6576
kt_jvm_library(
66-
name = "codeql-extractor-kotlin-standalone-%s" % v,
77+
name = "%s-%s" % (_extractor_name_prefix, v),
6778
srcs =
6879
[":generated-dbscheme"] +
6980
glob(
@@ -75,18 +86,31 @@ _resources = [
7586
) + get_compatilibity_sources(v, "src/main/kotlin/utils/versions"),
7687
kotlinc_opts = ":kotlinc-options-%s" % v,
7788
module_name = "codeql-kotlin-extractor",
78-
resource_strip_prefix = "%s/%s" % (
89+
resource_strip_prefix = "../%s/%s" % (
90+
repo_name(),
91+
v,
92+
) if _for_embeddable else "%s/%s" % (
7993
package_name(),
8094
v,
8195
),
8296
resources = [
8397
":resources-%s" % v,
8498
],
99+
visibility = ["//visibility:public"],
85100
deps = [
86-
"@kotlin-compiler-%s" % v,
101+
"@kotlin-compiler%s-%s" % (
102+
"-embeddable" if _for_embeddable else "",
103+
v,
104+
),
87105
"@kotlin-stdlib-%s" % v,
88106
],
89107
),
108+
# if in main repository, alias the embeddable versions from the modified @codeql_kotlin_embeddable repo
109+
alias(
110+
name = "%s-embeddable-%s" % (_common_extractor_name_prefix, v),
111+
actual = "@codeql_kotlin_embeddable//:%s-embeddable-%s" % (_common_extractor_name_prefix, v),
112+
visibility = ["//visibility:public"],
113+
) if not _for_embeddable else None,
90114
)
91115
for v in VERSIONS
92116
]
Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,52 @@ _kotlin_dep = repository_rule(
3434
implementation = _kotlin_dep_impl,
3535
)
3636

37+
def _walk(dir):
38+
res = []
39+
next_dirs = [dir]
40+
41+
# loops must be bounded in starlark
42+
for i in range(100):
43+
current_dirs = next_dirs
44+
next_dirs = []
45+
for d in current_dirs:
46+
children = d.readdir()
47+
next_dirs.extend([c for c in children if c.is_dir])
48+
res.extend([c for c in children if not c.is_dir])
49+
if not next_dirs:
50+
break
51+
return res
52+
53+
def _embeddable_source_impl(repository_ctx):
54+
src_dir = repository_ctx.path(Label("//java/kotlin-extractor:src"))
55+
for src in _walk(src_dir):
56+
contents = repository_ctx.read(src)
57+
contents = contents.replace(
58+
"import com.intellij",
59+
"import org.jetbrains.kotlin.com.intellij",
60+
)
61+
repository_ctx.file(str(src).replace(str(src_dir), "src"), contents)
62+
repository_ctx.symlink(
63+
Label("//java/kotlin-extractor:generate_dbscheme.py"),
64+
"generate_dbscheme.py",
65+
)
66+
repository_ctx.symlink(
67+
Label("//java/kotlin-extractor:BUILD.bazel"),
68+
"BUILD.bazel",
69+
)
70+
71+
_embeddable_source = repository_rule(implementation = _embeddable_source_impl)
72+
73+
def _add_rule(rules, rule, *, name, **kwargs):
74+
rule(name = name, **kwargs)
75+
rules.append(name)
76+
3777
def _kotlin_deps_impl(module_ctx):
3878
deps = []
3979
for v in VERSIONS:
4080
for lib in ("compiler", "compiler-embeddable", "stdlib"):
41-
dep = "kotlin-%s-%s" % (lib, v)
42-
_kotlin_dep(name = dep)
43-
deps.append(dep)
81+
_add_rule(deps, _kotlin_dep, name = "kotlin-%s-%s" % (lib, v))
82+
_add_rule(deps, _embeddable_source, name = "codeql_kotlin_embeddable")
4483
return module_ctx.extension_metadata(
4584
root_module_direct_deps = deps,
4685
root_module_direct_dev_deps = [],

0 commit comments

Comments
 (0)