[Gradle] Apply attribute disambiguation rules conditionally

Not all Kotlin disambiguation rules are required for non-MPP projects.
On the other hand, they lead to increased memory consumption in Gradle
daemon especially notable on big multimodule projects.

To address this memory increase, we start to apply these rules
conditionally based on the applied plugin.

^KT-63005 Fixed
This commit is contained in:
Yahor Berdnikau
2023-11-13 14:44:00 +01:00
committed by Space Team
parent 26183237ab
commit 580c2a490d
3 changed files with 51 additions and 26 deletions
@@ -199,12 +199,18 @@ abstract class DefaultKotlinBasePlugin : KotlinBasePlugin {
) = with(project.dependencies.attributesSchema) {
KotlinPlatformType.setupAttributesMatchingStrategy(this)
KotlinUsages.setupAttributesMatchingStrategy(this, isKotlinGranularMetadata)
KotlinJsCompilerAttribute.setupAttributesMatchingStrategy(project.dependencies.attributesSchema)
KotlinWasmTargetAttribute.setupAttributesMatchingStrategy(project.dependencies.attributesSchema)
ProjectLocalConfigurations.setupAttributesMatchingStrategy(this)
CInteropKlibLibraryElements.setupAttributesMatchingStrategy(this)
CommonizerTargetAttribute.setupAttributesMatchingStrategy(this)
CInteropCommonizerArtifactTypeAttribute.setupTransform(project)
project.whenJsOrMppEnabled {
KotlinJsCompilerAttribute.setupAttributesMatchingStrategy(project.dependencies.attributesSchema)
KotlinWasmTargetAttribute.setupAttributesMatchingStrategy(project.dependencies.attributesSchema)
}
project.whenMppEnabled {
CInteropKlibLibraryElements.setupAttributesMatchingStrategy(this)
CommonizerTargetAttribute.setupAttributesMatchingStrategy(this)
CInteropCommonizerArtifactTypeAttribute.setupTransform(project)
}
}
open fun whenBuildEvaluated(project: Project) {
@@ -1,21 +0,0 @@
/*
* Copyright 2010-2022 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.utils
import org.gradle.api.Project
internal fun Project.whenKaptEnabled(block: () -> Unit) {
var triggered = false
fun trigger() {
if (triggered) return
triggered = true
block()
}
pluginManager.withPlugin("kotlin-kapt") { trigger() }
pluginManager.withPlugin("org.jetbrains.kotlin.kapt") { trigger() }
}
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2022 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.utils
import org.gradle.api.Project
internal fun Project.whenKaptEnabled(action: () -> Unit) = whenPluginsEnabled(
setOf("org.jetbrains.kotlin.kapt", "kotlin-kapt"),
action,
)
internal fun Project.whenMppEnabled(action: () -> Unit) = whenPluginsEnabled(
setOf("org.jetbrains.kotlin.multiplatform", "kotlin-multiplatform"),
action,
)
internal fun Project.whenJsOrMppEnabled(action: () -> Unit) = whenPluginsEnabled(
setOf("org.jetbrains.kotlin.js", "org.jetbrains.kotlin.multiplatform", "kotlin-multiplatform"),
action,
)
private fun Project.whenPluginsEnabled(
pluginIds: Set<String>,
action: () -> Unit,
) {
var triggered = false
fun trigger() {
if (triggered) return
triggered = true
action()
}
for (pluginId in pluginIds) {
pluginManager.withPlugin(pluginId) { trigger() }
}
}