Skip to content

Parse Wasm targets from KLib dump's manifest #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]