@@ -11,15 +11,15 @@ import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
1111import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
1212import org.jetbrains.kotlin.cli.js.K2JSCompiler
1313import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
14+ import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
1415import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
16+ import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
1517import org.jetbrains.kotlin.config.Services
1618import org.jetbrains.kotlin.load.java.JvmAbi
1719import org.jetbrains.kotlin.util.ServiceLoaderLite
1820import java.io.File
1921import java.io.OutputStream
2022import java.io.PrintStream
21- import java.lang.reflect.InvocationTargetException
22- import java.lang.reflect.ReflectPermission
2323import java.net.URI
2424import java.nio.file.Files
2525import java.nio.file.Paths
@@ -29,6 +29,7 @@ import java.nio.file.Paths
2929 * functionality. Should not be used outside of this library as it is an
3030 * implementation detail.
3131 */
32+ @ExperimentalCompilerApi
3233abstract class AbstractKotlinCompilation <A : CommonCompilerArguments > internal constructor() {
3334 /* * Working directory for the compilation */
3435 var workingDir: File by default {
@@ -49,10 +50,29 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
4950 */
5051 var pluginClasspaths: List <File > = emptyList()
5152
53+
54+ @Suppress(" DEPRECATION" )
55+ @Deprecated(
56+ " Renamed to componentRegistrars due to introduction of CompilerPluginRegistrar" ,
57+ ReplaceWith (" componentRegistrars" ),
58+ DeprecationLevel .ERROR
59+ )
60+ var compilerPlugins: List <ComponentRegistrar > = emptyList()
61+
62+
63+
64+ /* *
65+ * Legacy compiler plugins that should be added to the compilation.
66+ * This option will be removed in the future; you should migrate to [CompilerPluginRegistrar].
67+ */
68+ @Suppress(" DEPRECATION" )
69+ @Deprecated(" Deprecated in Kotlin compiler. Migrate to compilerPluginRegistrars instead" )
70+ var componentRegistrars: List <ComponentRegistrar > = emptyList()
71+
5272 /* *
5373 * Compiler plugins that should be added to the compilation
5474 */
55- var compilerPlugins : List <ComponentRegistrar > = emptyList()
75+ var compilerPluginRegistrars : List <CompilerPluginRegistrar > = emptyList()
5676
5777 /* *
5878 * Commandline processors for compiler plugins that should be added to the compilation
@@ -115,6 +135,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
115135 HostEnvironment .kotlinStdLibCommonJar
116136 }
117137
138+ /* * Enable support for the new K2 compiler. */
139+ var supportsK2 = false
140+
118141 // Directory for input source files
119142 protected val sourcesDir get() = workingDir.resolve(" sources" )
120143
@@ -188,11 +211,14 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
188211 * To avoid that the annotation processors are executed twice,
189212 * the list is set to empty
190213 */
191- MainComponentRegistrar .threadLocalParameters.set(
192- MainComponentRegistrar .ThreadLocalParameters (
214+ @Suppress(" DEPRECATION" )
215+ MainComponentAndPluginRegistrar .threadLocalParameters.set(
216+ MainComponentAndPluginRegistrar .ThreadLocalParameters (
193217 listOf (),
194218 KaptOptions .Builder (),
195- compilerPlugins
219+ componentRegistrars,
220+ compilerPluginRegistrars,
221+ supportsK2
196222 )
197223 )
198224
@@ -207,14 +233,15 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
207233 } else {
208234 emptyList()
209235 }
236+ @Suppress(" DEPRECATION" )
210237 args.pluginClasspaths = (args.pluginClasspaths ? : emptyArray()) +
211238 /* * The resources path contains the MainComponentRegistrar and MainCommandLineProcessor which will
212239 be found by the Kotlin compiler's service loader. We add it only when the user has actually given
213240 us ComponentRegistrar instances to be loaded by the MainComponentRegistrar because the experimental
214241 K2 compiler doesn't support plugins yet. This way, users of K2 can prevent MainComponentRegistrar
215- from being loaded and crashing K2 by setting both [compilerPlugins ] and [commandLineProcessors] to
216- the emptyList. */
217- if (compilerPlugins.isNotEmpty() || commandLineProcessors.isNotEmpty())
242+ from being loaded and crashing K2 by setting both [componentRegistrars ] and [commandLineProcessors]
243+ and [compilerPluginRegistrars] to the emptyList. */
244+ if (componentRegistrars.union(compilerPluginRegistrars).union( commandLineProcessors) .isNotEmpty())
218245 arrayOf(getResourcesPath())
219246 else emptyArray()
220247 }
@@ -229,7 +256,7 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
229256 }
230257
231258 protected fun getResourcesPath (): String {
232- val resourceName = " META-INF/services/org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar "
259+ val resourceName = " META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar "
233260 return this ::class .java.classLoader.getResources(resourceName)
234261 .asSequence()
235262 .mapNotNull { url ->
@@ -241,9 +268,9 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
241268 }.toAbsolutePath()
242269 }
243270 .find { resourcesPath ->
244- ServiceLoaderLite .findImplementations(ComponentRegistrar ::class .java, listOf (resourcesPath.toFile()))
245- .any { implementation -> implementation == MainComponentRegistrar ::class .java.name }
246- }?.toString() ? : throw AssertionError (" Could not get path to ComponentRegistrar service from META-INF" )
271+ ServiceLoaderLite .findImplementations(CompilerPluginRegistrar ::class .java, listOf (resourcesPath.toFile()))
272+ .any { implementation -> implementation == MainComponentAndPluginRegistrar ::class .java.name }
273+ }?.toString() ? : throw AssertionError (" Could not get path to CompilerPluginRegistrar service from META-INF" )
247274 }
248275
249276 /* * Searches compiler log for known errors that are hard to debug for the user */
@@ -298,7 +325,6 @@ abstract class AbstractKotlinCompilation<A : CommonCompilerArguments> internal c
298325
299326internal fun convertKotlinExitCode (code : ExitCode ) = when (code) {
300327 ExitCode .OK -> KotlinCompilation .ExitCode .OK
301- ExitCode .OOM_ERROR ,
302328 ExitCode .INTERNAL_ERROR -> KotlinCompilation .ExitCode .INTERNAL_ERROR
303329 ExitCode .COMPILATION_ERROR -> KotlinCompilation .ExitCode .COMPILATION_ERROR
304330 ExitCode .SCRIPT_EXECUTION_ERROR -> KotlinCompilation .ExitCode .SCRIPT_EXECUTION_ERROR
0 commit comments