Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple Kotlin compiler from Kotlin rules #899

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ kt_compiler_plugin(
"annotation": "plugin.allopen.OpenForTesting",
},
deps = [
"@com_github_jetbrains_kotlin//:allopen-compiler-plugin",
"@rules_kotlin//kotlin/compiler:allopen-compiler-plugin",
],
)

Expand Down
4 changes: 2 additions & 2 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ kt_jvm_test(<a href="#kt_jvm_test-name">name</a>, <a href="#kt_jvm_test-associat

**Notes:**
* The kotlin test library is not added implicitly, it is available with the label
`@com_github_jetbrains_kotlin//:kotlin-test`.
`@rules_kotlin//kotlin/compiler:kotlin-test`.


**ATTRIBUTES**
Expand Down Expand Up @@ -330,7 +330,7 @@ kt_compiler_plugin(<a href="#kt_compiler_plugin-name">name</a>, <a href="#kt_com
"annotation": "plugin.OpenForTesting",
},
deps = [
"@com_github_jetbrains_kotlin//:allopen-compiler-plugin",
"@rules_kotlin//kotlin/compiler:allopen-compiler-plugin",
],
)

Expand Down
1 change: 1 addition & 0 deletions kotlin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ release_archive(
"BUILD.release.bazel": "BUILD.bazel",
},
deps = [
"//kotlin/compiler:pkg",
"//kotlin/internal:pkg",
],
)
Expand Down
28 changes: 28 additions & 0 deletions kotlin/compiler/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2020 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.
load(":compiler.bzl", "kt_configure_compiler")
load("//src/main/starlark/release:packager.bzl", "release_archive")

package(default_visibility = ["//visibility:public"])

# Configures the compiler
kt_configure_compiler()

release_archive(
name = "pkg",
srcs = glob(["*.bzl"]),
src_map = {
"BUILD.release.bazel": "BUILD.bazel",
},
)
19 changes: 19 additions & 0 deletions kotlin/compiler/BUILD.release.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2020 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.
load(":compiler.bzl", "kt_configure_compiler")

package(default_visibility = ["//visibility:public"])

# Configures the compiler
kt_configure_compiler()
127 changes: 127 additions & 0 deletions kotlin/compiler/compiler.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
# Copyright 2022 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.

load("//kotlin:jvm.bzl", "kt_jvm_import")
load("//kotlin:js.bzl", "kt_js_import")
load("@rules_java//java:defs.bzl", "java_import")
load("//kotlin/internal:defs.bzl", _KT_COMPILER_REPO = "KT_COMPILER_REPO")

_KT_COMPILER_REPO_PREFIX = "@" + _KT_COMPILER_REPO + "//:"

def kt_configure_compiler():
"""
Defines the toolchain_type and default toolchain for kotlin compilation.

Must be called in kotlin/internal/BUILD.bazel
"""
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",
]
]
6 changes: 4 additions & 2 deletions kotlin/internal/js/js.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ kt_js_library = rule(
),
"_toolchain": attr.label(
doc = """The Kotlin JS Runtime.""",
default = Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib-js"),
default = Label("//kotlin/compiler:kotlin-stdlib-js"),
cfg = "target",
),
},
Expand All @@ -104,11 +104,13 @@ kt_js_library = rule(
provides = [_KtJsInfo],
)

_KOTLIN_STDLIB_JS = Label("//kotlin/compiler:kotlin-stdlib-js")
comius marked this conversation as resolved.
Show resolved Hide resolved

def kt_js_library_macro(name, **kwargs):
kwargs = _lock_attrs(name, kwargs)

# TODO this is a runtime dep, it should be picked up from the _toolchain attr or from a provider.
kwargs["deps"] = kwargs.get("deps", []) + ["@" + _KT_COMPILER_REPO + "//:kotlin-stdlib-js"]
kwargs["deps"] = kwargs.get("deps", []) + [_KOTLIN_STDLIB_JS]
kt_js_library(name = name, **kwargs)

