diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/CompilerArgumentAware.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/CompilerArgumentAware.kt index edacd207c26..d25c2c7edee 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/CompilerArgumentAware.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/internal/CompilerArgumentAware.kt @@ -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 { + @Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.ERROR) + @Suppress("DEPRECATION") @get:Internal val serializedCompilerArguments: List get() = ArgumentUtils.convertArgumentsToStringList(prepareCompilerArguments()) + @Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.ERROR) + @Suppress("DEPRECATION") @get:Internal val serializedCompilerArgumentsIgnoreClasspathIssues: List get() = ArgumentUtils.convertArgumentsToStringList(prepareCompilerArguments(ignoreClasspathResolutionErrors = true)) + @Deprecated("Replaced by KotlinCompilerArgumentsProducer", level = DeprecationLevel.ERROR) + @Suppress("DEPRECATION") @get:Internal val defaultSerializedCompilerArguments: List 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 CompilerArgumentAware.prepareCompilerArguments(ignoreClasspathResolutionErrors: Boolean = false) = createCompilerArgs().also { setupCompilerArgs(it, ignoreClasspathResolutionErrors = ignoreClasspathResolutionErrors) } diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/model/builder/KotlinModelBuilder.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/model/builder/KotlinModelBuilder.kt index f07ed4bd423..eacdea3f71d 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/model/builder/KotlinModelBuilder.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/model/builder/KotlinModelBuilder.kt @@ -122,6 +122,7 @@ class KotlinModelBuilder(private val kotlinPluginVersion: String, private val an ) } + @Suppress("DEPRECATION_ERROR") private fun AbstractKotlinCompile<*>.createCompilerArguments(): CompilerArguments { return CompilerArgumentsImpl( serializedCompilerArguments, diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer.kt new file mode 100644 index 00000000000..e490f9a9e70 --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/plugin/KotlinCompilerArgumentsProducer.kt @@ -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 create(type: KClass, action: ContributeCompilerArgumentsContext.() -> Unit): T + + companion object { + val default: CreateCompilerArgumentsContext = CreateCompilerArgumentsContext() + val lenient: CreateCompilerArgumentsContext = CreateCompilerArgumentsContext(isLenient = true) + + inline fun CreateCompilerArgumentsContext.create( + noinline action: ContributeCompilerArgumentsContext.() -> Unit + ) = create(T::class, action) + } + } + + interface ContributeCompilerArgumentsContext { + fun tryLenient(action: () -> T): T? + fun contribute(type: ArgumentType, contribution: (T) -> Unit) + } + + fun createCompilerArguments( + context: CreateCompilerArgumentsContext = CreateCompilerArgumentsContext() + ): CommonCompilerArguments +} + +internal fun CreateCompilerArgumentsContext( + includeArgumentTypes: Set = KotlinCompilerArgumentsProducer.ArgumentType.all, + isLenient: Boolean = false +): KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext { + return CreateCompilerArgumentsContextImpl(includeArgumentTypes, isLenient) +} + +private class CreateCompilerArgumentsContextImpl( + private val includeArgumentTypes: Set, + private val isLenient: Boolean +) : KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext { + + override fun create( + type: KClass, action: ContributeCompilerArgumentsContext.() -> 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( + private val arguments: T + ) : ContributeCompilerArgumentsContext { + + override fun 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) + } + } +} diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/AbstractKotlinCompile.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/AbstractKotlinCompile.kt index 1e69eea61ce..ee471cc4c51 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/AbstractKotlinCompile.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/AbstractKotlinCompile.kt @@ -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 @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( diff --git a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt index f16c9761bf5..fec07abd6f8 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/common/kotlin/org/jetbrains/kotlin/gradle/tasks/KotlinJsDce.kt @@ -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 diff --git a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/K2MultiplatformStructureTest.kt b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/K2MultiplatformStructureTest.kt index 447ebb1bed5..51e2e907f4e 100644 --- a/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/K2MultiplatformStructureTest.kt +++ b/libraries/tools/kotlin-gradle-plugin/src/functionalTest/kotlin/org/jetbrains/kotlin/gradle/unitTests/K2MultiplatformStructureTest.kt @@ -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