[KAPT4] Create basic infrastructure for KAPT4
This commit is contained in:
committed by
Space Team
parent
fc57f48c8f
commit
083f54aceb
@@ -114,7 +114,12 @@ private fun transformKaptToolArgs(args: List<String>, messageCollector: MessageC
|
||||
}
|
||||
|
||||
if (!aptModePassed) {
|
||||
transformed.addAll(0, kaptArg(KaptCliOption.APT_MODE_OPTION, "compile"))
|
||||
val isK2 = "-Xuse-kapt4" in transformed && ("-Xuse-k2" in transformed ||
|
||||
transformed.any { it.startsWith("-language-version=2") } ||
|
||||
transformed.lastIndexOf("-language-version").takeIf { it >= 0 }
|
||||
?.let { transformed.getOrNull(it + 1)?.startsWith('2') } == true)
|
||||
|
||||
transformed.addAll(0, kaptArg(KaptCliOption.APT_MODE_OPTION, if (isK2) "stubsAndApt" else "compile"))
|
||||
}
|
||||
|
||||
if (!isTest && !isAtLeastJava9() && !areJavacComponentsAvailable() && !toolsJarPassed) {
|
||||
|
||||
@@ -47,8 +47,7 @@ output/sources/generated/Test.java
|
||||
Test.kt
|
||||
|
||||
# output
|
||||
warning: kapt currently doesn't support language version 2.0+.
|
||||
Falling back to 1.9.
|
||||
warning: kapt currently doesn't support language version 2.0+. Falling back to 1.9.
|
||||
|
||||
# java
|
||||
-cp output/classes:output/javaClasses:output/ap.jar:%KOTLIN_STDLIB%
|
||||
|
||||
@@ -21,13 +21,13 @@ import com.intellij.openapi.project.Project
|
||||
import org.jetbrains.kotlin.analyzer.AnalysisResult
|
||||
import org.jetbrains.kotlin.base.kapt3.*
|
||||
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
|
||||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||
import org.jetbrains.kotlin.cli.common.messages.MessageRenderer
|
||||
import org.jetbrains.kotlin.cli.common.messages.PrintingMessageCollector
|
||||
import org.jetbrains.kotlin.cli.jvm.config.JavaSourceRoot
|
||||
import org.jetbrains.kotlin.cli.jvm.config.JvmClasspathRoot
|
||||
import org.jetbrains.kotlin.compiler.plugin.*
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys.USE_FIR
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.config.CompilerConfigurationKey
|
||||
import org.jetbrains.kotlin.config.JVMConfigurationKeys
|
||||
@@ -59,7 +59,7 @@ import java.io.File
|
||||
import java.io.ObjectInputStream
|
||||
import java.util.*
|
||||
|
||||
private val KAPT_OPTIONS = CompilerConfigurationKey.create<KaptOptions.Builder>("Kapt options")
|
||||
val KAPT_OPTIONS = CompilerConfigurationKey.create<KaptOptions.Builder>("Kapt options")
|
||||
|
||||
class Kapt3CommandLineProcessor : CommandLineProcessor {
|
||||
override val pluginId: String = ANNOTATION_PROCESSING_COMPILER_PLUGIN_ID
|
||||
@@ -71,12 +71,6 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
|
||||
if (option !is KaptCliOption) {
|
||||
throw CliOptionProcessingException("Unknown option: ${option.optionName}")
|
||||
}
|
||||
if (configuration.getBoolean(CommonConfigurationKeys.USE_FIR)) {
|
||||
configuration[CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY]?.report(
|
||||
CompilerMessageSeverity.ERROR,
|
||||
"kapt currently doesn't support language version 2.0\nPlease use language version 1.9 or below"
|
||||
)
|
||||
}
|
||||
|
||||
val kaptOptions = configuration[KAPT_OPTIONS]
|
||||
?: KaptOptions.Builder().also { configuration.put(KAPT_OPTIONS, it) }
|
||||
@@ -173,6 +167,7 @@ class Kapt3CommandLineProcessor : CommandLineProcessor {
|
||||
@Suppress("DEPRECATION")
|
||||
class Kapt3ComponentRegistrar : ComponentRegistrar {
|
||||
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
|
||||
if (configuration.getBoolean(USE_FIR)) return
|
||||
doOpenInternalPackagesIfRequired()
|
||||
val contentRoots = configuration[CLIConfigurationKeys.CONTENT_ROOTS] ?: emptyList()
|
||||
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
|
||||
description = "Annotation Processor for Kotlin"
|
||||
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
id("jps-compatible")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compileOnly(project(":compiler:frontend.java"))
|
||||
compileOnly(project(":compiler:plugin-api"))
|
||||
|
||||
implementation(project(":kotlin-annotation-processing-compiler"))
|
||||
embedded(project(":kotlin-annotation-processing-compiler")) { isTransitive = false }
|
||||
}
|
||||
|
||||
optInToExperimentalCompilerApi()
|
||||
|
||||
sourceSets {
|
||||
"main" { projectDefault() }
|
||||
"test" {
|
||||
projectDefault()
|
||||
generatedTestDir()
|
||||
}
|
||||
}
|
||||
|
||||
testsJar {}
|
||||
|
||||
kaptTestTask("test", JavaLanguageVersion.of(8))
|
||||
kaptTestTask("testJdk11", JavaLanguageVersion.of(11))
|
||||
kaptTestTask("testJdk17", JavaLanguageVersion.of(17))
|
||||
|
||||
fun Project.kaptTestTask(name: String, javaLanguageVersion: JavaLanguageVersion) {
|
||||
val service = extensions.getByType<JavaToolchainService>()
|
||||
|
||||
projectTest(taskName = name, parallel = true) {
|
||||
useJUnitPlatform {
|
||||
excludeTags = setOf("IgnoreJDK11")
|
||||
}
|
||||
workingDir = rootDir
|
||||
dependsOn(":dist")
|
||||
javaLauncher.set(service.launcherFor { languageVersion.set(javaLanguageVersion) })
|
||||
}
|
||||
}
|
||||
|
||||
publish()
|
||||
runtimeJar()
|
||||
sourcesJar()
|
||||
javadocJar()
|
||||
|
||||
|
||||
allprojects {
|
||||
tasks.withType(KotlinCompile::class).all {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs += "-Xcontext-receivers"
|
||||
}
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
org.jetbrains.kotlin.kapt4.Kapt4CompilerPluginRegistrar
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.kapt4
|
||||
|
||||
import org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar
|
||||
import org.jetbrains.kotlin.config.CommonConfigurationKeys
|
||||
import org.jetbrains.kotlin.config.CompilerConfiguration
|
||||
import org.jetbrains.kotlin.fir.extensions.FirAnalysisHandlerExtension
|
||||
import org.jetbrains.kotlin.kapt3.KAPT_OPTIONS
|
||||
|
||||
|
||||
class Kapt4AnalysisHandlerExtension : FirAnalysisHandlerExtension() {
|
||||
override fun isApplicable(configuration: CompilerConfiguration): Boolean =
|
||||
configuration.getBoolean(CommonConfigurationKeys.USE_FIR) && configuration[KAPT_OPTIONS] != null
|
||||
|
||||
override fun doAnalysis(configuration: CompilerConfiguration): Boolean {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
|
||||
class Kapt4CompilerPluginRegistrar : CompilerPluginRegistrar() {
|
||||
override fun ExtensionStorage.registerExtensions(configuration: CompilerConfiguration) {
|
||||
Companion.registerExtensions(this)
|
||||
}
|
||||
|
||||
override val supportsK2: Boolean
|
||||
get() = true
|
||||
|
||||
|
||||
companion object {
|
||||
fun registerExtensions(extensionStorage: ExtensionStorage) = with(extensionStorage) {
|
||||
FirAnalysisHandlerExtension.registerExtension(Kapt4AnalysisHandlerExtension())
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user