Skip to content

Commit

Permalink
Decouple Kotlin compiler from Kotlin rules
Browse files Browse the repository at this point in the history
Move Kotlin specific targets (like `kt_jvm_import`) from `BUILD.com_github_jetbrains_kotlin` to `//kotlin/compiler`.

This will make it possible to release `kotlin-compiler` module on `bazel-central-registry` without depending on `rules_kotlin`. The release will contain the new form `BUILD.com_github_jetbrains_kotlin` which doesn't depend on `rules_kotlin`.

RELNOTES[INC]: This breaks users that depend on `@com_github_jetbrains_kotlin//:*` targets. The same targets can be found under  `@io_bazel_rules_kotlin//kotlin/compiler:*`.

It's not possible to add an `alias` to `@com_github_jetbrains_kotlin` repository, becuase it would already create a dependency on `rules_kotlin` repository in the bzlmod world.

Technically it's possible to release 2 bzlmod modules that have a circular dependency. But this might create some unexpected problems. It addition it would be harder to convince `com_github_jetbrains_kotlin` owners to depend on `rules_kotlin`.

Works toward: bazelbuild#660
  • Loading branch information
comius committed Dec 22, 2022
1 parent 4a559b0 commit c7dee10
Show file tree
Hide file tree
Showing 34 changed files with 248 additions and 182 deletions.
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
2 changes: 1 addition & 1 deletion examples/android/libKtAndroid/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kt_compiler_plugin(
id = "org.jetbrains.kotlin.serialization",
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:kotlinx-serialization-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:kotlinx-serialization-compiler-plugin",
],
)

Expand Down
2 changes: 1 addition & 1 deletion examples/plugin/src/allopen/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ kt_compiler_plugin(
},
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:allopen-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:allopen-compiler-plugin",
],
)

Expand Down
6 changes: 3 additions & 3 deletions examples/plugin/src/allopennoarg/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ kt_compiler_plugin(
},
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:allopen-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:allopen-compiler-plugin",
],
)

Expand All @@ -23,7 +23,7 @@ kt_compiler_plugin(
},
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:noarg-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:noarg-compiler-plugin",
],
)

Expand Down Expand Up @@ -64,7 +64,7 @@ kt_jvm_test(
test_class = "plugin.allopennoarg.UserHasNoargConstructorTest",
deps = [
":user",
"@com_github_jetbrains_kotlin//:kotlin-reflect",
"@io_bazel_rules_kotlin//kotlin/compiler:kotlin-reflect",
"@maven//:junit_junit",
],
)
4 changes: 2 additions & 2 deletions examples/plugin/src/noarg/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ kt_compiler_plugin(
},
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:noarg-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:noarg-compiler-plugin",
],
)

Expand All @@ -35,7 +35,7 @@ kt_jvm_test(
test_class = "plugin.noarg.UserHasNoargConstructorTest",
deps = [
":user",
"@com_github_jetbrains_kotlin//:kotlin-reflect",
"@io_bazel_rules_kotlin//kotlin/compiler:kotlin-reflect",
"@maven//:junit_junit",
],
)
4 changes: 2 additions & 2 deletions examples/plugin/src/parcelize/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ kt_compiler_plugin(
id = "org.jetbrains.kotlin.parcelize",
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:parcelize-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:parcelize-compiler-plugin",
],
)

Expand All @@ -21,7 +21,7 @@ kt_jvm_library(
],
visibility = ["//visibility:public"],
exports = [
"@com_github_jetbrains_kotlin//:parcelize-runtime",
"@io_bazel_rules_kotlin//kotlin/compiler:parcelize-runtime",
],
)

Expand Down
2 changes: 1 addition & 1 deletion examples/plugin/src/sam_with_receiver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ kt_compiler_plugin(
},
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:sam-with-receiver-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:sam-with-receiver-compiler-plugin",
],
)

Expand Down
2 changes: 1 addition & 1 deletion examples/plugin/src/serialization/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ kt_compiler_plugin(
id = "org.jetbrains.kotlin.serialization",
stubs_phase = True,
deps = [
"@com_github_jetbrains_kotlin//:kotlinx-serialization-compiler-plugin",
"@io_bazel_rules_kotlin//kotlin/compiler:kotlinx-serialization-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")

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
Loading

0 comments on commit c7dee10

Please sign in to comment.