Skip to content

Commit

Permalink
Make loading the kotlinc repository lazy (#1126)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
restingbull committed Jun 1, 2024
1 parent 257b9ad commit fe5c6fa
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 113 deletions.
121 changes: 22 additions & 99 deletions kotlin/compiler/compiler.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,25 @@ load("@rules_java//java:defs.bzl", "java_import")
load("//kotlin:js.bzl", "kt_js_import")
load("//kotlin:jvm.bzl", "kt_jvm_import")
load("//kotlin/internal:defs.bzl", _KT_COMPILER_REPO = "KT_COMPILER_REPO")

_KT_COMPILER_REPO_PREFIX = "@" + _KT_COMPILER_REPO + "//:"
load("@com_github_jetbrains_kotlin//:artifacts.bzl", "KOTLINC_ARTIFACTS")

def _import_artifacts(artifacts, rule_kind):
_import_labels(artifacts.plugin, rule_kind)
_import_labels(artifacts.runtime, rule_kind)
_import_labels(artifacts.compile, rule_kind, neverlink = 1)

def _import_labels(labels, rule_kind, **rule_args):
for label in labels:
if "-sources" in label:
continue
args = dict(rule_args.items())
args["visibility"] = ["//visibility:public"]
args["name"] = label
args["jars"] = ["@%s//:%s" % (_KT_COMPILER_REPO, label)]
sources = label + "-sources"
if sources in labels:
args["srcjar"] = "@%s//:%s" % (_KT_COMPILER_REPO, sources)
rule_kind(**args)

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

kt_jvm_import(
name = "annotations",
jar = _KT_COMPILER_REPO_PREFIX + "lib/annotations-13.0.jar",
neverlink = 1,
)

kt_jvm_import(
name = "jvm-abi-gen",
jar = _KT_COMPILER_REPO_PREFIX + "lib/jvm-abi-gen.jar",
)

# Kotlin dependencies that are internal to this repo and are meant to be loaded manually into a classloader.
[
kt_jvm_import(
name = "kotlin-%s" % art,
jar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art,
neverlink = 1,
)
for art in [
"annotation-processing",
"annotation-processing-runtime",
"compiler",
]
]

kt_jvm_import(
name = "kotlinx-serialization-compiler-plugin",
jar = _KT_COMPILER_REPO_PREFIX + "lib/kotlinx-serialization-compiler-plugin.jar",
)

kt_jvm_import(
name = "allopen-compiler-plugin",
jar = _KT_COMPILER_REPO_PREFIX + "lib/allopen-compiler-plugin.jar",
)

kt_jvm_import(
name = "noarg-compiler-plugin",
jar = _KT_COMPILER_REPO_PREFIX + "lib/noarg-compiler-plugin.jar",
)

kt_jvm_import(
name = "sam-with-receiver-compiler-plugin",
jar = _KT_COMPILER_REPO_PREFIX + "lib/sam-with-receiver-compiler-plugin.jar",
)

kt_jvm_import(
name = "parcelize-compiler-plugin",
jar = _KT_COMPILER_REPO_PREFIX + "lib/parcelize-compiler.jar",
)

kt_jvm_import(
name = "parcelize-runtime",
jar = _KT_COMPILER_REPO_PREFIX + "lib/parcelize-runtime.jar",
)

# Kotlin dependencies that are internal to this repo and may be linked.
[
java_import(
name = "kotlin-%s" % art,
jars = [_KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art],
)
for art in [
"preloader",
]
]

# The Kotlin standard libraries. These should be setup in a Toolchain.
[
kt_jvm_import(
name = "kotlin-%s" % art,
jar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art,
srcjar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s-sources.jar" % art,
visibility = ["//visibility:public"],
)
for art in [
"stdlib",
"stdlib-jdk7",
"stdlib-jdk8",
"reflect",
"test",
"script-runtime",
]
]

# The Kotlin JS standard libraries. These should be setup in a Toolchain.
[
kt_js_import(
name = "kotlin-%s" % art,
jars = [_KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s.jar" % art],
srcjar = _KT_COMPILER_REPO_PREFIX + "lib/kotlin-%s-sources.jar" % art,
visibility = ["//visibility:public"],
)
for art in [
"test-js",
"stdlib-js",
]
]
_import_artifacts(KOTLINC_ARTIFACTS.js, kt_js_import)
_import_artifacts(KOTLINC_ARTIFACTS.jvm, kt_jvm_import)
_import_artifacts(KOTLINC_ARTIFACTS.core, kt_jvm_import)
2 changes: 1 addition & 1 deletion kotlin/internal/jvm/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def _unify_jars(ctx):
elif file.basename.endswith(".jar"):
jars.append(file)
else:
fail("a jar pointing to a filegroup must either end with -sources.jar or .jar")
fail("a jar pointing to a filegroup must either end with -sources.jar or .jar: %s", file)

if len(jars) > 1:
fail("Got more than one jar, this is an error create an issue: %s" % jars)
Expand Down
3 changes: 1 addition & 2 deletions src/main/starlark/core/repositories/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,6 +11,7 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("//src/main/starlark/release:packager.bzl", "release_archive")

release_archive(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
package(default_visibility = ["//visibility:public"])

# Kotlin home filegroup containing everything that is needed.
[
filegroup(
name = name.replace(".", "_"),
srcs = glob(["" + name]),
)
for name in glob(["lib/**"])
]

filegroup(
name = "home",
srcs = glob(["**"]),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
package(default_visibility = ["//visibility:public"])

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load(":artifacts.bzl", "KOTLINC_ARTIFACT_LIST")

bzl_library(
name = "capabilities",
srcs = glob(["*.bzl"]),
)

alias(
name = "home",
actual = "@$git_repo$//:home",
)

[
alias(
name = label,
actual = "@$git_repo$//:%s" % file.replace(".", "_"),
)
for (label, file) in KOTLINC_ARTIFACT_LIST.items()
]
72 changes: 62 additions & 10 deletions src/main/starlark/core/repositories/compiler.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
"""
Defines kotlin compiler repositories.
"""

def _kotlin_compiler_impl(repository_ctx):
"""Creates the kotlinc repository."""
attr = repository_ctx.attr
repository_ctx.download_and_extract(
attr.urls,
sha256 = attr.sha256,
stripPrefix = "kotlinc",
)
repository_ctx.template(
"BUILD.bazel",
attr._template,
executable = False,
)

def _kotlin_capabilities_impl(repository_ctx):
"""Creates the kotlinc repository."""
attr = repository_ctx.attr
repository_ctx.file(
"WORKSPACE",
content = """workspace(name = "%s")""" % attr.name,
Expand All @@ -14,8 +26,15 @@ def _kotlin_compiler_impl(repository_ctx):
"BUILD.bazel",
attr._template,
executable = False,
substitutions = {
"$git_repo$": attr.git_repository_name,
},
)
repository_ctx.template(
"artifacts.bzl",
attr._artifacts_template,
executable = False,
)

repository_ctx.template(
"capabilities.bzl",
_get_capability_template(attr.compiler_version, attr._capabilities_templates),
Expand Down Expand Up @@ -45,7 +64,31 @@ _CAPABILITIES_TEMPLATES = {
"2.0": "//src/main/starlark/core/repositories/kotlin:capabilities_2.0.bzl.com_github_jetbrains_kotlin.bazel",
}

kotlin_compiler_repository = repository_rule(
kotlin_capabilities_repository = repository_rule(
implementation = _kotlin_capabilities_impl,
attrs = {
"git_repository_name": attr.string(
doc = "Name of the repository containing kotlin compiler libraries",
),
"compiler_version": attr.string(
doc = "compiler version",
),
"_capabilities_templates": attr.label_list(
doc = "compiler capabilities file templates",
default = _CAPABILITIES_TEMPLATES.values(),
),
"_template": attr.label(
doc = "repository build file template",
default = ":BUILD.kotlin_capabilities.bazel",
),
"_artifacts_template": attr.label(
doc = "kotlinc artifacts template",
default = "//src/main/starlark/core/repositories/kotlin:artifacts.bzl",
),
},
)

kotlin_compiler_git_repository = repository_rule(
implementation = _kotlin_compiler_impl,
attrs = {
"urls": attr.string_list(
Expand All @@ -55,16 +98,25 @@ kotlin_compiler_repository = repository_rule(
"sha256": attr.string(
doc = "sha256 of the compiler archive",
),
"compiler_version": attr.string(
doc = "compiler version",
),
"_template": attr.label(
doc = "repository build file template",
default = ":BUILD.com_github_jetbrains_kotlin.bazel",
),
"_capabilities_templates": attr.label_list(
doc = "compiler capabilities file templates",
default = _CAPABILITIES_TEMPLATES.values(),
),
},
)

def kotlin_compiler_repository(name, urls, sha256, compiler_version):
"""
Creates two repositories, necessary for lazily loading the kotlin compiler binaries for git.
"""
git_repo = name + "_git"
kotlin_compiler_git_repository(
name = git_repo,
urls = urls,
sha256 = sha256,
)
kotlin_capabilities_repository(
name = name,
git_repository_name = git_repo,
compiler_version = compiler_version,
)
Loading

0 comments on commit fe5c6fa

Please sign in to comment.