Skip to content

Commit fe5c6fa

Browse files
authored
Make loading the kotlinc repository lazy (#1126)
* Also, cleanup the imports of libraries to be easier to reason about. * .klib remains an open question. Will need ot address in the near future. fixes 1106
1 parent 257b9ad commit fe5c6fa

File tree

8 files changed

+218
-113
lines changed

8 files changed

+218
-113
lines changed

kotlin/compiler/compiler.bzl

Lines changed: 22 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,25 @@ load("@rules_java//java:defs.bzl", "java_import")
1616
load("//kotlin:js.bzl", "kt_js_import")
1717
load("//kotlin:jvm.bzl", "kt_jvm_import")
1818
load("//kotlin/internal:defs.bzl", _KT_COMPILER_REPO = "KT_COMPILER_REPO")
19-
20-
_KT_COMPILER_REPO_PREFIX = "@" + _KT_COMPILER_REPO + "//:"
19+
load("@com_github_jetbrains_kotlin//:artifacts.bzl", "KOTLINC_ARTIFACTS")
20+
21+
def _import_artifacts(artifacts, rule_kind):
22+
_import_labels(artifacts.plugin, rule_kind)
23+
_import_labels(artifacts.runtime, rule_kind)
24+
_import_labels(artifacts.compile, rule_kind, neverlink = 1)
25+
26+
def _import_labels(labels, rule_kind, **rule_args):
27+
for label in labels:
28+
if "-sources" in label:
29+
continue
30+
args = dict(rule_args.items())
31+
args["visibility"] = ["//visibility:public"]
32+
args["name"] = label
33+
args["jars"] = ["@%s//:%s" % (_KT_COMPILER_REPO, label)]
34+
sources = label + "-sources"
35+
if sources in labels:
36+
args["srcjar"] = "@%s//:%s" % (_KT_COMPILER_REPO, sources)
37+
rule_kind(**args)
2138

2239
def kt_configure_compiler():
2340
"""
@@ -28,100 +45,6 @@ def kt_configure_compiler():
2845
if native.package_name() != "kotlin/compiler":
2946
fail("kt_configure_compiler must be called in kotlin/compiler not %s" % native.package_name())
3047

31-
kt_jvm_import(
32-
name = "annotations",
33-
jar = _KT_COMPILER_REPO_PREFIX + "lib/annotations-13.0.jar",
34-
neverlink = 1,
35-
)
36-
37-
kt_jvm_import(
38-
name = "jvm-abi-gen",
39-
jar = _KT_COMPILER_REPO_PREFIX + "lib/jvm-abi-gen.jar",
40-
)
41-
42-
# Kotlin dependencies that are internal to this repo and are meant to be loaded manually into a classloader.
43-
[
44-
kt_jvm_import(
45-
name = "kotlin-%s" % art,
46-
jar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art,
47-
neverlink = 1,
48-
)
49-
for art in [
50-
"annotation-processing",
51-
"annotation-processing-runtime",
52-
"compiler",
53-
]
54-
]
55-
56-
kt_jvm_import(
57-
name = "kotlinx-serialization-compiler-plugin",
58-
jar = _KT_COMPILER_REPO_PREFIX + "lib/kotlinx-serialization-compiler-plugin.jar",
59-
)
60-
61-
kt_jvm_import(
62-
name = "allopen-compiler-plugin",
63-
jar = _KT_COMPILER_REPO_PREFIX + "lib/allopen-compiler-plugin.jar",
64-
)
65-
66-
kt_jvm_import(
67-
name = "noarg-compiler-plugin",
68-
jar = _KT_COMPILER_REPO_PREFIX + "lib/noarg-compiler-plugin.jar",
69-
)
70-
71-
kt_jvm_import(
72-
name = "sam-with-receiver-compiler-plugin",
73-
jar = _KT_COMPILER_REPO_PREFIX + "lib/sam-with-receiver-compiler-plugin.jar",
74-
)
75-
76-
kt_jvm_import(
77-
name = "parcelize-compiler-plugin",
78-
jar = _KT_COMPILER_REPO_PREFIX + "lib/parcelize-compiler.jar",
79-
)
80-
81-
kt_jvm_import(
82-
name = "parcelize-runtime",
83-
jar = _KT_COMPILER_REPO_PREFIX + "lib/parcelize-runtime.jar",
84-
)
85-
86-
# Kotlin dependencies that are internal to this repo and may be linked.
87-
[
88-
java_import(
89-
name = "kotlin-%s" % art,
90-
jars = [_KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art],
91-
)
92-
for art in [
93-
"preloader",
94-
]
95-
]
96-
97-
# The Kotlin standard libraries. These should be setup in a Toolchain.
98-
[
99-
kt_jvm_import(
100-
name = "kotlin-%s" % art,
101-
jar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art,
102-
srcjar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s-sources.jar" % art,
103-
visibility = ["//visibility:public"],
104-
)
105-
for art in [
106-
"stdlib",
107-
"stdlib-jdk7",
108-
"stdlib-jdk8",
109-
"reflect",
110-
"test",
111-
"script-runtime",
112-
]
113-
]
114-
115-
# The Kotlin JS standard libraries. These should be setup in a Toolchain.
116-
[
117-
kt_js_import(
118-
name = "kotlin-%s" % art,
119-
jars = [_KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art],
120-
srcjar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s-sources.jar" % art,
121-
visibility = ["//visibility:public"],
122-
)
123-
for art in [
124-
"test-js",
125-
"stdlib-js",
126-
]
127-
]
48+
_import_artifacts(KOTLINC_ARTIFACTS.js, kt_js_import)
49+
_import_artifacts(KOTLINC_ARTIFACTS.jvm, kt_jvm_import)
50+
_import_artifacts(KOTLINC_ARTIFACTS.core, kt_jvm_import)

kotlin/internal/jvm/impl.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def _unify_jars(ctx):
167167
elif file.basename.endswith(".jar"):
168168
jars.append(file)
169169
else:
170-
fail("a jar pointing to a filegroup must either end with -sources.jar or .jar")
170+
fail("a jar pointing to a filegroup must either end with -sources.jar or .jar: %s", file)
171171

172172
if len(jars) > 1:
173173
fail("Got more than one jar, this is an error create an issue: %s" % jars)

src/main/starlark/core/repositories/BUILD

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
2-
31
# Copyright 2018 The Bazel Authors. All rights reserved.
42
#
53
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +11,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1311
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1412
# See the License for the specific language governing permissions and
1513
# limitations under the License.
14+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1615
load("//src/main/starlark/release:packager.bzl", "release_archive")
1716

1817
release_archive(

src/main/starlark/core/repositories/BUILD.com_github_jetbrains_kotlin.bazel

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
1616
package(default_visibility = ["//visibility:public"])
1717

1818
# Kotlin home filegroup containing everything that is needed.
19+
[
20+
filegroup(
21+
name = name.replace(".", "_"),
22+
srcs = glob(["" + name]),
23+
)
24+
for name in glob(["lib/**"])
25+
]
26+
1927
filegroup(
2028
name = "home",
2129
srcs = glob(["**"]),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Copyright 2018 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
package(default_visibility = ["//visibility:public"])
15+
16+
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
17+
load(":artifacts.bzl", "KOTLINC_ARTIFACT_LIST")
18+
19+
bzl_library(
20+
name = "capabilities",
21+
srcs = glob(["*.bzl"]),
22+
)
23+
24+
alias(
25+
name = "home",
26+
actual = "@$git_repo$//:home",
27+
)
28+
29+
[
30+
alias(
31+
name = label,
32+
actual = "@$git_repo$//:%s" % file.replace(".", "_"),
33+
)
34+
for (label, file) in KOTLINC_ARTIFACT_LIST.items()
35+
]

src/main/starlark/core/repositories/compiler.bzl

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
"""
2+
Defines kotlin compiler repositories.
3+
"""
4+
15
def _kotlin_compiler_impl(repository_ctx):
2-
"""Creates the kotlinc repository."""
36
attr = repository_ctx.attr
47
repository_ctx.download_and_extract(
58
attr.urls,
69
sha256 = attr.sha256,
710
stripPrefix = "kotlinc",
811
)
12+
repository_ctx.template(
13+
"BUILD.bazel",
14+
attr._template,
15+
executable = False,
16+
)
17+
18+
def _kotlin_capabilities_impl(repository_ctx):
19+
"""Creates the kotlinc repository."""
20+
attr = repository_ctx.attr
921
repository_ctx.file(
1022
"WORKSPACE",
1123
content = """workspace(name = "%s")""" % attr.name,
@@ -14,8 +26,15 @@ def _kotlin_compiler_impl(repository_ctx):
1426
"BUILD.bazel",
1527
attr._template,
1628
executable = False,
29+
substitutions = {
30+
"$git_repo$": attr.git_repository_name,
31+
},
32+
)
33+
repository_ctx.template(
34+
"artifacts.bzl",
35+
attr._artifacts_template,
36+
executable = False,
1737
)
18-
1938
repository_ctx.template(
2039
"capabilities.bzl",
2140
_get_capability_template(attr.compiler_version, attr._capabilities_templates),
@@ -45,7 +64,31 @@ _CAPABILITIES_TEMPLATES = {
4564
"2.0": "//src/main/starlark/core/repositories/kotlin:capabilities_2.0.bzl.com_github_jetbrains_kotlin.bazel",
4665
}
4766

48-
kotlin_compiler_repository = repository_rule(
67+
kotlin_capabilities_repository = repository_rule(
68+
implementation = _kotlin_capabilities_impl,
69+
attrs = {
70+
"git_repository_name": attr.string(
71+
doc = "Name of the repository containing kotlin compiler libraries",
72+
),
73+
"compiler_version": attr.string(
74+
doc = "compiler version",
75+
),
76+
"_capabilities_templates": attr.label_list(
77+
doc = "compiler capabilities file templates",
78+
default = _CAPABILITIES_TEMPLATES.values(),
79+
),
80+
"_template": attr.label(
81+
doc = "repository build file template",
82+
default = ":BUILD.kotlin_capabilities.bazel",
83+
),
84+
"_artifacts_template": attr.label(
85+
doc = "kotlinc artifacts template",
86+
default = "//src/main/starlark/core/repositories/kotlin:artifacts.bzl",
87+
),
88+
},
89+
)
90+
91+
kotlin_compiler_git_repository = repository_rule(
4992
implementation = _kotlin_compiler_impl,
5093
attrs = {
5194
"urls": attr.string_list(
@@ -55,16 +98,25 @@ kotlin_compiler_repository = repository_rule(
5598
"sha256": attr.string(
5699
doc = "sha256 of the compiler archive",
57100
),
58-
"compiler_version": attr.string(
59-
doc = "compiler version",
60-
),
61101
"_template": attr.label(
62102
doc = "repository build file template",
63103
default = ":BUILD.com_github_jetbrains_kotlin.bazel",
64104
),
65-
"_capabilities_templates": attr.label_list(
66-
doc = "compiler capabilities file templates",
67-
default = _CAPABILITIES_TEMPLATES.values(),
68-
),
69105
},
70106
)
107+
108+
def kotlin_compiler_repository(name, urls, sha256, compiler_version):
109+
"""
110+
Creates two repositories, necessary for lazily loading the kotlin compiler binaries for git.
111+
"""
112+
git_repo = name + "_git"
113+
kotlin_compiler_git_repository(
114+
name = git_repo,
115+
urls = urls,
116+
sha256 = sha256,
117+
)
118+
kotlin_capabilities_repository(
119+
name = name,
120+
git_repository_name = git_repo,
121+
compiler_version = compiler_version,
122+
)

0 commit comments

Comments
 (0)