Skip to content

Commit

Permalink
[Gradle] Made klib unpacked for native metadata compile task
Browse files Browse the repository at this point in the history
^KT-63363 Fixed
  • Loading branch information
dkrasnoff authored and qodana-bot committed Dec 20, 2023
1 parent 262697d commit b7b0397
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ open class CommonizerHierarchicalIT : KGPBaseTest() {
nativeProject("commonizeHierarchically", gradleVersion) {
if (HostManager.hostIsMac) {
build(":p1:compileIosMainKotlinMetadata") {
assertFileInProjectExists("p1/build/classes/kotlin/metadata/iosMain/klib/p1_iosMain.klib")
assertDirectoryInProjectExists("p1/build/classes/kotlin/metadata/iosMain/klib/p1_iosMain")
assertNoDuplicateLibraryWarning()
}

build(":p1:compileAppleMainKotlinMetadata") {
assertFileInProjectExists("p1/build/classes/kotlin/metadata/appleMain/klib/p1_appleMain.klib")
assertDirectoryInProjectExists("p1/build/classes/kotlin/metadata/appleMain/klib/p1_appleMain")
assertNoDuplicateLibraryWarning()
}
}

build(":p1:compileLinuxMainKotlinMetadata") {
assertFileInProjectExists("p1/build/classes/kotlin/metadata/linuxMain/klib/p1_linuxMain.klib")
assertDirectoryInProjectExists("p1/build/classes/kotlin/metadata/linuxMain/klib/p1_linuxMain")
assertNoDuplicateLibraryWarning()
}

build(":p1:compileAppleAndLinuxMainKotlinMetadata") {
assertFileInProjectExists("p1/build/classes/kotlin/metadata/appleAndLinuxMain/klib/p1_appleAndLinuxMain.klib")
assertDirectoryInProjectExists("p1/build/classes/kotlin/metadata/appleAndLinuxMain/klib/p1_appleAndLinuxMain")
assertNoDuplicateLibraryWarning()
}

build(":p1:compileNativeMainKotlinMetadata") {
assertFileInProjectExists("p1/build/classes/kotlin/metadata/nativeMain/klib/p1_nativeMain.klib")
assertDirectoryInProjectExists("p1/build/classes/kotlin/metadata/nativeMain/klib/p1_nativeMain")
assertNoDuplicateLibraryWarning()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ class ConfigurationCacheIT : AbstractConfigurationCacheIT() {
}
}

@MppGradlePluginTests
@DisplayName("KT-63363: all metadata jar works well with configuration cache")
@GradleTestVersions(
minVersion = TestVersions.Gradle.G_7_4,
additionalVersions = [TestVersions.Gradle.G_7_6],
)
@GradleTest
fun testAllMetadataJarWithConfigurationCache(gradleVersion: GradleVersion) {
project("new-mpp-lib-and-app/sample-lib", gradleVersion) {
testConfigurationCacheOf(":allMetadataJar")
}
}

@NativeGradlePluginTests
@DisplayName("works with commonizer")
@GradleTestVersions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ open class HierarchicalMppIT : KGPBaseTest() {
) {
build("assemble") {
assertFileExists(projectPath.resolve("build/libs/test-project-jvm.jar"))
assertFileExists(projectPath.resolve("build/classes/kotlin/metadata/nativeMain/klib/test-project_nativeMain.klib"))
assertDirectoryExists(projectPath.resolve("build/classes/kotlin/metadata/nativeMain/klib/test-project_nativeMain"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ open class KlibBasedMppIT : BaseGradleIT() {

assertFileExists("build/classes/kotlin/metadata/commonMain/default/manifest")
assertFileExists("build/classes/kotlin/metadata/jvmAndJsMain/default/manifest")
assertFileExists("build/classes/kotlin/metadata/linuxMain/klib/${projectName}_linuxMain.klib")
assertFileExists("build/classes/kotlin/metadata/linuxMain/klib/${projectName}_linuxMain")

// Check that the common and JVM+JS source sets don't receive the Kotlin/Native stdlib in the classpath:
run {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class MppSharedNativeCompileIT : KGPBaseTest() {
fun `test - shared native klib - does not contain 'depends=' manifest property`(gradleVersion: GradleVersion) {
project("kt-54995-compileSharedNative-with-okio", gradleVersion) {
build("compileNativeMainKotlinMetadata") {
val nativeMainKlib = projectPath.resolve("build/classes/kotlin/metadata/nativeMain/klib/test-project_nativeMain.klib")
assertFileExists(nativeMainKlib)
val nativeMainKlib = projectPath.resolve("build/classes/kotlin/metadata/nativeMain/klib/test-project_nativeMain")
assertDirectoryExists(nativeMainKlib)

val libraryFile = org.jetbrains.kotlin.library.resolveSingleFileKlib(
org.jetbrains.kotlin.konan.file.File(nativeMainKlib),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ package org.jetbrains.kotlin.gradle.plugin.mpp.pm20
import org.gradle.api.artifacts.Dependency
import org.gradle.jvm.tasks.Jar
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.disambiguateName
import org.jetbrains.kotlin.gradle.targets.metadata.filesWithUnpackedArchives
import org.jetbrains.kotlin.gradle.tasks.registerTask
import org.jetbrains.kotlin.library.KLIB_FILE_EXTENSION

/**
* Will register a 'hostSpecificMetadataJar' [Jar] task containing compilation outputs of host specific metadata.
Expand All @@ -29,7 +27,7 @@ val GradleKpmHostSpecificMetadataArtifact = GradleKpmConfigurationArtifactsSetup
if (metadataCompilation is GradleKpmNativeFragmentMetadataCompilationData) {
jar.from(project.files(project.provider {
if (metadataFragment in fragment.withRefinesClosure && metadataFragment.isNativeHostSpecific())
project.filesWithUnpackedArchives(metadataCompilation.output.allOutputs, setOf(KLIB_FILE_EXTENSION))
metadataCompilation.output.allOutputs
else emptyList<Any>()
})) { spec -> spec.into(metadataFragment.name) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.ComputedCapability
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.disambiguateName
import org.jetbrains.kotlin.gradle.targets.metadata.KotlinMetadataTargetConfigurator
import org.jetbrains.kotlin.gradle.targets.metadata.createGenerateProjectStructureMetadataTask
import org.jetbrains.kotlin.gradle.targets.metadata.filesWithUnpackedArchives
import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.tasks.withType
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.gradle.utils.dashSeparatedName
import org.jetbrains.kotlin.gradle.utils.filesProvider
import org.jetbrains.kotlin.gradle.utils.lowerCamelCaseName
import org.jetbrains.kotlin.library.KLIB_FILE_EXTENSION
import org.jetbrains.kotlin.project.model.KpmFragment
import java.util.concurrent.Callable

Expand Down Expand Up @@ -162,7 +160,7 @@ private fun configureMetadataJarTask(
val compilationData = registry.getForFragmentOrNull(fragment)
.takeIf { !fragment.isNativeHostSpecific() }
?: return@filesProvider emptyList<Any>()
project.filesWithUnpackedArchives(compilationData.output.allOutputs, setOf(KLIB_FILE_EXTENSION))
compilationData.output.allOutputs
}
jar.from(metadataOutput) { spec ->
spec.into(fragment.fragmentName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,7 @@ class KotlinMetadataTargetConfigurator :
configureMetadataDependenciesForCompilation(this@apply)

if (!isHostSpecific) {
val metadataContent = project.filesWithUnpackedArchives(this@apply.output.allOutputs, setOf("klib"))
allMetadataJar.configure { it.from(metadataContent) { spec -> spec.into(this@apply.defaultSourceSet.name) } }
allMetadataJar.configure { it.from(this@apply.output.allOutputs) { spec -> spec.into(this@apply.defaultSourceSet.name) } }
if (this is KotlinSharedNativeCompilation) {
project.includeCommonizedCInteropMetadata(allMetadataJar, this)
}
Expand Down Expand Up @@ -477,16 +476,6 @@ internal suspend fun getPublishedPlatformCompilations(project: Project): Map<Kot
return result
}

internal fun Project.filesWithUnpackedArchives(from: FileCollection, extensions: Set<String>): FileCollection =
project.files(project.provider {
from.mapNotNull {
@Suppress("IMPLICIT_CAST_TO_ANY")
if (it.extension in extensions) {
if (it.exists()) project.zipTree(it) else null
} else it
}
}).builtBy(from)

private val KotlinMetadataTarget.metadataCompilationsCreated: CompletableFuture<Unit> by extrasLazyProperty("metadataCompilationsCreated") {
CompletableFuture()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ abstract class KotlinNativeTarget @Inject constructor(
}

metadataCompilations.forEach { compilation ->
metadataJar.from(project.filesWithUnpackedArchives(compilation.output.allOutputs, setOf("klib"))) { spec ->
metadataJar.from(compilation.output.allOutputs) { spec ->
spec.into(compilation.name)
}
metadataJar.dependsOn(compilation.output.classesDirs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.*
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.XcodeVersionTask
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.registerEmbedAndSignAppleFrameworkTask
import org.jetbrains.kotlin.gradle.plugin.mpp.apple.version
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.GradleKpmMetadataCompilationData
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.GradleKpmVariant
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.util.copyAttributes
import org.jetbrains.kotlin.gradle.targets.metadata.isKotlinGranularMetadataEnabled
Expand All @@ -45,8 +46,6 @@ import org.jetbrains.kotlin.gradle.tasks.*
import org.jetbrains.kotlin.gradle.testing.internal.configureConventions
import org.jetbrains.kotlin.gradle.testing.internal.kotlinTestRegistry
import org.jetbrains.kotlin.gradle.testing.testTaskName
import org.jetbrains.kotlin.gradle.utils.XcodeUtils
import org.jetbrains.kotlin.gradle.utils.named
import org.jetbrains.kotlin.gradle.utils.newInstance
import org.jetbrains.kotlin.gradle.utils.valueSourceWithExecProviderCompat
import org.jetbrains.kotlin.konan.target.HostManager
Expand Down Expand Up @@ -366,6 +365,7 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
): TaskProvider<KotlinNativeCompile> {
val project = compilationInfo.project
val ext = project.topLevelExtension
val isMetadataCompilation = checkCompilationIsMetadataCompilation(compilationInfo)
val compileTaskProvider = project.registerTask<KotlinNativeCompile>(
compilationInfo.compileKotlinTaskName,
listOf(
Expand Down Expand Up @@ -399,6 +399,8 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
)
.finalizeValueOnRead()

// for metadata tasks we should provide unpacked klib
it.produceUnpackedKlib.set(isMetadataCompilation)
}

compilationInfo.classesDirs.from(compileTaskProvider.map { it.outputFile })
Expand Down Expand Up @@ -429,6 +431,13 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
return compileTaskProvider
}

private fun checkCompilationIsMetadataCompilation(compilationInfo: KotlinCompilationInfo): Boolean {
return when (compilationInfo) {
is KPM -> compilationInfo.compilationData is GradleKpmMetadataCompilationData<*>
is KotlinCompilationInfo.TCS -> compilationInfo.compilation is KotlinMetadataCompilation<*>
}
}

private fun Project.klibOutputDirectory(
compilation: KotlinCompilationInfo,
): DirectoryProperty {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ import org.jetbrains.kotlin.gradle.targets.native.tasks.*
import org.jetbrains.kotlin.gradle.utils.*
import org.jetbrains.kotlin.gradle.utils.GradleLoggerAdapter
import org.jetbrains.kotlin.gradle.utils.listFilesOrEmpty
import org.jetbrains.kotlin.incremental.deleteDirectoryContents
import org.jetbrains.kotlin.ir.linkage.partial.PartialLinkageMode
import org.jetbrains.kotlin.konan.library.KLIB_INTEROP_IR_PROVIDER_IDENTIFIER
import org.jetbrains.kotlin.konan.properties.saveToFile
Expand Down Expand Up @@ -152,6 +151,9 @@ abstract class AbstractKotlinNativeCompile<
@get:Internal
abstract val baseName: String

@get:Input
internal abstract val produceUnpackedKlib: Property<Boolean>

@get:Input
@get:Optional
internal abstract val explicitApiMode: Property<ExplicitApiMode>
Expand Down Expand Up @@ -242,7 +244,7 @@ abstract class AbstractKotlinNativeCompile<
open val outputFile: Provider<File>
get() = destinationDirectory.flatMap {
val prefix = outputKind.prefix(konanTarget)
val suffix = outputKind.suffix(konanTarget)
val suffix = if (produceUnpackedKlib.get()) "" else outputKind.suffix(konanTarget)
val filename = "$prefix${baseName}$suffix".let {
when {
outputKind == FRAMEWORK ->
Expand Down Expand Up @@ -346,10 +348,6 @@ internal constructor(
@get:Internal
val moduleName: String get() = compilerOptions.moduleName.get()

@get:OutputFile
override val outputFile: Provider<File>
get() = super.outputFile

@get:Input
val shortModuleName: String by providerFactory.provider { baseName }

Expand Down Expand Up @@ -466,6 +464,7 @@ internal constructor(
args.metadataKlib = sharedCompilationData != null
args.nodefaultlibs = sharedCompilationData != null
args.manifestFile = sharedCompilationData?.manifestFile?.absolutePath
args.nopack = produceUnpackedKlib.get()

args.pluginOptions = compilerPlugins.flatMap { it.options.arguments }.toTypedArray()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class KotlinNativeCompileRefinesPathsTest {

assertEquals(
project.files(
"build/classes/kotlin/metadata/nativeMain/klib/test_nativeMain.klib",
"build/classes/kotlin/metadata/commonMain/klib/test_commonMain.klib"
"build/classes/kotlin/metadata/nativeMain/klib/test_nativeMain",
"build/classes/kotlin/metadata/commonMain/klib/test_commonMain"
).toSet().relativeTo(project),
compileTask.refinesModule.files.relativeTo(project)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DependsOnClosureCompilePathTest {
project.evaluate()

assertEquals(
listOf("test_appleMain.klib", "test_nativeMain.klib", "test_commonMain.klib"),
listOf("test_appleMain", "test_nativeMain", "test_commonMain"),
kotlin.sourceSets.iosMain.get().dependsOnClosureCompilePath.toList().map { it.name }
)
}
Expand Down Expand Up @@ -78,7 +78,7 @@ class DependsOnClosureCompilePathTest {
project.evaluate()

assertEquals(
listOf("test_c.klib", "test_a.klib", "test_b.klib", "test_commonMain.klib"),
listOf("test_c", "test_a", "test_b", "test_commonMain"),
kotlin.sourceSets.linuxMain.get().dependsOnClosureCompilePath.toList().map { it.name }
)
}
Expand Down Expand Up @@ -130,7 +130,7 @@ class DependsOnClosureCompilePathTest {
If the order changed, please investigate the root cause; Do not update the assertion!
*/
assertEquals(
listOf("test_bottom.klib", "test_left.klib", "test_right.klib", "test_commonMain.klib"),
listOf("test_bottom", "test_left", "test_right", "test_commonMain"),
kotlin.sourceSets.linuxMain.get().dependsOnClosureCompilePath.toList().map { it.name }
)
}
Expand Down

0 comments on commit b7b0397

Please sign in to comment.