Test stale metadata compilations

These compilations shouldn't exist as fragments in the resulting Uklib

^KT-74005
diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/uklibs/UklibPublicationIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/uklibs/UklibPublicationIT.kt
index 18b4264..3f576b1 100644
--- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/uklibs/UklibPublicationIT.kt
+++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/uklibs/UklibPublicationIT.kt
@@ -74,16 +74,7 @@
             Fragment(identifier = "wasmWasiMain", targets = listOf("wasm_wasi")),
         )
 
-        assertEquals(
-            Umanifest(expectedFragments),
-            publisher.umanifest,
-        )
-        assertEquals(
-            expectedFragments.map { it.identifier }.toSet(),
-            publisher.uklibContents.listDirectoryEntries().map {
-                it.name
-            }.filterNot { it == Uklib.UMANIFEST_FILE_NAME }.toSet(),
-        )
+        assertPublishedFragments(expectedFragments, publisher)
     }
 
     @GradleTest
@@ -101,15 +92,9 @@
             Fragment(identifier = "iosArm64Main", targets = listOf("ios_arm64")),
         )
 
-        assertEquals(
-            Umanifest(expectedFragments),
-            publisher.umanifest,
-        )
-        assertEquals(
-            expectedFragments.map { it.identifier }.toSet(),
-            publisher.uklibContents.listDirectoryEntries().map {
-                it.name
-            }.filterNot { it == Uklib.UMANIFEST_FILE_NAME }.toSet(),
+        assertPublishedFragments(
+            expectedFragments,
+            publisher,
         )
     }
 
@@ -140,9 +125,60 @@
             Fragment(identifier = "commonMain", targets = listOf("ios_arm64", "ios_x64")),
         )
 
-        assertEquals(
-            Umanifest(expectedFragments),
-            publishedUklib.umanifest,
+        assertPublishedFragments(
+            expectedFragments,
+            publishedUklib,
+        )
+    }
+
+    @GradleTest
+    fun `uklib contents - multiple targets with stale metadata compilation`(
+        gradleVersion: GradleVersion,
+    ) {
+        val compileAppleMain = "compileAppleMain"
+        val project = project(
+            "empty",
+            gradleVersion,
+        ) {
+            addKgpToBuildScriptCompilationClasspath()
+            buildScriptInjection {
+                project.enableUklibPublication()
+                project.applyMultiplatform {
+                    iosArm64()
+                    iosX64()
+
+                    if (project.hasProperty(compileAppleMain)) {
+                        sourceSets.appleMain.get().compileSource("class Apple")
+                    } else {
+                        sourceSets.commonMain.get().compileSource("class Common")
+                    }
+                }
+            }
+        }
+
+        val publicationWithApple = project.publish(
+            "-P${compileAppleMain}",
+            publisherConfiguration = PublisherConfiguration(repoPath = "withApple"),
+        )
+        assertPublishedFragments(
+            setOf(
+                Fragment(identifier = "iosArm64Main", targets = listOf("ios_arm64")),
+                Fragment(identifier = "iosX64Main", targets = listOf("ios_x64")),
+                Fragment(identifier = "appleMain", targets = listOf("ios_arm64", "ios_x64")),
+            ),
+            readProducedUklib(publicationWithApple)
+        )
+
+        val incrementalPublicationWithoutApple = project.publish(
+            publisherConfiguration = PublisherConfiguration(repoPath = "withoutApple"),
+        )
+        assertPublishedFragments(
+            setOf(
+                Fragment(identifier = "iosArm64Main", targets = listOf("ios_arm64")),
+                Fragment(identifier = "iosX64Main", targets = listOf("ios_x64")),
+                Fragment(identifier = "commonMain", targets = listOf("ios_arm64", "ios_x64")),
+            ),
+            readProducedUklib(incrementalPublicationWithoutApple)
         )
     }
 
@@ -231,6 +267,16 @@
             Fragment(identifier = "iosArm64Main", targets = listOf("ios_arm64")),
         )
 
+        assertPublishedFragments(
+            expectedFragments,
+            publisher,
+        )
+    }
+
+    private fun assertPublishedFragments(
+        expectedFragments: Set<Fragment>,
+        publisher: ProducedUklib,
+    ) {
         assertEquals(
             Umanifest(expectedFragments),
             publisher.umanifest,