Precise platform importing changes ported from new repository
This commit is contained in:
@@ -41,7 +41,7 @@ class KotlinSourceSetInfo @PropertyMapping("kotlinModule") constructor(val kotli
|
|||||||
|
|
||||||
@Deprecated("Returns only single TargetPlatform", ReplaceWith("actualPlatforms.actualPlatforms"), DeprecationLevel.ERROR)
|
@Deprecated("Returns only single TargetPlatform", ReplaceWith("actualPlatforms.actualPlatforms"), DeprecationLevel.ERROR)
|
||||||
val platform: KotlinPlatform
|
val platform: KotlinPlatform
|
||||||
get() = actualPlatforms.getSinglePlatform()
|
get() = actualPlatforms.platforms.singleOrNull() ?: KotlinPlatform.COMMON
|
||||||
|
|
||||||
@Transient
|
@Transient
|
||||||
var defaultCompilerArguments: CommonCompilerArguments? = null
|
var defaultCompilerArguments: CommonCompilerArguments? = null
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 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
|
||||||
|
|
||||||
|
@RequiresOptIn(level = RequiresOptIn.Level.WARNING, message = "This API might change in the future")
|
||||||
|
annotation class ExperimentalGradleToolingApi
|
||||||
@@ -21,5 +21,5 @@ interface KotlinSourceSetImportingDiagnostic : KotlinImportingDiagnostic {
|
|||||||
data class OrphanSourceSetsImportingDiagnostic(override val kotlinSourceSet: KotlinSourceSet) : KotlinSourceSetImportingDiagnostic {
|
data class OrphanSourceSetsImportingDiagnostic(override val kotlinSourceSet: KotlinSourceSet) : KotlinSourceSetImportingDiagnostic {
|
||||||
override fun deepCopy(cache: MutableMap<Any, Any>): OrphanSourceSetsImportingDiagnostic =
|
override fun deepCopy(cache: MutableMap<Any, Any>): OrphanSourceSetsImportingDiagnostic =
|
||||||
(cache[kotlinSourceSet] as? KotlinSourceSet)?.let { OrphanSourceSetsImportingDiagnostic(it) }
|
(cache[kotlinSourceSet] as? KotlinSourceSet)?.let { OrphanSourceSetsImportingDiagnostic(it) }
|
||||||
?: OrphanSourceSetsImportingDiagnostic(KotlinSourceSetImpl(kotlinSourceSet, cache).apply { cache[kotlinSourceSet] = this })
|
?: OrphanSourceSetsImportingDiagnostic(KotlinSourceSetImpl(kotlinSourceSet).apply { cache[kotlinSourceSet] = this })
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
* 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("DeprecatedCallableAddReplaceWith")
|
||||||
|
|
||||||
package org.jetbrains.kotlin.gradle
|
package org.jetbrains.kotlin.gradle
|
||||||
|
|
||||||
import org.jetbrains.plugins.gradle.model.ExternalDependency
|
import org.jetbrains.plugins.gradle.model.ExternalDependency
|
||||||
@@ -53,12 +55,43 @@ interface KotlinSourceSet : KotlinModule {
|
|||||||
val languageSettings: KotlinLanguageSettings
|
val languageSettings: KotlinLanguageSettings
|
||||||
val sourceDirs: Set<File>
|
val sourceDirs: Set<File>
|
||||||
val resourceDirs: Set<File>
|
val resourceDirs: Set<File>
|
||||||
val dependsOnSourceSets: Set<String>
|
|
||||||
val actualPlatforms: KotlinPlatformContainer
|
val actualPlatforms: KotlinPlatformContainer
|
||||||
|
|
||||||
@Deprecated("Returns single target platform", ReplaceWith("actualPlatforms.actualPlatforms"), DeprecationLevel.ERROR)
|
|
||||||
|
/**
|
||||||
|
* All source sets that this source set explicitly declared a 'dependsOn' relation to
|
||||||
|
*/
|
||||||
|
val declaredDependsOnSourceSets: Set<String>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The whole transitive closure of source sets this source set depends on.
|
||||||
|
* ([declaredDependsOnSourceSets] and their dependencies recursively)
|
||||||
|
*/
|
||||||
|
@Suppress("DEPRECATION")
|
||||||
|
@Deprecated(
|
||||||
|
"This property might be misleading. " +
|
||||||
|
"Replace with 'KotlinSourceSetContainer.resolveAllDependsOnSourceSets' to make intention of " +
|
||||||
|
"receiving the full transitive closure explicit",
|
||||||
|
level = DeprecationLevel.ERROR
|
||||||
|
)
|
||||||
|
val dependsOnSourceSets: Set<String>
|
||||||
|
get() = allDependsOnSourceSets
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The whole transitive closure of source sets this source set depends on.
|
||||||
|
* ([declaredDependsOnSourceSets] and their dependencies recursively)
|
||||||
|
*/
|
||||||
|
@Deprecated(
|
||||||
|
"This set of source sets might be inconsistent with any KotlinSourceSetContainer different to the one used to build this instance" +
|
||||||
|
"Replace with 'KotlinSourceSetContainer.resolveAllDependsOnSourceSets' to get consistent resolution",
|
||||||
|
level = DeprecationLevel.WARNING
|
||||||
|
)
|
||||||
|
val allDependsOnSourceSets: Set<String>
|
||||||
|
|
||||||
|
|
||||||
|
@Deprecated("Returns single target platform. Use actualPlatforms instead", level = DeprecationLevel.ERROR)
|
||||||
val platform: KotlinPlatform
|
val platform: KotlinPlatform
|
||||||
get() = actualPlatforms.getSinglePlatform()
|
get() = actualPlatforms.platforms.singleOrNull() ?: KotlinPlatform.COMMON
|
||||||
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -98,6 +131,11 @@ interface KotlinNativeCompilationExtensions : Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface KotlinCompilation : KotlinModule {
|
interface KotlinCompilation : KotlinModule {
|
||||||
|
|
||||||
|
@Deprecated("Use allSourceSets or declaredSourceSets instead")
|
||||||
|
val sourceSets: Collection<KotlinSourceSet>
|
||||||
|
get() = declaredSourceSets
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* All source sets participated in this compilation, including those available
|
* All source sets participated in this compilation, including those available
|
||||||
* via dependsOn.
|
* via dependsOn.
|
||||||
@@ -105,14 +143,14 @@ interface KotlinCompilation : KotlinModule {
|
|||||||
val allSourceSets: Collection<KotlinSourceSet>
|
val allSourceSets: Collection<KotlinSourceSet>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Only default source sets of this compilation, i.e. those which are included
|
* Only directly declared source sets of this compilation, i.e. those which are included
|
||||||
* into compilations directly.
|
* into compilations directly.
|
||||||
*
|
*
|
||||||
* Usually, those are automatically created source sets for automatically created
|
* Usually, those are automatically created source sets for automatically created
|
||||||
* compilations (like jvmMain for JVM compilations) or manually included source sets
|
* compilations (like jvmMain for JVM compilations) or manually included source sets
|
||||||
* (like 'jvm().compilations["main"].source(mySourceSet)' )
|
* (like 'jvm().compilations["main"].source(mySourceSet)' )
|
||||||
*/
|
*/
|
||||||
val defaultSourceSets: Collection<KotlinSourceSet>
|
val declaredSourceSets: Collection<KotlinSourceSet>
|
||||||
|
|
||||||
val output: KotlinCompilationOutput
|
val output: KotlinCompilationOutput
|
||||||
val arguments: KotlinCompilationArguments
|
val arguments: KotlinCompilationArguments
|
||||||
@@ -140,15 +178,50 @@ enum class KotlinPlatform(val id: String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface KotlinPlatformContainer : Serializable {
|
interface KotlinPlatformContainer : Serializable, Iterable<KotlinPlatform> {
|
||||||
|
/**
|
||||||
|
* Distinct collection of Platforms.
|
||||||
|
* Keeping 'Collection' as type for binary compatibility
|
||||||
|
*/
|
||||||
val platforms: Collection<KotlinPlatform>
|
val platforms: Collection<KotlinPlatform>
|
||||||
val arePlatformsInitialized: Boolean
|
val arePlatformsInitialized: Boolean
|
||||||
|
|
||||||
|
@Deprecated(
|
||||||
|
"Ambiguous semantics of 'supports' for COMMON or (ANDROID/JVM) platforms. Use 'platforms' directly to express clear intention",
|
||||||
|
level = DeprecationLevel.ERROR
|
||||||
|
)
|
||||||
fun supports(simplePlatform: KotlinPlatform): Boolean
|
fun supports(simplePlatform: KotlinPlatform): Boolean
|
||||||
|
|
||||||
fun addSimplePlatforms(platforms: Collection<KotlinPlatform>)
|
@Deprecated(
|
||||||
|
"Unclear semantics: Use 'platforms' directly to express intention",
|
||||||
|
level = DeprecationLevel.ERROR
|
||||||
|
)
|
||||||
fun getSinglePlatform() = platforms.singleOrNull() ?: KotlinPlatform.COMMON
|
fun getSinglePlatform() = platforms.singleOrNull() ?: KotlinPlatform.COMMON
|
||||||
|
|
||||||
|
@Deprecated(
|
||||||
|
"Unclear semantics: Use 'pushPlatform' instead",
|
||||||
|
ReplaceWith("pushPlatform"),
|
||||||
|
level = DeprecationLevel.ERROR
|
||||||
|
)
|
||||||
|
fun addSimplePlatforms(platforms: Collection<KotlinPlatform>) = pushPlatforms(platforms)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the given [platforms] to this container.
|
||||||
|
* Note: If any of the pushed [platforms] is common, then this container will drop all non-common platforms and subsequent invocations
|
||||||
|
* to this function will have no further effect.
|
||||||
|
*/
|
||||||
|
fun pushPlatforms(platforms: Iterable<KotlinPlatform>)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see pushPlatforms
|
||||||
|
*/
|
||||||
|
fun pushPlatforms(vararg platform: KotlinPlatform) {
|
||||||
|
pushPlatforms(platform.toList())
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun iterator(): Iterator<KotlinPlatform> {
|
||||||
|
return platforms.toSet().iterator()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -190,14 +263,19 @@ interface ExtraFeatures : Serializable {
|
|||||||
val isNativeDependencyPropagationEnabled: Boolean
|
val isNativeDependencyPropagationEnabled: Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
interface KotlinMPPGradleModel : Serializable {
|
interface KotlinMPPGradleModel : KotlinSourceSetContainer, Serializable {
|
||||||
val dependencyMap: Map<KotlinDependencyId, KotlinDependency>
|
val dependencyMap: Map<KotlinDependencyId, KotlinDependency>
|
||||||
val sourceSets: Map<String, KotlinSourceSet>
|
|
||||||
val targets: Collection<KotlinTarget>
|
val targets: Collection<KotlinTarget>
|
||||||
val extraFeatures: ExtraFeatures
|
val extraFeatures: ExtraFeatures
|
||||||
val kotlinNativeHome: String
|
val kotlinNativeHome: String
|
||||||
val kotlinImportingDiagnostics: KotlinImportingDiagnosticsContainer
|
val kotlinImportingDiagnostics: KotlinImportingDiagnosticsContainer
|
||||||
|
|
||||||
|
@Deprecated("Use 'sourceSetsByName' instead", ReplaceWith("sourceSetsByName"), DeprecationLevel.ERROR)
|
||||||
|
val sourceSets: Map<String, KotlinSourceSet>
|
||||||
|
get() = sourceSetsByName
|
||||||
|
|
||||||
|
override val sourceSetsByName: Map<String, KotlinSourceSet>
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val NO_KOTLIN_NATIVE_HOME = ""
|
const val NO_KOTLIN_NATIVE_HOME = ""
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,46 +45,46 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun buildAll(modelName: String, project: Project): Any? {
|
override fun buildAll(modelName: String, project: Project): Any? {
|
||||||
val projectTargets = project.getTargets() ?: return null
|
try {
|
||||||
val dependencyResolver = DependencyResolverImpl(
|
val projectTargets = project.getTargets() ?: return null
|
||||||
project,
|
val dependencyResolver = DependencyResolverImpl(project, false, true, SourceSetCachedFinder(project))
|
||||||
false,
|
val dependencyMapper = KotlinDependencyMapper()
|
||||||
false,
|
val importingContext = MultiplatformModelImportingContextImpl(project)
|
||||||
true,
|
|
||||||
SourceSetCachedFinder(project)
|
|
||||||
)
|
|
||||||
val dependencyMapper = KotlinDependencyMapper()
|
|
||||||
val importingContext = MultiplatformModelImportingContextImpl(project)
|
|
||||||
|
|
||||||
importingContext.initializeSourceSets(buildSourceSets(importingContext, dependencyResolver, dependencyMapper) ?: return null)
|
importingContext.initializeSourceSets(buildSourceSets(importingContext, dependencyResolver, dependencyMapper) ?: return null)
|
||||||
|
|
||||||
val targets = buildTargets(importingContext, projectTargets, dependencyResolver, dependencyMapper)
|
val targets = buildTargets(importingContext, projectTargets, dependencyResolver, dependencyMapper)
|
||||||
importingContext.initializeTargets(targets)
|
importingContext.initializeTargets(targets)
|
||||||
importingContext.initializeCompilations(targets.flatMap { it.compilations })
|
importingContext.initializeCompilations(targets.flatMap { it.compilations })
|
||||||
|
|
||||||
computeSourceSetsDeferredInfo(importingContext)
|
computeSourceSetsDeferredInfo(importingContext)
|
||||||
|
|
||||||
val coroutinesState = getCoroutinesState(project)
|
val coroutinesState = getCoroutinesState(project)
|
||||||
val kotlinNativeHome = KotlinNativeHomeEvaluator.getKotlinNativeHome(project) ?: NO_KOTLIN_NATIVE_HOME
|
val kotlinNativeHome = KotlinNativeHomeEvaluator.getKotlinNativeHome(project) ?: NO_KOTLIN_NATIVE_HOME
|
||||||
return KotlinMPPGradleModelImpl(
|
val model = KotlinMPPGradleModelImpl(
|
||||||
filterOrphanSourceSets(importingContext),
|
sourceSetsByName = filterOrphanSourceSets(importingContext),
|
||||||
importingContext.targets,
|
targets = importingContext.targets,
|
||||||
ExtraFeaturesImpl(
|
extraFeatures = ExtraFeaturesImpl(
|
||||||
coroutinesState,
|
coroutinesState = coroutinesState,
|
||||||
importingContext.getProperty(IS_HMPP_ENABLED),
|
isHMPPEnabled = importingContext.getProperty(IS_HMPP_ENABLED),
|
||||||
importingContext.getProperty(ENABLE_NATIVE_DEPENDENCY_PROPAGATION)
|
isNativeDependencyPropagationEnabled = importingContext.getProperty(ENABLE_NATIVE_DEPENDENCY_PROPAGATION)
|
||||||
),
|
),
|
||||||
kotlinNativeHome,
|
kotlinNativeHome = kotlinNativeHome,
|
||||||
dependencyMapper.toDependencyMap()
|
dependencyMap = dependencyMapper.toDependencyMap()
|
||||||
).apply {
|
).apply {
|
||||||
kotlinImportingDiagnostics += collectDiagnostics(importingContext)
|
kotlinImportingDiagnostics += collectDiagnostics(importingContext)
|
||||||
|
}
|
||||||
|
return model
|
||||||
|
} catch (throwable: Throwable) {
|
||||||
|
project.logger.error("Failed building KotlinMPPGradleModel", throwable)
|
||||||
|
throw throwable
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun filterOrphanSourceSets(
|
private fun filterOrphanSourceSets(
|
||||||
importingContext: MultiplatformModelImportingContext
|
importingContext: MultiplatformModelImportingContext
|
||||||
): Map<String, KotlinSourceSetImpl> {
|
): Map<String, KotlinSourceSetImpl> {
|
||||||
if (importingContext.getProperty(IMPORT_ORPHAN_SOURCE_SETS)) return importingContext.sourceSetsByNames
|
if (importingContext.getProperty(IMPORT_ORPHAN_SOURCE_SETS)) return importingContext.sourceSetsByName
|
||||||
|
|
||||||
val (orphanSourceSets, nonOrphanSourceSets) = importingContext.sourceSets.partition { importingContext.isOrphanSourceSet(it) }
|
val (orphanSourceSets, nonOrphanSourceSets) = importingContext.sourceSets.partition { importingContext.isOrphanSourceSet(it) }
|
||||||
|
|
||||||
@@ -118,17 +118,16 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
val allSourceSetsProtosByNames = sourceSets.mapNotNull {
|
val allSourceSetsProtosByNames = sourceSets.mapNotNull {
|
||||||
buildSourceSet(it, dependencyResolver, importingContext.project, dependencyMapper, androidDeps)
|
buildSourceSet(it, dependencyResolver, importingContext.project, dependencyMapper, androidDeps)
|
||||||
}.associateBy { it.name }
|
}.associateBy { it.name }
|
||||||
val dependsOnCache = HashMap<String, Set<String>>()
|
|
||||||
|
|
||||||
// Some performance optimisation: do not build metadata dependencies if source set is not common
|
// Some performance optimisation: do not build metadata dependencies if source set is not common
|
||||||
return if (importingContext.getProperty(BUILD_METADATA_DEPENDENCIES)) {
|
return if (importingContext.getProperty(BUILD_METADATA_DEPENDENCIES)) {
|
||||||
allSourceSetsProtosByNames.mapValues { (_, proto) ->
|
allSourceSetsProtosByNames.mapValues { (_, proto) ->
|
||||||
proto.buildKotlinSourceSetImpl(true, allSourceSetsProtosByNames, dependsOnCache)
|
proto.buildKotlinSourceSetImpl(true, allSourceSetsProtosByNames)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val unactualizedSourceSets = allSourceSetsProtosByNames.values.flatMap { it.dependsOnSourceSets }.distinct()
|
val unactualizedSourceSets = allSourceSetsProtosByNames.values.flatMap { it.dependsOnSourceSets }.distinct()
|
||||||
allSourceSetsProtosByNames.mapValues { (name, proto) ->
|
allSourceSetsProtosByNames.mapValues { (name, proto) ->
|
||||||
proto.buildKotlinSourceSetImpl(unactualizedSourceSets.contains(name), allSourceSetsProtosByNames, dependsOnCache)
|
proto.buildKotlinSourceSetImpl(unactualizedSourceSets.contains(name), allSourceSetsProtosByNames)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -306,8 +305,9 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
): KotlinTarget? {
|
): KotlinTarget? {
|
||||||
val targetClass = gradleTarget.javaClass
|
val targetClass = gradleTarget.javaClass
|
||||||
|
|
||||||
val metadataTargetClass = targetClass.classLoader.loadClass("org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget")
|
/* Loading class safely to still support Kotlin Gradle Plugin 1.3.30 */
|
||||||
if (metadataTargetClass.isInstance(gradleTarget)) return null
|
val metadataTargetClass = targetClass.classLoader.loadClassOrNull("org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget")
|
||||||
|
if (metadataTargetClass?.isInstance(gradleTarget) == true) return null
|
||||||
|
|
||||||
val getPlatformType = targetClass.getMethodOrNull("getPlatformType") ?: return null
|
val getPlatformType = targetClass.getMethodOrNull("getPlatformType") ?: return null
|
||||||
val getDisambiguationClassifier = targetClass.getMethodOrNull("getDisambiguationClassifier") ?: return null
|
val getDisambiguationClassifier = targetClass.getMethodOrNull("getDisambiguationClassifier") ?: return null
|
||||||
@@ -494,23 +494,20 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
}
|
}
|
||||||
val nativeExtensions = konanTarget?.let(::KotlinNativeCompilationExtensionsImpl)
|
val nativeExtensions = konanTarget?.let(::KotlinNativeCompilationExtensionsImpl)
|
||||||
|
|
||||||
val allSourceSets = if (platform != KotlinPlatform.ANDROID) {
|
val allSourceSets = kotlinSourceSets
|
||||||
kotlinSourceSets
|
.flatMap { sourceSet -> importingContext.resolveAllDependsOnSourceSets(sourceSet) }
|
||||||
} else {
|
.union(kotlinSourceSets)
|
||||||
kotlinSourceSets.flatMap { it.dependsOnSourceSets }.mapNotNull { importingContext.sourceSetByName(it) }
|
|
||||||
.union(kotlinSourceSets)
|
|
||||||
}
|
|
||||||
|
|
||||||
return KotlinCompilationImpl(
|
return KotlinCompilationImpl(
|
||||||
gradleCompilation.name,
|
name = gradleCompilation.name,
|
||||||
allSourceSets,
|
allSourceSets = allSourceSets,
|
||||||
kotlinSourceSets,
|
declaredSourceSets = if (platform == KotlinPlatform.ANDROID) allSourceSets else kotlinSourceSets,
|
||||||
dependencies.map { dependencyMapper.getId(it) }.distinct().toTypedArray(),
|
dependencies = dependencies.map { dependencyMapper.getId(it) }.distinct().toTypedArray(),
|
||||||
output,
|
output = output,
|
||||||
arguments,
|
arguments = arguments,
|
||||||
dependencyClasspath.toTypedArray(),
|
dependencyClasspath = dependencyClasspath.toTypedArray(),
|
||||||
kotlinTaskProperties,
|
kotlinTaskProperties = kotlinTaskProperties,
|
||||||
nativeExtensions
|
nativeExtensions = nativeExtensions
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -737,24 +734,24 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService {
|
|||||||
// Explicitly set platform of orphan source-sets to only used platforms, not all supported platforms
|
// Explicitly set platform of orphan source-sets to only used platforms, not all supported platforms
|
||||||
// Otherwise, the tooling might be upset after trying to provide some support for a target which actually
|
// Otherwise, the tooling might be upset after trying to provide some support for a target which actually
|
||||||
// doesn't exist in this project (e.g. after trying to draw gutters, while test tasks do not exist)
|
// doesn't exist in this project (e.g. after trying to draw gutters, while test tasks do not exist)
|
||||||
sourceSet.actualPlatforms.addSimplePlatforms(projectPlatforms)
|
sourceSet.actualPlatforms.pushPlatforms(projectPlatforms)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldCoerceToCommon(sourceSet)) {
|
if (shouldCoerceToCommon(sourceSet)) {
|
||||||
sourceSet.actualPlatforms.addSimplePlatforms(listOf(KotlinPlatform.COMMON))
|
sourceSet.actualPlatforms.pushPlatforms(KotlinPlatform.COMMON)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!getProperty(IS_HMPP_ENABLED) && !isDefaultSourceSet(sourceSet)) {
|
if (!getProperty(IS_HMPP_ENABLED) && !isDeclaredSourceSet(sourceSet)) {
|
||||||
// intermediate source sets should be common if HMPP is disabled
|
// intermediate source sets should be common if HMPP is disabled
|
||||||
sourceSet.actualPlatforms.addSimplePlatforms(listOf(KotlinPlatform.COMMON))
|
sourceSet.actualPlatforms.pushPlatforms(KotlinPlatform.COMMON)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
compilationsBySourceSet(sourceSet)?.let { compilations ->
|
compilationsBySourceSet(sourceSet)?.let { compilations ->
|
||||||
val platforms = compilations.map { it.platform }
|
val platforms = compilations.map { it.platform }
|
||||||
sourceSet.actualPlatforms.addSimplePlatforms(platforms)
|
sourceSet.actualPlatforms.pushPlatforms(platforms)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2021 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
|
||||||
|
|
||||||
|
interface KotlinSourceSetContainer {
|
||||||
|
val sourceSetsByName: Map<String, KotlinSourceSet>
|
||||||
|
}
|
||||||
|
|
||||||
|
val KotlinSourceSetContainer.sourceSets: List<KotlinSourceSet> get() = sourceSetsByName.values.toList()
|
||||||
|
|
||||||
|
fun KotlinSourceSetContainer.resolveDeclaredDependsOnSourceSets(sourceSet: KotlinSourceSet): Set<KotlinSourceSet> {
|
||||||
|
return sourceSet.declaredDependsOnSourceSets.mapNotNull { name -> sourceSetsByName[name] }.toSet()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun KotlinSourceSetContainer.resolveAllDependsOnSourceSets(sourceSet: KotlinSourceSet): Set<KotlinSourceSet> {
|
||||||
|
/* Fast path */
|
||||||
|
if (sourceSet.declaredDependsOnSourceSets.isEmpty()) return emptySet()
|
||||||
|
|
||||||
|
/* Aggregating set containing all currently resolved source sets */
|
||||||
|
val resolvedSourceSets = mutableSetOf<KotlinSourceSet>()
|
||||||
|
|
||||||
|
/* Queue of source set names that shall be resolved */
|
||||||
|
val declaredDependsOnSourceSetsQueue = ArrayDeque<String>()
|
||||||
|
|
||||||
|
declaredDependsOnSourceSetsQueue.addAll(sourceSet.declaredDependsOnSourceSets)
|
||||||
|
while (declaredDependsOnSourceSetsQueue.isNotEmpty()) {
|
||||||
|
val sourceSetName = declaredDependsOnSourceSetsQueue.removeFirst()
|
||||||
|
val resolvedSourceSet = sourceSetsByName[sourceSetName]
|
||||||
|
if (resolvedSourceSet != null) {
|
||||||
|
if (resolvedSourceSets.add(resolvedSourceSet)) {
|
||||||
|
declaredDependsOnSourceSetsQueue.addAll(resolvedSourceSet.declaredDependsOnSourceSets)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return resolvedSourceSets
|
||||||
|
}
|
||||||
|
|
||||||
|
fun KotlinSourceSetContainer.isDependsOn(from: KotlinSourceSet, to: KotlinSourceSet): Boolean {
|
||||||
|
return to in resolveAllDependsOnSourceSets(from)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun KotlinSourceSet.isDependsOn(model: KotlinSourceSetContainer, sourceSet: KotlinSourceSet): Boolean {
|
||||||
|
return model.isDependsOn(from = this, to = sourceSet)
|
||||||
|
}
|
||||||
@@ -15,7 +15,7 @@ internal object OrphanSourceSetImportingChecker : MultiplatformModelImportingChe
|
|||||||
reportTo: KotlinImportingDiagnosticsContainer,
|
reportTo: KotlinImportingDiagnosticsContainer,
|
||||||
context: MultiplatformModelImportingContext
|
context: MultiplatformModelImportingContext
|
||||||
) {
|
) {
|
||||||
model.sourceSets.values.filter { context.isOrphanSourceSet(it) }
|
model.sourceSetsByName.values.filter { context.isOrphanSourceSet(it) }
|
||||||
.mapTo(reportTo) { OrphanSourceSetsImportingDiagnostic(it) }
|
.mapTo(reportTo) { OrphanSourceSetsImportingDiagnostic(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,9 +8,8 @@ package org.jetbrains.kotlin.gradle
|
|||||||
import org.gradle.api.Project
|
import org.gradle.api.Project
|
||||||
import org.gradle.api.logging.Logging
|
import org.gradle.api.logging.Logging
|
||||||
|
|
||||||
private val logger = Logging.getLogger(KotlinMPPGradleModelBuilder::class.java)
|
|
||||||
|
|
||||||
internal interface MultiplatformModelImportingContext {
|
internal interface MultiplatformModelImportingContext: KotlinSourceSetContainer {
|
||||||
val project: Project
|
val project: Project
|
||||||
|
|
||||||
val targets: Collection<KotlinTarget>
|
val targets: Collection<KotlinTarget>
|
||||||
@@ -20,8 +19,8 @@ internal interface MultiplatformModelImportingContext {
|
|||||||
* All source sets in a project, including those that are created but not included into any compilations
|
* All source sets in a project, including those that are created but not included into any compilations
|
||||||
* (so-called "orphan" source sets). Use [isOrphanSourceSet] to get only compiled source sets
|
* (so-called "orphan" source sets). Use [isOrphanSourceSet] to get only compiled source sets
|
||||||
*/
|
*/
|
||||||
val sourceSets: Collection<KotlinSourceSetImpl>
|
val sourceSets: Collection<KotlinSourceSetImpl> get() = sourceSetsByName.values
|
||||||
val sourceSetsByNames: Map<String, KotlinSourceSetImpl>
|
override val sourceSetsByName: Map<String, KotlinSourceSetImpl>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Platforms, which are actually used in this project (i.e. platforms, for which targets has been created)
|
* Platforms, which are actually used in this project (i.e. platforms, for which targets has been created)
|
||||||
@@ -41,12 +40,12 @@ internal interface MultiplatformModelImportingContext {
|
|||||||
fun isOrphanSourceSet(sourceSet: KotlinSourceSet): Boolean = compilationsBySourceSet(sourceSet) == null
|
fun isOrphanSourceSet(sourceSet: KotlinSourceSet): Boolean = compilationsBySourceSet(sourceSet) == null
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* "Default" source-set is a source-set which is included into compilation directly, rather
|
* "Declared" source-set is a source-set which is included into compilation directly, rather
|
||||||
* through closure over dependsOn-relation.
|
* through closure over dependsOn-relation.
|
||||||
*
|
*
|
||||||
* See also KDoc for [KotlinCompilation.defaultSourceSets]
|
* See also KDoc for [KotlinCompilation.declaredSourceSets]
|
||||||
*/
|
*/
|
||||||
fun isDefaultSourceSet(sourceSet: KotlinSourceSet): Boolean
|
fun isDeclaredSourceSet(sourceSet: KotlinSourceSet): Boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun MultiplatformModelImportingContext.getProperty(property: GradleImportProperties): Boolean = project.getProperty(property)
|
internal fun MultiplatformModelImportingContext.getProperty(property: GradleImportProperties): Boolean = project.getProperty(property)
|
||||||
@@ -75,16 +74,14 @@ internal enum class GradleImportProperties(val id: String, val defaultValue: Boo
|
|||||||
|
|
||||||
internal class MultiplatformModelImportingContextImpl(override val project: Project) : MultiplatformModelImportingContext {
|
internal class MultiplatformModelImportingContextImpl(override val project: Project) : MultiplatformModelImportingContext {
|
||||||
/** see [initializeSourceSets] */
|
/** see [initializeSourceSets] */
|
||||||
override lateinit var sourceSetsByNames: Map<String, KotlinSourceSetImpl>
|
override lateinit var sourceSetsByName: Map<String, KotlinSourceSetImpl>
|
||||||
private set
|
private set
|
||||||
override val sourceSets: Collection<KotlinSourceSetImpl>
|
|
||||||
get() = sourceSetsByNames.values
|
|
||||||
|
|
||||||
/** see [initializeCompilations] */
|
/** see [initializeCompilations] */
|
||||||
override lateinit var compilations: Collection<KotlinCompilation>
|
override lateinit var compilations: Collection<KotlinCompilation>
|
||||||
private set
|
private set
|
||||||
private lateinit var sourceSetToParticipatedCompilations: Map<KotlinSourceSet, Set<KotlinCompilation>>
|
private lateinit var sourceSetToParticipatedCompilations: Map<KotlinSourceSet, Set<KotlinCompilation>>
|
||||||
private lateinit var allDefaultSourceSets: Set<KotlinSourceSet>
|
private lateinit var allDeclaredSourceSets: Set<KotlinSourceSet>
|
||||||
|
|
||||||
|
|
||||||
/** see [initializeTargets] */
|
/** see [initializeTargets] */
|
||||||
@@ -95,12 +92,13 @@ internal class MultiplatformModelImportingContextImpl(override val project: Proj
|
|||||||
private set
|
private set
|
||||||
|
|
||||||
internal fun initializeSourceSets(sourceSetsByNames: Map<String, KotlinSourceSetImpl>) {
|
internal fun initializeSourceSets(sourceSetsByNames: Map<String, KotlinSourceSetImpl>) {
|
||||||
require(!this::sourceSetsByNames.isInitialized) {
|
require(!this::sourceSetsByName.isInitialized) {
|
||||||
"Attempt to re-initialize source sets for $this. Previous value: ${this.sourceSetsByNames}"
|
"Attempt to re-initialize source sets for $this. Previous value: ${this.sourceSetsByName}"
|
||||||
}
|
}
|
||||||
this.sourceSetsByNames = sourceSetsByNames
|
this.sourceSetsByName = sourceSetsByNames
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@OptIn(ExperimentalGradleToolingApi::class)
|
||||||
internal fun initializeCompilations(compilations: Collection<KotlinCompilation>) {
|
internal fun initializeCompilations(compilations: Collection<KotlinCompilation>) {
|
||||||
require(!this::compilations.isInitialized) { "Attempt to re-initialize compilations for $this. Previous value: ${this.compilations}" }
|
require(!this::compilations.isInitialized) { "Attempt to re-initialize compilations for $this. Previous value: ${this.compilations}" }
|
||||||
this.compilations = compilations
|
this.compilations = compilations
|
||||||
@@ -111,7 +109,7 @@ internal class MultiplatformModelImportingContextImpl(override val project: Proj
|
|||||||
for (compilation in target.compilations) {
|
for (compilation in target.compilations) {
|
||||||
for (sourceSet in compilation.allSourceSets) {
|
for (sourceSet in compilation.allSourceSets) {
|
||||||
sourceSetToCompilations.getOrPut(sourceSet) { LinkedHashSet() } += compilation
|
sourceSetToCompilations.getOrPut(sourceSet) { LinkedHashSet() } += compilation
|
||||||
sourceSet.dependsOnSourceSets.mapNotNull { sourceSetByName(it) }.forEach {
|
resolveAllDependsOnSourceSets(sourceSet).forEach {
|
||||||
sourceSetToCompilations.getOrPut(it) { LinkedHashSet() } += compilation
|
sourceSetToCompilations.getOrPut(it) { LinkedHashSet() } += compilation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,7 +118,7 @@ internal class MultiplatformModelImportingContextImpl(override val project: Proj
|
|||||||
|
|
||||||
this.sourceSetToParticipatedCompilations = sourceSetToCompilations
|
this.sourceSetToParticipatedCompilations = sourceSetToCompilations
|
||||||
|
|
||||||
this.allDefaultSourceSets = compilations.flatMapTo(mutableSetOf()) { it.defaultSourceSets }
|
this.allDeclaredSourceSets = compilations.flatMapTo(mutableSetOf()) { it.declaredSourceSets }
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun initializeTargets(targets: Collection<KotlinTarget>) {
|
internal fun initializeTargets(targets: Collection<KotlinTarget>) {
|
||||||
@@ -132,10 +130,10 @@ internal class MultiplatformModelImportingContextImpl(override val project: Proj
|
|||||||
// overload for small optimization
|
// overload for small optimization
|
||||||
override fun isOrphanSourceSet(sourceSet: KotlinSourceSet): Boolean = sourceSet !in sourceSetToParticipatedCompilations.keys
|
override fun isOrphanSourceSet(sourceSet: KotlinSourceSet): Boolean = sourceSet !in sourceSetToParticipatedCompilations.keys
|
||||||
|
|
||||||
override fun isDefaultSourceSet(sourceSet: KotlinSourceSet): Boolean = sourceSet in allDefaultSourceSets
|
override fun isDeclaredSourceSet(sourceSet: KotlinSourceSet): Boolean = sourceSet in allDeclaredSourceSets
|
||||||
|
|
||||||
override fun compilationsBySourceSet(sourceSet: KotlinSourceSet): Collection<KotlinCompilation>? =
|
override fun compilationsBySourceSet(sourceSet: KotlinSourceSet): Collection<KotlinCompilation>? =
|
||||||
sourceSetToParticipatedCompilations[sourceSet]
|
sourceSetToParticipatedCompilations[sourceSet]
|
||||||
|
|
||||||
override fun sourceSetByName(name: String): KotlinSourceSet? = sourceSetsByNames[name]
|
override fun sourceSetByName(name: String): KotlinSourceSet? = sourceSetsByName[name]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.gradle
|
package org.jetbrains.kotlin.gradle
|
||||||
|
|
||||||
|
import org.gradle.api.Named
|
||||||
|
|
||||||
fun Class<*>.getMethodOrNull(name: String, vararg parameterTypes: Class<*>) =
|
fun Class<*>.getMethodOrNull(name: String, vararg parameterTypes: Class<*>) =
|
||||||
try {
|
try {
|
||||||
getMethod(name, *parameterTypes)
|
getMethod(name, *parameterTypes)
|
||||||
@@ -19,5 +21,13 @@ fun Class<*>.getDeclaredMethodOrNull(name: String, vararg parameterTypes: Class<
|
|||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ClassLoader.loadClassOrNull(name: String): Class<*>? {
|
||||||
|
return try {
|
||||||
|
loadClass(name)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun compilationFullName(simpleName: String, classifier: String?) =
|
fun compilationFullName(simpleName: String, classifier: String?) =
|
||||||
if (classifier != null) classifier + simpleName.capitalize() else simpleName
|
if (classifier != null) classifier + simpleName.capitalize() else simpleName
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* commonMain----
|
||||||
|
* / \
|
||||||
|
* jvmAndLinuxMain \
|
||||||
|
* / \ \
|
||||||
|
* jvm linuxX64 macosX64
|
||||||
|
*
|
||||||
|
* (the tests structure is the same)
|
||||||
|
*/
|
||||||
|
|
||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("multiplatform").version("{{kotlin_plugin_version}}")
|
||||||
|
}
|
||||||
|
|
||||||
|
group = "project"
|
||||||
|
version = "1.0"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvm()
|
||||||
|
linuxX64()
|
||||||
|
macosX64()
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
val commonMain by getting {
|
||||||
|
}
|
||||||
|
|
||||||
|
val commonTest by getting {
|
||||||
|
dependencies {
|
||||||
|
implementation(kotlin("test-common"))
|
||||||
|
implementation(kotlin("test-annotations-common"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmAndLinuxMain by creating {
|
||||||
|
dependsOn(commonMain)
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmAndLinuxTest by creating {
|
||||||
|
dependsOn(commonTest)
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmMain by getting {
|
||||||
|
dependsOn(jvmAndLinuxMain)
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmTest by getting {
|
||||||
|
dependsOn(jvmAndLinuxTest)
|
||||||
|
dependencies {
|
||||||
|
implementation(kotlin("test-junit"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val linuxX64Main by getting {
|
||||||
|
dependsOn(jvmAndLinuxMain)
|
||||||
|
}
|
||||||
|
|
||||||
|
val linuxX64Test by getting {
|
||||||
|
dependsOn(jvmAndLinuxTest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
kotlin.mpp.enableGranularSourceSetsMetadata=true
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
pluginManagement {
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rootProject.name = "my-app"
|
||||||
|
|
||||||
|
enableFeaturePreview("GRADLE_METADATA")
|
||||||
+71
@@ -0,0 +1,71 @@
|
|||||||
|
/**
|
||||||
|
* commonMain----
|
||||||
|
* / \
|
||||||
|
* jvmAndLinuxMain \
|
||||||
|
* / \ \
|
||||||
|
* jvm linuxX64 macosX64
|
||||||
|
*
|
||||||
|
* (the tests structure is the same)
|
||||||
|
*/
|
||||||
|
|
||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("multiplatform").version("{{kotlin_plugin_version}}")
|
||||||
|
}
|
||||||
|
|
||||||
|
group = "project"
|
||||||
|
version = "1.0"
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvm()
|
||||||
|
linuxX64()
|
||||||
|
macosX64()
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
val commonMain by getting {
|
||||||
|
}
|
||||||
|
|
||||||
|
val commonTest by getting {
|
||||||
|
dependencies {
|
||||||
|
implementation(kotlin("test-common"))
|
||||||
|
implementation(kotlin("test-annotations-common"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmAndLinuxMain by creating {
|
||||||
|
dependsOn(commonMain)
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmAndLinuxTest by creating {
|
||||||
|
dependsOn(commonTest)
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmMain by getting {
|
||||||
|
dependsOn(jvmAndLinuxMain)
|
||||||
|
}
|
||||||
|
|
||||||
|
val jvmTest by getting {
|
||||||
|
dependsOn(jvmAndLinuxTest)
|
||||||
|
dependencies {
|
||||||
|
implementation(kotlin("test-junit"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
val linuxX64Main by getting {
|
||||||
|
dependsOn(jvmAndLinuxMain)
|
||||||
|
}
|
||||||
|
|
||||||
|
val linuxX64Test by getting {
|
||||||
|
dependsOn(jvmAndLinuxTest)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
@@ -0,0 +1 @@
|
|||||||
|
kotlin.mpp.enableGranularSourceSetsMetadata=true
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
pluginManagement {
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rootProject.name = "my-app"
|
||||||
|
|
||||||
|
enableFeaturePreview("GRADLE_METADATA")
|
||||||
|
|
||||||
|
include(":submodule")
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
{{kts_kotlin_plugin_repositories}}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
kotlin("multiplatform")
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
js()
|
||||||
|
jvm()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user