[Gradle] CInterop Task input should be modifiable through Cinterop DSL

For configuration cache compatibility reasons Project object
was removed from DefaultCInteropSettings and instead
related factory services were used.

^KT-53704 Verification Pending
This commit is contained in:
Anton Lakotka
2022-08-29 14:29:55 +02:00
committed by Space
parent 45581c11fa
commit 073d75179d
3 changed files with 47 additions and 31 deletions
@@ -7,10 +7,13 @@
package org.jetbrains.kotlin.gradle.plugin.mpp
import org.gradle.api.Action
import org.gradle.api.Project
import org.gradle.api.file.FileCollection
import org.gradle.api.file.ProjectLayout
import org.gradle.api.internal.file.FileOperations
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.ProviderFactory
import org.jetbrains.kotlin.gradle.plugin.CInteropSettings
import org.jetbrains.kotlin.gradle.plugin.CInteropSettings.IncludeDirectories
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation
@@ -23,23 +26,29 @@ import java.io.File
import javax.inject.Inject
abstract class DefaultCInteropSettings @Inject constructor(
private val project: Project,
private val name: String,
override val compilation: KotlinNativeCompilationData<*>
) : CInteropSettings {
@Transient
override val compilation: KotlinNativeCompilationData<*>,
private val providerFactory: ProviderFactory,
private val objectFactory: ObjectFactory,
private val projectLayout: ProjectLayout,
private val fileOperations: FileOperations,
) : CInteropSettings {
private fun files() = objectFactory.fileCollection()
private fun files(vararg paths: Any) = objectFactory.fileCollection().from(*paths)
inner class DefaultIncludeDirectories : IncludeDirectories {
var allHeadersDirs: FileCollection = project.files()
var headerFilterDirs: FileCollection = project.files()
var allHeadersDirs: FileCollection = files()
var headerFilterDirs: FileCollection = files()
override fun allHeaders(vararg includeDirs: Any) = allHeaders(includeDirs.toList())
override fun allHeaders(includeDirs: Collection<Any>) {
allHeadersDirs += project.files(*includeDirs.toTypedArray())
allHeadersDirs += files(*includeDirs.toTypedArray())
}
override fun headerFilterOnly(vararg includeDirs: Any) = headerFilterOnly(includeDirs.toList())
override fun headerFilterOnly(includeDirs: Collection<Any>) {
headerFilterDirs += project.files(*includeDirs.toTypedArray())
headerFilterDirs += files(*includeDirs.toTypedArray())
}
}
@@ -54,7 +63,7 @@ abstract class DefaultCInteropSettings @Inject constructor(
override val dependencyConfigurationName: String
get() = compilation.disambiguateName("${name.capitalize()}CInterop")
override var dependencyFiles: FileCollection = project.files()
override var dependencyFiles: FileCollection = files()
val interopProcessingTaskName: String
get() = lowerCamelCaseName(
@@ -64,8 +73,8 @@ abstract class DefaultCInteropSettings @Inject constructor(
target?.disambiguationClassifier ?: compilation.compilationClassifier
)
val defFileProperty: Property<File> = project.objects.property(File::class.java)
.apply { set(project.projectDir.resolve("src/nativeInterop/cinterop/$name.def")) }
val defFileProperty: Property<File> = objectFactory.property(File::class.java)
.apply { set(projectLayout.projectDirectory.asFile.resolve("src/nativeInterop/cinterop/$name.def")) }
var defFile: File
get() = defFileProperty.get()
@@ -79,26 +88,26 @@ abstract class DefaultCInteropSettings @Inject constructor(
_packageNameProp.set(value)
}
internal val _packageNameProp: Property<String> = project.objects.property(String::class.java)
internal val _packageNameProp: Property<String> = objectFactory.property(String::class.java)
val compilerOpts = mutableListOf<String>()
val linkerOpts = mutableListOf<String>()
var extraOpts: List<String>
get() = _extraOptsProp.get()
set(value) {
_extraOptsProp = project.objects.listProperty(String::class.java)
_extraOptsProp = objectFactory.listProperty(String::class.java)
extraOpts(value)
}
internal var _extraOptsProp: ListProperty<String> = project.objects.listProperty(String::class.java)
internal var _extraOptsProp: ListProperty<String> = objectFactory.listProperty(String::class.java)
val includeDirs = DefaultIncludeDirectories()
var headers: FileCollection = project.files()
var headers: FileCollection = files()
// DSL methods.
override fun defFile(file: Any) {
defFileProperty.set(project.file(file))
defFileProperty.set(fileOperations.file(file))
}
override fun packageName(value: String) {
@@ -106,7 +115,7 @@ abstract class DefaultCInteropSettings @Inject constructor(
}
override fun header(file: Any) = headers(file)
override fun headers(vararg files: Any) = headers(project.files(files))
override fun headers(vararg files: Any) = headers(files(files))
override fun headers(files: FileCollection) {
headers += files
}
@@ -127,7 +136,7 @@ abstract class DefaultCInteropSettings @Inject constructor(
override fun extraOpts(vararg values: Any) = extraOpts(values.toList())
override fun extraOpts(values: List<Any>) {
_extraOptsProp.addAll(project.provider { values.map { it.toString() } })
_extraOptsProp.addAll(providerFactory.provider { values.map { it.toString() } })
}
}
@@ -96,7 +96,7 @@ abstract class KotlinNativeCompilation @Inject constructor(
// Interop DSL.
val cinterops = project.container(DefaultCInteropSettings::class.java) { cinteropName ->
project.objects.newInstance(DefaultCInteropSettings::class.java, project, cinteropName, this)
project.objects.newInstance(DefaultCInteropSettings::class.java, cinteropName, this)
}
fun cinterops(action: Action<NamedDomainObjectContainer<DefaultCInteropSettings>>) = action.execute(cinterops)
@@ -61,6 +61,7 @@ import org.jetbrains.kotlin.library.*
import org.jetbrains.kotlin.project.model.LanguageSettings
import java.io.File
import java.nio.file.Files
import java.nio.file.Path
import javax.inject.Inject
import org.jetbrains.kotlin.konan.file.File as KFile
import org.jetbrains.kotlin.utils.ResolvedDependencies as KResolvedDependencies
@@ -112,8 +113,13 @@ private fun File.providedByCompiler(project: Project): Boolean =
// We need to filter out interop duplicates because we create copy of them for IDE.
// TODO: Remove this after interop rework.
private fun FileCollection.filterOutPublishableInteropLibs(project: Project): FileCollection {
val libDirectories = project.rootProject.allprojects.map { it.buildDir.resolve("libs").absoluteFile.toPath() }
private fun FileCollection.filterOutPublishableInteropLibs(project: Project): FileCollection =
filterOutPublishableInteropLibs(project.buildLibDirectories())
private fun Project.buildLibDirectories(): List<Path> =
rootProject.allprojects.map { it.buildDir.resolve("libs").absoluteFile.toPath() }
private fun FileCollection.filterOutPublishableInteropLibs(libDirectories: List<Path>): FileCollection {
return filter { file ->
!(file.name.contains("-cinterop-") && libDirectories.any { file.toPath().startsWith(it) })
}
@@ -1148,7 +1154,6 @@ internal class CacheBuilder(
@CacheableTask
open class CInteropProcess
@Inject constructor(
@Transient
@Internal
val settings: DefaultCInteropSettings,
private val objectFactory: ObjectFactory,
@@ -1196,36 +1201,38 @@ open class CInteropProcess
@get:InputFile
@get:PathSensitive(PathSensitivity.RELATIVE)
val defFile: File = settings.defFileProperty.get()
val defFile: File get() = settings.defFileProperty.get()
@get:Optional
@get:Input
val packageName: String? = settings.packageName
val packageName: String? get() = settings.packageName
@get:Input
val compilerOpts: List<String> = settings.compilerOpts
val compilerOpts: List<String> get() = settings.compilerOpts
@get:Input
val linkerOpts: List<String> = settings.linkerOpts
val linkerOpts: List<String> get() = settings.linkerOpts
@get:IgnoreEmptyDirectories
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
val headers: FileCollection = settings.headers
val headers: FileCollection get() = settings.headers
@get:Input
val allHeadersDirs: Set<File> = settings.includeDirs.allHeadersDirs.files
val allHeadersDirs: Set<File> get() = settings.includeDirs.allHeadersDirs.files
@get:Input
val headerFilterDirs: Set<File> = settings.includeDirs.headerFilterDirs.files
val headerFilterDirs: Set<File> get() = settings.includeDirs.headerFilterDirs.files
private val libDirectories = project.buildLibDirectories()
@get:IgnoreEmptyDirectories
@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
val libraries: FileCollection = settings.dependencyFiles.filterOutPublishableInteropLibs(project)
val libraries: FileCollection get() = settings.dependencyFiles.filterOutPublishableInteropLibs(libDirectories)
@get:Input
val extraOpts: List<String> = settings.extraOpts
val extraOpts: List<String> get() = settings.extraOpts
private val isInIdeaSync = project.isInIdeaSync