kt_js_import = rule(
Expand Down
11 changes: 5 additions & 6 deletions kotlin/internal/jvm/jvm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ test --strategy=KotlinCompile=worker
### Standard Libraries

The Kotlin libraries that are bundled in a kotlin release should be used with the rules, the mandatory standard libraries are added implicetly. After enabling
the repository the following Kotlin Libraries are also made available from the workspace `com_github_jetbrains_kotlin`:
the repository the following Kotlin Libraries are also made available from the package `//kotlin/compiler`:

* `kotlin-test`,
* `kotlin-reflect`.

So if you needed to add reflect as a dep use the following label `@com_github_jetbrains_kotlin//:kotlin-reflect`.
So if you needed to add reflect as a dep use the following label `@rules_kotlin//kotlin/compiler:kotlin-reflect`.

### Mixed Mode compilation

Expand Down Expand Up @@ -96,7 +96,6 @@ load(
"//kotlin/internal:defs.bzl",
_JAVA_RUNTIME_TOOLCHAIN_TYPE = "JAVA_RUNTIME_TOOLCHAIN_TYPE",
_JAVA_TOOLCHAIN_TYPE = "JAVA_TOOLCHAIN_TYPE",
_KT_COMPILER_REPO = "KT_COMPILER_REPO",
_KtCompilerPluginInfo = "KtCompilerPluginInfo",
_KtJvmInfo = "KtJvmInfo",
_TOOLCHAIN_TYPE = "TOOLCHAIN_TYPE",
Expand Down Expand Up @@ -141,7 +140,7 @@ _implicit_deps = {
"_toolchain": attr.label(
doc = """The Kotlin JVM Runtime. it's only purpose is to enable the Android native rules to discover the Kotlin
runtime for dexing""",
default = Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib"),
default = Label("//kotlin/compiler:kotlin-stdlib"),
cfg = "target",
),
"_java_toolchain": attr.label(
Expand Down Expand Up @@ -335,7 +334,7 @@ Setup a simple kotlin_test.

**Notes:**
* The kotlin test library is not added implicitly, it is available with the label
`@com_github_jetbrains_kotlin//:kotlin-test`.
`@rules_kotlin//kotlin/compiler:kotlin-test`.
""",
attrs = utils.add_dicts(_runnable_common_attr, {
"_bazel_test_runner": attr.label(
Expand Down Expand Up @@ -492,7 +491,7 @@ kt_compiler_plugin(
"annotation": "plugin.OpenForTesting",
},
deps = [
"@com_github_jetbrains_kotlin//:allopen-compiler-plugin",
"@rules_kotlin//kotlin/compiler:allopen-compiler-plugin",
],
)

Expand Down
12 changes: 6 additions & 6 deletions kotlin/internal/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -153,20 +153,20 @@ _kt_toolchain = rule(
"jvm_runtime": attr.label_list(
doc = "The implicit jvm runtime libraries. This is internal.",
default = [
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib"),
Label("//kotlin/compiler:kotlin-stdlib"),
],
providers = [JavaInfo],
cfg = "target",
),
"jvm_stdlibs": attr.label_list(
doc = "The jvm stdlibs. This is internal.",
default = [
Label("@" + _KT_COMPILER_REPO + "//:annotations"),
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib"),
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib-jdk7"),
Label("//kotlin/compiler:annotations"),
Label("//kotlin/compiler:kotlin-stdlib"),
Label("//kotlin/compiler:kotlin-stdlib-jdk7"),
# JDK8 is being added blindly but I think we will probably not support bytecode levels 1.6 when the
# repo stabelizes so this should be fine.
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib-jdk8"),
Label("//kotlin/compiler:kotlin-stdlib-jdk8"),
],
providers = [JavaInfo],
cfg = "target",
Expand All @@ -193,7 +193,7 @@ _kt_toolchain = rule(
),
"js_stdlibs": attr.label_list(
default = [
Label("@" + _KT_COMPILER_REPO + "//:kotlin-stdlib-js"),
Label("//kotlin/compiler:kotlin-stdlib-js"),
],
providers = [_KtJsInfo],
),
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/BUILD.release.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ java_binary(
data = [
":jdeps-gen",
":skip-code-gen",
"//kotlin/compiler:jvm-abi-gen",
"//kotlin/compiler:kotlin-compiler",
"//src/main/kotlin/io/bazel/kotlin/compiler",
"@com_github_jetbrains_kotlin//:jvm-abi-gen",
"@com_github_jetbrains_kotlin//:kotlin-compiler",
],
jvm_flags = [
"-D@com_github_jetbrains_kotlin...jvm-abi-gen=$(rootpath @com_github_jetbrains_kotlin//:jvm-abi-gen)",
"-D@com_github_jetbrains_kotlin...kotlin-compiler=$(rootpath @com_github_jetbrains_kotlin//:kotlin-compiler)",
"-D..kotlin.compiler.jvm-abi-gen=$(rootpath //kotlin/compiler:jvm-abi-gen)",
"-D..kotlin.compiler.kotlin-compiler=$(rootpath //kotlin/compiler:kotlin-compiler)",
"-XX:-MaxFDLimit",
],
main_class = "io.bazel.kotlin.builder.cmd.Build",
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ CP="%s"
ARGS="%s"

CMD="$(JAVA) -Xmx256M -Xms32M -noverify \
-cp $(location @com_github_jetbrains_kotlin//:kotlin-preloader) org.jetbrains.kotlin.preloading.Preloader \
-cp $(location @com_github_jetbrains_kotlin//:kotlin-compiler) org.jetbrains.kotlin.cli.jvm.K2JVMCompiler \
-cp $(location //kotlin/compiler:kotlin-preloader) org.jetbrains.kotlin.preloading.Preloader \
-cp $(location //kotlin/compiler:kotlin-compiler) org.jetbrains.kotlin.cli.jvm.K2JVMCompiler \
$$CP -d $(@D)/$${NAME}_temp.jar $${ARGS} $(SRCS)"

$$CMD
Expand All @@ -91,8 +91,8 @@ rm $(@D)/$${NAME}_temp.jar
name = jar_label,
tools = [
"@com_github_jetbrains_kotlin//:home",
"@com_github_jetbrains_kotlin//:kotlin-preloader",
"@com_github_jetbrains_kotlin//:kotlin-compiler",
"//kotlin/compiler:kotlin-preloader",
"//kotlin/compiler:kotlin-compiler",
"@bazel_tools//tools/jdk:singlejar",
dep_label,
],
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/io/bazel/kotlin/builder/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ java_library(
srcs = glob(["*.java"]),
visibility = ["//src:__subpackages__"],
runtime_deps = [
"@com_github_jetbrains_kotlin//:kotlin-stdlib-jdk7",
"@com_github_jetbrains_kotlin//:kotlin-stdlib-jdk8",
"//kotlin/compiler:kotlin-stdlib-jdk7",
"//kotlin/compiler:kotlin-stdlib-jdk8",
],
deps = [
"//kotlin/compiler:annotations",
"//kotlin/compiler:kotlin-stdlib",
"//src/main/kotlin/io/bazel/kotlin/builder/tasks",
"//src/main/kotlin/io/bazel/kotlin/builder/toolchain",
"//src/main/kotlin/io/bazel/kotlin/builder/utils",
Expand All @@ -30,8 +32,6 @@ java_library(
"//src/main/protobuf:kotlin_model_java_proto",
"//src/main/protobuf:worker_protocol_java_proto",
"//third_party:dagger",
"@com_github_jetbrains_kotlin//:annotations",
"@com_github_jetbrains_kotlin//:kotlin-stdlib",
"@kotlin_rules_maven//:javax_annotation_javax_annotation_api",
"@kotlin_rules_maven//:javax_inject_javax_inject",
],
Expand Down
Loading