[CLI] Introduce new compiler arguments for registering compiler plugins
With new syntax each plugin should be registered in separate argument with syntax `-Xcompiler-plugin=classpath1,classpath2[=argument1=value1,argument2=value2]`
This commit is contained in:
committed by
teamcity
parent
8919703448
commit
928416c9c5
+7
-3
@@ -20,6 +20,7 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
|
||||
private val serialVersionUID = 0L
|
||||
|
||||
const val PLUGIN_OPTION_FORMAT = "plugin:<pluginId>:<optionName>=<value>"
|
||||
const val PLUGIN_DECLARATION_FORMAT = "<path>[=<optionName>=<value>]"
|
||||
|
||||
const val WARN = "warn"
|
||||
const val ERROR = "error"
|
||||
@@ -70,9 +71,6 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
|
||||
@Argument(value = "-script", description = "Evaluate the given Kotlin script (*.kts) file")
|
||||
var script: Boolean by FreezableVar(false)
|
||||
|
||||
@Argument(value = "-P", valueDescription = PLUGIN_OPTION_FORMAT, description = "Pass an option to a plugin")
|
||||
var pluginOptions: Array<String>? by FreezableVar(null)
|
||||
|
||||
@Argument(
|
||||
value = "-opt-in",
|
||||
deprecatedName = "-Xopt-in",
|
||||
@@ -107,6 +105,12 @@ abstract class CommonCompilerArguments : CommonToolArguments() {
|
||||
@Argument(value = "-Xplugin", valueDescription = "<path>", description = "Load plugins from the given classpath")
|
||||
var pluginClasspaths: Array<String>? by FreezableVar(null)
|
||||
|
||||
@Argument(value = "-P", valueDescription = PLUGIN_OPTION_FORMAT, description = "Pass an option to a plugin")
|
||||
var pluginOptions: Array<String>? by FreezableVar(null)
|
||||
|
||||
@Argument(value = "-Xcompiler-plugin", valueDescription = "<path1>,<path2>:<optionName>=<value>,<optionName>=<value>", description = "Register compiler plugin", delimiter = "")
|
||||
var pluginConfigurations: Array<String>? by FreezableVar(null)
|
||||
|
||||
@Argument(value = "-Xmulti-platform", description = "Enable experimental language support for multi-platform projects")
|
||||
var multiPlatform: Boolean by FreezableVar(false)
|
||||
|
||||
|
||||
+60
-27
@@ -10,46 +10,79 @@ import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
import org.jetbrains.kotlin.compiler.plugin.*
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
|
||||
data class PluginClasspathAndOptions(
|
||||
val rawArgument: String,
|
||||
val classpath: List<String>,
|
||||
val options: List<CliOptionValue>
|
||||
)
|
||||
|
||||
private const val regularDelimiter = ","
|
||||
private const val classpathOptionsDelimiter = "="
|
||||
|
||||
fun extractPluginClasspathAndOptions(pluginConfigurations: Iterable<String>): List<PluginClasspathAndOptions> {
|
||||
return pluginConfigurations.map { extractPluginClasspathAndOptions(it)}
|
||||
}
|
||||
|
||||
fun extractPluginClasspathAndOptions(pluginConfiguration: String): PluginClasspathAndOptions {
|
||||
val rawClasspath = pluginConfiguration.substringBefore(classpathOptionsDelimiter)
|
||||
val rawOptions = pluginConfiguration.substringAfter(classpathOptionsDelimiter, missingDelimiterValue = "")
|
||||
val classPath = rawClasspath.split(regularDelimiter)
|
||||
val options = rawOptions.takeIf { it.isNotBlank() }
|
||||
?.split(regularDelimiter)
|
||||
?.mapNotNull { parseModernPluginOption(it) }
|
||||
?: emptyList()
|
||||
return PluginClasspathAndOptions(pluginConfiguration, classPath, options)
|
||||
}
|
||||
|
||||
fun processCompilerPluginsOptions(
|
||||
configuration: CompilerConfiguration,
|
||||
pluginOptions: Iterable<String>?,
|
||||
commandLineProcessors: List<CommandLineProcessor>
|
||||
) {
|
||||
val optionValuesByPlugin = pluginOptions?.map(::parsePluginOption)?.groupBy {
|
||||
val optionValuesByPlugin = pluginOptions?.map(::parseLegacyPluginOption)?.groupBy {
|
||||
if (it == null) throw CliOptionProcessingException("Wrong plugin option format: $it, should be ${CommonCompilerArguments.PLUGIN_OPTION_FORMAT}")
|
||||
it.pluginId
|
||||
} ?: mapOf()
|
||||
|
||||
for (processor in commandLineProcessors) {
|
||||
val declaredOptions = processor.pluginOptions.associateBy { it.optionName }
|
||||
val optionsToValues = MultiMap<AbstractCliOption, CliOptionValue>()
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
processCompilerPluginOptions(processor, optionValuesByPlugin[processor.pluginId].orEmpty() as List<CliOptionValue>, configuration)
|
||||
}
|
||||
}
|
||||
|
||||
for (optionValue in optionValuesByPlugin[processor.pluginId].orEmpty()) {
|
||||
val option = declaredOptions[optionValue!!.optionName]
|
||||
?: throw CliOptionProcessingException("Unsupported plugin option: $optionValue")
|
||||
optionsToValues.putValue(option, optionValue)
|
||||
fun processCompilerPluginOptions(
|
||||
processor: CommandLineProcessor,
|
||||
pluginOptions: List<CliOptionValue>,
|
||||
configuration: CompilerConfiguration
|
||||
) {
|
||||
val declaredOptions = processor.pluginOptions.associateBy { it.optionName }
|
||||
val optionsToValues = MultiMap<AbstractCliOption, CliOptionValue>()
|
||||
|
||||
for (optionValue in pluginOptions) {
|
||||
val option = declaredOptions[optionValue.optionName]
|
||||
?: throw CliOptionProcessingException("Unsupported plugin option: $optionValue")
|
||||
optionsToValues.putValue(option, optionValue)
|
||||
}
|
||||
|
||||
for (option in processor.pluginOptions) {
|
||||
val values = optionsToValues[option]
|
||||
if (option.required && values.isEmpty()) {
|
||||
throw PluginCliOptionProcessingException(
|
||||
processor.pluginId,
|
||||
processor.pluginOptions,
|
||||
"Required plugin option not present: ${processor.pluginId}:${option.optionName}"
|
||||
)
|
||||
}
|
||||
if (!option.allowMultipleOccurrences && values.size > 1) {
|
||||
throw PluginCliOptionProcessingException(
|
||||
processor.pluginId,
|
||||
processor.pluginOptions,
|
||||
"Multiple values are not allowed for plugin option ${processor.pluginId}:${option.optionName}"
|
||||
)
|
||||
}
|
||||
|
||||
for (option in processor.pluginOptions) {
|
||||
val values = optionsToValues[option]
|
||||
if (option.required && values.isEmpty()) {
|
||||
throw PluginCliOptionProcessingException(
|
||||
processor.pluginId,
|
||||
processor.pluginOptions,
|
||||
"Required plugin option not present: ${processor.pluginId}:${option.optionName}"
|
||||
)
|
||||
}
|
||||
if (!option.allowMultipleOccurrences && values.size > 1) {
|
||||
throw PluginCliOptionProcessingException(
|
||||
processor.pluginId,
|
||||
processor.pluginOptions,
|
||||
"Multiple values are not allowed for plugin option ${processor.pluginId}:${option.optionName}"
|
||||
)
|
||||
}
|
||||
|
||||
for (value in values) {
|
||||
processor.processOption(option, value.value, configuration)
|
||||
}
|
||||
for (value in values) {
|
||||
processor.processOption(option, value.value, configuration)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -634,5 +634,5 @@ fun loadPluginsForTests(configuration: CompilerConfiguration): ExitCode {
|
||||
PathUtil.KOTLIN_SCRIPTING_PLUGIN_CLASSPATH_JARS.mapNotNull { File(libPath, it) }.partition { it.exists() }
|
||||
pluginClasspaths = jars.map { it.canonicalPath } + pluginClasspaths
|
||||
|
||||
return PluginCliParser.loadPluginsSafe(pluginClasspaths, mutableListOf(), configuration)
|
||||
return PluginCliParser.loadPluginsSafe(pluginClasspaths, listOf(), listOf(), configuration)
|
||||
}
|
||||
|
||||
@@ -170,6 +170,7 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||
protected fun loadPlugins(paths: KotlinPaths?, arguments: A, configuration: CompilerConfiguration): ExitCode {
|
||||
val pluginClasspaths = arguments.pluginClasspaths.orEmpty().toMutableList()
|
||||
val pluginOptions = arguments.pluginOptions.orEmpty().toMutableList()
|
||||
val pluginConfigurations = arguments.pluginConfigurations.orEmpty().toMutableList()
|
||||
val messageCollector = configuration.getNotNull(MESSAGE_COLLECTOR_KEY)
|
||||
|
||||
for (classpath in pluginClasspaths) {
|
||||
@@ -178,6 +179,25 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||
}
|
||||
}
|
||||
|
||||
if (pluginConfigurations.isNotEmpty() && (pluginClasspaths.isNotEmpty() || pluginOptions.isNotEmpty())) {
|
||||
val message = buildString {
|
||||
appendLine("Mixing legacy and modern plugin arguments is prohibited. Please use only one syntax")
|
||||
appendLine("Legacy arguments:")
|
||||
if (pluginClasspaths.isNotEmpty()) {
|
||||
appendLine(" -Xplugin=${pluginClasspaths.joinToString(",")}")
|
||||
}
|
||||
pluginOptions.forEach {
|
||||
appendLine(" -P $it")
|
||||
}
|
||||
appendLine("Modern arguments:")
|
||||
pluginConfigurations.forEach {
|
||||
appendLine(" -Xcompiler-plugin=$it")
|
||||
}
|
||||
}
|
||||
messageCollector.report(ERROR, message)
|
||||
return INTERNAL_ERROR
|
||||
}
|
||||
|
||||
if (!arguments.disableDefaultScriptingPlugin) {
|
||||
pluginOptions.addPlatformOptions(arguments)
|
||||
val explicitOrLoadedScriptingPlugin =
|
||||
@@ -192,7 +212,7 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||
pluginClasspaths.addAll(0, jars.map { it.canonicalPath })
|
||||
} else {
|
||||
messageCollector.report(
|
||||
CompilerMessageSeverity.LOGGING,
|
||||
LOGGING,
|
||||
"Scripting plugin will not be loaded: not all required jars are present in the classpath (missing files: $missingJars)"
|
||||
)
|
||||
}
|
||||
@@ -200,7 +220,7 @@ abstract class CLICompiler<A : CommonCompilerArguments> : CLITool<A>() {
|
||||
} else {
|
||||
pluginOptions.add("plugin:kotlin.scripting:disable=true")
|
||||
}
|
||||
return PluginCliParser.loadPluginsSafe(pluginClasspaths, pluginOptions, configuration)
|
||||
return PluginCliParser.loadPluginsSafe(pluginClasspaths, pluginOptions, pluginConfigurations, configuration)
|
||||
}
|
||||
|
||||
private fun tryLoadScriptingPluginFromCurrentClassLoader(configuration: CompilerConfiguration, pluginOptions: List<String>): Boolean =
|
||||
|
||||
@@ -18,8 +18,11 @@ package org.jetbrains.kotlin.cli.jvm.plugins
|
||||
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.ExitCode
|
||||
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageCollectorUtil
|
||||
import org.jetbrains.kotlin.cli.plugins.extractPluginClasspathAndOptions
|
||||
import org.jetbrains.kotlin.cli.plugins.processCompilerPluginOptions
|
||||
import org.jetbrains.kotlin.cli.plugins.processCompilerPluginsOptions
|
||||
import org.jetbrains.kotlin.compiler.plugin.*
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
@@ -29,44 +32,113 @@ import java.net.URLClassLoader
|
||||
|
||||
object PluginCliParser {
|
||||
@JvmStatic
|
||||
fun loadPluginsSafe(pluginClasspaths: Array<String>?, pluginOptions: Array<String>?, configuration: CompilerConfiguration): ExitCode =
|
||||
loadPluginsSafe(pluginClasspaths?.asIterable(), pluginOptions?.asIterable(), configuration)
|
||||
fun loadPluginsSafe(
|
||||
pluginClasspaths: Array<String>?,
|
||||
pluginOptions: Array<String>?,
|
||||
pluginConfigurations: Array<String>?,
|
||||
configuration: CompilerConfiguration
|
||||
): ExitCode {
|
||||
return loadPluginsSafe(
|
||||
pluginClasspaths?.toList() ?: emptyList(),
|
||||
pluginOptions?.toList() ?: emptyList(),
|
||||
pluginConfigurations?.toList() ?: emptyList(),
|
||||
configuration
|
||||
)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun loadPluginsSafe(
|
||||
pluginClasspaths: Iterable<String>?,
|
||||
pluginOptions: Iterable<String>?,
|
||||
pluginClasspaths: Collection<String>,
|
||||
pluginOptions: Collection<String>,
|
||||
pluginConfigurations: Collection<String>,
|
||||
configuration: CompilerConfiguration
|
||||
): ExitCode {
|
||||
val messageCollector = configuration.getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
|
||||
|
||||
try {
|
||||
PluginCliParser.loadPlugins(pluginClasspaths, pluginOptions, configuration)
|
||||
loadPluginsLegacyStyle(pluginClasspaths, pluginOptions, configuration)
|
||||
loadPluginsModernStyle(pluginConfigurations, configuration)
|
||||
return ExitCode.OK
|
||||
} catch (e: PluginProcessingException) {
|
||||
messageCollector.report(CompilerMessageSeverity.ERROR, e.message!!)
|
||||
} catch (e: PluginCliOptionProcessingException) {
|
||||
val message = e.message + "\n\n" + cliPluginUsageString(e.pluginId, e.options)
|
||||
messageCollector.report(CompilerMessageSeverity.ERROR, message)
|
||||
return ExitCode.INTERNAL_ERROR
|
||||
} catch (e: CliOptionProcessingException) {
|
||||
messageCollector.report(CompilerMessageSeverity.ERROR, e.message!!)
|
||||
return ExitCode.INTERNAL_ERROR
|
||||
} catch (t: Throwable) {
|
||||
MessageCollectorUtil.reportException(messageCollector, t)
|
||||
return ExitCode.INTERNAL_ERROR
|
||||
}
|
||||
return ExitCode.OK
|
||||
return ExitCode.INTERNAL_ERROR
|
||||
}
|
||||
|
||||
class RegisteredPluginInfo(
|
||||
@Suppress("DEPRECATION") val componentRegistrar: ComponentRegistrar?,
|
||||
val compilerPluginRegistrar: CompilerPluginRegistrar?,
|
||||
val commandLineProcessor: CommandLineProcessor?,
|
||||
val pluginOptions: List<CliOptionValue>
|
||||
)
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun loadRegisteredPluginsInfo(rawPluginConfigurations: Iterable<String>): List<RegisteredPluginInfo> {
|
||||
val pluginConfigurations = extractPluginClasspathAndOptions(rawPluginConfigurations)
|
||||
val pluginInfos = pluginConfigurations.map { pluginConfiguration ->
|
||||
val classLoader = createClassLoader(pluginConfiguration.classpath)
|
||||
val componentRegistrars = ServiceLoaderLite.loadImplementations(ComponentRegistrar::class.java, classLoader)
|
||||
val compilerPluginRegistrars = ServiceLoaderLite.loadImplementations(CompilerPluginRegistrar::class.java, classLoader)
|
||||
|
||||
fun multiplePluginsErrorMessage(pluginObjects: List<Any>): String {
|
||||
return buildString {
|
||||
append("Multiple plugins found in given classpath: ")
|
||||
val extensionNames = pluginObjects.mapNotNull { it::class.qualifiedName }
|
||||
appendLine(extensionNames.joinToString(", "))
|
||||
append(" Plugin configuration is: ${pluginConfiguration.rawArgument}")
|
||||
}
|
||||
}
|
||||
|
||||
when (componentRegistrars.size + compilerPluginRegistrars.size) {
|
||||
0 -> throw PluginProcessingException("No plugins found in given classpath: ${pluginConfiguration.classpath.joinToString(",")}")
|
||||
1 -> {}
|
||||
else -> throw PluginProcessingException(multiplePluginsErrorMessage(componentRegistrars + compilerPluginRegistrars))
|
||||
}
|
||||
|
||||
val commandLineProcessor = ServiceLoaderLite.loadImplementations(CommandLineProcessor::class.java, classLoader)
|
||||
if (commandLineProcessor.size > 1) {
|
||||
throw PluginProcessingException(multiplePluginsErrorMessage(commandLineProcessor))
|
||||
}
|
||||
RegisteredPluginInfo(
|
||||
componentRegistrars.firstOrNull(),
|
||||
compilerPluginRegistrars.firstOrNull(),
|
||||
commandLineProcessor.firstOrNull(),
|
||||
pluginConfiguration.options
|
||||
)
|
||||
}
|
||||
return pluginInfos
|
||||
}
|
||||
|
||||
private fun loadPluginsModernStyle(rawPluginConfigurations: Iterable<String>?, configuration: CompilerConfiguration) {
|
||||
if (rawPluginConfigurations == null) return
|
||||
val pluginInfos = loadRegisteredPluginsInfo(rawPluginConfigurations)
|
||||
for (pluginInfo in pluginInfos) {
|
||||
pluginInfo.componentRegistrar?.let {
|
||||
@Suppress("DEPRECATION")
|
||||
configuration.add(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, it)
|
||||
}
|
||||
pluginInfo.compilerPluginRegistrar?.let { configuration.add(CompilerPluginRegistrar.COMPILER_PLUGIN_REGISTRARS, it) }
|
||||
|
||||
if (pluginInfo.pluginOptions.isEmpty()) continue
|
||||
val commandLineProcessor = pluginInfo.commandLineProcessor ?: throw RuntimeException() // TODO: proper exception
|
||||
processCompilerPluginOptions(commandLineProcessor, pluginInfo.pluginOptions, configuration)
|
||||
}
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
@Suppress("DEPRECATION")
|
||||
fun loadPlugins(pluginClasspaths: Iterable<String>?, pluginOptions: Iterable<String>?, configuration: CompilerConfiguration) {
|
||||
val classLoader = URLClassLoader(
|
||||
pluginClasspaths
|
||||
?.map { File(it).toURI().toURL() }
|
||||
?.toTypedArray()
|
||||
?: emptyArray(),
|
||||
this::class.java.classLoader
|
||||
)
|
||||
|
||||
private fun loadPluginsLegacyStyle(
|
||||
pluginClasspaths: Iterable<String>?,
|
||||
pluginOptions: Iterable<String>?,
|
||||
configuration: CompilerConfiguration
|
||||
) {
|
||||
val classLoader = createClassLoader(pluginClasspaths ?: emptyList())
|
||||
val componentRegistrars = ServiceLoaderLite.loadImplementations(ComponentRegistrar::class.java, classLoader)
|
||||
configuration.addAll(ComponentRegistrar.PLUGIN_COMPONENT_REGISTRARS, componentRegistrars)
|
||||
|
||||
@@ -86,4 +158,8 @@ object PluginCliParser {
|
||||
|
||||
processCompilerPluginsOptions(configuration, pluginOptions, commandLineProcessors)
|
||||
}
|
||||
|
||||
private fun createClassLoader(classpath: Iterable<String>): URLClassLoader {
|
||||
return URLClassLoader(classpath.map { File(it).toURI().toURL() }.toTypedArray(), this::class.java.classLoader)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user