Skip to content

Commit

Permalink
Refactor compiler configuration repositories
Browse files Browse the repository at this point in the history
Add `capabilities.bzl` to Kotlin compiler repository. Generate `kt_kotlinc_options` rule from it, that is filter out options that are not available in a specific version.

This change makes a narrow interface between Kotlin compiler and Kotlin rules. It will make it possible to release and use new versions of Kotlin compiler independently from Kotlin rules.

The release of Kotlin compiler should include the new `capabilities.bzl` file.

The `capabilities.bzl` files were generated by grepping respective version of Kotlin compiler sources for `@Argument` annotation (some of them are not documented). The `legacy` file was generated from Kotlin compiler 1.3.0 additionally removing options that weren't available in `src/legacy`.

There are some minor differences from previous `src/legacy` - javac opts are the same for versions.

Works toward: bazelbuild#660
  • Loading branch information
comius committed Dec 23, 2022
1 parent 0f375f7 commit 6b3506d
Show file tree
Hide file tree
Showing 59 changed files with 866 additions and 1,443 deletions.
12 changes: 0 additions & 12 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
load("//src/main/starlark/release:packager.bzl", "release_archive")
load("//src/main/starlark/core/repositories:versions.bzl", "versions")
load("//kotlin:lint.bzl", "ktlint_config")

