Skip to content
Merged
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
1 change: 1 addition & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use_repo(
"com_github_jetbrains_kotlin",
"com_github_jetbrains_kotlin_git",
"com_github_pinterest_ktlint",
"kotlin_build_tools_impl",
"kotlinx_serialization_core_jvm",
"kotlinx_serialization_json",
"kotlinx_serialization_json_jvm",
Expand Down
1 change: 1 addition & 0 deletions MODULE.release.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use_repo(
"kotlinx_serialization_core_jvm",
"kotlinx_serialization_json",
"kotlinx_serialization_json_jvm",
"kotlin_build_tools_impl",
)

register_toolchains("//kotlin/internal:default_toolchain")
7 changes: 4 additions & 3 deletions docs/kotlin.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,9 @@ define_kt_toolchain(<a href="#define_kt_toolchain-name">name</a>, <a href="#defi
<a href="#define_kt_toolchain-experimental_treat_internal_as_private_in_abi_jars">experimental_treat_internal_as_private_in_abi_jars</a>,
<a href="#define_kt_toolchain-experimental_remove_private_classes_in_abi_jars">experimental_remove_private_classes_in_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-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>)
<a href="#define_kt_toolchain-experimental_multiplex_workers">experimental_multiplex_workers</a>, <a href="#define_kt_toolchain-experimental_build_tools_api">experimental_build_tools_api</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-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 @@ -538,6 +538,7 @@ Define the Kotlin toolchain.
| <a id="define_kt_toolchain-experimental_report_unused_deps"></a>experimental_report_unused_deps | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-experimental_reduce_classpath_mode"></a>experimental_reduce_classpath_mode | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-experimental_multiplex_workers"></a>experimental_multiplex_workers | <p align="center"> - </p> | `None` |
| <a id="define_kt_toolchain-experimental_build_tools_api"></a>experimental_build_tools_api | <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` |
Expand Down
1 change: 1 addition & 0 deletions kotlin/internal/jvm/compile.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ def _run_kt_builder_action(
args.add("--strict_kotlin_deps", toolchains.kt.experimental_strict_kotlin_deps)
args.add_all("--classpath", compile_deps.compile_jars)
args.add("--reduced_classpath_mode", toolchains.kt.experimental_reduce_classpath_mode)
args.add("--build_tools_api", toolchains.kt.experimental_build_tools_api)
args.add_all("--sources", srcs.all_srcs, omit_if_empty = True)
args.add_all("--source_jars", srcs.src_jars + generated_src_jars, omit_if_empty = True)
args.add_all("--deps_artifacts", deps_artifacts, omit_if_empty = True)
Expand Down
7 changes: 7 additions & 0 deletions kotlin/internal/toolchains.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def _kotlin_toolchain_impl(ctx):
experimental_strict_kotlin_deps = ctx.attr.experimental_strict_kotlin_deps,
experimental_report_unused_deps = ctx.attr.experimental_report_unused_deps,
experimental_reduce_classpath_mode = ctx.attr.experimental_reduce_classpath_mode,
experimental_build_tools_api = ctx.attr.experimental_build_tools_api,
javac_options = ctx.attr.javac_options[JavacOptions] if ctx.attr.javac_options else None,
kotlinc_options = ctx.attr.kotlinc_options[KotlincOptions] if ctx.attr.kotlinc_options else None,
empty_jar = ctx.file._empty_jar,
Expand Down Expand Up @@ -247,6 +248,10 @@ _kt_toolchain = rule(
"KOTLINBUILDER_REDUCED",
],
),
"experimental_build_tools_api": attr.bool(
doc = "Enables experimental support for Build Tools API integration",
default = False,
),
"javac_options": attr.label(
doc = "Compiler options for javac",
providers = [JavacOptions],
Expand Down Expand Up @@ -321,6 +326,7 @@ def define_kt_toolchain(
experimental_report_unused_deps = None,
experimental_reduce_classpath_mode = None,
experimental_multiplex_workers = None,
experimental_build_tools_api = None,
javac_options = Label("//kotlin/internal:default_javac_options"),
kotlinc_options = Label("//kotlin/internal:default_kotlinc_options"),
jvm_stdlibs = None,
Expand Down Expand Up @@ -349,6 +355,7 @@ def define_kt_toolchain(
experimental_strict_kotlin_deps = experimental_strict_kotlin_deps,
experimental_report_unused_deps = experimental_report_unused_deps,
experimental_reduce_classpath_mode = experimental_reduce_classpath_mode,
experimental_build_tools_api = experimental_build_tools_api,
javac_options = javac_options,
kotlinc_options = kotlinc_options,
visibility = ["//visibility:public"],
Expand Down
6 changes: 6 additions & 0 deletions kotlin/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,9 @@ bool_flag(
build_setting_default = False,
visibility = ["//visibility:public"],
)

bool_flag(
name = "experimental_build_tools_api",
build_setting_default = False,
visibility = ["//visibility:public"],
)
7 changes: 7 additions & 0 deletions kotlin/settings/BUILD.release.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ bool_flag(
build_setting_default = False,
visibility = ["//visibility:public"],
)

# --@rules_kotlin//kotlin/settings:experimental_build_tools_api=True
bool_flag(
name = "experimental_build_tools_api",
build_setting_default = False,
visibility = ["//visibility:public"],
)
2 changes: 2 additions & 0 deletions src/main/kotlin/BUILD.release.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ java_binary(
"//kotlin/compiler:symbol-processing-cmdline",
"//src/main/kotlin/io/bazel/kotlin/compiler",
"@com_github_jetbrains_kotlin//:home",
"@kotlin_build_tools_impl//jar",
"@kotlinx_serialization_core_jvm//jar",
"@kotlinx_serialization_json//jar",
"@kotlinx_serialization_json_jvm//jar",
Expand All @@ -55,6 +56,7 @@ java_binary(
"-D@com_github_jetbrains_kotlinx...serialization-core-jvm=$(rlocationpath @kotlinx_serialization_core_jvm//jar)",
"-D@com_github_jetbrains_kotlinx...serialization-json=$(rlocationpath @kotlinx_serialization_json//jar)",
"-D@com_github_jetbrains_kotlinx...serialization-json-jvm=$(rlocationpath @kotlinx_serialization_json_jvm//jar)",
"-D@com_github_jetbrains_kotlin...build-tools-impl=$(rlocationpath @kotlin_build_tools_impl//jar)",
"-D@com_github_jetbrains_kotlin...jvm-abi-gen=$(rlocationpath //kotlin/compiler:jvm-abi-gen)",
"-D@com_github_jetbrains_kotlin...kotlin-compiler=$(rlocationpath //kotlin/compiler:kotlin-compiler)",
"-D@com_github_jetbrains_kotlin...kapt=$(rlocationpath //kotlin/compiler:kotlin-annotation-processing)",
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/io/bazel/kotlin/builder/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kt_bootstrap_binary(
"//src/main/kotlin:skip-code-gen",
"//src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar",
"@com_github_jetbrains_kotlin//:home",
"@kotlin_build_tools_impl//jar",
"@kotlinx_serialization_core_jvm//jar",
"@kotlinx_serialization_json//jar",
"@kotlinx_serialization_json_jvm//jar",
Expand All @@ -34,6 +35,7 @@ kt_bootstrap_binary(
"-D@com_github_jetbrains_kotlinx...serialization-core-jvm=$(rlocationpath @kotlinx_serialization_core_jvm//jar)",
"-D@com_github_jetbrains_kotlinx...serialization-json=$(rlocationpath @kotlinx_serialization_json//jar)",
"-D@com_github_jetbrains_kotlinx...serialization-json-jvm=$(rlocationpath @kotlinx_serialization_json_jvm//jar)",
"-D@com_github_jetbrains_kotlin...build-tools-impl=$(rlocationpath @kotlin_build_tools_impl//jar)",
"-D@com_github_jetbrains_kotlin...jvm-abi-gen=$(rlocationpath //kotlin/compiler:jvm-abi-gen)",
"-D@com_github_jetbrains_kotlin...kotlin-compiler=$(rlocationpath //kotlin/compiler:kotlin-compiler)",
"-D@com_github_jetbrains_kotlin...kapt=$(rlocationpath //kotlin/compiler:kotlin-annotation-processing)",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class KotlinBuilder
REDUCED_CLASSPATH_MODE("--reduced_classpath_mode"),
INSTRUMENT_COVERAGE("--instrument_coverage"),
KSP_GENERATED_JAVA_SRCJAR("--ksp_generated_java_srcjar"),
BUILD_TOOLS_API("--build_tools_api"),
}
}

Expand Down Expand Up @@ -169,6 +170,9 @@ class KotlinBuilder
argMap.optionalSingle(KotlinBuilderFlags.ABI_JAR_REMOVE_PRIVATE_CLASSES)?.let {
removePrivateClassesInAbiJar = it == "true"
}
argMap.optionalSingle(KotlinBuilderFlags.BUILD_TOOLS_API)?.let {
buildToolsApi = it == "true"
}
this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const val X_FRIENDS_PATH_SEPARATOR = ","
class KotlinJvmTaskExecutor
@Inject
internal constructor(
private val compiler: KotlinToolchain.KotlincInvoker,
private val compilerBuilder: KotlinToolchain.KotlincInvokerBuilder,
private val plugins: InternalCompilerPlugins,
) {
private fun combine(
Expand All @@ -54,6 +54,8 @@ class KotlinJvmTaskExecutor
context: CompilationTaskContext,
task: JvmCompilationTask,
) {
val compiler = compilerBuilder.build(context.info.buildToolsApi)

val preprocessedTask =
task
.preProcessingSteps(context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ class KotlinToolchain private constructor(
).toPath()
}

private val BUILD_TOOLS_API by lazy {
BazelRunFiles
.resolveVerifiedFromProperty(
"@com_github_jetbrains_kotlin...build-tools-impl",
).toPath()
}

private val JAVA_HOME by lazy {
FileSystems
.getDefault()
Expand All @@ -144,6 +151,7 @@ class KotlinToolchain private constructor(
JAVA_HOME,
KOTLINC.verified().absoluteFile,
COMPILER.verified().absoluteFile,
BUILD_TOOLS_API.verified().absoluteFile,
JVM_ABI_PLUGIN.verified().absoluteFile,
SKIP_CODE_GEN_PLUGIN.verified().absoluteFile,
JDEPS_GEN_PLUGIN.verified().absoluteFile,
Expand All @@ -159,6 +167,7 @@ class KotlinToolchain private constructor(
fun createToolchain(
javaHome: Path,
kotlinc: File,
buildTools: File,
compiler: File,
jvmAbiGenFile: File,
skipCodeGenFile: File,
Expand All @@ -174,6 +183,7 @@ class KotlinToolchain private constructor(
listOf(
kotlinc,
compiler,
buildTools,
// plugins *must* be preloaded. Not doing so causes class conflicts
// (and a NoClassDef err) in the compiler extension interfaces.
// This may cause issues in accepting user defined compiler plugins.
Expand Down Expand Up @@ -263,7 +273,7 @@ class KotlinToolchain private constructor(
val id: String,
)

open class KotlinCliToolInvoker internal constructor(
open class KotlincInvoker internal constructor(
toolchain: KotlinToolchain,
clazz: String,
) {
Expand Down Expand Up @@ -296,12 +306,19 @@ class KotlinToolchain private constructor(
}

@Singleton
class KotlincInvoker
class KotlincInvokerBuilder
@Inject
constructor(
toolchain: KotlinToolchain,
) : KotlinCliToolInvoker(
toolchain.toolchainWithReflect(),
"io.bazel.kotlin.compiler.BazelK2JVMCompiler",
)
private val toolchain: KotlinToolchain,
) {
fun build(useExperimentalBuildToolsAPI: Boolean): KotlincInvoker {
val clazz =
if (useExperimentalBuildToolsAPI) {
"io.bazel.kotlin.compiler.BuildToolsAPICompiler"
} else {
"io.bazel.kotlin.compiler.BazelK2JVMCompiler"
}
return KotlincInvoker(toolchain = toolchain, clazz = clazz)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2025 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 io.bazel.kotlin.compiler

import org.jetbrains.kotlin.buildtools.api.CompilationResult
import org.jetbrains.kotlin.buildtools.api.CompilationService
import org.jetbrains.kotlin.buildtools.api.ExperimentalBuildToolsApi
import org.jetbrains.kotlin.buildtools.api.ProjectId
import org.jetbrains.kotlin.cli.common.ExitCode
import java.util.UUID

@Suppress("unused")
class BuildToolsAPICompiler {
@OptIn(ExperimentalBuildToolsApi::class)
fun exec(
errStream: java.io.PrintStream,
vararg args: String,
): ExitCode {
System.setProperty("zip.handler.uses.crc.instead.of.timestamp", "true")

val kotlinService = CompilationService.loadImplementation(this.javaClass.classLoader!!)
// The execution configuration. Controls in-process vs daemon execution strategies. Default is in-process.
val executionConfig = kotlinService.makeCompilerExecutionStrategyConfiguration()
// The compilation configuration. Controls everything related to incremental compilation.
val compilationConfig =
kotlinService.makeJvmCompilationConfiguration().apply {
// useLogger(BasicKotlinLogger(true, "/tmp/kotlin_log/$label.log"))
}

val result =
kotlinService.compileJvm(
ProjectId.ProjectUUID(UUID.randomUUID()),
executionConfig,
compilationConfig,
emptyList(),
args.toList(),
)

// BTAPI returns a different type than K2JVMCompiler (CompilationResult vs ExitCode).
return when (result) {
CompilationResult.COMPILATION_SUCCESS -> ExitCode.OK
CompilationResult.COMPILATION_ERROR -> ExitCode.COMPILATION_ERROR
CompilationResult.COMPILATION_OOM_ERROR -> ExitCode.OOM_ERROR
CompilationResult.COMPILER_INTERNAL_ERROR -> ExitCode.INTERNAL_ERROR
}
}
}
Loading