[Gradle][K2] Respect compile task's sourceFileFilter when building -Xfragment-sources

^KT-58466 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-05-04 17:47:32 +02:00
committed by Space Team
parent 82786fe6bf
commit 75d771fc39
7 changed files with 36 additions and 48 deletions
@@ -87,7 +87,7 @@ internal fun buildKotlinNativeKlibCompilerArgs(
*/ */
if (sharedCompilationData == null) { if (sharedCompilationData == null) {
add("-Xfragments=${k2MultiplatformCompilationData.fragmentsCompilerArgs.joinToString(",")}") add("-Xfragments=${k2MultiplatformCompilationData.fragmentsCompilerArgs.joinToString(",")}")
add("-Xfragment-sources=${k2MultiplatformCompilationData.fragmentSourcesCompilerArgs.joinToString(",")}") add("-Xfragment-sources=${k2MultiplatformCompilationData.fragmentSourcesCompilerArgs().joinToString(",")}")
add("-Xfragment-refines=${k2MultiplatformCompilationData.fragmentRefinesCompilerArgs.joinToString(",")}") add("-Xfragment-refines=${k2MultiplatformCompilationData.fragmentRefinesCompilerArgs.joinToString(",")}")
} }
} }
@@ -476,7 +476,7 @@ internal constructor(
sources { args -> sources { args ->
/* Shared native compilations in K2 still use -Xcommon-sources and klib dependencies */ /* Shared native compilations in K2 still use -Xcommon-sources and klib dependencies */
if (compilerOptions.usesK2.get() && sharedCompilationData == null) { if (compilerOptions.usesK2.get() && sharedCompilationData == null) {
args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs(sourceFileFilter)
} else { } else {
args.commonSources = commonSourcesTree.files.takeIf { it.isNotEmpty() }?.toPathsArray() args.commonSources = commonSourcesTree.files.takeIf { it.isNotEmpty() }?.toPathsArray()
} }
@@ -29,17 +29,18 @@ import org.jetbrains.kotlin.gradle.utils.property
import javax.inject.Inject import javax.inject.Inject
abstract class AbstractKotlinCompileTool<T : CommonToolArguments> @Inject constructor( abstract class AbstractKotlinCompileTool<T : CommonToolArguments> @Inject constructor(
objectFactory: ObjectFactory objectFactory: ObjectFactory,
) : DefaultTask(), ) : DefaultTask(),
KotlinCompileTool, KotlinCompileTool,
KotlinCompilerArgumentsProducer, KotlinCompilerArgumentsProducer,
CompilerArgumentAware<T>, CompilerArgumentAware<T>,
TaskWithLocalState { TaskWithLocalState {
private val patternFilterable = PatternSet() @Internal
protected val sourceFileFilter = PatternSet()
init { init {
patternFilterable.include( sourceFileFilter.include(
DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS.flatMap { ext -> ext.fileExtensionCasePermutations().map { "**/*.$it" } } DEFAULT_KOTLIN_SOURCE_FILES_EXTENSIONS.flatMap { ext -> ext.fileExtensionCasePermutations().map { "**/*.$it" } }
) )
} }
@@ -48,7 +49,7 @@ abstract class AbstractKotlinCompileTool<T : CommonToolArguments> @Inject constr
override val sources: FileCollection = objectFactory.fileCollection() override val sources: FileCollection = objectFactory.fileCollection()
.from( .from(
{ sourceFiles.asFileTree.matching(patternFilterable) } { sourceFiles.asFileTree.matching(sourceFileFilter) }
) )
override fun source(vararg sources: Any) { override fun source(vararg sources: Any) {
@@ -64,49 +65,49 @@ abstract class AbstractKotlinCompileTool<T : CommonToolArguments> @Inject constr
} }
@Internal @Internal
final override fun getIncludes(): MutableSet<String> = patternFilterable.includes final override fun getIncludes(): MutableSet<String> = sourceFileFilter.includes
@Internal @Internal
final override fun getExcludes(): MutableSet<String> = patternFilterable.excludes final override fun getExcludes(): MutableSet<String> = sourceFileFilter.excludes
final override fun setIncludes(includes: Iterable<String>): PatternFilterable = also { final override fun setIncludes(includes: Iterable<String>): PatternFilterable = also {
patternFilterable.setIncludes(includes) sourceFileFilter.setIncludes(includes)
} }
final override fun setExcludes(excludes: Iterable<String>): PatternFilterable = also { final override fun setExcludes(excludes: Iterable<String>): PatternFilterable = also {
patternFilterable.setExcludes(excludes) sourceFileFilter.setExcludes(excludes)
} }
final override fun include(vararg includes: String?): PatternFilterable = also { final override fun include(vararg includes: String?): PatternFilterable = also {
patternFilterable.include(*includes) sourceFileFilter.include(*includes)
} }
final override fun include(includes: Iterable<String>): PatternFilterable = also { final override fun include(includes: Iterable<String>): PatternFilterable = also {
patternFilterable.include(includes) sourceFileFilter.include(includes)
} }
final override fun include(includeSpec: Spec<FileTreeElement>): PatternFilterable = also { final override fun include(includeSpec: Spec<FileTreeElement>): PatternFilterable = also {
patternFilterable.include(includeSpec) sourceFileFilter.include(includeSpec)
} }
final override fun include(includeSpec: Closure<*>): PatternFilterable = also { final override fun include(includeSpec: Closure<*>): PatternFilterable = also {
patternFilterable.include(includeSpec) sourceFileFilter.include(includeSpec)
} }
final override fun exclude(vararg excludes: String?): PatternFilterable = also { final override fun exclude(vararg excludes: String?): PatternFilterable = also {
patternFilterable.exclude(*excludes) sourceFileFilter.exclude(*excludes)
} }
final override fun exclude(excludes: Iterable<String>): PatternFilterable = also { final override fun exclude(excludes: Iterable<String>): PatternFilterable = also {
patternFilterable.exclude(excludes) sourceFileFilter.exclude(excludes)
} }
final override fun exclude(excludeSpec: Spec<FileTreeElement>): PatternFilterable = also { final override fun exclude(excludeSpec: Spec<FileTreeElement>): PatternFilterable = also {
patternFilterable.exclude(excludeSpec) sourceFileFilter.exclude(excludeSpec)
} }
final override fun exclude(excludeSpec: Closure<*>): PatternFilterable = also { final override fun exclude(excludeSpec: Closure<*>): PatternFilterable = also {
patternFilterable.exclude(excludeSpec) sourceFileFilter.exclude(excludeSpec)
} }
@get:Internal @get:Internal
@@ -9,12 +9,10 @@ import org.gradle.api.file.FileCollection
import org.gradle.api.provider.ListProperty import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.SetProperty import org.gradle.api.provider.SetProperty
import org.gradle.api.tasks.* import org.gradle.api.tasks.*
import org.gradle.api.tasks.util.PatternFilterable
import org.gradle.work.Incremental import org.gradle.work.Incremental
import org.gradle.work.NormalizeLineEndings import org.gradle.work.NormalizeLineEndings
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi import org.jetbrains.kotlin.gradle.InternalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.KotlinCommonCompilerOptions
import org.jetbrains.kotlin.gradle.dsl.usesK2
@InternalKotlinGradlePluginApi @InternalKotlinGradlePluginApi
abstract class K2MultiplatformStructure { abstract class K2MultiplatformStructure {
@@ -24,7 +22,7 @@ abstract class K2MultiplatformStructure {
@Input @Input
val fromFragmentName: String, val fromFragmentName: String,
@Input @Input
val toFragmentName: String val toFragmentName: String,
) )
@InternalKotlinGradlePluginApi @InternalKotlinGradlePluginApi
@@ -37,7 +35,7 @@ abstract class K2MultiplatformStructure {
@get:Incremental @get:Incremental
@get:NormalizeLineEndings @get:NormalizeLineEndings
@get:PathSensitive(PathSensitivity.RELATIVE) @get:PathSensitive(PathSensitivity.RELATIVE)
val sources: FileCollection val sources: FileCollection,
) )
@get:Nested @get:Nested
@@ -50,9 +48,11 @@ abstract class K2MultiplatformStructure {
internal val K2MultiplatformStructure.fragmentsCompilerArgs: Array<String> internal val K2MultiplatformStructure.fragmentsCompilerArgs: Array<String>
get() = fragments.get().map { it.fragmentName }.toSet().toTypedArray() get() = fragments.get().map { it.fragmentName }.toSet().toTypedArray()
internal val K2MultiplatformStructure.fragmentSourcesCompilerArgs: Array<String> internal fun K2MultiplatformStructure.fragmentSourcesCompilerArgs(sourceFileFilter: PatternFilterable? = null): Array<String> =
get() = fragments.get().flatMap { sourceSet -> fragments.get().flatMap { sourceSet ->
sourceSet.sources.files.map { sourceFile -> "${sourceSet.fragmentName}:${sourceFile.absolutePath}" } sourceSet.sources
.run { if (sourceFileFilter != null) asFileTree.matching(sourceFileFilter) else this }
.files.map { sourceFile -> "${sourceSet.fragmentName}:${sourceFile.absolutePath}" }
}.toTypedArray() }.toTypedArray()
internal val K2MultiplatformStructure.fragmentRefinesCompilerArgs: Array<String> internal val K2MultiplatformStructure.fragmentRefinesCompilerArgs: Array<String>
@@ -60,20 +60,3 @@ internal val K2MultiplatformStructure.fragmentRefinesCompilerArgs: Array<String>
"${edge.fromFragmentName}:${edge.toFragmentName}" "${edge.fromFragmentName}:${edge.toFragmentName}"
}.toTypedArray() }.toTypedArray()
internal fun CommonCompilerArguments.configureK2Multiplatform(multiplatformStructure: K2MultiplatformStructure) {
fragments = multiplatformStructure.fragmentsCompilerArgs
fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs
fragmentRefines = multiplatformStructure.fragmentRefinesCompilerArgs
}
internal fun CommonCompilerArguments.configureMultiplatform(
options: KotlinCommonCompilerOptions,
k1CommonSources: FileCollection,
k2MultiplatformFragments: K2MultiplatformStructure
) {
if (options.usesK2.get()) {
configureK2Multiplatform(k2MultiplatformFragments)
} else {
commonSources = k1CommonSources.map { it.absolutePath }.toTypedArray()
}
}
@@ -197,7 +197,7 @@ abstract class Kotlin2JsCompile @Inject constructor(
} }
if (compilerOptions.usesK2.get()) { if (compilerOptions.usesK2.get()) {
args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs(sourceFileFilter)
} else { } else {
args.commonSources = commonSourceSet.asFileTree.toPathsArray() args.commonSources = commonSourceSet.asFileTree.toPathsArray()
} }
@@ -267,7 +267,7 @@ abstract class KotlinCompile @Inject constructor(
sources { args -> sources { args ->
if (compilerOptions.usesK2.get()) { if (compilerOptions.usesK2.get()) {
args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs args.fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs(sourceFileFilter)
} else { } else {
args.commonSources = commonSourceSet.asFileTree.toPathsArray() args.commonSources = commonSourceSet.asFileTree.toPathsArray()
} }
@@ -18,11 +18,9 @@ import org.jetbrains.kotlin.gradle.internal.CompilerArgumentAware
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext.Companion.lenient import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext.Companion.lenient
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType.IR import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType.IR
import org.jetbrains.kotlin.gradle.tasks.K2MultiplatformCompilationTask import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.K2MultiplatformStructure
import org.jetbrains.kotlin.gradle.tasks.K2MultiplatformStructure.Fragment import org.jetbrains.kotlin.gradle.tasks.K2MultiplatformStructure.Fragment
import org.jetbrains.kotlin.gradle.tasks.K2MultiplatformStructure.RefinesEdge import org.jetbrains.kotlin.gradle.tasks.K2MultiplatformStructure.RefinesEdge
import org.jetbrains.kotlin.gradle.tasks.configureK2Multiplatform
import org.jetbrains.kotlin.gradle.util.applyMultiplatformPlugin import org.jetbrains.kotlin.gradle.util.applyMultiplatformPlugin
import org.jetbrains.kotlin.gradle.util.buildProject import org.jetbrains.kotlin.gradle.util.buildProject
import org.jetbrains.kotlin.gradle.util.enableDefaultStdlibDependency import org.jetbrains.kotlin.gradle.util.enableDefaultStdlibDependency
@@ -159,3 +157,9 @@ private fun K2MultiplatformCompilationTask.buildCompilerArguments(): CommonCompi
this as CompilerArgumentAware<CommonCompilerArguments> this as CompilerArgumentAware<CommonCompilerArguments>
return this.createCompilerArguments(lenient) return this.createCompilerArguments(lenient)
} }
internal fun CommonCompilerArguments.configureK2Multiplatform(multiplatformStructure: K2MultiplatformStructure) {
fragments = multiplatformStructure.fragmentsCompilerArgs
fragmentSources = multiplatformStructure.fragmentSourcesCompilerArgs()
fragmentRefines = multiplatformStructure.fragmentRefinesCompilerArgs
}