exports_files([
Expand Down Expand Up @@ -52,20 +51,9 @@ test_suite(
],
)

[
release_archive(
name = version,
deps = [
"@%s//:pkg" % version,
],
)
for version in versions.CORE
]

# Release target.
release_archive(
name = "rules_kotlin_release",
srcs = ["%s.tgz" % v for v in versions.CORE],
src_map = {
"BUILD.release.bazel": "BUILD.bazel",
"WORKSPACE.release.bazel": "WORKSPACE",
Expand Down
12 changes: 0 additions & 12 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ The `rules_kotlin` runtime is comprised of multiple repositories. The end user w
versioned feature sub-repositories. Currently, the delegation is managed by using well known names (e.g. core lives in `@io_bazel_rules_kotlin_configured`),
a necessity while the initial repository can be named arbitrarily. Future development intends to remove this restriction.

### Versioning

To cope with API churn, the release archive can configure different rule attributes for depending on the chosen kotlin version.
Each major release of kotlin (1.4, 1.5) has a specific sub-repository under [src/main/starlark](src/main/starlark). The naming convention for these
is `rkt_<major>_<minor>` ([r]elease [k]o[t]lin).

The version is selected by the [kotlin_repositories](src/main/starlark/repositories/initialize.release.bzl) rule during initialization.
New versions of kotlin that change the API should be added to [versions.bzl](src/main/starlark/repositories/versions.bzl), under `CORE` following the
existing naming convention.

Multiple versions of kotlin are not currently handled.(_help wanted_)

## Idioms and Styles
TBD

Expand Down
3 changes: 1 addition & 2 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ This macro registers the kotlin toolchain.
## kotlin_repositories

<pre>
kotlin_repositories(<a href="#kotlin_repositories-compiler_repository_name">compiler_repository_name</a>, <a href="#kotlin_repositories-compiler_release">compiler_release</a>, <a href="#kotlin_repositories-configured_repository_name">configured_repository_name</a>)
kotlin_repositories(<a href="#kotlin_repositories-compiler_repository_name">compiler_repository_name</a>, <a href="#kotlin_repositories-compiler_release">compiler_release</a>)
</pre>

Call this in the WORKSPACE file to setup the Kotlin rules.
Expand All @@ -497,6 +497,5 @@ Call this in the WORKSPACE file to setup the Kotlin rules.
| :------------- | :------------- | :------------- |
| <a id="kotlin_repositories-compiler_repository_name"></a>compiler_repository_name | for the kotlinc compiler repository. | <code>"com_github_jetbrains_kotlin"</code> |
| <a id="kotlin_repositories-compiler_release"></a>compiler_release | version provider from versions.bzl. | <code>struct()</code> |
| <a id="kotlin_repositories-configured_repository_name"></a>configured_repository_name | for the default versioned kt_* rules repository. If None, no versioned repository is created. | <code>"io_bazel_rules_kotlin_configured"</code> |


7 changes: 1 addition & 6 deletions kotlin/internal/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.
load("//kotlin/internal:toolchains.bzl", "kt_configure_toolchains")
load("//src/main/starlark/release:packager.bzl", "release_archive")
load("//src/main/starlark/core/repositories:versions.bzl", "versions")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")

# Configures the toolchains
Expand All @@ -23,11 +22,9 @@ release_archive(
name = "pkg",
srcs = glob(
["*.bzl"],
exclude = ["opts.bzl"],
),
src_map = {
"BUILD.release.bazel": "BUILD.bazel",
"opts.release.bzl": "opts.bzl",
},
deps = [
"//kotlin/internal/js:pkg",
Expand All @@ -47,8 +44,6 @@ bzl_library(
"//kotlin/internal/lint",
"//kotlin/internal/utils",
"//src/main/starlark",
] + [
"@%s//starlark" % v
for v in versions.CORE
"@com_github_jetbrains_kotlin//:capabilities_bzl",
],
)
4 changes: 2 additions & 2 deletions kotlin/internal/opts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
# limitations under the License.

load(
"@rkt_1_7//starlark/kotlin:opts.bzl",
":opts.kotlinc.bzl",
_KotlincOptions = "KotlincOptions",
_kotlinc_options_to_flags = "kotlinc_options_to_flags",
_kt_kotlinc_options = "kt_kotlinc_options",
)
load(
"@rkt_1_7//starlark/jvm:opts.bzl",
":opts.javac.bzl",
_JavacOptions = "JavacOptions",
_javac_options_to_flags = "javac_options_to_flags",
_kt_javac_options = "kt_javac_options",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
load("@dev_io_bazel_rules_kotlin//src/main/starlark/core/options:derive.bzl", "derive")
load("@dev_io_bazel_rules_kotlin//src/main/starlark/core/options:convert.bzl", "convert")
# 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("//src/main/starlark/core/options:derive.bzl", "derive")
load("//src/main/starlark/core/options:convert.bzl", "convert")

_JOPTS = {
"warn": struct(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
load("@dev_io_bazel_rules_kotlin//src/main/starlark/core/options:convert.bzl", "convert")
# 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("//src/main/starlark/core/options:convert.bzl", "convert")
load("@com_github_jetbrains_kotlin//:capabilities.bzl", _KOTLIN_OPTS = "KOTLIN_OPTS")

def _map_optin_class_to_flag(values):
return ["-opt-in=%s" % v for v in values]
Expand All @@ -13,7 +28,7 @@ def _map_jvm_target_to_flag(version):
return None
return ["-jvm-target=%s" % version]

_KOPTS = {
_KOPTS_ALL = {
"warn": struct(
args = dict(
default = "report",
Expand Down Expand Up @@ -41,6 +56,7 @@ _KOPTS = {
},
),
"x_skip_prerelease_check": struct(
flag = "-Xskip-prerelease-check",
args = dict(
default = False,
doc = "Suppress errors thrown when using pre-release classes.",
Expand All @@ -51,6 +67,7 @@ _KOPTS = {
},
),
"x_inline_classes": struct(
flag = "-Xinline-classes",
args = dict(
default = False,
doc = "Enable experimental inline classes",
Expand All @@ -61,6 +78,7 @@ _KOPTS = {
},
),
"x_allow_result_return_type": struct(
flag = "-Xallow-result-return-type",
args = dict(
default = False,
doc = "Enable kotlin.Result as a return type",
Expand All @@ -71,6 +89,7 @@ _KOPTS = {
},
),
"x_jvm_default": struct(
flag = "-Xjvm-default",
args = dict(
default = "off",
doc = "Specifies that a JVM default method should be generated for non-abstract Kotlin interface member.",
Expand All @@ -87,6 +106,7 @@ _KOPTS = {
},
),
"x_no_call_assertions": struct(
flag = "-Xno-call-assertions",
args = dict(
default = False,
doc = "Don't generate not-null assertions for arguments of platform types",
Expand All @@ -97,6 +117,7 @@ _KOPTS = {
},
),
"x_no_param_assertions": struct(
flag = "-Xno-param-assertions",
args = dict(
default = False,
doc = "Don't generate not-null assertions on parameters of methods accessible from Java",
Expand All @@ -107,6 +128,7 @@ _KOPTS = {
},
),
"x_no_receiver_assertions": struct(
flag = "-Xno-receiver-assertions",
args = dict(
default = False,
doc = "Don't generate not-null assertion for extension receiver arguments of platform types",
Expand All @@ -117,16 +139,18 @@ _KOPTS = {
},
),
"x_no_optimized_callable_references": struct(
flag = "-Xno-optimized-callable-references",
args = dict(
default = False,
doc = "Do not use optimized callable reference superclasses. Available from 1.4.",
),
type = attr.bool,
value_to_flag = {
True: ["-Xno-optimized-callable-reference"],
True: ["-Xno-optimized-callable-references"],
},
),
"x_explicit_api_mode": struct(
flag = "-Xexplicit-api",
args = dict(
default = "off",
doc = "Enable explicit API mode for Kotlin libraries.",
Expand All @@ -150,6 +174,7 @@ _KOPTS = {
},
),
"x_multi_platform": struct(
flag = "-Xmulti-platform",
args = dict(
default = False,
doc = "Enable experimental language support for multi-platform projects",
Expand All @@ -160,6 +185,7 @@ _KOPTS = {
},
),
"x_sam_conversions": struct(
flag = "-Xsam-conversions",
args = dict(
default = "class",
doc = "Change codegen behavior of SAM/functional interfaces",
Expand All @@ -172,6 +198,7 @@ _KOPTS = {
},
),
"x_lambdas": struct(
flag = "-Xlambdas",
args = dict(
default = "class",
doc = "Change codegen behavior of lambdas",
Expand All @@ -184,6 +211,7 @@ _KOPTS = {
},
),
"x_emit_jvm_type_annotations": struct(
flag = "-Xemit-jvm-type-annotations",
args = dict(
default = False,
doc = "Basic support for type annotations in JVM bytecode.",
Expand All @@ -202,7 +230,21 @@ _KOPTS = {
value_to_flag = None,
map_value_to_flag = _map_optin_class_to_flag,
),
"x_use_fir": struct(
# 1.6
flag = "-Xuse-fir",
args = dict(
default = False,
doc = "Compile using the experimental Kotlin Front-end IR. Available from 1.6.",
),
type = attr.bool,
value_to_flag = {
True: ["-Xuse-fir"],
},
),
"x_use_k2": struct(
# 1.7
flag = "-Xuse-k2",
args = dict(
default = False,
doc = "Compile using experimental K2. K2 is a new compiler pipeline, no compatibility guarantees are yet provided",
Expand All @@ -213,6 +255,7 @@ _KOPTS = {
},
),
"x_no_optimize": struct(
flag = "-Xno-optimize",
args = dict(
default = False,
doc = "Disable optimizations",
Expand All @@ -223,6 +266,8 @@ _KOPTS = {
},
),
"x_backend_threads": struct(
# 1.6.20, 1.7
flag = "-Xbackend-threads",
args = dict(
default = 1,
doc = "When using the IR backend, run lowerings by file in N parallel threads. 0 means use a thread per processor core. Default value is 1.",
Expand All @@ -232,6 +277,7 @@ _KOPTS = {
map_value_to_flag = _map_backend_threads_to_flag,
),
"x_report_perf": struct(
flag = "-Xreport-perf",
args = dict(
default = False,
doc = "Report detailed performance statistics",
Expand All @@ -253,6 +299,9 @@ _KOPTS = {
),
}

# Filters out options that are not available in current compiler release
_KOPTS = {attr: defn for (attr, defn) in _KOPTS_ALL.items() if not hasattr(defn, "flag") or defn.flag in _KOTLIN_OPTS}

KotlincOptions = provider(
fields = {
name: o.args["doc"]
Expand Down
34 changes: 0 additions & 34 deletions kotlin/internal/opts.release.bzl

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/starlark/core/repositories/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ release_archive(
srcs = [
"BUILD.com_github_jetbrains_kotlin.bazel",
"compiler.bzl",
"configured_rules.bzl",
"versions.bzl",
],
] + glob(["capabilities_*.bazel"]),
src_map = {
"initialize.release.bzl": "initialize.bzl",
"BUILD.release.bazel": "BUILD.bazel",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# 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")

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

Expand All @@ -19,3 +20,8 @@ filegroup(
name = "home",
srcs = glob(["**"]),
)

bzl_library(
name = "capabilities_bzl",
srcs = ["capabilities.bzl"],
)
Loading

0 comments on commit 6b3506d

Please sign in to comment.