[Gradle, K/N] Prefer injection over lateinit vars for task configuration
#KT-45801 Fixed
This commit is contained in:
+5
@@ -15,6 +15,11 @@ repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
// KT-45801
|
||||
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile).configureEach {
|
||||
kotlinOptions
|
||||
}
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
|
||||
+5
@@ -7,6 +7,11 @@ repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
// KT-45801
|
||||
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile>().configureEach {
|
||||
kotlinOptions
|
||||
}
|
||||
|
||||
kotlin {
|
||||
sourceSets["commonMain"].apply {
|
||||
dependencies {
|
||||
|
||||
-1
@@ -56,7 +56,6 @@ abstract class AbstractKotlinNativeCompilation(
|
||||
override val compileKotlinTask: KotlinNativeCompile
|
||||
get() = super.compileKotlinTask as KotlinNativeCompile
|
||||
|
||||
// A collection containing all source sets used by this compilation
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
override val compileKotlinTaskProvider: TaskProvider<out KotlinNativeCompile>
|
||||
get() = super.compileKotlinTaskProvider as TaskProvider<out KotlinNativeCompile>
|
||||
|
||||
+2
-4
@@ -130,10 +130,9 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
|
||||
// region Task creation.
|
||||
private fun Project.createLinkTask(binary: NativeBinary) {
|
||||
val result = registerTask<KotlinNativeLink>(
|
||||
binary.linkTaskName
|
||||
binary.linkTaskName, listOf(binary)
|
||||
) {
|
||||
val target = binary.target
|
||||
it.binary = binary
|
||||
it.group = BasePlugin.BUILD_GROUP
|
||||
it.description = "Links ${binary.outputKind.description} '${binary.name}' for a target '${target.name}'."
|
||||
it.enabled = binary.konanTarget.enabledOnCurrentHost
|
||||
@@ -261,9 +260,8 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget>(
|
||||
|
||||
internal fun Project.createKlibCompilationTask(compilation: AbstractKotlinNativeCompilation): TaskProvider<KotlinNativeCompile> {
|
||||
val compileTaskProvider = registerTask<KotlinNativeCompile>(
|
||||
compilation.compileKotlinTaskName
|
||||
compilation.compileKotlinTaskName, listOf(compilation)
|
||||
) {
|
||||
it.compilation.set(compilation)
|
||||
it.group = BasePlugin.BUILD_GROUP
|
||||
it.description = "Compiles a klibrary from the '${compilation.name}' " +
|
||||
"compilation for target '${compilation.platformType.name}'."
|
||||
|
||||
+3
-3
@@ -70,15 +70,15 @@ open class NativePerformanceReport : DefaultTask() {
|
||||
}
|
||||
|
||||
private fun getPerformanceCompilerOptions() =
|
||||
(compilerFlagsFromBinary() + binary.linkTask.compilation.get().kotlinOptions.freeCompilerArgs)
|
||||
(compilerFlagsFromBinary() + binary.linkTask.compilation.kotlinOptions.freeCompilerArgs)
|
||||
.filter { it in listOf("-g", "-opt", "-Xg0") }.map { "\"$it\"" }
|
||||
|
||||
@TaskAction
|
||||
fun generate() {
|
||||
val compileTasks = if (settings.includeAssociatedTasks)
|
||||
getAllExecutedTasks(binary.linkTask.compilation.get())
|
||||
getAllExecutedTasks(binary.linkTask.compilation)
|
||||
else
|
||||
listOf(binary.linkTask.compilation.get().compileKotlinTask)
|
||||
listOf(binary.linkTask.compilation.compileKotlinTask)
|
||||
val allExecutedTasks = listOf(binary.linkTask) + compileTasks
|
||||
val upToDateTasks = allExecutedTasks.filter { it.state.upToDate }.map { it.name }
|
||||
if (upToDateTasks.isNotEmpty()) {
|
||||
|
||||
+50
-54
@@ -14,7 +14,6 @@ import org.gradle.api.file.ConfigurableFileCollection
|
||||
import org.gradle.api.file.FileCollection
|
||||
import org.gradle.api.file.FileTree
|
||||
import org.gradle.api.logging.Logger
|
||||
import org.gradle.api.provider.Property
|
||||
import org.gradle.api.provider.Provider
|
||||
import org.gradle.api.tasks.*
|
||||
import org.gradle.api.tasks.compile.AbstractCompile
|
||||
@@ -31,7 +30,6 @@ import org.jetbrains.kotlin.gradle.plugin.sources.DefaultLanguageSettingsBuilder
|
||||
import org.jetbrains.kotlin.gradle.targets.native.internal.isAllowCommonizer
|
||||
import org.jetbrains.kotlin.gradle.utils.getValue
|
||||
import org.jetbrains.kotlin.gradle.utils.klibModuleName
|
||||
import org.jetbrains.kotlin.gradle.utils.newProperty
|
||||
import org.jetbrains.kotlin.gradle.utils.listFilesOrEmpty
|
||||
import org.jetbrains.kotlin.konan.library.KLIB_INTEROP_IR_PROVIDER_IDENTIFIER
|
||||
import org.jetbrains.kotlin.konan.properties.resolvablePropertyList
|
||||
@@ -117,12 +115,7 @@ private fun Collection<File>.filterKlibsPassedToCompiler(project: Project) = fil
|
||||
// endregion
|
||||
abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : AbstractKotlinNativeCompilation> : AbstractCompile() {
|
||||
@get:Internal
|
||||
abstract val compilation: Provider<K>
|
||||
|
||||
@get:Internal
|
||||
internal val compilationIsShared by lazy {
|
||||
compilation.get() is KotlinSharedNativeCompilation
|
||||
}
|
||||
abstract val compilation: K
|
||||
|
||||
// region inputs/outputs
|
||||
@get:Input
|
||||
@@ -138,11 +131,15 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
abstract val baseName: String
|
||||
|
||||
@get:Internal
|
||||
internal val objects = project.objects
|
||||
protected val objects = project.objects
|
||||
|
||||
@get:Internal
|
||||
internal val konanTarget by lazy {
|
||||
compilation.get().konanTarget
|
||||
protected val konanTarget by project.provider {
|
||||
compilation.konanTarget
|
||||
}
|
||||
|
||||
private val isSharedCompilation by project.provider {
|
||||
compilation is KotlinSharedNativeCompilation
|
||||
}
|
||||
|
||||
// Inputs and outputs
|
||||
@@ -157,7 +154,7 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
val libraries: FileCollection by project.provider {
|
||||
// Avoid resolving these dependencies during task graph construction when we can't build the target:
|
||||
if (konanTarget.enabledOnCurrentHost)
|
||||
compilation.get().compileDependencyFiles.filterOutPublishableInteropLibs(project)
|
||||
compilation.compileDependencyFiles.filterOutPublishableInteropLibs(project)
|
||||
else objects.fileCollection()
|
||||
}
|
||||
|
||||
@@ -168,7 +165,7 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
}
|
||||
|
||||
@get:Input
|
||||
val target: String by project.provider { compilation.get().konanTarget.name }
|
||||
val target: String by project.provider { compilation.konanTarget.name }
|
||||
|
||||
// region Compiler options.
|
||||
@get:Internal
|
||||
@@ -180,8 +177,8 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
abstract val additionalCompilerOptions: Provider<Collection<String>>
|
||||
|
||||
@get:Internal
|
||||
val languageSettings: LanguageSettingsBuilder by lazy {
|
||||
compilation.get().defaultSourceSet.languageSettings
|
||||
val languageSettings: LanguageSettingsBuilder by project.provider {
|
||||
compilation.defaultSourceSet.languageSettings
|
||||
}
|
||||
|
||||
@get:Input
|
||||
@@ -190,7 +187,7 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
// endregion.
|
||||
|
||||
@get:Input
|
||||
val enableEndorsedLibs: Boolean by project.provider { compilation.map { it.enableEndorsedLibs }.get() }
|
||||
val enableEndorsedLibs: Boolean by project.provider { compilation.enableEndorsedLibs }
|
||||
|
||||
@get:Input
|
||||
val kotlinNativeVersion: String
|
||||
@@ -235,9 +232,8 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
val defaultSerializedCompilerArguments: List<String>
|
||||
get() = buildCommonArgs(true)
|
||||
|
||||
@get:Internal
|
||||
internal val languageSettingsBuilder by lazy {
|
||||
compilation.get().defaultSourceSet.languageSettings
|
||||
private val languageSettingsBuilder by project.provider {
|
||||
compilation.defaultSourceSet.languageSettings
|
||||
}
|
||||
|
||||
// Args used by both the compiler and IDEA.
|
||||
@@ -299,7 +295,7 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
addArg("-target", konanTarget.name)
|
||||
addArg("-p", outputKind.name.toLowerCase())
|
||||
|
||||
if (compilationIsShared) {
|
||||
if (isSharedCompilation) {
|
||||
add("-Xexpect-actual-linker")
|
||||
add("-Xmetadata-klib")
|
||||
addArg("-manifest", manifestFile.get().absolutePath)
|
||||
@@ -326,7 +322,7 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
val output = outputFile.get()
|
||||
output.parentFile.mkdirs()
|
||||
|
||||
if (compilationIsShared) {
|
||||
if (isSharedCompilation) {
|
||||
val manifestFile: File = manifestFile.get()
|
||||
manifestFile.ensureParentDirsCreated()
|
||||
val properties = java.util.Properties()
|
||||
@@ -342,12 +338,14 @@ abstract class AbstractKotlinNativeCompile<T : KotlinCommonToolOptions, K : Abst
|
||||
* A task producing a klibrary from a compilation.
|
||||
*/
|
||||
@CacheableTask
|
||||
open class KotlinNativeCompile : AbstractKotlinNativeCompile<KotlinCommonOptions, AbstractKotlinNativeCompilation>(),
|
||||
KotlinCompile<KotlinCommonOptions> {
|
||||
open class KotlinNativeCompile
|
||||
@Inject
|
||||
constructor(
|
||||
@Internal
|
||||
@Transient // can't be serialized for Gradle configuration avoidance
|
||||
final override val compilation: Property<AbstractKotlinNativeCompilation> =
|
||||
project.newProperty()
|
||||
@Transient // can't be serialized for Gradle configuration cache
|
||||
final override val compilation: AbstractKotlinNativeCompilation
|
||||
) : AbstractKotlinNativeCompile<KotlinCommonOptions, AbstractKotlinNativeCompilation>(),
|
||||
KotlinCompile<KotlinCommonOptions> {
|
||||
|
||||
@get:Input
|
||||
override val outputKind = LIBRARY
|
||||
@@ -359,8 +357,7 @@ open class KotlinNativeCompile : AbstractKotlinNativeCompile<KotlinCommonOptions
|
||||
override val debuggable = true
|
||||
|
||||
@get:Internal
|
||||
override val baseName: String by
|
||||
compilation.map { if (it.isMain()) project.name else "${project.name}_${it.name}" }
|
||||
override val baseName: String by project.provider { if (compilation.isMain()) project.name else "${project.name}_${compilation.name}" }
|
||||
|
||||
// Store as an explicit provider in order to allow Gradle Instant Execution to capture the state
|
||||
// private val allSourceProvider = compilation.map { project.files(it.allSources).asFileTree }
|
||||
@@ -392,10 +389,10 @@ open class KotlinNativeCompile : AbstractKotlinNativeCompile<KotlinCommonOptions
|
||||
get() = commonSources.asFileTree
|
||||
|
||||
|
||||
private val friendModule: FileCollection by compilation.map { compilationInstance ->
|
||||
private val friendModule: FileCollection by project.provider {
|
||||
project.files(
|
||||
compilationInstance.associateWithTransitiveClosure.map { it.output.allOutputs },
|
||||
compilationInstance.friendArtifacts
|
||||
compilation.associateWithTransitiveClosure.map { it.output.allOutputs },
|
||||
compilation.friendArtifacts
|
||||
)
|
||||
}
|
||||
// endregion.
|
||||
@@ -415,8 +412,8 @@ open class KotlinNativeCompile : AbstractKotlinNativeCompile<KotlinCommonOptions
|
||||
// endregion.
|
||||
|
||||
// region Kotlin options.
|
||||
override val kotlinOptions: KotlinCommonOptions by lazy {
|
||||
compilation.get().kotlinOptions
|
||||
override val kotlinOptions: KotlinCommonOptions by project.provider {
|
||||
compilation.kotlinOptions
|
||||
}
|
||||
|
||||
@get:Input
|
||||
@@ -474,31 +471,30 @@ open class KotlinNativeCompile : AbstractKotlinNativeCompile<KotlinCommonOptions
|
||||
* A task producing a final binary from a compilation.
|
||||
*/
|
||||
@CacheableTask
|
||||
open class KotlinNativeLink : AbstractKotlinNativeCompile<KotlinCommonToolOptions, KotlinNativeCompilation>() {
|
||||
|
||||
open class KotlinNativeLink
|
||||
@Inject
|
||||
constructor(
|
||||
@Internal
|
||||
val binary: NativeBinary
|
||||
) : AbstractKotlinNativeCompile<KotlinCommonToolOptions, KotlinNativeCompilation>() {
|
||||
@get:Internal
|
||||
@Transient // can't be serialized for Gradle configuration avoidance
|
||||
final override val compilation: Property<KotlinNativeCompilation> = project.newProperty() { binary.compilation }
|
||||
final override val compilation: KotlinNativeCompilation
|
||||
get() = binary.compilation
|
||||
|
||||
init {
|
||||
dependsOn(project.provider { compilation.get().compileKotlinTask })
|
||||
dependsOn(project.provider { compilation.compileKotlinTaskProvider })
|
||||
// Frameworks actively uses symlinks.
|
||||
// Gradle build cache transforms symlinks into regular files https://guides.gradle.org/using-build-cache/#symbolic_links
|
||||
outputs.cacheIf { outputKind != FRAMEWORK }
|
||||
}
|
||||
|
||||
@Internal
|
||||
lateinit var binary: NativeBinary
|
||||
|
||||
@Internal // Taken into account by getSources().
|
||||
val intermediateLibrary: Provider<File> = compilation.map { compilationInstance ->
|
||||
compilationInstance.compileKotlinTask.outputFile.get()
|
||||
}
|
||||
val intermediateLibrary: Provider<File> = project.provider { compilation.compileKotlinTask.outputFile.get() }
|
||||
|
||||
@InputFiles
|
||||
@SkipWhenEmpty
|
||||
override fun getSource(): FileTree =
|
||||
objects.fileCollection().from(intermediateLibrary.get()).asFileTree
|
||||
objects.fileCollection().from(intermediateLibrary).asFileTree
|
||||
|
||||
@OutputDirectory
|
||||
override fun getDestinationDir(): File {
|
||||
@@ -522,7 +518,7 @@ open class KotlinNativeLink : AbstractKotlinNativeCompile<KotlinCommonToolOption
|
||||
@Input get() = binary.baseName
|
||||
|
||||
@get:Input
|
||||
protected val konanCacheKind: NativeCacheKind by lazy {
|
||||
protected val konanCacheKind: NativeCacheKind by project.provider {
|
||||
project.getKonanCacheKind(konanTarget)
|
||||
}
|
||||
|
||||
@@ -535,8 +531,8 @@ open class KotlinNativeLink : AbstractKotlinNativeCompile<KotlinCommonToolOption
|
||||
|
||||
// We propagate compilation free args to the link task for now (see KT-33717).
|
||||
@get:Input
|
||||
override val additionalCompilerOptions: Provider<Collection<String>> = compilation.map { compilationInstance ->
|
||||
kotlinOptions.freeCompilerArgs + compilationInstance.kotlinOptions.freeCompilerArgs
|
||||
override val additionalCompilerOptions: Provider<Collection<String>> = project.provider {
|
||||
kotlinOptions.freeCompilerArgs + compilation.kotlinOptions.freeCompilerArgs
|
||||
}
|
||||
|
||||
override val kotlinOptions: KotlinCommonToolOptions = NativeLinkOptions()
|
||||
@@ -563,7 +559,7 @@ open class KotlinNativeLink : AbstractKotlinNativeCompile<KotlinCommonToolOption
|
||||
@Input get() = binary is TestExecutable
|
||||
|
||||
@get:Classpath
|
||||
val exportLibraries: FileCollection by lazy {
|
||||
val exportLibraries: FileCollection by project.provider {
|
||||
binary.let {
|
||||
if (it is AbstractNativeLibrary) {
|
||||
project.configurations.getByName(it.exportConfigurationName)
|
||||
@@ -620,8 +616,9 @@ open class KotlinNativeLink : AbstractKotlinNativeCompile<KotlinCommonToolOption
|
||||
listOf("-Xinclude=${intermediateLibrary.get().absolutePath}")
|
||||
|
||||
@get:Internal
|
||||
val apiFilesProvider =
|
||||
compilation.map { project.configurations.getByName(it.apiConfigurationName).files.filterKlibsPassedToCompiler(project) }
|
||||
val apiFilesProvider = project.provider {
|
||||
project.configurations.getByName(compilation.apiConfigurationName).files.filterKlibsPassedToCompiler(project)
|
||||
}
|
||||
|
||||
private fun validatedExportedLibraries() {
|
||||
val exportConfiguration = exportLibraries as? Configuration ?: return
|
||||
@@ -689,7 +686,7 @@ internal class CacheBuilder(val project: Project, val binary: NativeBinary, val
|
||||
private val target: String
|
||||
get() = konanTarget.name
|
||||
|
||||
private val rootCacheDirectory by lazy {
|
||||
private val rootCacheDirectory by project.provider {
|
||||
getRootCacheDirectory(File(project.konanHome), konanTarget, debuggable, konanCacheKind)
|
||||
}
|
||||
|
||||
@@ -957,8 +954,7 @@ open class CInteropProcess @Inject constructor(@get:Internal val settings: Defau
|
||||
// Inputs and outputs.
|
||||
|
||||
@OutputFile
|
||||
val outputFileProvider: Provider<File> =
|
||||
project.provider { destinationDir.get().resolve(outputFileName) }
|
||||
val outputFileProvider: Provider<File> = project.provider { destinationDir.get().resolve(outputFileName) }
|
||||
|
||||
@get:InputFile
|
||||
@get:PathSensitive(PathSensitivity.RELATIVE)
|
||||
|
||||
Reference in New Issue
Block a user