[KAPT4] Create basic infrastructure for KAPT4

This commit is contained in:
Pavel Mikhailovskii
2023-07-07 11:09:16 +00:00
committed by Space Team
parent fc57f48c8f
commit 083f54aceb
19 changed files with 220 additions and 26 deletions
@@ -76,6 +76,7 @@ import org.jetbrains.kotlin.extensions.*
import org.jetbrains.kotlin.extensions.internal.CandidateInterceptor
import org.jetbrains.kotlin.extensions.internal.InternalNonStableExtensionPoints
import org.jetbrains.kotlin.extensions.internal.TypeResolutionInterceptor
import org.jetbrains.kotlin.fir.extensions.FirAnalysisHandlerExtension
import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrarAdapter
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.js.translate.extensions.JsSyntheticTranslateExtension
@@ -673,6 +674,7 @@ class KotlinCoreEnvironment private constructor(
FirExtensionRegistrarAdapter.registerExtensionPoint(project)
TypeAttributeTranslatorExtension.registerExtensionPoint(project)
AssignResolutionAltererExtension.registerExtensionPoint(project)
FirAnalysisHandlerExtension.registerExtensionPoint(project)
}
internal fun registerExtensionsFromPlugins(project: MockProject, configuration: CompilerConfiguration) {
@@ -78,6 +78,7 @@ fun copyK2JVMCompilerArguments(from: K2JVMCompilerArguments, to: K2JVMCompilerAr
to.typeEnhancementImprovementsInStrictMode = from.typeEnhancementImprovementsInStrictMode
to.useFastJarFileSystem = from.useFastJarFileSystem
to.useJavac = from.useJavac
to.useKapt4 = from.useKapt4
to.useOldBackend = from.useOldBackend
to.useOldClassFilesReading = from.useOldClassFilesReading
to.useOldInlineClassesManglingScheme = from.useOldInlineClassesManglingScheme
@@ -846,6 +846,15 @@ Also sets `-jvm-target` value equal to the selected JDK version"""
field = value
}
@Argument(
value = "-Xuse-kapt4",
description = "Enable the experimental KAPT 4."
)
var useKapt4 = false
set(value) {
checkFrozen()
field = value
}
override fun configureAnalysisFlags(collector: MessageCollector, languageVersion: LanguageVersion): MutableMap<AnalysisFlag<*>, Any> {
val result = super.configureAnalysisFlags(collector, languageVersion)
@@ -56,17 +56,18 @@ fun CompilerConfiguration.setupCommonArguments(
buildHmppModuleStructure(arguments)?.let { put(CommonConfigurationKeys.HMPP_MODULE_STRUCTURE, it) }
}
fun switchToFallbackModeIfNecessary(arguments: CommonCompilerArguments, messageCollector: MessageCollector) {
val isK2 = arguments.useK2 || (arguments.languageVersion?.startsWith('2') ?: (LanguageVersion.LATEST_STABLE >= LanguageVersion.KOTLIN_2_0))
if (isK2) {
val isKaptUsed = arguments.pluginOptions?.any { it.startsWith("plugin:org.jetbrains.kotlin.kapt3") } == true
if (isKaptUsed) {
if (!arguments.suppressVersionWarnings) {
messageCollector.report(
CompilerMessageSeverity.STRONG_WARNING,
"Kapt currently doesn't support language version 2.0+.\nFalling back to 1.9."
)
}
private fun switchToFallbackModeIfNecessary(arguments: CommonCompilerArguments, messageCollector: MessageCollector) {
fun warn(message: String) {
if (!arguments.suppressVersionWarnings) messageCollector.report(CompilerMessageSeverity.STRONG_WARNING, message)
}
if (arguments !is K2JVMCompilerArguments) return
val isK2 =
arguments.useK2 || (arguments.languageVersion?.startsWith('2') ?: (LanguageVersion.LATEST_STABLE >= LanguageVersion.KOTLIN_2_0))
val isKaptUsed = arguments.pluginOptions?.any { it.startsWith("plugin:org.jetbrains.kotlin.kapt3") } == true
when {
isK2 && isKaptUsed && !arguments.useKapt4 -> {
warn("Kapt currently doesn't support language version 2.0+. Falling back to 1.9.")
arguments.languageVersion = LanguageVersion.KOTLIN_1_9.versionString
if (arguments.apiVersion?.startsWith("2") == true) {
arguments.apiVersion = ApiVersion.KOTLIN_1_9.versionString
@@ -74,8 +75,11 @@ fun switchToFallbackModeIfNecessary(arguments: CommonCompilerArguments, messageC
arguments.useK2 = false
arguments.skipMetadataVersionCheck = true
arguments.skipPrereleaseCheck = true
(arguments as? K2JVMCompilerArguments)?.allowUnstableDependencies = true
arguments.allowUnstableDependencies = true
}
isK2 && isKaptUsed && arguments.useKapt4 -> warn("Kapt 4 is still experimental. Use with caution.")
arguments.useKapt4 && !isK2 -> warn("-Xuse-kapt4 flag can be only used with language version 2.0+.")
arguments.useKapt4 && !isKaptUsed -> warn("-Xuse-kapt4 flag is present but no Kapt configuration options are provided.")
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2023 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.fir.extensions
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.extensions.ProjectExtensionDescriptor
abstract class FirAnalysisHandlerExtension {
companion object : ProjectExtensionDescriptor<FirAnalysisHandlerExtension>(
"org.jetbrains.kotlin.fir.firAnalyzeCompleteHandlerExtension",
FirAnalysisHandlerExtension::class.java
)
/**
* Checks whether [doAnalysis] should be called
* @param configuration compiler configuration
* @return true if [doAnalysis] should be called
*/
abstract fun isApplicable(configuration: CompilerConfiguration): Boolean
/**
* Performs code analysis
* @param configuration compiler configuration
* @return true if analysis completed successfully. There can be different causes of failure, an incorrect configuration for example.
* A failure means that there's no reason to continue building the project.
*/
abstract fun doAnalysis(configuration: CompilerConfiguration): Boolean
}
+1
View File
@@ -143,6 +143,7 @@ where advanced options include:
See KT-45671 for more details
-Xuse-fast-jar-file-system Use fast implementation on Jar FS. This may speed up compilation time, but currently it's an experimental mode
-Xuse-javac Use javac for Java source and class files analysis
-Xuse-kapt4 Enable the experimental KAPT 4.
-Xuse-old-backend Use the old JVM backend
-Xuse-old-class-files-reading Use old class files reading implementation. This may slow down the build and cause problems with Groovy interop.
Should be used in case of problems with the new implementation