Skip to content

Commit

Permalink
move compiler to source package. add .bazelversion for development
Browse files Browse the repository at this point in the history
  • Loading branch information
Corbin Smith committed Feb 24, 2020
1 parent 381015f commit 3a4eb06
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 118 deletions.
8 changes: 5 additions & 3 deletions .bazelproject
Original file line number Diff line number Diff line change
Expand Up @@ -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/*
Expand All @@ -36,4 +38,4 @@ additional_languages:
kotlin

import_run_configurations:
src/test/Bazel_all_local_tests.xml
src/test/Bazel_all_local_tests.xml
2 changes: 1 addition & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
build --strategy=KotlinCompile=worker
build --test_output=errors
build --test_output=all
build --verbose_failures

17 changes: 2 additions & 15 deletions src/main/kotlin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = [
Expand All @@ -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 = {
Expand Down
200 changes: 102 additions & 98 deletions src/main/kotlin/io/bazel/kotlin/builder/toolchain/KotlinToolchain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<String> = 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<Any>()

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

private fun createClassLoader(javaHome: Path, baseJars: List<File>): ClassLoader =
ClassPreloadingUtils.preloadClasses(
mutableListOf<File>().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<Any>()

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

private fun createClassLoader(javaHome: Path, baseJars: List<File>): ClassLoader =
ClassPreloadingUtils.preloadClasses(
mutableListOf<File>().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<String>::class.java)
private val mPw = c.getMethod("compile", Array<String>::class.java, PrintWriter::class.java)
fun compile(args: Array<String>) = m.invoke(c, args) as Int
fun compile(args: Array<String>, 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<String>::class.java, PrintWriter::class.java)
fun run(args: Array<String>, 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<String>::class.java)
private val mPw = c.getMethod("compile", Array<String>::class.java, PrintWriter::class.java)
fun compile(args: Array<String>) = m.invoke(c, args) as Int
fun compile(args: Array<String>, 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<String>::class.java, PrintWriter::class.java)
fun run(args: Array<String>, 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<String>::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<String>::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<String>, 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<String>, 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")
}


27 changes: 27 additions & 0 deletions src/main/kotlin/io/bazel/kotlin/compiler/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
],
)
2 changes: 1 addition & 1 deletion src/test/kotlin/io/bazel/kotlin/builder/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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 + [
Expand Down

0 comments on commit 3a4eb06

Please sign in to comment.