Coerce commonTest to common in HMPP
Otherwise, Android import won't add dependency from androidTest to commoonTest. See KT-38744 for details ^KT-38744 Fixed
This commit is contained in:
@@ -20,6 +20,8 @@ import org.gradle.api.provider.Provider
|
|||||||
import org.gradle.api.tasks.Exec
|
import org.gradle.api.tasks.Exec
|
||||||
import org.gradle.api.tasks.TaskProvider
|
import org.gradle.api.tasks.TaskProvider
|
||||||
import org.jetbrains.kotlin.gradle.KotlinMPPGradleModel.Companion.NO_KOTLIN_NATIVE_HOME
|
import org.jetbrains.kotlin.gradle.KotlinMPPGradleModel.Companion.NO_KOTLIN_NATIVE_HOME
|
||||||
|
import org.jetbrains.kotlin.gradle.KotlinSourceSet.Companion.COMMON_MAIN_SOURCE_SET_NAME
|
||||||
|
import org.jetbrains.kotlin.gradle.KotlinSourceSet.Companion.COMMON_TEST_SOURCE_SET_NAME
|
||||||
import org.jetbrains.plugins.gradle.DefaultExternalDependencyId
|
import org.jetbrains.plugins.gradle.DefaultExternalDependencyId
|
||||||
import org.jetbrains.plugins.gradle.model.*
|
import org.jetbrains.plugins.gradle.model.*
|
||||||
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder
|
import org.jetbrains.plugins.gradle.tooling.ErrorMessageBuilder
|
||||||
@@ -58,7 +60,7 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
val sourceSets = buildSourceSets(dependencyResolver, project, dependencyMapper) ?: return null
|
val sourceSets = buildSourceSets(dependencyResolver, project, dependencyMapper) ?: return null
|
||||||
val sourceSetMap = sourceSets.map { it.name to it }.toMap()
|
val sourceSetMap = sourceSets.map { it.name to it }.toMap()
|
||||||
val targets = buildTargets(projectTargets, sourceSetMap, dependencyResolver, project, dependencyMapper) ?: return null
|
val targets = buildTargets(projectTargets, sourceSetMap, dependencyResolver, project, dependencyMapper) ?: return null
|
||||||
computeSourceSetsDeferredInfo(sourceSetMap, targets, isHMPPEnabled(project))
|
computeSourceSetsDeferredInfo(sourceSetMap, targets, isHMPPEnabled(project), shouldCoerceRootSourceSetToCommon(project))
|
||||||
val coroutinesState = getCoroutinesState(project)
|
val coroutinesState = getCoroutinesState(project)
|
||||||
reportUnresolvedDependencies(targets)
|
reportUnresolvedDependencies(targets)
|
||||||
val kotlinNativeHome = KotlinNativeHomeEvaluator.getKotlinNativeHome(project) ?: NO_KOTLIN_NATIVE_HOME
|
val kotlinNativeHome = KotlinNativeHomeEvaluator.getKotlinNativeHome(project) ?: NO_KOTLIN_NATIVE_HOME
|
||||||
@@ -94,6 +96,10 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
return (project.findProperty("kotlin.mpp.enableGranularSourceSetsMetadata") as? String)?.toBoolean() ?: false
|
return (project.findProperty("kotlin.mpp.enableGranularSourceSetsMetadata") as? String)?.toBoolean() ?: false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun shouldCoerceRootSourceSetToCommon(project: Project): Boolean {
|
||||||
|
return (project.findProperty("kotlin.mpp.coerceRootSourceSetsToCommon") as? String)?.toBoolean() ?: true
|
||||||
|
}
|
||||||
|
|
||||||
private fun isNativeDependencyPropagationEnabled(project: Project): Boolean {
|
private fun isNativeDependencyPropagationEnabled(project: Project): Boolean {
|
||||||
return (project.findProperty("kotlin.native.enableDependencyPropagation") as? String)?.toBoolean() ?: true
|
return (project.findProperty("kotlin.native.enableDependencyPropagation") as? String)?.toBoolean() ?: true
|
||||||
}
|
}
|
||||||
@@ -771,7 +777,8 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
private fun computeSourceSetsDeferredInfo(
|
private fun computeSourceSetsDeferredInfo(
|
||||||
sourceSets: Map<String, KotlinSourceSetImpl>,
|
sourceSets: Map<String, KotlinSourceSetImpl>,
|
||||||
targets: Collection<KotlinTarget>,
|
targets: Collection<KotlinTarget>,
|
||||||
isHMPPEnabled: Boolean
|
isHMPPEnabled: Boolean,
|
||||||
|
coerceRootSourceSetsToCommon: Boolean
|
||||||
) {
|
) {
|
||||||
// includes only compilations where source set is listed
|
// includes only compilations where source set is listed
|
||||||
val compiledSourceSetToCompilations = LinkedHashMap<KotlinSourceSet, MutableSet<KotlinCompilation>>()
|
val compiledSourceSetToCompilations = LinkedHashMap<KotlinSourceSet, MutableSet<KotlinCompilation>>()
|
||||||
@@ -810,12 +817,32 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
val platforms = compilations.map { it.platform }
|
val platforms = compilations.map { it.platform }
|
||||||
sourceSet.actualPlatforms.addSimplePlatforms(platforms)
|
sourceSet.actualPlatforms.addSimplePlatforms(platforms)
|
||||||
}
|
}
|
||||||
if ((!isHMPPEnabled) && sourceSet.actualPlatforms.platforms.size != 1) {
|
|
||||||
|
if (sourceSet.shouldCoerceToCommon(isHMPPEnabled, coerceRootSourceSetsToCommon)) {
|
||||||
sourceSet.actualPlatforms.addSimplePlatforms(listOf(KotlinPlatform.COMMON))
|
sourceSet.actualPlatforms.addSimplePlatforms(listOf(KotlinPlatform.COMMON))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KotlinSourceSetImpl.shouldCoerceToCommon(isHMPPEnabled: Boolean, coerceRootSourceSetsToCommon: Boolean): Boolean {
|
||||||
|
val isRoot = name == COMMON_MAIN_SOURCE_SET_NAME || name == COMMON_TEST_SOURCE_SET_NAME
|
||||||
|
|
||||||
|
// never makes sense to coerce single-targeted source-sets
|
||||||
|
if (actualPlatforms.platforms.size == 1) return false
|
||||||
|
|
||||||
|
return when {
|
||||||
|
// pre-HMPP has only single-targeted source sets and COMMON
|
||||||
|
!isHMPPEnabled -> true
|
||||||
|
|
||||||
|
// in HMPP, we might want to coerce source sets to common, but only root ones, and only
|
||||||
|
// when the corresponding setting is turned on
|
||||||
|
isHMPPEnabled && isRoot && coerceRootSourceSetsToCommon -> true
|
||||||
|
|
||||||
|
// in all other cases, in HMPP we shouldn't coerce anything
|
||||||
|
else -> false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private class DependencyAdjuster(
|
private class DependencyAdjuster(
|
||||||
private val configuration: Configuration,
|
private val configuration: Configuration,
|
||||||
private val scope: String,
|
private val scope: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user