org.jetbrains.kotlin.cli.common.arguments.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.cli.common
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
import org.jetbrains.kotlin.cli.common.arguments.ManualLanguageFeatureSetting
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.metadata.deserialization.BinaryVersion
import org.jetbrains.kotlin.utils.KotlinPaths
import org.jetbrains.kotlin.utils.KotlinPathsFromHomeDir
import org.jetbrains.kotlin.utils.PathUtil
import java.io.File
fun CompilerConfiguration.setupCommonArguments(
arguments: A,
createMetadataVersion: ((IntArray) -> BinaryVersion)? = null
) {
put(CommonConfigurationKeys.DISABLE_INLINE, arguments.noInline)
put(CommonConfigurationKeys.USE_FIR, arguments.useFir)
putIfNotNull(CLIConfigurationKeys.INTELLIJ_PLUGIN_ROOT, arguments.intellijPluginRoot)
put(CommonConfigurationKeys.REPORT_OUTPUT_FILES, arguments.reportOutputFiles)
val metadataVersionString = arguments.metadataVersion
if (metadataVersionString != null) {
val versionArray = BinaryVersion.parseVersionArray(metadataVersionString)
val messageCollector = getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY)
when {
versionArray == null -> messageCollector.report(
CompilerMessageSeverity.ERROR, "Invalid metadata version: $metadataVersionString", null
)
createMetadataVersion == null -> throw IllegalStateException("Unable to create metadata version: missing argument")
else -> put(CommonConfigurationKeys.METADATA_VERSION, createMetadataVersion(versionArray))
}
}
setupLanguageVersionSettings(arguments)
}
fun CompilerConfiguration.setupLanguageVersionSettings(arguments: A) {
languageVersionSettings = arguments.toLanguageVersionSettings(getNotNull(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY))
}
const val KOTLIN_HOME_PROPERTY = "kotlin.home"
fun computeKotlinPaths(messageCollector: MessageCollector, arguments: CommonCompilerArguments): KotlinPaths? {
val kotlinHomeProperty = System.getProperty(KOTLIN_HOME_PROPERTY)
val kotlinHome = when {
arguments.kotlinHome != null -> File(arguments.kotlinHome!!)
kotlinHomeProperty != null -> File(kotlinHomeProperty)
else -> null
}
return when {
kotlinHome == null -> PathUtil.kotlinPathsForCompiler
kotlinHome.isDirectory -> KotlinPathsFromHomeDir(kotlinHome)
else -> {
messageCollector.report(CompilerMessageSeverity.ERROR, "Kotlin home does not exist or is not a directory: $kotlinHome", null)
null
}
}?.also {
messageCollector.report(CompilerMessageSeverity.LOGGING, "Using Kotlin home directory " + it.homePath, null)
}
}
fun MessageCollector.reportArgumentParseProblems(arguments: A) {
val errors = arguments.errors ?: return
for (flag in errors.unknownExtraFlags) {
report(CompilerMessageSeverity.STRONG_WARNING, "Flag is not supported by this version of the compiler: $flag")
}
for (argument in errors.extraArgumentsPassedInObsoleteForm) {
report(
CompilerMessageSeverity.STRONG_WARNING,
"Advanced option value is passed in an obsolete form. Please use the '=' character to specify the value: $argument=..."
)
}
for ((key, value) in errors.duplicateArguments) {
report(CompilerMessageSeverity.STRONG_WARNING, "Argument $key is passed multiple times. Only the last value will be used: $value")
}
for ((deprecatedName, newName) in errors.deprecatedArguments) {
report(CompilerMessageSeverity.STRONG_WARNING, "Argument $deprecatedName is deprecated. Please use $newName instead")
}
for (argfileError in errors.argfileErrors) {
report(CompilerMessageSeverity.STRONG_WARNING, argfileError)
}
reportUnsafeInternalArgumentsIfAny(arguments)
for (internalArgumentsError in errors.internalArgumentsParsingProblems) {
report(CompilerMessageSeverity.STRONG_WARNING, internalArgumentsError)
}
}
private fun MessageCollector.reportUnsafeInternalArgumentsIfAny(arguments: A) {
val unsafeArguments = arguments.internalArguments.filterNot {
// -XXLanguage which turns on BUG_FIX considered safe
it is ManualLanguageFeatureSetting && it.languageFeature.kind == LanguageFeature.Kind.BUG_FIX && it.state == LanguageFeature.State.ENABLED
}
if (unsafeArguments.isNotEmpty()) {
val unsafeArgumentsString = unsafeArguments.joinToString(prefix = "\n", postfix = "\n\n", separator = "\n") {
it.stringRepresentation
}
report(
CompilerMessageSeverity.STRONG_WARNING,
"ATTENTION!\n" +
"This build uses unsafe internal compiler arguments:\n" +
unsafeArgumentsString +
"This mode is not recommended for production use,\n" +
"as no stability/compatibility guarantees are given on\n" +
"compiler or generated code. Use it at your own risk!\n"
)
}
}