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

[infra] Use kotlin standard libraries from the toolchain, as opposed to hard coded. #1225

Merged
merged 14 commits into from
Oct 9, 2024
Merged
12 changes: 2 additions & 10 deletions .bazelproject
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ directories:
.

targets:
//:all_local_tests
# These targets are built for the ide only. Primary purpose is to ensure the builder can build the targets, but it's
# also a good way of testing the intellij plugin.
//src/main/kotlin/io/bazel/kotlin/builder/tasks:tasks_for_ide
//src/main/kotlin/io/bazel/kotlin/builder/utils:utils_for_ide
//src/main/kotlin/io/bazel/kotlin/builder/toolchain:toolchain_for_ide
//src/main/kotlin/io/bazel/kotlin/compiler:compiler_for_ide
//kotlin:stardoc
//src/main/starlark/core/repositories:all
//...

test_sources:
src/test/*
Expand All @@ -37,4 +29,4 @@ additional_languages:
import_run_configurations:
src/test/Bazel_all_local_tests.xml

android_sdk_platform: android-31
#android_sdk_platform: android-31
6 changes: 4 additions & 2 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ load("@rules_kotlin//kotlin:core.bzl", "define_kt_toolchain")
define_kt_toolchain(<a href="#define_kt_toolchain-name">name</a>, <a href="#define_kt_toolchain-language_version">language_version</a>, <a href="#define_kt_toolchain-api_version">api_version</a>, <a href="#define_kt_toolchain-jvm_target">jvm_target</a>, <a href="#define_kt_toolchain-experimental_use_abi_jars">experimental_use_abi_jars</a>,
<a href="#define_kt_toolchain-experimental_strict_kotlin_deps">experimental_strict_kotlin_deps</a>, <a href="#define_kt_toolchain-experimental_report_unused_deps">experimental_report_unused_deps</a>,
<a href="#define_kt_toolchain-experimental_reduce_classpath_mode">experimental_reduce_classpath_mode</a>, <a href="#define_kt_toolchain-experimental_multiplex_workers">experimental_multiplex_workers</a>, <a href="#define_kt_toolchain-javac_options">javac_options</a>,
<a href="#define_kt_toolchain-kotlinc_options">kotlinc_options</a>, <a href="#define_kt_toolchain-jacocorunner">jacocorunner</a>, <a href="#define_kt_toolchain-exec_compatible_with">exec_compatible_with</a>, <a href="#define_kt_toolchain-target_compatible_with">target_compatible_with</a>,
<a href="#define_kt_toolchain-target_settings">target_settings</a>)
<a href="#define_kt_toolchain-kotlinc_options">kotlinc_options</a>, <a href="#define_kt_toolchain-jvm_stdlibs">jvm_stdlibs</a>, <a href="#define_kt_toolchain-jvm_runtime">jvm_runtime</a>, <a href="#define_kt_toolchain-jacocorunner">jacocorunner</a>, <a href="#define_kt_toolchain-exec_compatible_with">exec_compatible_with</a>,
<a href="#define_kt_toolchain-target_compatible_with">target_compatible_with</a>, <a href="#define_kt_toolchain-target_settings">target_settings</a>)
</pre>

Define the Kotlin toolchain.
Expand All @@ -530,6 +530,8 @@ Define the Kotlin toolchain.
| <a id="define_kt_toolchain-experimental_multiplex_workers"></a>experimental_multiplex_workers | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-javac_options"></a>javac_options | <p align="center"> - </p> | `Label("@rules_kotlin//kotlin/internal:default_javac_options")` |
| <a id="define_kt_toolchain-kotlinc_options"></a>kotlinc_options | <p align="center"> - </p> | `Label("@rules_kotlin//kotlin/internal:default_kotlinc_options")` |
| <a id="define_kt_toolchain-jvm_stdlibs"></a>jvm_stdlibs | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-jvm_runtime"></a>jvm_runtime | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-jacocorunner"></a>jacocorunner | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-exec_compatible_with"></a>exec_compatible_with | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-target_compatible_with"></a>target_compatible_with | <p align="center"> - </p> | `None` |
Expand Down
9 changes: 7 additions & 2 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ def _run_merge_jdeps_action(ctx, toolchains, jdeps, outputs, deps):
outputs = [f for f in outputs.values()],
executable = toolchains.kt.jdeps_merger.files_to_run.executable,
execution_requirements = toolchains.kt.execution_requirements,
arguments = [args],
arguments = [
ctx.actions.args().add_all(toolchains.kt.builder_args),
args,
],
progress_message = progress_message,
)

Expand Down Expand Up @@ -471,6 +474,7 @@ def _run_kt_builder_action(
"""Creates a KotlinBuilder action invocation."""
kotlinc_options = ctx.attr.kotlinc_opts[KotlincOptions] if ctx.attr.kotlinc_opts else toolchains.kt.kotlinc_options
javac_options = ctx.attr.javac_opts[JavacOptions] if ctx.attr.javac_opts else toolchains.kt.javac_options

args = _utils.init_args(ctx, rule_kind, associates.module_name, kotlinc_options)

for f, path in outputs.items():
Expand All @@ -497,6 +501,7 @@ def _run_kt_builder_action(
omit_if_empty = True,
uniquify = True,
)

args.add_all(
"--processorpath",
annotation_processors,
Expand Down Expand Up @@ -563,7 +568,7 @@ def _run_kt_builder_action(
toolchains.kt.execution_requirements,
{"worker-key-mnemonic": mnemonic},
),
arguments = [args],
arguments = [ctx.actions.args().add_all(toolchains.kt.builder_args), args],
progress_message = progress_message,
env = {
"LC_CTYPE": "en_US.UTF-8", # For Java source files
Expand Down
50 changes: 29 additions & 21 deletions kotlin/internal/toolchains.bzl
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
load(
"//kotlin/internal:defs.bzl",
_KT_COMPILER_REPO = "KT_COMPILER_REPO",
_TOOLCHAIN_TYPE = "TOOLCHAIN_TYPE",
)

# Copyright 2018 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,6 +11,13 @@ load(
# 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//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_java//java:defs.bzl", "JavaInfo", "java_common")
load(
"//kotlin/internal:defs.bzl",
_KT_COMPILER_REPO = "KT_COMPILER_REPO",
_TOOLCHAIN_TYPE = "TOOLCHAIN_TYPE",
)
load(
"//kotlin/internal:opts.bzl",
"JavacOptions",
Expand Down Expand Up @@ -73,6 +72,11 @@ def _kotlin_toolchain_impl(ctx):
debug = ctx.attr.debug,
jvm_target = ctx.attr.jvm_target,
kotlinbuilder = ctx.attr.kotlinbuilder,
builder_args = [
"--wrapper_script_flag=--main_advice_classpath=%s" % (
":".join([f.path for f in ctx.files.jvm_stdlibs])
),
],
jdeps_merger = ctx.attr.jdeps_merger,
kotlin_home = ctx.attr.kotlin_home,
jvm_stdlibs = java_common.merge(compile_time_providers + runtime_providers),
Expand Down Expand Up @@ -122,7 +126,7 @@ _kt_toolchain = rule(
),
"language_version": attr.string(
doc = "this is the -language_version flag [see](https://kotlinlang.org/docs/reference/compatibility.html)",
default = "2.0",
default = "1.9",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we going back to an older version?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't merge?

values = [
"1.1",
"1.2",
Expand All @@ -138,7 +142,7 @@ _kt_toolchain = rule(
),
"api_version": attr.string(
doc = "this is the -api_version flag [see](https://kotlinlang.org/docs/reference/compatibility.html).",
default = "2.0",
default = "1.9",
values = [
"1.1",
"1.2",
Expand All @@ -161,22 +165,11 @@ _kt_toolchain = rule(
),
"jvm_runtime": attr.label_list(
doc = "The implicit jvm runtime libraries. This is internal.",
default = [
Label("//kotlin/compiler:kotlin-stdlib"),
],
providers = [JavaInfo],
cfg = "target",
),
"jvm_stdlibs": attr.label_list(
doc = "The jvm stdlibs. This is internal.",
default = [
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("//kotlin/compiler:kotlin-stdlib-jdk8"),
],
providers = [JavaInfo],
cfg = "target",
),
Expand All @@ -200,6 +193,10 @@ _kt_toolchain = rule(
"21",
],
),
"js_target": attr.string(
restingbull marked this conversation as resolved.
Show resolved Hide resolved
default = "v5",
values = ["v5"],
),
"experimental_multiplex_workers": attr.bool(
doc = """Run workers in multiplex mode.""",
default = False,
Expand Down Expand Up @@ -301,6 +298,8 @@ def define_kt_toolchain(
experimental_multiplex_workers = None,
javac_options = Label("//kotlin/internal:default_javac_options"),
kotlinc_options = Label("//kotlin/internal:default_kotlinc_options"),
jvm_stdlibs = None,
jvm_runtime = None,
jacocorunner = None,
exec_compatible_with = None,
target_compatible_with = None,
Expand All @@ -327,6 +326,15 @@ def define_kt_toolchain(
kotlinc_options = kotlinc_options,
visibility = ["//visibility:public"],
jacocorunner = jacocorunner,
jvm_stdlibs = jvm_stdlibs if jvm_stdlibs != None else [
Label("//kotlin/compiler:annotations"),
Label("//kotlin/compiler:kotlin-stdlib"),
Label("//kotlin/compiler:kotlin-stdlib-jdk7"),
Label("//kotlin/compiler:kotlin-stdlib-jdk8"),
],
jvm_runtime = jvm_runtime if jvm_runtime != None else [
Label("//kotlin/compiler:kotlin-stdlib"),
],
)
native.toolchain(
name = name,
Expand Down
6 changes: 2 additions & 4 deletions src/main/kotlin/io/bazel/kotlin/builder/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ java_library(
name = "builder",
srcs = glob(["*.java"]),
visibility = ["//src:__subpackages__"],
runtime_deps = [
"//kotlin/compiler:kotlin-stdlib-jdk7",
"//kotlin/compiler:kotlin-stdlib-jdk8",
],
deps = [
"//kotlin/compiler:annotations",
"//kotlin/compiler:kotlin-stdlib",
"//kotlin/compiler:kotlin-stdlib-jdk7",
"//kotlin/compiler:kotlin-stdlib-jdk8",
"//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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import io.bazel.kotlin.builder.utils.jars.SourceJarExtractor
import io.bazel.kotlin.builder.utils.partitionJvmSources
import io.bazel.kotlin.model.JvmCompilationTask
import io.bazel.kotlin.model.JvmCompilationTask.Directories
import io.bazel.kotlin.model.RuleKind
import java.io.BufferedInputStream
import java.io.ByteArrayOutputStream
import java.io.File
Expand Down Expand Up @@ -91,6 +92,9 @@ fun JvmCompilationTask.baseArgs(overrides: Map<String, String> = emptyMap()): Co
overrides[LANGUAGE_VERSION_ARG] ?: info.toolchainInfo.common.languageVersion,
).flag("-jvm-target", info.toolchainInfo.jvm.jvmTarget)
.flag("-module-name", info.moduleName)
.given(info.ruleKind in setOf(RuleKind.IMPORT, RuleKind.LIBRARY)) {
flag("-no-stdlib").flag("-no-reflect")
}
}

internal fun JvmCompilationTask.plugins(
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/shade.jarjar
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rule dagger.** io.bazel.kotlin.builder.dagger.@1
rule com.google.common.** io.bazel.kotlin.builder.guava.@1
zap kotlin.**