From 866ab51ce584cbee94dc8dbee2d19d9eeba224ee Mon Sep 17 00:00:00 2001 From: Andrey Uskov Date: Mon, 14 Oct 2019 17:42:59 +0300 Subject: [PATCH] Work-around for importing intermediate android source sets Fixed failure on import fully actualized intermediate source sets with K2Metadata compiler arguments (Android plugin expects compiler arguments of another type) --- .../configuration/KotlinMPPGradleProjectResolver.kt | 4 ++-- .../src/KotlinMPPGradleModelBuilder.kt | 12 +++++++++++- .../org/jetbrains/kotlin/idea/facet/facetUtils.kt | 8 +++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinMPPGradleProjectResolver.kt b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinMPPGradleProjectResolver.kt index 4586338242d..3481bdd90b7 100644 --- a/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinMPPGradleProjectResolver.kt +++ b/idea/idea-gradle/src/org/jetbrains/kotlin/idea/configuration/KotlinMPPGradleProjectResolver.kt @@ -557,7 +557,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() { } val closedSourceSetGraph = Graphs.transitiveClosure(sourceSetGraph) for (sourceSet in closedSourceSetGraph.nodes()) { - val isAndroid = sourceSet.actualPlatforms.supports(KotlinPlatform.ANDROID) + val isAndroid = sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID val fromDataNode = if (isAndroid) { ideModule } else { @@ -577,7 +577,7 @@ open class KotlinMPPGradleProjectResolver : AbstractProjectResolverExtension() { sourceSetInfo.addSourceSets(dependeeSourceSets, selfName, gradleModule, resolverCtx) } } - if (sourceSet.actualPlatforms.supports(KotlinPlatform.ANDROID)) continue + if (sourceSet.actualPlatforms.platforms.singleOrNull() == KotlinPlatform.ANDROID) continue for (dependeeSourceSet in dependeeSourceSets) { val toDataNode = getSiblingKotlinModuleData(dependeeSourceSet, gradleModule, ideModule, resolverCtx) ?: continue addDependency(fromDataNode, toDataNode, dependeeSourceSet.isTestModule) diff --git a/idea/kotlin-gradle-tooling/src/KotlinMPPGradleModelBuilder.kt b/idea/kotlin-gradle-tooling/src/KotlinMPPGradleModelBuilder.kt index 67d8ff6c967..646460cd3f5 100644 --- a/idea/kotlin-gradle-tooling/src/KotlinMPPGradleModelBuilder.kt +++ b/idea/kotlin-gradle-tooling/src/KotlinMPPGradleModelBuilder.kt @@ -332,7 +332,12 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService { val gradleCompilations = getCompilations(gradleTarget) ?: return null val compilations = gradleCompilations.mapNotNull { - buildCompilation(it, disambiguationClassifier, sourceSetMap, dependencyResolver, project, dependencyMapper) + val compilation = buildCompilation(it, disambiguationClassifier, sourceSetMap, dependencyResolver, project, dependencyMapper) + if (compilation == null || platform != KotlinPlatform.ANDROID) { + compilation + } else { + compilation.addDependsOnSourceSetsToCompilation(sourceSetMap) + } } val jar = buildTargetJar(gradleTarget, project) val testTasks = buildTestTasks(project, gradleTarget) @@ -354,6 +359,11 @@ class KotlinMPPGradleModelBuilder : ModelBuilderService { return target } + private fun KotlinCompilationImpl.addDependsOnSourceSetsToCompilation(sourceSetMap: Map): KotlinCompilationImpl { + val closedSourceSets = this.sourceSets.union(this.sourceSets.flatMap { it.dependsOnSourceSets }.mapNotNull { sourceSetMap[it] }) + return KotlinCompilationImpl(this.name, closedSourceSets, this.dependencies, this.output, this.arguments, this.dependencyClasspath, this.kotlinTaskProperties, this.nativeExtensions) + } + private fun buildTestTasks(project: Project, gradleTarget: Named): Collection { val getTestRunsMethod = gradleTarget.javaClass.getMethodOrNull("getTestRuns") if (getTestRunsMethod != null) { diff --git a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt index acb4fd73b8d..97cf9f349c4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/facet/facetUtils.kt @@ -337,7 +337,13 @@ fun applyCompilerArgumentsToFacet( val oldPluginOptions = compilerArguments.pluginOptions val emptyArgs = compilerArguments::class.java.newInstance() - copyBeanTo(arguments, compilerArguments) { property, value -> value != property.get(emptyArgs) } + + // Ad-hoc work-around for android compilations: middle source sets could be actualized up to + // Android target, meanwhile compiler arguments are of type K2Metadata + // TODO(auskov): merge classpath once compiler arguments are removed from KotlinFacetSettings + if (arguments.javaClass == compilerArguments.javaClass) { + copyBeanTo(arguments, compilerArguments) { property, value -> value != property.get(emptyArgs) } + } compilerArguments.pluginOptions = joinPluginOptions(oldPluginOptions, arguments.pluginOptions) compilerArguments.convertPathsToSystemIndependent()