1515 */
1616package com.tschuchort.kotlinelements
1717
18- import com.google.common.collect.LinkedHashMultimap
1918import okio.Buffer
20- import okio.Okio
2119import okio.buffer
2220import okio.sink
2321import java.io.File
@@ -28,21 +26,33 @@ import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
2826import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
2927import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
3028import org.jetbrains.kotlin.config.Services
31- import java.io.FileOutputStream
3229import java.io.ObjectOutputStream
3330import java.io.PrintStream
3431import java.net.URLClassLoader
3532import 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
3945class 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)
0 commit comments