diff --git a/src/main/kotlin/BUILD.release.bazel b/src/main/kotlin/BUILD.release.bazel index 37272bdc6..5f04f0e85 100644 --- a/src/main/kotlin/BUILD.release.bazel +++ b/src/main/kotlin/BUILD.release.bazel @@ -42,6 +42,7 @@ java_binary( "//kotlin/compiler:jvm-abi-gen", "//kotlin/compiler:kotlin-annotation-processing", "//kotlin/compiler:kotlin-compiler", + "//kotlin/compiler:kotlin-reflect", "//kotlin/compiler:symbol-processing-api", "//kotlin/compiler:symbol-processing-cmdline", "//src/main/kotlin/io/bazel/kotlin/compiler", @@ -56,6 +57,7 @@ java_binary( "-D@rules_kotlin...compiler=$(rlocationpath //src/main/kotlin/io/bazel/kotlin/compiler)", "-D@com_github_google_ksp...symbol-processing-api=$(rlocationpath //kotlin/compiler:symbol-processing-api)", "-D@com_github_google_ksp...symbol-processing-cmdline=$(rlocationpath //kotlin/compiler:symbol-processing-cmdline)", + "-D@rules_kotlin..kotlin.compiler.kotlin-reflect=$(rlocationpath //kotlin/compiler:kotlin-reflect)", "-XX:-MaxFDLimit", ], main_class = "io.bazel.kotlin.builder.cmd.Build", diff --git a/src/main/kotlin/bootstrap.bzl b/src/main/kotlin/bootstrap.bzl index 20a45b3e8..9f7985d4a 100644 --- a/src/main/kotlin/bootstrap.bzl +++ b/src/main/kotlin/bootstrap.bzl @@ -21,12 +21,8 @@ def kt_bootstrap_library(name, deps = [], neverlink_deps = [], **kwargs): """ Simple compilation of a kotlin library using a non-persistent worker. The target is a JavaInfo provider. - The target is tagged `"no-ide"` as intellij can't compile it. A seperate private target is created which is suffixed - with `_for_ide`. If the dep is under the package `//src/main/kotlin/io/bazel/kotlin/builder/...` then it will be - added to the `_for_ide` target by adding a `_for_ide` prefix. - deps: the dependenices, the are setup as runtime_deps of the library. - neverlink_deps: deps that won't be linked. These deps are added to the `"for_ide"` target. + neverlink_deps: deps that won't be linked. """ kt_jvm_library( name = "%s_neverlink" % name, diff --git a/src/main/kotlin/io/bazel/kotlin/builder/cmd/BUILD.bazel b/src/main/kotlin/io/bazel/kotlin/builder/cmd/BUILD.bazel index 7f6c7c79f..5be5d51a6 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/cmd/BUILD.bazel +++ b/src/main/kotlin/io/bazel/kotlin/builder/cmd/BUILD.bazel @@ -18,6 +18,7 @@ kt_bootstrap_binary( "//kotlin/compiler:jvm-abi-gen", "//kotlin/compiler:kotlin-annotation-processing", "//kotlin/compiler:kotlin-compiler", + "//kotlin/compiler:kotlin-reflect", "//kotlin/compiler:symbol-processing-api", "//kotlin/compiler:symbol-processing-cmdline", "//src/main/kotlin:jdeps-gen", @@ -34,6 +35,7 @@ kt_bootstrap_binary( "-D@rules_kotlin...compiler=$(rlocationpath //src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar)", "-D@com_github_google_ksp...symbol-processing-api=$(rlocationpath //kotlin/compiler:symbol-processing-api)", "-D@com_github_google_ksp...symbol-processing-cmdline=$(rlocationpath //kotlin/compiler:symbol-processing-cmdline)", + "-D@rules_kotlin..kotlin.compiler.kotlin-reflect=$(rlocationpath //kotlin/compiler:kotlin-reflect)", "-XX:-MaxFDLimit", ], main_class = "io.bazel.kotlin.builder.cmd.Build", diff --git a/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt b/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt index 564cd8def..a47bf4d09 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt @@ -30,9 +30,10 @@ import java.nio.file.Path import java.nio.file.Paths import javax.inject.Inject import javax.inject.Singleton +import java.lang.ClassLoader class KotlinToolchain private constructor( - val classLoader: ClassLoader, + private val baseJars: List, val kapt3Plugin: CompilerPlugin, val jvmAbiGen: CompilerPlugin, val skipCodeGen: CompilerPlugin, @@ -90,33 +91,26 @@ class KotlinToolchain private constructor( ).toPath() } + private val KOTLIN_REFLECT by lazy { + BazelRunFiles.resolveVerifiedFromProperty( + "@rules_kotlin..kotlin.compiler.kotlin-reflect", + ).toPath() + } + + private val JAVA_HOME by lazy { + FileSystems.getDefault().getPath(System.getProperty("java.home")).let { path -> + path.takeIf { !it.endsWith(Paths.get("jre")) } ?: path.parent + }.verifiedPath() + } + internal val NO_ARGS = arrayOf() private val isJdk9OrNewer = !System.getProperty("java.version").startsWith("1.") - private fun createClassLoader(javaHome: Path, baseJars: List): ClassLoader = - runCatching { - ClassPreloadingUtils.preloadClasses( - mutableListOf().also { - it += baseJars - if (!isJdk9OrNewer) { - it += javaHome.resolveVerified("lib", "tools.jar") - } - }, - Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE, - ClassLoader.getSystemClassLoader(), - null, - ) - }.onFailure { - throw RuntimeException("$javaHome, $baseJars", it) - }.getOrThrow() - @JvmStatic fun createToolchain(): KotlinToolchain { return createToolchain( - FileSystems.getDefault().getPath(System.getProperty("java.home")).let { path -> - path.takeIf { !it.endsWith(Paths.get("jre")) } ?: path.parent - }.verifiedPath(), + JAVA_HOME, KOTLINC.verified().absoluteFile, COMPILER.verified().absoluteFile, JVM_ABI_PLUGIN.verified().absoluteFile, @@ -141,20 +135,17 @@ class KotlinToolchain private constructor( kspSymbolProcessingCommandLine: File, ): KotlinToolchain { return KotlinToolchain( - createClassLoader( - javaHome, - listOf( - kotlinc, - compiler, - // 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. - jvmAbiGenFile, - skipCodeGenFile, - jdepsGenFile, - kspSymbolProcessingApi, - kspSymbolProcessingCommandLine, - ), + listOf( + kotlinc, + compiler, + // 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. + jvmAbiGenFile, + skipCodeGenFile, + jdepsGenFile, + kspSymbolProcessingApi, + kspSymbolProcessingCommandLine, ), jvmAbiGen = CompilerPlugin( jvmAbiGenFile.path, @@ -184,6 +175,48 @@ class KotlinToolchain private constructor( } } + + private fun createClassLoader( + javaHome: Path, + baseJars: List, + classLoader: ClassLoader = ClassLoader.getSystemClassLoader(), + ): ClassLoader = + runCatching { + ClassPreloadingUtils.preloadClasses( + mutableListOf().also { + it += baseJars + if (!isJdk9OrNewer) { + it += javaHome.resolveVerified("lib", "tools.jar") + } + }, + Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE, + classLoader, + null, + ) + }.onFailure { + throw RuntimeException("$javaHome, $baseJars", it) + }.getOrThrow() + + + val classLoader by lazy { + createClassLoader( + JAVA_HOME, + baseJars, + ) + } + + fun toolchainWithReflect(kotlinReflect: File? = null): KotlinToolchain { + return KotlinToolchain( + baseJars + listOf(kotlinReflect ?: KOTLIN_REFLECT.toFile()), + kapt3Plugin, + jvmAbiGen, + skipCodeGen, + jdepsGen, + kspSymbolProcessingApi, + kspSymbolProcessingCommandLine, + ) + } + data class CompilerPlugin(val jarPath: String, val id: String) open class KotlinCliToolInvoker internal constructor( @@ -223,5 +256,8 @@ class KotlinToolchain private constructor( @Singleton class K2JSCompilerInvoker @Inject constructor( toolchain: KotlinToolchain, - ) : KotlinCliToolInvoker(toolchain, "org.jetbrains.kotlin.cli.js.K2JSCompiler") + ) : KotlinCliToolInvoker( + toolchain.toolchainWithReflect(), + "org.jetbrains.kotlin.cli.js.K2JSCompiler", + ) } diff --git a/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java b/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java index f64de1578..224191238 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java +++ b/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java @@ -236,7 +236,7 @@ static KotlinToolchain toolchainForTest() { return KotlinToolchain.createToolchain( javaHome, new File(Deps.Dep.fromLabel("//kotlin/compiler:kotlin-compiler").singleCompileJar()), - new File(Deps.Dep.fromLabel("//src/main/kotlin/io/bazel/kotlin/compiler").singleCompileJar()), + new File(Deps.Dep.fromLabel("//src/main/kotlin/io/bazel/kotlin/compiler:compiler.jar").singleCompileJar()), new File(Deps.Dep.fromLabel("//kotlin/compiler:jvm-abi-gen").singleCompileJar()), new File(Deps.Dep.fromLabel("//src/main/kotlin:skip-code-gen").singleCompileJar()), new File(Deps.Dep.fromLabel("//src/main/kotlin:jdeps-gen").singleCompileJar()), diff --git a/src/test/kotlin/io/bazel/kotlin/builder/KotlinJsTestBuilder.java b/src/test/kotlin/io/bazel/kotlin/builder/KotlinJsTestBuilder.java index 9707c817c..35315cef1 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/KotlinJsTestBuilder.java +++ b/src/test/kotlin/io/bazel/kotlin/builder/KotlinJsTestBuilder.java @@ -15,8 +15,11 @@ */ package io.bazel.kotlin.builder; +import io.bazel.kotlin.builder.toolchain.KotlinToolchain; import io.bazel.kotlin.model.CompilationTaskInfo; import io.bazel.kotlin.model.JsCompilationTask; + +import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.util.Arrays; @@ -29,11 +32,17 @@ public final class KotlinJsTestBuilder extends KotlinAbstractTestBuilder ALL_DIRECTORY_TYPES = EnumSet.allOf(DirectoryType.class); private final TaskBuilder taskBuilderInstance = new TaskBuilder(); + private static KotlinToolchain withReflect(KotlinToolchain toolchain) { + return toolchain.toolchainWithReflect( + new File(Deps.Dep.fromLabel("@rules_kotlin//kotlin/compiler:kotlin-reflect").singleCompileJar()) + ); + } + @Override JsCompilationTask buildTask() { return taskBuilder.build(); diff --git a/src/test/kotlin/io/bazel/kotlin/builder/tasks/BUILD.bazel b/src/test/kotlin/io/bazel/kotlin/builder/tasks/BUILD.bazel index ae51378a8..8c4aae4f8 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/tasks/BUILD.bazel +++ b/src/test/kotlin/io/bazel/kotlin/builder/tasks/BUILD.bazel @@ -90,8 +90,6 @@ kt_rules_test( kt_rules_test( name = "KotlinBuilderJsTest", srcs = ["js/KotlinBuilderJsTest.java"], - args = [ - ], data = [ "@com_github_jetbrains_kotlin//:lib/kotlin-stdlib-js.jar", ], diff --git a/src/test/kotlin/io/bazel/kotlin/defs.bzl b/src/test/kotlin/io/bazel/kotlin/defs.bzl index e93967d83..a94783daf 100644 --- a/src/test/kotlin/io/bazel/kotlin/defs.bzl +++ b/src/test/kotlin/io/bazel/kotlin/defs.bzl @@ -43,6 +43,7 @@ def kt_rules_test(name, **kwargs): "//kotlin/compiler:kotlin-stdlib-jdk7", "//kotlin/compiler:kotlin-stdlib-jdk8", "//kotlin/compiler:kotlin-annotation-processing", + "@rules_kotlin//kotlin/compiler:kotlin-reflect", ] + args["data"]: if dep not in args["data"]: args["data"].append(dep)