Skip to content

Commit

Permalink
Parse Wasm targets from KLib dump's manifest
Browse files Browse the repository at this point in the history
Fixes #193
  • Loading branch information
fzhinkin committed Apr 23, 2024
1 parent f677423 commit 286ca68
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/main/kotlin/api/klib/KlibAbiDumpFileMerger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private const val INDENT_WIDTH = 4
private const val ALIAS_PREFIX = "// Alias: "
private const val PLATFORM_PREFIX = "// Platform: "
private const val NATIVE_TARGETS_PREFIX = "// Native targets: "
private const val WASM_TARGETS_PREFIX = "// WASM targets: "
private const val LIBRARY_NAME_PREFIX = "// Library unique name:"

private fun String.depth(): Int {
Expand Down Expand Up @@ -263,7 +264,8 @@ internal class KlibAbiDumpMerger {
platform = next.split(": ")[1].trim()
}

next.startsWith(NATIVE_TARGETS_PREFIX) -> {
next.startsWith(NATIVE_TARGETS_PREFIX) || next.startsWith(WASM_TARGETS_PREFIX) -> {
check(targets == null) { "Targets list was already parsed" }
targets = next.split(": ")[1].trim()
}
}
Expand All @@ -285,12 +287,12 @@ internal class KlibAbiDumpMerger {
"The dump does not contain platform name. Please make sure that the manifest was included in the dump"
}

if (platformString == "WASM") {
// Currently, there's no way to distinguish Wasm targets without explicitly specifying a target name
if (platformString == "WASM" && targetsString == null) {
// For older dumps, there's no way to distinguish Wasm targets without explicitly specifying a target name
check(configurableTargetName != null) { "targetName has to be specified for a Wasm target" }
return setOf(KlibTarget(configurableTargetName))
}
if (platformString != "NATIVE") {
if (platformString != "NATIVE" && platformString != "WASM") {
val platformStringLc = platformString.toLowerCase(Locale.ROOT)
return if (configurableTargetName == null) {
setOf(KlibTarget(platformStringLc))
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/api/klib/TargetHierarchy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -134,5 +134,7 @@ internal val konanTargetNameMapping = mapOf(
"ios_arm32" to "iosArm32",
"watchos_x86" to "watchosX86",
"linux_arm32_hfp" to "linuxArm32Hfp",
"mingw_x86" to "mingwX86"
"mingw_x86" to "mingwX86",
"wasm-wasi" to "wasmWasi",
"wasm-js" to "wasmJs"
)
35 changes: 35 additions & 0 deletions src/test/kotlin/tests/KlibAbiMergingTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,41 @@ class KlibAbiMergingTest {
)
}

@Test
fun explicitWasmTargets() {
val klib = KlibAbiDumpMerger()
klib.merge(file("/merge/explicitWasmTargets/wasmWasi.abi"))
klib.merge(file("/merge/explicitWasmTargets/wasmJs.abi"))

val merged = dumpToFile(klib)

assertContentEquals(
lines("/merge/explicitWasmTargets/merged.abi"),
Files.readAllLines(merged.toPath()).asSequence()
)
}

@Test
fun renameWasmTargetHavingNameInManifest() {
val klib = KlibAbiDumpMerger()
klib.merge(file("/merge/explicitWasmTargets/wasmWasi.abi"), "wasm")
assertEquals(setOf(KlibTarget.parse("wasmWasi.wasm")), klib.targets)
}

@Test
fun wasmDumpWithMultipleTargets() {
val klib = KlibAbiDumpMerger()
assertFailsWith<IllegalArgumentException>{
klib.merge(file("/merge/explicitWasmTargets/wasmMulti.abi"), "wasm")
}

klib.merge(file("/merge/explicitWasmTargets/wasmMulti.abi"))
assertContentEquals(
lines("/merge/explicitWasmTargets/merged.abi"),
Files.readAllLines(dumpToFile(klib).toPath()).asSequence()
)
}

@Test
fun unqualifiedWasmTarget() {
// currently, there's no way to distinguish wasmWasi from wasmJs
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/merge/explicitWasmTargets/merged.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Klib ABI Dump
// Targets: [wasmJs, wasmWasi]
// Rendering settings:
// - Signature version: 2
// - Show manifest properties: false
// - Show declarations: true

// Library unique name: <org.example:bcv-klib-test>
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
final val value // org.example/ShardedClass.value|{}value[0]
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
}
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]
17 changes: 17 additions & 0 deletions src/test/resources/merge/explicitWasmTargets/wasmJs.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Rendering settings:
// - Signature version: 2
// - Show manifest properties: false
// - Show declarations: true

// Library unique name: <org.example:bcv-klib-test>
// Platform: WASM
// WASM targets: wasm-js
// Compiler version: 2.0.255-SNAPSHOT
// ABI version: 1.8.0
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
final val value // org.example/ShardedClass.value|{}value[0]
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
}
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]
17 changes: 17 additions & 0 deletions src/test/resources/merge/explicitWasmTargets/wasmMulti.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Rendering settings:
// - Signature version: 2
// - Show manifest properties: false
// - Show declarations: true

// Library unique name: <org.example:bcv-klib-test>
// Platform: WASM
// WASM targets: wasm-wasi, wasm-js
// Compiler version: 2.0.255-SNAPSHOT
// ABI version: 1.8.0
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
final val value // org.example/ShardedClass.value|{}value[0]
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
}
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]
17 changes: 17 additions & 0 deletions src/test/resources/merge/explicitWasmTargets/wasmWasi.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Rendering settings:
// - Signature version: 2
// - Show manifest properties: false
// - Show declarations: true

// Library unique name: <org.example:bcv-klib-test>
// Platform: WASM
// WASM targets: wasm-wasi
// Compiler version: 2.0.255-SNAPSHOT
// ABI version: 1.8.0
final class org.example/ShardedClass { // org.example/ShardedClass|null[0]
final val value // org.example/ShardedClass.value|{}value[0]
final fun <get-value>(): kotlin/Int // org.example/ShardedClass.value.<get-value>|<get-value>(){}[0]
constructor <init>(kotlin/Int) // org.example/ShardedClass.<init>|<init>(kotlin.Int){}[0]
final fun add(kotlin/Int): kotlin/Int // org.example/ShardedClass.add|add(kotlin.Int){}[0]
}
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 comments on commit 286ca68

Please sign in to comment.