Skip to content

Commit

Permalink
Add reflect for js.
Browse files Browse the repository at this point in the history
  • Loading branch information
restingbull committed Dec 8, 2023
1 parent d745aa5 commit dd2af02
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 45 deletions.
2 changes: 2 additions & 0 deletions src/main/kotlin/BUILD.release.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
6 changes: 1 addition & 5 deletions src/main/kotlin/bootstrap.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
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 @@ -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",
Expand All @@ -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",
Expand Down
108 changes: 72 additions & 36 deletions src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<File>,
val kapt3Plugin: CompilerPlugin,
val jvmAbiGen: CompilerPlugin,
val skipCodeGen: CompilerPlugin,
Expand Down Expand Up @@ -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<Any>()

private val isJdk9OrNewer = !System.getProperty("java.version").startsWith("1.")

private fun createClassLoader(javaHome: Path, baseJars: List<File>): ClassLoader =
runCatching {
ClassPreloadingUtils.preloadClasses(
mutableListOf<File>().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,
Expand All @@ -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,
Expand Down Expand Up @@ -184,6 +175,48 @@ class KotlinToolchain private constructor(
}
}


private fun createClassLoader(
javaHome: Path,
baseJars: List<File>,
classLoader: ClassLoader = ClassLoader.getSystemClassLoader(),
): ClassLoader =
runCatching {
ClassPreloadingUtils.preloadClasses(
mutableListOf<File>().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(
Expand Down Expand Up @@ -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",
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down
11 changes: 10 additions & 1 deletion src/test/kotlin/io/bazel/kotlin/builder/KotlinJsTestBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,11 +32,17 @@ public final class KotlinJsTestBuilder extends KotlinAbstractTestBuilder<JsCompi
Arrays.asList("-source-map", "-module-kind", "commonjs", "-target", "v5", "-Xir-produce-klib-dir");
private static final JsCompilationTask.Builder taskBuilder = JsCompilationTask.newBuilder();
private static final KotlinBuilderComponent component =
DaggerKotlinBuilderComponent.builder().toolchain(toolchainForTest()).build();
DaggerKotlinBuilderComponent.builder().toolchain(withReflect(toolchainForTest())).build();
private static final EnumSet<DirectoryType> 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();
Expand Down
2 changes: 0 additions & 2 deletions src/test/kotlin/io/bazel/kotlin/builder/tasks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
Expand Down
1 change: 1 addition & 0 deletions src/test/kotlin/io/bazel/kotlin/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit dd2af02

Please sign in to comment.