Improve deprecation warning about usage of old binary DSL

Previously we showed list of all deprecated APIs even if a user uses
only one of them. This patch changes the behaviour and shows only
APIs really used by the user. An approach similar to the one used by
SingleWarningPerBuild in applied here.

Issue #KT-29998 Fixed
This commit is contained in:
Ilya Matveev
2019-03-15 19:44:21 +07:00
parent bfb0d738b1
commit fd9ac54d81
@@ -11,6 +11,7 @@ import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.file.SourceDirectorySet import org.gradle.api.file.SourceDirectorySet
import org.gradle.api.invocation.Gradle
import org.gradle.util.ConfigureUtil import org.gradle.util.ConfigureUtil
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions import org.jetbrains.kotlin.gradle.dsl.KotlinCommonOptions
import org.jetbrains.kotlin.gradle.dsl.KotlinNativeBinaryContainer import org.jetbrains.kotlin.gradle.dsl.KotlinNativeBinaryContainer
@@ -19,38 +20,39 @@ import org.jetbrains.kotlin.gradle.plugin.KotlinCompilationWithResources
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
import org.jetbrains.kotlin.gradle.utils.SingleWarningPerBuild
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import java.io.File import java.io.File
import java.util.*
private const val OLD_BINARY_API_DEPRECATION = private const val OLD_BINARY_API_DEPRECATION =
"Use the `binaries` block instead. See: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#building-final-native-binaries" "Use the `binaries` block instead. See: https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#building-final-native-binaries"
private fun KotlinNativeCompilation.printDeprecationWarning() { private val usedDeprecatedAPIs = WeakHashMap<Project, MutableSet<String>>()
SingleWarningPerBuild.show(
target.project, private fun showDeprecationWarning(gradle: Gradle) {
val rootProject = gradle.rootProject
val deprecatedAPIs = usedDeprecatedAPIs[rootProject]?.sorted()?.joinToString(separator = "\n") { "| $it" }.orEmpty()
if (deprecatedAPIs.isNotEmpty()) {
rootProject.logger.warn(
""" """
Some native binaries in this build are configured using deprecated APIs. Use the `binaries` block instead. |
The following APIs are deprecated: |Some native binaries in this build are configured using deprecated DSL elements. Use the `binaries` DSL block instead.
KotlinNativeCompilation.buildTypes |The following deprecated DSL elements are used in this build:
KotlinNativeCompilation.outputKinds $deprecatedAPIs
KotlinNativeCompilation.outputKinds(...) |
KotlinNativeCompilation.outputKind(...) |See details about the `binaries` block at https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#building-final-native-binaries
KotlinNativeCompilation.entryPoint """.trimMargin()
KotlinNativeCompilation.entryPoint(...)
KotlinNativeCompilation.linkerOpts
KotlinNativeCompilation.linkerOpts(...)
KotlinNativeCompilation.findLinkTask(...)
KotlinNativeCompilation.getLinkTask(...)
KotlinNativeCompilation.findBinary(...)
KotlinNativeCompilation.getBinary(...)
KotlinNativeCompilation.linkTaskName(...)
See details about the `binaries` block at https://kotlinlang.org/docs/reference/building-mpp-with-gradle.html#building-final-native-binaries
""".trimIndent()
) )
} }
}
private fun KotlinNativeCompilation.registerDeprecatedApi(api: String) = with(target.project.rootProject) {
val deprecatedAPIs = usedDeprecatedAPIs.computeIfAbsent(this) {
gradle.projectsEvaluated(::showDeprecationWarning)
mutableSetOf()
}
deprecatedAPIs.add(api)
}
class KotlinNativeCompilation( class KotlinNativeCompilation(
override val target: KotlinNativeTarget, override val target: KotlinNativeTarget,
@@ -102,9 +104,9 @@ class KotlinNativeCompilation(
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
var buildTypes: MutableList<NativeBuildType> var buildTypes: MutableList<NativeBuildType>
get() = buildTypesNoWarn.also { printDeprecationWarning() } get() = buildTypesNoWarn.also { registerDeprecatedApi("KotlinNativeCompilation.buildTypes") }
set(value) { set(value) {
printDeprecationWarning() registerDeprecatedApi("KotlinNativeCompilation.buildTypes")
buildTypesNoWarn = value buildTypesNoWarn = value
} }
@@ -115,27 +117,32 @@ class KotlinNativeCompilation(
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
var outputKinds: MutableList<NativeOutputKind> var outputKinds: MutableList<NativeOutputKind>
get() = outputKindsNoWarn.also { printDeprecationWarning() } get() = outputKindsNoWarn.also { registerDeprecatedApi("KotlinNativeCompilation.outputKinds") }
set(value) { set(value) {
printDeprecationWarning() registerDeprecatedApi("KotlinNativeCompilation.outputKinds")
outputKindsNoWarn = value outputKindsNoWarn = value
} }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun outputKind(kind: NativeOutputKind) = outputKinds.add(kind) fun outputKind(kind: NativeOutputKind) = outputKinds.add(kind).also {
registerDeprecatedApi("KotlinNativeCompilation.outputKind(...)")
}
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun outputKinds(vararg kinds: NativeOutputKind) { fun outputKinds(vararg kinds: NativeOutputKind) {
registerDeprecatedApi("KotlinNativeCompilation.outputKinds(...)")
outputKinds = kinds.toMutableList() outputKinds = kinds.toMutableList()
} }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun outputKinds(vararg kinds: String) { fun outputKinds(vararg kinds: String) {
registerDeprecatedApi("KotlinNativeCompilation.outputKinds(...)")
outputKinds = kinds.map { NativeOutputKind.valueOf(it.toUpperCase()) }.toMutableList() outputKinds = kinds.map { NativeOutputKind.valueOf(it.toUpperCase()) }.toMutableList()
} }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun outputKinds(kinds: List<Any>) { fun outputKinds(kinds: List<Any>) {
registerDeprecatedApi("KotlinNativeCompilation.outputKinds(...)")
outputKinds = kinds.map { outputKinds = kinds.map {
when (it) { when (it) {
is NativeOutputKind -> it is NativeOutputKind -> it
@@ -152,14 +159,15 @@ class KotlinNativeCompilation(
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
var entryPoint: String? var entryPoint: String?
get() = entryPointNoWarn.also { printDeprecationWarning() } get() = entryPointNoWarn.also { registerDeprecatedApi("KotlinNativeCompilation.entryPoint") }
set(value) { set(value) {
printDeprecationWarning() registerDeprecatedApi("KotlinNativeCompilation.entryPoint")
entryPointNoWarn = value entryPointNoWarn = value
} }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun entryPoint(value: String) { fun entryPoint(value: String) {
registerDeprecatedApi("KotlinNativeCompilation.entryPoint(...)")
entryPoint = value entryPoint = value
} }
@@ -175,9 +183,9 @@ class KotlinNativeCompilation(
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
var linkerOpts: MutableList<String> var linkerOpts: MutableList<String>
get() = linkerOptsNoWarn.also { printDeprecationWarning() } get() = linkerOptsNoWarn.also { registerDeprecatedApi("KotlinNativeCompilation.linkerOpts") }
set(value) { set(value) {
printDeprecationWarning() registerDeprecatedApi("KotlinNativeCompilation.linkerOpts")
linkerOptsNoWarn = value linkerOptsNoWarn = value
} }
@@ -185,49 +193,53 @@ class KotlinNativeCompilation(
fun cinterops(action: Action<NamedDomainObjectContainer<DefaultCInteropSettings>>) = action.execute(cinterops) fun cinterops(action: Action<NamedDomainObjectContainer<DefaultCInteropSettings>>) = action.execute(cinterops)
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun linkerOpts(vararg values: String) = linkerOpts(values.toList()) fun linkerOpts(vararg values: String) = linkerOpts(values.toList()).also {
registerDeprecatedApi("KotlinNativeCompilation.linkerOpts(...)")
}
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun linkerOpts(values: List<String>) { fun linkerOpts(values: List<String>) {
registerDeprecatedApi("KotlinNativeCompilation.linkerOpts(...)")
linkerOpts.addAll(values) linkerOpts.addAll(values)
} }
// Task accessors. // Task accessors.
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun findLinkTask(kind: NativeOutputKind, buildType: NativeBuildType): KotlinNativeLink? = fun findLinkTask(kind: NativeOutputKind, buildType: NativeBuildType): KotlinNativeLink? =
binaries[kind to buildType]?.linkTask.also { printDeprecationWarning() } binaries[kind to buildType]?.linkTask.also { registerDeprecatedApi("KotlinNativeCompilation.findLinkTask(...)") }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun getLinkTask(kind: NativeOutputKind, buildType: NativeBuildType): KotlinNativeLink = fun getLinkTask(kind: NativeOutputKind, buildType: NativeBuildType): KotlinNativeLink =
findLinkTask(kind, buildType).also { printDeprecationWarning() } findLinkTask(kind, buildType).also { registerDeprecatedApi("KotlinNativeCompilation.getLinkTask(...)") }
?: throw IllegalArgumentException("Cannot find a link task for the binary kind '$kind' and the build type '$buildType'") ?: throw IllegalArgumentException("Cannot find a link task for the binary kind '$kind' and the build type '$buildType'")
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun findLinkTask(kind: String, buildType: String) = fun findLinkTask(kind: String, buildType: String) =
findLinkTask(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase())) findLinkTask(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase()))
.also { printDeprecationWarning() } .also { registerDeprecatedApi("KotlinNativeCompilation.findLinkTask(...)") }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun getLinkTask(kind: String, buildType: String) = fun getLinkTask(kind: String, buildType: String) =
getLinkTask(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase())) getLinkTask(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase()))
.also { printDeprecationWarning() } .also { registerDeprecatedApi("KotlinNativeCompilation.getLinkTask(...)") }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun findBinary(kind: NativeOutputKind, buildType: NativeBuildType): File? = fun findBinary(kind: NativeOutputKind, buildType: NativeBuildType): File? =
findLinkTask(kind, buildType)?.outputFile?.get().also { printDeprecationWarning() } findLinkTask(kind, buildType)?.outputFile?.get().also { registerDeprecatedApi("KotlinNativeCompilation.findBinary(...)") }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun getBinary(kind: NativeOutputKind, buildType: NativeBuildType): File = fun getBinary(kind: NativeOutputKind, buildType: NativeBuildType): File =
getLinkTask(kind, buildType).outputFile.get().also { printDeprecationWarning() } getLinkTask(kind, buildType).outputFile.get().also { registerDeprecatedApi("KotlinNativeCompilation.getBinary(...)") }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun findBinary(kind: String, buildType: String) = fun findBinary(kind: String, buildType: String) =
findBinary(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase())) findBinary(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase()))
.also { printDeprecationWarning() } .also { registerDeprecatedApi("KotlinNativeCompilation.findBinary(...)") }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun getBinary(kind: String, buildType: String) = fun getBinary(kind: String, buildType: String) =
getBinary(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase())) getBinary(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase()))
.also { printDeprecationWarning() } .also { registerDeprecatedApi("KotlinNativeCompilation.getBinary(...)") }
// Naming // Naming
override val processResourcesTaskName: String override val processResourcesTaskName: String
@@ -239,12 +251,12 @@ class KotlinNativeCompilation(
"link", "link",
KotlinNativeBinaryContainer.generateBinaryName(compilationName, buildType, kind.taskNameClassifier), KotlinNativeBinaryContainer.generateBinaryName(compilationName, buildType, kind.taskNameClassifier),
target.targetName target.targetName
).also { printDeprecationWarning() } ).also { registerDeprecatedApi("KotlinNativeCompilation.linkTaskName(...)") }
@Deprecated(OLD_BINARY_API_DEPRECATION) @Deprecated(OLD_BINARY_API_DEPRECATION)
fun linkTaskName(kind: String, buildType: String) = fun linkTaskName(kind: String, buildType: String) =
linkTaskName(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase())) linkTaskName(NativeOutputKind.valueOf(kind.toUpperCase()), NativeBuildType.valueOf(buildType.toUpperCase()))
.also { printDeprecationWarning() } .also { registerDeprecatedApi("KotlinNativeCompilation.linkTaskName(...)") }
override val compileDependencyConfigurationName: String override val compileDependencyConfigurationName: String
get() = lowerCamelCaseName( get() = lowerCamelCaseName(