Skip to content

Commit

Permalink
Revert: Remove compileDependencyFiles configuration outside of task
Browse files Browse the repository at this point in the history
This changes brakes agreement with Dokka.
It is required for the Dokka project,
compileDependencyFiles contains native dependencies,
and the commonize task is executed

#KT-66423
  • Loading branch information
nav-nav authored and qodana-bot committed Oct 24, 2024
1 parent a4804ed commit 7695e25
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.jetbrains.kotlin.gradle.report.BuildReportType
import org.jetbrains.kotlin.gradle.testbase.*
import org.jetbrains.kotlin.gradle.util.replaceText
import org.jetbrains.kotlin.test.TestMetadata
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.condition.OS
import org.junit.jupiter.api.io.TempDir
Expand Down Expand Up @@ -353,7 +352,6 @@ class ConfigurationCacheIT : AbstractConfigurationCacheIT() {
@NativeGradlePluginTests
@GradleTest
@GradleTestVersions(minVersion = TestVersions.Gradle.MAX_SUPPORTED)
@Disabled("[KT-66423](http://youtrack.jetbrains.com/issue/KT-66423): ignore test until source-value changes are made")
fun testNativeBundleDownloadForConfigurationCache(gradleVersion: GradleVersion, @TempDir konanDirTemp: Path) {
nativeProject(
"native-simple-project", gradleVersion, buildOptions = defaultBuildOptions.copy(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6831,7 +6831,6 @@ public abstract class org/jetbrains/kotlin/gradle/tasks/KotlinNativeCompile : or
public final fun getLanguageVersion ()Ljava/lang/String;
public final fun getModuleName ()Ljava/lang/String;
public fun getMultiplatformStructure ()Lorg/jetbrains/kotlin/gradle/tasks/K2MultiplatformStructure;
public final fun getNativeDistributionDependencies ()Lorg/gradle/api/provider/Provider;
public final fun getOptInAnnotationsInUse ()Ljava/util/Set;
public fun getOptimized ()Z
public fun getOutputKind ()Lorg/jetbrains/kotlin/konan/target/CompilerOutputKind;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.apple.swiftexport.internal.SwiftEx
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.swiftexport.internal.SwiftExportTaskParameters
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.swiftexport.internal.createSwiftExportedModule
import org.jetbrains.kotlin.gradle.targets.native.toolchain.KotlinNativeProvider
import org.jetbrains.kotlin.gradle.targets.native.toolchain.konanDistribution
import org.jetbrains.kotlin.gradle.utils.getFile
import org.jetbrains.kotlin.konan.target.Distribution
import javax.inject.Inject

@DisableCachingByDefault(because = "Swift Export is experimental, so no caching for now")
Expand Down Expand Up @@ -79,7 +79,7 @@ internal abstract class SwiftExportTask @Inject constructor(
workParameters.stableDeclarationsOrder.set(parameters.stableDeclarationsOrder)
workParameters.swiftModulesFile.set(parameters.swiftModulesFile)
workParameters.swiftModules.set(swiftModules)
workParameters.konanDistribution.set(kotlinNativeProvider.flatMap { it.bundleDirectory }.map { Distribution(it) })
workParameters.konanDistribution.set(kotlinNativeProvider.flatMap { it.konanDistribution })
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,22 @@ package org.jetbrains.kotlin.gradle.plugin.mpp

import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.tasks.TaskProvider
import org.jetbrains.kotlin.gradle.dsl.*
import org.jetbrains.kotlin.gradle.internal.properties.nativeProperties
import org.jetbrains.kotlin.gradle.plugin.*
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
import org.jetbrains.kotlin.gradle.plugin.mpp.compilationImpl.KotlinCompilationImpl
import org.jetbrains.kotlin.gradle.targets.native.NativeCompilerOptions
import org.jetbrains.kotlin.gradle.targets.native.internal.getNativeDistributionDependencies
import org.jetbrains.kotlin.gradle.targets.native.internal.getOriginalPlatformLibrariesFor
import org.jetbrains.kotlin.gradle.targets.native.internal.inferCommonizerTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.konan.target.KonanTarget
import org.jetbrains.kotlin.tooling.core.UnsafeApi
import javax.inject.Inject

@Suppress("TYPEALIAS_EXPANSION_DEPRECATION", "DEPRECATION")
abstract class AbstractKotlinNativeCompilation internal constructor(
compilation: KotlinCompilationImpl,
val konanTarget: KonanTarget,
val konanTarget: KonanTarget
) : DeprecatedAbstractKotlinCompilation<KotlinCommonOptions>(compilation) {

@Suppress("DEPRECATION")
Expand All @@ -55,16 +50,6 @@ abstract class AbstractKotlinNativeCompilation internal constructor(

internal val useGenericPluginArtifact: Boolean
get() = project.nativeProperties.shouldUseEmbeddableCompilerJar.get()

internal val nativeDependencies: ConfigurableFileCollection
get() = compilation.project.objects.fileCollection().from(compilation.project.getOriginalPlatformLibrariesFor(konanTarget))

@OptIn(UnsafeApi::class)
internal val nativeDistributionDependencies: ConfigurableFileCollection
get() = inferCommonizerTarget(compilation)?.let {
compilation.project.objects.fileCollection().from(compilation.project.getNativeDistributionDependencies(it))
} ?: compilation.project.objects.fileCollection()

}

open class KotlinNativeCompilation @Inject internal constructor(
Expand Down Expand Up @@ -98,7 +83,7 @@ open class KotlinNativeCompilation @Inject internal constructor(
@Suppress("DEPRECATION")
open class KotlinSharedNativeCompilation @Inject internal constructor(
val konanTargets: List<KonanTarget>,
compilation: KotlinCompilationImpl,
compilation: KotlinCompilationImpl
) : AbstractKotlinNativeCompilation(
compilation,
konanTargets.find { it.enabledOnCurrentHostForKlibCompilation(compilation.project.kotlinPropertiesProvider) } ?: konanTargets.first()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.resolvableMetadataConfiguration
import org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSet
import org.jetbrains.kotlin.gradle.plugin.sources.internal
import org.jetbrains.kotlin.gradle.targets.metadata.isNativeSourceSet
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeLink
import org.jetbrains.kotlin.gradle.tasks.withType
import org.jetbrains.kotlin.gradle.utils.filesProvider
import org.jetbrains.kotlin.gradle.utils.konanDistribution
import org.jetbrains.kotlin.konan.target.KonanTarget
Expand All @@ -37,14 +40,20 @@ internal val SetupKotlinNativePlatformDependenciesAndStdlib = KotlinProjectSetup
}
}

excludeDefaultPlatformDependenciesFromKotlinNativeCompileTasks()
launch { kotlin.excludeStdlibFromNativeSourceSetDependencies() }
}

private fun AbstractKotlinNativeCompilation.configureStdlibAndPlatformDependencies(
private suspend fun AbstractKotlinNativeCompilation.configureStdlibAndPlatformDependencies(
stdlib: FileCollection
) {
// Commonizer target must not be null for AbstractKotlinNativeCompilation, but we are graceful here and just return
val commonizerTarget = commonizerTarget.await() ?: return
val nativeDistributionDependencies = project.getNativeDistributionDependencies(commonizerTarget)

val updatedCompileDependencyFiles = project.files().from(
stdlib,
nativeDistributionDependencies,
compileDependencyFiles
)

Expand All @@ -65,6 +74,28 @@ private suspend fun KotlinMultiplatformExtension.excludeStdlibFromNativeSourceSe
}
}

/**
* Platform dependencies are added to compilation "compile files" in [configureStdlibAndPlatformDependencies]
* So user code that integrates with Kotlin Native Compilations can safely rely on that classpath.
* However, for performance optimization reasons, Kotlin Native automatically loads Platform Dependencies from its distribution.
* And because of that KGP has to explicitly filter out these platform dependencies from tasks.
*
* NB: This is not applicable for Native Shared Metadata Compilation, they will receive commonized versions of platform libs.
*/
private fun Project.excludeDefaultPlatformDependenciesFromKotlinNativeCompileTasks() {
tasks.withType<KotlinNativeLink>().configureEach { task ->
@Suppress("DEPRECATION")
val konanTarget = task.compilation.konanTarget
task.excludeOriginalPlatformLibraries = task.project.getOriginalPlatformLibrariesFor(konanTarget)
}
tasks.withType<KotlinNativeCompile>().configureEach { task ->
// metadata compilations should have commonized platform libraries in the classpath i.e. they are not "original"
if (task.isMetadataCompilation) return@configureEach
val konanTarget = task.konanTarget
task.excludeOriginalPlatformLibraries = task.project.getOriginalPlatformLibrariesFor(konanTarget)
}
}

internal val SetupKotlinNativeStdlibAndPlatformDependenciesImport = KotlinProjectSetupCoroutine {
val multiplatform = multiplatformExtensionOrNull ?: return@KotlinProjectSetupCoroutine
val sourceSets = multiplatform
Expand Down Expand Up @@ -94,7 +125,7 @@ internal fun Project.getNativeDistributionDependencies(target: CommonizerTarget)
private fun Project.getOriginalPlatformLibrariesFor(target: LeafCommonizerTarget): FileCollection =
getOriginalPlatformLibrariesFor(target.konanTarget)

internal fun Project.getOriginalPlatformLibrariesFor(konanTarget: KonanTarget): FileCollection = project.filesProvider {
private fun Project.getOriginalPlatformLibrariesFor(konanTarget: KonanTarget): FileCollection = project.filesProvider {
konanDistribution.platformLibsDir.resolve(konanTarget.name).listLibraryFiles().toSet()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.apple.useXcodeMessageStyle
import org.jetbrains.kotlin.gradle.plugin.statistics.UsesBuildFusService
import org.jetbrains.kotlin.gradle.report.UsesBuildMetricsService
import org.jetbrains.kotlin.gradle.targets.native.UsesKonanPropertiesBuildService
import org.jetbrains.kotlin.gradle.targets.native.internal.*
import org.jetbrains.kotlin.gradle.targets.native.tasks.CompilerPluginData
import org.jetbrains.kotlin.gradle.targets.native.toolchain.NoopKotlinNativeProvider
import org.jetbrains.kotlin.gradle.targets.native.toolchain.KotlinNativeProvider
Expand Down Expand Up @@ -82,32 +81,21 @@ constructor(

override val destinationDirectory: DirectoryProperty = binary.outputDirectoryProperty

@Suppress("DEPRECATION")
@get:Internal
internal val konanTarget = compilation.konanTarget

// Avoid resolving these dependencies during task graph construction when we can't build the target:
@Suppress("DEPRECATION")
@get:Internal
internal val nativeDependencies = compilation.nativeDependencies

@Suppress("DEPRECATION")
@get:Internal
internal val nativeDistributionDependencies = compilation.nativeDistributionDependencies

@get:Classpath
override val libraries: ConfigurableFileCollection = objectFactory.fileCollection().from(
{
// Avoid resolving these dependencies during task graph construction when we can't build the target:
@Suppress("DEPRECATION")
if (konanTarget.enabledOnCurrentHostForBinariesCompilation()) project.files().from(
compilation.nativeDistributionDependencies,
compilation.compileDependencyFiles
)
if (konanTarget.enabledOnCurrentHostForBinariesCompilation()) compilation.compileDependencyFiles
else objectFactory.fileCollection()
}
)

@get:InputFiles
@get:Optional
@get:PathSensitive(PathSensitivity.RELATIVE)
internal var excludeOriginalPlatformLibraries: FileCollection? = null

@get:Input
val outputKind: CompilerOutputKind by lazyConvention { binary.outputKind.compilerOutputKind }

Expand All @@ -123,6 +111,10 @@ constructor(
@get:Input
internal val binaryName: String by lazyConvention { binary.name }

@Suppress("DEPRECATION")
@get:Internal
internal val konanTarget = compilation.konanTarget

@Suppress("DEPRECATION")
@Deprecated("Use toolOptions to configure the task")
@get:Internal
Expand Down Expand Up @@ -221,7 +213,7 @@ constructor(

private val cacheBuilderSettings
get() = CacheBuilder.Settings(
konanHome = kotlinNativeProvider.flatMap { it.bundleDirectory }.map { File(it) },
konanHome = kotlinNativeProvider.flatMap { it.bundleDirectory.asFile },
konanCacheKind = project.getKonanCacheKind(konanTarget),
gradleUserHomeDir = project.gradle.gradleUserHomeDir,
konanTarget = konanTarget,
Expand Down Expand Up @@ -281,7 +273,7 @@ constructor(

dependencyClasspath { args ->
args.libraries = runSafe {
libraries.exclude(originalPlatformLibraries()).files.filterKlibsPassedToCompiler()
libraries.exclude(excludeOriginalPlatformLibraries).files.filterKlibsPassedToCompiler()
}?.toPathsArray()
args.exportedLibraries = runSafe { exportLibraries.files.filterKlibsPassedToCompiler() }?.toPathsArray()
args.friendModules = runSafe { friendModule.files.toList().takeIf { it.isNotEmpty() } }
Expand All @@ -293,8 +285,6 @@ constructor(
}
}

internal fun originalPlatformLibraries() = objectFactory.fileCollection().from(nativeDependencies)

private fun validatedExportedLibraries() {
if (exportLibrariesResolvedConfiguration == null) return

Expand Down Expand Up @@ -389,7 +379,7 @@ constructor(
message = "This property will be removed in future releases. Don't use it in your code.",
)
@get:Internal
val konanHome: Provider<String> = kotlinNativeProvider.flatMap { it.bundleDirectory }
val konanHome: Provider<String> = kotlinNativeProvider.map { it.bundleDirectory.get().asFile.absolutePath }

@get:Internal
internal abstract val kotlinCompilerArgumentsLogLevel: Property<KotlinCompilerArgumentsLogLevel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import org.gradle.work.DisableCachingByDefault
import org.gradle.work.NormalizeLineEndings
import org.jetbrains.kotlin.build.report.metrics.*
import org.jetbrains.kotlin.cli.common.arguments.*
import org.jetbrains.kotlin.commonizer.KonanDistribution
import org.jetbrains.kotlin.commonizer.platformLibsDir
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
import org.jetbrains.kotlin.compilerRunner.KotlinCompilerArgumentsLogLevel
import org.jetbrains.kotlin.compilerRunner.addBuildMetricsForTaskAction
Expand All @@ -49,8 +47,6 @@ import org.jetbrains.kotlin.gradle.plugin.statistics.UsesBuildFusService
import org.jetbrains.kotlin.gradle.report.*
import org.jetbrains.kotlin.gradle.targets.native.KonanPropertiesBuildService
import org.jetbrains.kotlin.gradle.targets.native.UsesKonanPropertiesBuildService
import org.jetbrains.kotlin.gradle.targets.native.internal.getNativeDistributionDependencies
import org.jetbrains.kotlin.gradle.targets.native.internal.inferCommonizerTarget
import org.jetbrains.kotlin.gradle.targets.native.tasks.*
import org.jetbrains.kotlin.gradle.targets.native.toolchain.NoopKotlinNativeProvider
import org.jetbrains.kotlin.gradle.targets.native.toolchain.KotlinNativeProvider
Expand All @@ -71,7 +67,6 @@ import org.jetbrains.kotlin.konan.target.buildDistribution
import org.jetbrains.kotlin.konan.util.DefFile
import org.jetbrains.kotlin.library.*
import org.jetbrains.kotlin.project.model.LanguageSettings
import org.jetbrains.kotlin.tooling.core.UnsafeApi
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
import java.io.File
import java.nio.file.Files
Expand Down Expand Up @@ -181,9 +176,6 @@ abstract class AbstractKotlinNativeCompile<
}
}

@get:Internal
internal val konanDistribution = project.nativeProperties.actualNativeHomeDirectory

@get:Internal
internal val enabledOnCurrentHostForKlibCompilationProperty: Property<Boolean> = project.objects.property<Boolean>().convention(
// For KT-66452 we need to get rid of invocation of 'Task.project'.
Expand Down Expand Up @@ -339,17 +331,6 @@ internal constructor(
else "${project.name}_${compilation.compilationName}"
}

@get:InputFiles
@get:PathSensitive(PathSensitivity.RELATIVE)
val nativeDistributionDependencies = project.provider {
when (val compilation = compilation) {
is KotlinCompilationInfo.TCS ->
@OptIn(UnsafeApi::class)
inferCommonizerTarget(compilation.compilation)
?.let { compilation.project.getNativeDistributionDependencies(it).exclude(originalPlatformLibraries()) }
}
}

@Deprecated(
message = "Please use 'compilerOptions.moduleName' to configure",
replaceWith = ReplaceWith("compilerOptions.moduleName.get()")
Expand Down Expand Up @@ -384,7 +365,7 @@ internal constructor(
message = "This property will be removed in future releases. Don't use it in your code.",
)
@get:Internal
val konanHome: Provider<String> = kotlinNativeProvider.flatMap { it.bundleDirectory }
val konanHome: Provider<String> = kotlinNativeProvider.map { it.bundleDirectory.get().asFile.absolutePath }

@get:Nested
override val multiplatformStructure: K2MultiplatformStructure = objectFactory.newInstance()
Expand Down Expand Up @@ -470,6 +451,16 @@ internal constructor(
override val klibOutput: Provider<File>
get() = outputFile

/**
* This is utility property that contains list of native platform dependencies that are present in [compileDependencyFiles]
* but should be excluded from actual classpath because they are included by default by Kotlin Native Compiler.
* this behaviour will be fixed as part of KT-65232
*/
@get:InputFiles
@get:Optional
@get:PathSensitive(PathSensitivity.RELATIVE)
internal var excludeOriginalPlatformLibraries: FileCollection? = null

@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("KTIJ-25227: Necessary override for IDEs < 2023.2", level = DeprecationLevel.ERROR)
override fun setupCompilerArgs(args: K2NativeCompilerArguments, defaultsOnly: Boolean, ignoreClasspathResolutionErrors: Boolean) {
Expand Down Expand Up @@ -524,10 +515,7 @@ internal constructor(

dependencyClasspath { args ->
args.libraries = runSafe {
//filterKlibsPassedToCompiler call exists on files
val filteredLibraries = libraries.exclude(originalPlatformLibraries()).files.filterKlibsPassedToCompiler().toMutableList()
nativeDistributionDependencies.orNull?.files?.also { filteredLibraries.addAll(it) }
filteredLibraries.toPathsArray()
libraries.exclude(excludeOriginalPlatformLibraries).files.filterKlibsPassedToCompiler().toPathsArray()
}
args.friendModules = runSafe {
friendModule.files.takeIf { it.isNotEmpty() }?.map { it.absolutePath }?.joinToString(File.pathSeparator)
Expand All @@ -554,22 +542,6 @@ internal constructor(
is KotlinCompilationInfo.TCS -> compilation.compilation is KotlinMetadataCompilation<*>
}

/**
* Retuns list of native platform dependencies that are present in [compileDependencyFiles]
* but should be excluded from actual classpath because they are included by default by Kotlin Native Compiler.
* this behaviour will be fixed as part of KT-65232
*/
internal fun originalPlatformLibraries() =
if (isMetadataCompilation) {
null
} else {
objectFactory.fileCollection()
.from(KonanDistribution(konanDistribution.get()).platformLibsDir.resolve(konanTarget.name).listLibraryFiles())
}

private fun File.listLibraryFiles(): List<File> = listFiles().orEmpty()
.filter { it.isDirectory || it.extension == "klib" }

private fun createSharedCompilationDataOrNull(): SharedCompilationData? {
if (!isMetadataCompilation) return null

Expand Down Expand Up @@ -1182,7 +1154,7 @@ abstract class CInteropProcess @Inject internal constructor(params: Params) :
message = "This property will be removed in future releases. Don't use it in your code.",
)
@get:Internal
val konanHome: Provider<String> = kotlinNativeProvider.flatMap { it.bundleDirectory }
val konanHome: Provider<String> = kotlinNativeProvider.map { it.bundleDirectory.get().asFile.absolutePath }

private val shouldUseEmbeddableCompilerJar = project.nativeProperties.shouldUseEmbeddableCompilerJar
private val actualNativeHomeDirectory = project.nativeProperties.actualNativeHomeDirectory
Expand Down
Loading

0 comments on commit 7695e25

Please sign in to comment.