[Gradle] Deprecate CompilerArgumentAware in favor of KotlinCompilerArgumentsProducer

KTIJ-24976
This commit is contained in:
Sebastian Sellmair
2023-03-21 09:47:39 +01:00
committed by Space Team
parent f5fadc1f68
commit 36ff6531ea
6 changed files with 113 additions and 2 deletions
@@ -14,30 +14,44 @@
* limitations under the License.
*/
@file:Suppress("DeprecatedCallableAddReplaceWith")
package org.jetbrains.kotlin.gradle.internal
import org.gradle.api.tasks.Internal
import org.jetbrains.kotlin.cli.common.arguments.CommonToolArguments
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
@Deprecated("Replaced by KotlinCompilerArgumentsProducer")
interface CompilerArgumentAware<T : CommonToolArguments> {
@Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION")
@get:Internal
val serializedCompilerArguments: List<String>
get() = ArgumentUtils.convertArgumentsToStringList(prepareCompilerArguments())
@Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION")
@get:Internal
val serializedCompilerArgumentsIgnoreClasspathIssues: List<String>
get() = ArgumentUtils.convertArgumentsToStringList(prepareCompilerArguments(ignoreClasspathResolutionErrors = true))
@Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.ERROR)
@Suppress("DEPRECATION")
@get:Internal
val defaultSerializedCompilerArguments: List<String>
get() = createCompilerArgs()
.also { setupCompilerArgs(it, defaultsOnly = true) }
.let(ArgumentUtils::convertArgumentsToStringList)
@Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.WARNING)
fun createCompilerArgs(): T
@Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.WARNING)
fun setupCompilerArgs(args: T, defaultsOnly: Boolean = false, ignoreClasspathResolutionErrors: Boolean = false)
}
@Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.WARNING)
@Suppress("DEPRECATION")
internal fun <T : CommonToolArguments> CompilerArgumentAware<T>.prepareCompilerArguments(ignoreClasspathResolutionErrors: Boolean = false) =
createCompilerArgs().also { setupCompilerArgs(it, ignoreClasspathResolutionErrors = ignoreClasspathResolutionErrors) }
@@ -122,6 +122,7 @@ class KotlinModelBuilder(private val kotlinPluginVersion: String, private val an
)
}
@Suppress("DEPRECATION_ERROR")
private fun AbstractKotlinCompile<*>.createCompilerArguments(): CompilerArguments {
return CompilerArgumentsImpl(
serializedCompilerArguments,
@@ -0,0 +1,89 @@
/*
* 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.gradle.plugin
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.ContributeCompilerArgumentsContext
import kotlin.reflect.KClass
import kotlin.reflect.cast
@InternalKotlinGradlePluginApi
interface KotlinCompilerArgumentsProducer {
enum class ArgumentType {
Primitive,
DependencyClasspath,
PluginClasspath,
Sources;
companion object {
val all = values().toSet()
}
}
interface CreateCompilerArgumentsContext {
fun <T : CommonCompilerArguments> create(type: KClass<T>, action: ContributeCompilerArgumentsContext<T>.() -> Unit): T
companion object {
val default: CreateCompilerArgumentsContext = CreateCompilerArgumentsContext()
val lenient: CreateCompilerArgumentsContext = CreateCompilerArgumentsContext(isLenient = true)
inline fun <reified T : CommonCompilerArguments> CreateCompilerArgumentsContext.create(
noinline action: ContributeCompilerArgumentsContext<T>.() -> Unit
) = create(T::class, action)
}
}
interface ContributeCompilerArgumentsContext<T : CommonCompilerArguments> {
fun <T> tryLenient(action: () -> T): T?
fun contribute(type: ArgumentType, contribution: (T) -> Unit)
}
fun createCompilerArguments(
context: CreateCompilerArgumentsContext = CreateCompilerArgumentsContext()
): CommonCompilerArguments
}
internal fun CreateCompilerArgumentsContext(
includeArgumentTypes: Set<KotlinCompilerArgumentsProducer.ArgumentType> = KotlinCompilerArgumentsProducer.ArgumentType.all,
isLenient: Boolean = false
): KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext {
return CreateCompilerArgumentsContextImpl(includeArgumentTypes, isLenient)
}
private class CreateCompilerArgumentsContextImpl(
private val includeArgumentTypes: Set<KotlinCompilerArgumentsProducer.ArgumentType>,
private val isLenient: Boolean
) : KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext {
override fun <T : CommonCompilerArguments> create(
type: KClass<T>, action: ContributeCompilerArgumentsContext<T>.() -> Unit
): T {
val constructor = type.java.constructors.firstOrNull { it.parameters.isEmpty() }
?: throw IllegalArgumentException("'${type.qualifiedName}' does not have an empty constructor")
val arguments = type.cast(constructor.newInstance())
ContributeCompilerArgumentsContextImpl(arguments).also(action)
return arguments
}
private inner class ContributeCompilerArgumentsContextImpl<T : CommonCompilerArguments>(
private val arguments: T
) : ContributeCompilerArgumentsContext<T> {
override fun <T> tryLenient(action: () -> T): T? {
return try {
action()
} catch (t: Throwable) {
if (isLenient) null else throw t
}
}
override fun contribute(type: KotlinCompilerArgumentsProducer.ArgumentType, contribution: (T) -> Unit) {
if (type in includeArgumentTypes) contribution(arguments)
}
}
}
@@ -38,6 +38,8 @@ import org.jetbrains.kotlin.gradle.internal.prepareCompilerArguments
import org.jetbrains.kotlin.gradle.internal.tasks.allOutputFiles
import org.jetbrains.kotlin.gradle.logging.GradleKotlinLogger
import org.jetbrains.kotlin.gradle.logging.kotlinDebug
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext.Companion.default
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.PropertyNames.KOTLIN_SUPPRESS_EXPERIMENTAL_IC_OPTIMIZATIONS_WARNING
import org.jetbrains.kotlin.gradle.plugin.UsesBuildFinishedListenerService
import org.jetbrains.kotlin.gradle.plugin.UsesVariantImplementationFactories
@@ -318,7 +320,11 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments> @Inject constr
return
}
val args = prepareCompilerArguments()
val args = @Suppress("unchecked_cast", "deprecation")
if (this is KotlinCompilerArgumentsProducer) createCompilerArguments(default) as T
else prepareCompilerArguments()
taskBuildCacheableOutputDirectory.get().asFile.mkdirs()
taskBuildLocalStateDirectory.get().asFile.mkdirs()
callCompilerAsync(
@@ -124,6 +124,7 @@ abstract class KotlinJsDce @Inject constructor(
val outputDirArgs = arrayOf("-output-dir", destinationDirectory.get().asFile.path)
@Suppress("DEPRECATION_ERROR")
val processedSerializedArgs = if (shouldPerformIncrementalCopy) {
var shouldAddStrategyAllArgument = true
val processedArgs = serializedCompilerArguments
@@ -3,7 +3,7 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:Suppress("FunctionName")
@file:Suppress("FunctionName", "DEPRECATION")
package org.jetbrains.kotlin.gradle.unitTests