From 3a4eb06a7dbc1476a8e648b980ff4f160bbb5ce6 Mon Sep 17 00:00:00 2001 From: Corbin Smith Date: Thu, 13 Feb 2020 17:14:02 -0800 Subject: [PATCH] move compiler to source package. add .bazelversion for development --- .bazelproject | 8 +- .bazelrc | 2 +- src/main/kotlin/BUILD | 17 +- .../builder/toolchain/KotlinToolchain.kt | 200 +++++++++--------- .../io/bazel/kotlin/compiler/BUILD.bazel | 27 +++ src/test/kotlin/io/bazel/kotlin/builder/BUILD | 2 +- 6 files changed, 138 insertions(+), 118 deletions(-) diff --git a/.bazelproject b/.bazelproject index b5daf792e..0be2b5a76 100644 --- a/.bazelproject +++ b/.bazelproject @@ -26,8 +26,10 @@ targets: //examples/dagger/... # 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:builder_kt_for_ide - //src/main/kotlin:compiler_lib_for_ide + //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 test_sources: src/test/* @@ -36,4 +38,4 @@ additional_languages: kotlin import_run_configurations: - src/test/Bazel_all_local_tests.xml \ No newline at end of file + src/test/Bazel_all_local_tests.xml diff --git a/.bazelrc b/.bazelrc index e75318b3a..6858b6449 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,4 +1,4 @@ build --strategy=KotlinCompile=worker -build --test_output=errors +build --test_output=all build --verbose_failures diff --git a/src/main/kotlin/BUILD b/src/main/kotlin/BUILD index bf72a07ce..7fd6f1c2f 100644 --- a/src/main/kotlin/BUILD +++ b/src/main/kotlin/BUILD @@ -12,23 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. load("@rules_java//java:defs.bzl", "java_binary") -load("//src/main/kotlin:bootstrap.bzl", "kt_bootstrap_library") load("//kotlin:kotlin.bzl", "kt_jvm_library") load("//third_party:jarjar.bzl", "jar_jar") load("//kotlin/internal/utils:packager.bzl", "release_archive") -# The compiler library, this is co-located in the kotlin compiler classloader. -kt_bootstrap_library( - name = "compiler_lib", - srcs = glob(["io/bazel/kotlin/compiler/*.kt"]), - neverlink_deps = [ - "@com_github_jetbrains_kotlin//:kotlin-compiler", - "@com_github_jetbrains_kotlin//:kotlin-annotation-processing", - "@com_github_jetbrains_kotlin//:kotlin-script-runtime", - ], - visibility = ["//src:__subpackages__"], -) - java_binary( name = "builder_raw", create_executable = False, @@ -46,7 +33,7 @@ jar_jar( java_binary( name = "builder", data = [ - ":compiler_lib.jar", + "//src/main/kotlin/io/bazel/kotlin/compiler:compiler", "@com_github_jetbrains_kotlin//:lib/kotlin-compiler.jar", ], jvm_flags = [ @@ -63,7 +50,7 @@ java_binary( release_archive( name = "pkg", srcs = [ - ":compiler_lib.jar", + "//src/main/kotlin/io/bazel/kotlin/compiler:compiler", ], package_dir = "src/main/kotlin", # explicitly set the package directory, as there are no parent release_archives. src_map = { 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 5fbf9e736..d76f48e0b 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt @@ -29,112 +29,116 @@ import javax.inject.Inject import javax.inject.Singleton class KotlinToolchain private constructor( - val kotlinHome: Path, - val classLoader: ClassLoader, - val kotlinStandardLibraries: List = listOf( - "kotlin-stdlib.jar", - "kotlin-stdlib-jdk7.jar", - "kotlin-stdlib-jdk8.jar" - ), - val kapt3Plugin: KotlinToolchain.CompilerPlugin = KotlinToolchain.CompilerPlugin( - kotlinHome.resolveVerified("lib", "kotlin-annotation-processing.jar").absolutePath, - "org.jetbrains.kotlin.kapt3" - ) + val kotlinHome: Path, + val classLoader: ClassLoader, + val kotlinStandardLibraries: List = listOf( + "kotlin-stdlib.jar", + "kotlin-stdlib-jdk7.jar", + "kotlin-stdlib-jdk8.jar" + ), + val kapt3Plugin: KotlinToolchain.CompilerPlugin = KotlinToolchain.CompilerPlugin( + kotlinHome.resolveVerified("lib", "kotlin-annotation-processing.jar").absolutePath, + "org.jetbrains.kotlin.kapt3" + ) ) { - companion object { - internal val NO_ARGS = arrayOf() - - private val isJdk9OrNewer = !System.getProperty("java.version").startsWith("1.") - - private fun createClassLoader(javaHome: Path, baseJars: List): ClassLoader = - ClassPreloadingUtils.preloadClasses( - mutableListOf().also { - it += baseJars - if (!isJdk9OrNewer) { - it += javaHome.resolveVerified("lib", "tools.jar") - } - }, - Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE, - ClassLoader.getSystemClassLoader(), - null - ) - - @JvmStatic - fun createToolchain(): KotlinToolchain { - val javaHome = Paths.get(System.getProperty("java.home")).let { path -> - path.takeIf { !it.endsWith(Paths.get("jre")) } ?: path.parent - } - val kotlinCompilerJar = BazelRunFiles.resolveVerified( - "external", "com_github_jetbrains_kotlin", "lib", "kotlin-compiler.jar") - return KotlinToolchain( - kotlinCompilerJar.toPath().parent.parent, - createClassLoader( - javaHome, - listOf( - kotlinCompilerJar, - BazelRunFiles.resolveVerified( - "io_bazel_rules_kotlin", "src", "main", "kotlin", "compiler_lib.jar") - ) - ) - ) - } + companion object { + internal val NO_ARGS = arrayOf() + + private val isJdk9OrNewer = !System.getProperty("java.version").startsWith("1.") + + private fun createClassLoader(javaHome: Path, baseJars: List): ClassLoader = + ClassPreloadingUtils.preloadClasses( + mutableListOf().also { + it += baseJars + if (!isJdk9OrNewer) { + it += javaHome.resolveVerified("lib", "tools.jar") + } + }, + Preloader.DEFAULT_CLASS_NUMBER_ESTIMATE, + ClassLoader.getSystemClassLoader(), + null + ) + + @JvmStatic + fun createToolchain(): KotlinToolchain { + val javaHome = Paths.get(System.getProperty("java.home")).let { path -> + path.takeIf { !it.endsWith(Paths.get("jre")) } ?: path.parent + } + val kotlinCompilerJar = BazelRunFiles.resolveVerified( + "external", "com_github_jetbrains_kotlin", "lib", "kotlin-compiler.jar") + return KotlinToolchain( + kotlinCompilerJar.toPath().parent.parent, + createClassLoader( + javaHome, + listOf( + kotlinCompilerJar, + BazelRunFiles.resolveVerified( + "io_bazel_rules_kotlin", + "src", "main", "kotlin", "io", "bazel", "kotlin", "compiler", + "compiler.jar") + ) + ) + ) } - - data class CompilerPlugin(val jarPath: String, val id: String) - - @Singleton - class JavacInvoker @Inject constructor(toolchain: KotlinToolchain) { - private val c = toolchain.classLoader.loadClass("com.sun.tools.javac.Main") - private val m = c.getMethod("compile", Array::class.java) - private val mPw = c.getMethod("compile", Array::class.java, PrintWriter::class.java) - fun compile(args: Array) = m.invoke(c, args) as Int - fun compile(args: Array, out: PrintWriter) = mPw.invoke(c, args, out) as Int - } - - @Singleton - class JDepsInvoker @Inject constructor(toolchain: KotlinToolchain) { - private val clazz = toolchain.classLoader.loadClass("com.sun.tools.jdeps.Main") - private val method = clazz.getMethod("run", Array::class.java, PrintWriter::class.java) - fun run(args: Array, out: PrintWriter): Int = method.invoke(clazz, args, out) as Int + } + + data class CompilerPlugin(val jarPath: String, val id: String) + + @Singleton + class JavacInvoker @Inject constructor(toolchain: KotlinToolchain) { + private val c = toolchain.classLoader.loadClass("com.sun.tools.javac.Main") + private val m = c.getMethod("compile", Array::class.java) + private val mPw = c.getMethod("compile", Array::class.java, PrintWriter::class.java) + fun compile(args: Array) = m.invoke(c, args) as Int + fun compile(args: Array, out: PrintWriter) = mPw.invoke(c, args, out) as Int + } + + @Singleton + class JDepsInvoker @Inject constructor(toolchain: KotlinToolchain) { + private val clazz = toolchain.classLoader.loadClass("com.sun.tools.jdeps.Main") + private val method = clazz.getMethod("run", Array::class.java, PrintWriter::class.java) + fun run(args: Array, out: PrintWriter): Int = method.invoke(clazz, args, out) as Int + } + + open class KotlinCliToolInvoker internal constructor( + toolchain: KotlinToolchain, + clazz: String + ) { + private val compiler: Any + private val execMethod: Method + private val getCodeMethod: Method + + init { + val compilerClass = toolchain.classLoader.loadClass(clazz) + val exitCodeClass = + toolchain.classLoader.loadClass("org.jetbrains.kotlin.cli.common.ExitCode") + + compiler = compilerClass.getConstructor().newInstance() + execMethod = + compilerClass.getMethod("exec", PrintStream::class.java, Array::class.java) + getCodeMethod = exitCodeClass.getMethod("getCode") } - open class KotlinCliToolInvoker internal constructor( - toolchain: KotlinToolchain, - clazz: String - ) { - private val compiler: Any - private val execMethod: Method - private val getCodeMethod: Method - - init { - val compilerClass = toolchain.classLoader.loadClass(clazz) - val exitCodeClass = toolchain.classLoader.loadClass("org.jetbrains.kotlin.cli.common.ExitCode") - - compiler = compilerClass.getConstructor().newInstance() - execMethod = compilerClass.getMethod("exec", PrintStream::class.java, Array::class.java) - getCodeMethod = exitCodeClass.getMethod("getCode") - } - - // Kotlin error codes: - // 1 is a standard compilation error - // 2 is an internal error - // 3 is the script execution error - fun compile(args: Array, out: PrintStream): Int { - val exitCodeInstance = execMethod.invoke(compiler, out, args) - return getCodeMethod.invoke(exitCodeInstance, *NO_ARGS) as Int - } + // Kotlin error codes: + // 1 is a standard compilation error + // 2 is an internal error + // 3 is the script execution error + fun compile(args: Array, out: PrintStream): Int { + val exitCodeInstance = execMethod.invoke(compiler, out, args) + return getCodeMethod.invoke(exitCodeInstance, *NO_ARGS) as Int } + } - @Singleton - class KotlincInvoker @Inject constructor( - toolchain: KotlinToolchain - ) : KotlinCliToolInvoker(toolchain, "io.bazel.kotlin.compiler.BazelK2JVMCompiler") + @Singleton + class KotlincInvoker @Inject constructor( + toolchain: KotlinToolchain + ) : KotlinCliToolInvoker(toolchain, "io.bazel.kotlin.compiler.BazelK2JVMCompiler") - @Singleton - class K2JSCompilerInvoker @Inject constructor( - toolchain: KotlinToolchain - ) : KotlinCliToolInvoker(toolchain, "org.jetbrains.kotlin.cli.js.K2JSCompiler") + @Singleton + class K2JSCompilerInvoker @Inject constructor( + toolchain: KotlinToolchain + ) : KotlinCliToolInvoker(toolchain, "org.jetbrains.kotlin.cli.js.K2JSCompiler") } diff --git a/src/main/kotlin/io/bazel/kotlin/compiler/BUILD.bazel b/src/main/kotlin/io/bazel/kotlin/compiler/BUILD.bazel index e69de29bb..d997f18ef 100644 --- a/src/main/kotlin/io/bazel/kotlin/compiler/BUILD.bazel +++ b/src/main/kotlin/io/bazel/kotlin/compiler/BUILD.bazel @@ -0,0 +1,27 @@ +# Copyright 2020 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/kotlin:bootstrap.bzl", "kt_bootstrap_library") + +# The compiler library, this is co-located in the kotlin compiler classloader. +kt_bootstrap_library( + name = "compiler", + srcs = glob(["*.kt"]), + visibility = ["//src:__subpackages__"], + neverlink_deps = [ + "@com_github_jetbrains_kotlin//:kotlin-compiler", + "@com_github_jetbrains_kotlin//:kotlin-annotation-processing", + "@com_github_jetbrains_kotlin//:kotlin-script-runtime", + ], +) diff --git a/src/test/kotlin/io/bazel/kotlin/builder/BUILD b/src/test/kotlin/io/bazel/kotlin/builder/BUILD index 47e3f0fa2..9c366abfb 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/BUILD +++ b/src/test/kotlin/io/bazel/kotlin/builder/BUILD @@ -39,7 +39,7 @@ java_library( "KotlinJvmTestBuilder.java", ], data = [ - "//src/main/kotlin:compiler_lib", + "//src/main/kotlin/io/bazel/kotlin/compiler", "@com_github_jetbrains_kotlin//:home", ], exports = _COMMON_DEPS + [