Skip to content

Commit b439c5c

Browse files
fzhinkinshanshin
authored andcommitted
[ABI Validation] Parse Wasm targets from KLib dump's manifest
Fixes Kotlin/binary-compatibility-validator#193 Pull request Kotlin/binary-compatibility-validator#221 Moved from Kotlin/binary-compatibility-validator@328f56f
1 parent 76ffb84 commit b439c5c

File tree

7 files changed

+110
-5
lines changed

7 files changed

+110
-5
lines changed

libraries/tools/abi-validation/src/main/kotlin/api/klib/KlibAbiDumpFileMerger.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ private const val INDENT_WIDTH = 4
4949
private const val ALIAS_PREFIX = "// Alias: "
5050
private const val PLATFORM_PREFIX = "// Platform: "
5151
private const val NATIVE_TARGETS_PREFIX = "// Native targets: "
52+
private const val WASM_TARGETS_PREFIX = "// WASM targets: "
5253
private const val LIBRARY_NAME_PREFIX = "// Library unique name:"
5354

5455
private fun String.depth(): Int {
@@ -265,7 +266,8 @@ internal class KlibAbiDumpMerger {
265266
platform = next.split(": ")[1].trim()
266267
}
267268

268-
next.startsWith(NATIVE_TARGETS_PREFIX) -> {
269+
next.startsWith(NATIVE_TARGETS_PREFIX) || next.startsWith(WASM_TARGETS_PREFIX) -> {
270+
check(targets == null) { "Targets list was already parsed" }
269271
targets = next.split(": ")[1].trim()
270272
}
271273
}
@@ -287,12 +289,12 @@ internal class KlibAbiDumpMerger {
287289
"The dump does not contain platform name. Please make sure that the manifest was included in the dump"
288290
}
289291

290-
if (platformString == "WASM") {
291-
// Currently, there's no way to distinguish Wasm targets without explicitly specifying a target name
292+
if (platformString == "WASM" && targetsString == null) {
293+
// For older dumps, there's no way to distinguish Wasm targets without explicitly specifying a target name
292294
check(configurableTargetName != null) { "targetName has to be specified for a Wasm target" }
293295
return setOf(KlibTarget(configurableTargetName))
294296
}
295-
if (platformString != "NATIVE") {
297+
if (platformString != "NATIVE" && platformString != "WASM") {
296298
val platformStringLc = platformString.toLowerCase(Locale.ROOT)
297299
return if (configurableTargetName == null) {
298300
setOf(KlibTarget(platformStringLc))

libraries/tools/abi-validation/src/main/kotlin/api/klib/TargetHierarchy.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,7 @@ internal val konanTargetNameMapping = mapOf(
134134
"ios_arm32" to "iosArm32",
135135
"watchos_x86" to "watchosX86",
136136
"linux_arm32_hfp" to "linuxArm32Hfp",
137-
"mingw_x86" to "mingwX86"
137+
"mingw_x86" to "mingwX86",
138+
"wasm-wasi" to "wasmWasi",
139+
"wasm-js" to "wasmJs"
138140
)

libraries/tools/abi-validation/src/test/kotlin/tests/KlibAbiMergingTest.kt

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,41 @@ class KlibAbiMergingTest {
248248
)
249249
}
250250

251+
@Test
252+
fun explicitWasmTargets() {
253+
val klib = KlibAbiDumpMerger()
254+
klib.merge(file("/merge/explicitWasmTargets/wasmWasi.abi"))
255+
klib.merge(file("/merge/explicitWasmTargets/wasmJs.abi"))
256+
257+
val merged = dumpToFile(klib)
258+
259+
assertContentEquals(
260+
lines("/merge/explicitWasmTargets/merged.abi"),
261+
Files.readAllLines(merged.toPath()).asSequence()
262+
)
263+
}
264+
265+
@Test
266+
fun renameWasmTargetHavingNameInManifest() {
267+
val klib = KlibAbiDumpMerger()
268+
klib.merge(file("/merge/explicitWasmTargets/wasmWasi.abi"), "wasm")
269+
assertEquals(setOf(KlibTarget.parse("wasmWasi.wasm")), klib.targets)
270+
}
271+
272+
@Test
273+
fun wasmDumpWithMultipleTargets() {
274+
val klib = KlibAbiDumpMerger()
275+
assertFailsWith<IllegalArgumentException>{
276+
klib.merge(file("/merge/explicitWasmTargets/wasmMulti.abi"), "wasm")
277+
}
278+
279+
klib.merge(file("/merge/explicitWasmTargets/wasmMulti.abi"))
280+
assertContentEquals(
281+
lines("/merge/explicitWasmTargets/merged.abi"),
282+
Files.readAllLines(dumpToFile(klib).toPath()).asSequence()
283+
)
284+
}
285+
251286
@Test
252287
fun unqualifiedWasmTarget() {
253288
// currently, there's no way to distinguish wasmWasi from wasmJs
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Klib ABI Dump
2+
// Targets: [wasmJs, wasmWasi]
3+
// Rendering settings:
4+
// - Signature version: 2
5+
// - Show manifest properties: false
6+
// - Show declarations: true
7+
8+
// Library unique name: <org.example:bcv-klib-test>
9+
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
10+
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
11+
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
12+
final val value // org.example/ShardedClass.value|{}value[0]
13+
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
14+
}
15+
final fun org.example/ShardedClass(kotlin/Int, kotlin/Float, kotlin/Long): org.example/ShardedClass // org.example/ShardedClass|ShardedClass(kotlin.Int;kotlin.Float;kotlin.Long){}[0]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Rendering settings:
2+
// - Signature version: 2
3+
// - Show manifest properties: false
4+
// - Show declarations: true
5+
6+
// Library unique name: <org.example:bcv-klib-test>
7+
// Platform: WASM
8+
// WASM targets: wasm-js
9+
// Compiler version: 2.0.255-SNAPSHOT
10+
// ABI version: 1.8.0
11+
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
12+
final val value // org.example/ShardedClass.value|{}value[0]
13+
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
14+
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
15+
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
16+
}
17+
final fun org.example/ShardedClass(kotlin/Int, kotlin/Float, kotlin/Long): org.example/ShardedClass // org.example/ShardedClass|ShardedClass(kotlin.Int;kotlin.Float;kotlin.Long){}[0]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Rendering settings:
2+
// - Signature version: 2
3+
// - Show manifest properties: false
4+
// - Show declarations: true
5+
6+
// Library unique name: <org.example:bcv-klib-test>
7+
// Platform: WASM
8+
// WASM targets: wasm-wasi, wasm-js
9+
// Compiler version: 2.0.255-SNAPSHOT
10+
// ABI version: 1.8.0
11+
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
12+
final val value // org.example/ShardedClass.value|{}value[0]
13+
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
14+
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
15+
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
16+
}
17+
final fun org.example/ShardedClass(kotlin/Int, kotlin/Float, kotlin/Long): org.example/ShardedClass // org.example/ShardedClass|ShardedClass(kotlin.Int;kotlin.Float;kotlin.Long){}[0]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Rendering settings:
2+
// - Signature version: 2
3+
// - Show manifest properties: false
4+
// - Show declarations: true
5+
6+
// Library unique name: <org.example:bcv-klib-test>
7+
// Platform: WASM
8+
// WASM targets: wasm-wasi
9+
// Compiler version: 2.0.255-SNAPSHOT
10+
// ABI version: 1.8.0
11+
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
12+
final val value // org.example/ShardedClass.value|{}value[0]
13+
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
14+
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
15+
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
16+
}
17+
final fun org.example/ShardedClass(kotlin/Int, kotlin/Float, kotlin/Long): org.example/ShardedClass // org.example/ShardedClass|ShardedClass(kotlin.Int;kotlin.Float;kotlin.Long){}[0]

0 commit comments

Comments
 (0)