Further refactor compile task configuration: extract the configurator
This commit is contained in:
+59
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* 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.plugin.mpp.pm20
|
||||
|
||||
import org.gradle.api.Project
|
||||
import org.gradle.api.tasks.TaskProvider
|
||||
import org.jetbrains.kotlin.gradle.plugin.Kotlin2JvmSourceSetProcessor
|
||||
import org.jetbrains.kotlin.gradle.plugin.KotlinNativeTargetConfigurator
|
||||
import org.jetbrains.kotlin.gradle.plugin.getKotlinPluginVersion
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.addCommonSourcesToKotlinCompileTask
|
||||
import org.jetbrains.kotlin.gradle.plugin.mpp.addSourcesToKotlinCompileTask
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
|
||||
import org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider
|
||||
import org.jetbrains.kotlin.gradle.tasks.withType
|
||||
|
||||
open class KotlinCompilationTaskConfigurator(
|
||||
private val project: Project
|
||||
) {
|
||||
private val kotlinPluginVersion =
|
||||
project.getKotlinPluginVersion() ?: error("Kotlin plugin version not found; the Kotlin plugin must be applied to $project")
|
||||
|
||||
open val variantSourcesProvider: VariantSourcesProvider = VariantSourcesProvider()
|
||||
|
||||
fun createKotlinJvmCompilationTask(
|
||||
variant: KotlinGradleVariant,
|
||||
compilationData: KotlinCompilationData<*>
|
||||
): TaskProvider<out KotlinCompile> {
|
||||
Kotlin2JvmSourceSetProcessor(KotlinTasksProvider(), compilationData, kotlinPluginVersion).run()
|
||||
val allSources = variantSourcesProvider.getSourcesFromRefinesClosure(variant)
|
||||
val commonSources = variantSourcesProvider.getCommonSourcesFromRefinesClosure(variant)
|
||||
|
||||
// FIXME support custom source file extensions in the two calls below
|
||||
addSourcesToKotlinCompileTask(project, compilationData.compileKotlinTaskName, emptyList()) { allSources }
|
||||
addCommonSourcesToKotlinCompileTask(project, compilationData.compileKotlinTaskName, emptyList()) { commonSources }
|
||||
|
||||
return project.tasks.withType<KotlinCompile>().named(compilationData.compileKotlinTaskName)
|
||||
}
|
||||
|
||||
fun createKotlinNativeCompilationTask(
|
||||
variant: KotlinNativeVariant,
|
||||
compilationData: KotlinNativeCompilationData<*>
|
||||
): TaskProvider<KotlinNativeCompile> {
|
||||
val compileTask = KotlinNativeTargetConfigurator.createKlibCompilationTask(compilationData)
|
||||
|
||||
val allSources = variantSourcesProvider.getSourcesFromRefinesClosure(variant)
|
||||
val commonSources = variantSourcesProvider.getSourcesFromRefinesClosure(variant)
|
||||
|
||||
compileTask.configure {
|
||||
it.source(allSources)
|
||||
it.commonSources.from(commonSources)
|
||||
}
|
||||
|
||||
return compileTask
|
||||
}
|
||||
}
|
||||
+1
-10
@@ -39,16 +39,7 @@ open class KotlinJvmVariantFactory(module: KotlinGradleModule) :
|
||||
override fun configureKotlinCompilation(fragment: KotlinJvmVariant) {
|
||||
val compilationData = fragment.compilationData
|
||||
LifecycleTasksManager(project).registerClassesTask(compilationData)
|
||||
|
||||
Kotlin2JvmSourceSetProcessor(KotlinTasksProvider(), compilationData, project.getKotlinPluginVersion() ?: "unknown"/*TODO*/).run()
|
||||
|
||||
val sources = VariantSourcesProvider()
|
||||
val allSources = sources.getSourcesFromRefinesClosure(fragment)
|
||||
val commonSources = sources.getCommonSourcesFromRefinesClosure(fragment)
|
||||
|
||||
// FIXME support custom source file extensions in the two calls below
|
||||
addSourcesToKotlinCompileTask(project, compilationData.compileKotlinTaskName, emptyList()) { allSources }
|
||||
addCommonSourcesToKotlinCompileTask(project, compilationData.compileKotlinTaskName, emptyList()) { commonSources }
|
||||
KotlinCompilationTaskConfigurator(project).createKotlinJvmCompilationTask(fragment, compilationData)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-13
@@ -44,17 +44,7 @@ open class KotlinNativeVariantFactory<T : KotlinNativeVariantInternal>(
|
||||
override fun configureKotlinCompilation(fragment: T) {
|
||||
val compilationData = fragment.compilationData
|
||||
LifecycleTasksManager(project).registerClassesTask(compilationData)
|
||||
|
||||
val compileTask = KotlinNativeTargetConfigurator.createKlibCompilationTask(compilationData)
|
||||
|
||||
val sources = VariantSourcesProvider()
|
||||
val allSources = sources.getSourcesFromRefinesClosure(fragment)
|
||||
val commonSources = sources.getSourcesFromRefinesClosure(fragment)
|
||||
|
||||
compileTask.configure {
|
||||
it.source(allSources)
|
||||
it.commonSources.from(commonSources)
|
||||
}
|
||||
KotlinCompilationTaskConfigurator(project).createKotlinNativeCompilationTask(fragment, compilationData)
|
||||
}
|
||||
|
||||
private fun configureHostSpecificMetadata(variant: T) {
|
||||
@@ -96,6 +86,4 @@ open class KotlinNativeVariantFactory<T : KotlinNativeVariantInternal>(
|
||||
configureApiElementsConfiguration(variant, this) // then override the Usage attribute
|
||||
attributes.attribute(Usage.USAGE_ATTRIBUTE, project.usageByName(KotlinUsages.KOTLIN_METADATA))
|
||||
}
|
||||
|
||||
open fun platformComponentName(variant: T) = variant.disambiguateName("")
|
||||
}
|
||||
+4
@@ -475,8 +475,12 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
|
||||
project.project.tasks.getByName(LifecycleBasePlugin.ASSEMBLE_TASK_NAME).apply {
|
||||
dependsOn(compileTaskProvider)
|
||||
}
|
||||
}
|
||||
val shouldAddCompileOutputsToElements = compilation.owner is KotlinGradleVariant || compilation.isMainCompilationData()
|
||||
if (shouldAddCompileOutputsToElements) {
|
||||
createRegularKlibArtifact(compilation, compileTaskProvider)
|
||||
}
|
||||
|
||||
if (compilation is AbstractKotlinNativeCompilation) {
|
||||
// FIXME: support compiler plugins for PM20
|
||||
addCompilerPlugins(compilation)
|
||||
|
||||
Reference in New Issue
Block a user