Skip to content

Commit 1340e49

Browse files
committed
find tools.jar at runtime and make it configurable
1 parent d0a5892 commit 1340e49

File tree

3 files changed

+38
-31
lines changed

3 files changed

+38
-31
lines changed

build.gradle

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ dependencies {
5454
// an obsolete version of Guava
5555
testCompile "org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.11"
5656
testCompile "org.jetbrains.kotlin:kotlin-annotation-processing-embeddable:1.3.11"
57-
58-
testCompile "com.google.testing.compile:compile-testing:0.15"
59-
60-
testCompile files("${System.properties['java.home']}/../lib/tools.jar")
6157
}
6258

6359
compileKotlin {

src/test/kotlin/KotlinCompilation.kt

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package com.tschuchort.kotlinelements
1717

18-
import com.google.common.collect.LinkedHashMultimap
1918
import okio.Buffer
20-
import okio.Okio
2119
import okio.buffer
2220
import okio.sink
2321
import java.io.File
@@ -28,21 +26,33 @@ import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
2826
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
2927
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
3028
import org.jetbrains.kotlin.config.Services
31-
import java.io.FileOutputStream
3229
import java.io.ObjectOutputStream
3330
import java.io.PrintStream
3431
import java.net.URLClassLoader
3532
import java.net.URLDecoder
36-
import java.util.zip.ZipEntry
37-
import java.util.zip.ZipOutputStream
33+
34+
private fun findJavaHome(): File {
35+
val path = System.getProperty("java.home")
36+
?: throw IllegalStateException("no java home found")
37+
38+
return File(path).also { check(it.isDirectory) }
39+
}
40+
41+
private fun findToolsJar(javaHome: File): File
42+
= File(javaHome.absolutePath + "/../lib/tools.jar").also { check(it.isFile) }
43+
3844

3945
class KotlinCompilation(
40-
val dir: File,
41-
val args: List<String> = emptyList(),
42-
val kaptArgs: Map<String, String> = emptyMap(),
43-
classpaths: List<String> = emptyList(),
44-
val sources: List<SourceFile> = emptyList(),
45-
inheritClassPath: Boolean = false
46+
val dir: File,
47+
val args: List<String> = emptyList(),
48+
val kaptArgs: Map<String, String> = emptyMap(),
49+
classpaths: List<String> = emptyList(),
50+
val sources: List<SourceFile> = emptyList(),
51+
private val jdkHome: File = findJavaHome(),
52+
toolsJar: File = findToolsJar(jdkHome),
53+
inheritClassPath: Boolean = false,
54+
correctErrorTypes: Boolean = false,
55+
private val skipRuntimeVersionCheck: Boolean = false
4656
) {
4757
val sourcesDir = File(dir, "sources")
4858
val classesDir = File(dir, "classes")
@@ -82,21 +92,25 @@ class KotlinCompilation(
8292
}
8393

8494
/** Returns arguments necessary to enable and configure kapt3. */
85-
private fun annotationProcessorArgs() = object {
95+
private val annotationProcessorArgs = object {
8696
private val kaptSourceDir = File(dir, "kapt/sources")
8797
private val kaptStubsDir = File(dir, "kapt/stubs")
8898

89-
val pluginClassPaths = arrayOf(getKapt3Jar().absolutePath)
99+
val pluginClassPaths = arrayOf(getKapt3Jar().absolutePath, toolsJar.absolutePath)
90100
val pluginOptions = arrayOf(
91101
"plugin:org.jetbrains.kotlin.kapt3:sources=$kaptSourceDir",
92102
"plugin:org.jetbrains.kotlin.kapt3:classes=$classesDir",
93103
"plugin:org.jetbrains.kotlin.kapt3:stubs=$kaptStubsDir",
94104
"plugin:org.jetbrains.kotlin.kapt3:apclasspath=$sourcesDir",
95-
"plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=true",
105+
"plugin:org.jetbrains.kotlin.kapt3:correctErrorTypes=$correctErrorTypes",
96106
// Don't forget aptMode! Without it, the compiler will crash with an obscure error about
97107
// write unsafe context
98108
"plugin:org.jetbrains.kotlin.kapt3:aptMode=stubsAndApt",
99-
"plugin:org.jetbrains.kotlin.kapt3:processors=com.tschuchort.kotlinelements.TestProcessor"
109+
"plugin:org.jetbrains.kotlin.kapt3:processors=com.tschuchort.kotlinelements.TestProcessor",
110+
*if (kaptArgs.isNotEmpty())
111+
arrayOf("plugin:org.jetbrains.kotlin.kapt3:apoptions=${encodeOptions(kaptArgs)}")
112+
else
113+
emptyArray()
100114
)
101115
}
102116

@@ -105,32 +119,28 @@ class KotlinCompilation(
105119
it.writeTo(sourcesDir)
106120
}
107121

108-
val annotationProcessorArgs = annotationProcessorArgs()
109-
110-
val args = K2JVMCompilerArguments().apply {
122+
val k2jvmArgs = K2JVMCompilerArguments().apply {
111123
freeArgs = sourcesDir.listFiles().map { it.absolutePath }
112-
pluginOptions = annotationProcessorArgs.pluginOptions +
113-
if (kaptArgs.isNotEmpty())
114-
arrayOf("plugin:org.jetbrains.kotlin.kapt3:apoptions=${encodeOptions(kaptArgs)}")
115-
else
116-
emptyArray()
124+
pluginOptions = annotationProcessorArgs.pluginOptions
117125
pluginClasspaths = annotationProcessorArgs.pluginClassPaths
118126
loadBuiltInsFromDependencies = true
119127
destination = classesDir.absolutePath
120128
classpath = allClasspaths.joinToString(separator = File.pathSeparator)
121129
noStdlib = true
122130
noReflect = true
123-
skipRuntimeVersionCheck = true
131+
skipRuntimeVersionCheck = this@KotlinCompilation.skipRuntimeVersionCheck
124132
reportPerf = false
133+
reportOutputFiles = true
134+
jdkHome = this@KotlinCompilation.jdkHome.absolutePath
125135
}
126136

127137
val compilerOutputbuffer = Buffer()
128138

129139
val exitCode = K2JVMCompiler().execImpl(
130140
PrintingMessageCollector(PrintStream(compilerOutputbuffer.outputStream()),
131141
MessageRenderer.WITHOUT_PATHS, true),
132-
services.build(),
133-
args
142+
Services.EMPTY,//services.build(),
143+
k2jvmArgs
134144
)
135145

136146
return Result(compilerOutputbuffer.readUtf8(), exitCode)

src/test/kotlin/SmokeTests.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class SmokeTests {
2626
val result = KotlinCompilation(
2727
dir = temporaryFolder.root,
2828
sources = listOf(source),
29-
inheritClassPath = true
29+
inheritClassPath = true,
30+
skipRuntimeVersionCheck = true
3031
).apply {
3132
addService(Processor::class, TestProcessor())
3233
}.run()

0 commit comments

Comments
 (0)