From 8c39d750f4de1003e8d41b9ee28f2c28216d5638 Mon Sep 17 00:00:00 2001 From: Kendall Weihe Date: Mon, 12 Aug 2024 11:21:09 -0400 Subject: [PATCH] Move sys target setting to UniFFI.kt --- Justfile | 1 + .../sdk/crypto/keys/InMemoryKeyManager.kt | 5 -- .../web5/sdk/crypto/signers/Ed25519Signer.kt | 6 -- .../main/kotlin/web5/sdk/dids/BearerDid.kt | 6 -- .../src/main/kotlin/web5/sdk/dids/Document.kt | 1 - .../main/kotlin/web5/sdk/dids/PortableDid.kt | 5 -- .../web5/sdk/dids/methods/dht/DidDht.kt | 6 -- .../web5/sdk/dids/methods/jwk/DidJwk.kt | 6 -- .../web5/sdk/dids/methods/web/DidWeb.kt | 6 -- .../main/kotlin/web5/sdk/rust/SystemTarget.kt | 84 ++++++++----------- .../src/main/kotlin/web5/sdk/rust/UniFFI.kt | 2 +- .../web5/sdk/vc/pex/PresentationDefinition.kt | 5 -- .../kotlin/web5/sdk/rust/SystemTargetTest.kt | 1 - 13 files changed, 38 insertions(+), 96 deletions(-) diff --git a/Justfile b/Justfile index 2b22e572..0f3287be 100644 --- a/Justfile +++ b/Justfile @@ -32,6 +32,7 @@ bind-kotlin: setup generate --library bound/kt/src/main/resources/libweb5_uniffi_aarch64_apple_darwin.dylib \ --language kotlin \ --out-dir target/bindgen-kotlin + sed -i '' 's/findLibraryName(componentName)/detectSystemTarget()/' target/bindgen-kotlin/web5/sdk/rust/web5.kt cp target/bindgen-kotlin/web5/sdk/rust/web5.kt bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt test-bound: setup diff --git a/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt b/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt index 095f0360..f8fd6424 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/crypto/keys/InMemoryKeyManager.kt @@ -1,17 +1,12 @@ package web5.sdk.crypto.keys import web5.sdk.crypto.signers.Signer -import web5.sdk.rust.SystemTarget import web5.sdk.rust.InMemoryKeyManager as RustCoreInMemoryKeyManager /** * A class for managing cryptographic keys in-memory. */ class InMemoryKeyManager : KeyManager { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - private val rustCoreInMemoryKeyManager = RustCoreInMemoryKeyManager() /** diff --git a/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Ed25519Signer.kt b/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Ed25519Signer.kt index ca353ac9..48f99639 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Ed25519Signer.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/crypto/signers/Ed25519Signer.kt @@ -1,15 +1,9 @@ package web5.sdk.crypto.signers import web5.sdk.crypto.keys.Jwk -import web5.sdk.rust.SystemTarget - import web5.sdk.rust.Ed25519Signer as RustCoreEd25519Signer class Ed25519Signer : Signer { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - private val rustCoreSigner: RustCoreEd25519Signer constructor(privateKey: Jwk) { diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt index 09928ffc..178d52e5 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/BearerDid.kt @@ -2,8 +2,6 @@ package web5.sdk.dids import web5.sdk.crypto.signers.Signer import web5.sdk.crypto.keys.KeyManager -import web5.sdk.rust.SystemTarget - import web5.sdk.rust.BearerDid as RustCoreBearerDid /** @@ -14,10 +12,6 @@ import web5.sdk.rust.BearerDid as RustCoreBearerDid * @property document The DID document associated with this instance. */ class BearerDid { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - val did: Did val document: Document val keyManager: KeyManager diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/Document.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/Document.kt index 77c51546..abf3e20a 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/dids/Document.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/Document.kt @@ -1,6 +1,5 @@ package web5.sdk.dids - import web5.sdk.rust.DocumentData as RustCoreDocumentData /** diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt index bc28c1eb..245d15e9 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/PortableDid.kt @@ -1,14 +1,9 @@ package web5.sdk.dids import web5.sdk.crypto.keys.Jwk -import web5.sdk.rust.SystemTarget import web5.sdk.rust.PortableDid as RustCorePortableDid class PortableDid { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - val didUri: String val document: Document val privateKeys: List diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/methods/dht/DidDht.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/methods/dht/DidDht.kt index c89e371d..dc9c4675 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/dids/methods/dht/DidDht.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/methods/dht/DidDht.kt @@ -5,8 +5,6 @@ import web5.sdk.crypto.signers.Signer import web5.sdk.dids.Did import web5.sdk.dids.Document import web5.sdk.dids.ResolutionResult -import web5.sdk.rust.SystemTarget - import web5.sdk.rust.didDhtResolve as rustCoreDidDhtResolve import web5.sdk.rust.DidDht as RustCoreDidDht import web5.sdk.rust.Signer as RustCoreSigner @@ -18,10 +16,6 @@ import web5.sdk.rust.Signer as RustCoreSigner * @property document The DID document associated with this instance. */ class DidDht { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - val did: Did val document: Document diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/methods/jwk/DidJwk.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/methods/jwk/DidJwk.kt index 8cf95e57..3bdc034f 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/dids/methods/jwk/DidJwk.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/methods/jwk/DidJwk.kt @@ -4,8 +4,6 @@ import web5.sdk.crypto.keys.Jwk import web5.sdk.dids.Did import web5.sdk.dids.Document import web5.sdk.dids.ResolutionResult -import web5.sdk.rust.SystemTarget - import web5.sdk.rust.didJwkResolve as rustCoreDidJwkResolve import web5.sdk.rust.DidJwk as RustCoreDidJwk @@ -16,10 +14,6 @@ import web5.sdk.rust.DidJwk as RustCoreDidJwk * @property document The DID document associated with this instance. */ class DidJwk { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - val did: Did val document: Document diff --git a/bound/kt/src/main/kotlin/web5/sdk/dids/methods/web/DidWeb.kt b/bound/kt/src/main/kotlin/web5/sdk/dids/methods/web/DidWeb.kt index 6ec25ab1..d848c194 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/dids/methods/web/DidWeb.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/dids/methods/web/DidWeb.kt @@ -5,8 +5,6 @@ import web5.sdk.crypto.keys.Jwk import web5.sdk.dids.Did import web5.sdk.dids.Document import web5.sdk.dids.ResolutionResult -import web5.sdk.rust.SystemTarget - import web5.sdk.rust.didWebResolve as rustCoreDidWebResolve import web5.sdk.rust.DidWeb as RustCoreDidWeb @@ -17,10 +15,6 @@ import web5.sdk.rust.DidWeb as RustCoreDidWeb * @property document The DID document associated with this instance. */ class DidWeb { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - val did: Did val document: Document diff --git a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt index 343700af..a31fb046 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/SystemTarget.kt @@ -2,59 +2,47 @@ package web5.sdk.rust import java.io.File -object SystemTarget { - @Volatile - private var isSet = false +internal val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase() - fun set() { - val logLevel = System.getenv("WEB5_SDK_LOG_LEVEL")?.lowercase() +internal fun log(message: String) { + if (logLevel == "debug") { + println("web5 sdk SystemArchitecture $message") + } +} - fun log(message: String) { - if (logLevel == "debug") { - println("web5 sdk SystemArchitecture $message") - } - } +internal fun detectSystemTarget(): String { + val arch = System.getProperty("os.arch")?.lowercase() ?: throw Exception("Unable to get OS arch") + val name = System.getProperty("os.name")?.lowercase() ?: throw Exception("Unable to get OS name") + + log("System architecture: $arch") + log("Operating system name: $name") + + when { + name.contains("mac") && arch.contains("aarch64") -> + return "web5_uniffi_aarch64_apple_darwin" - if (!isSet) { - synchronized(this) { - if (!isSet) { - val arch = System.getProperty("os.arch")?.lowercase() ?: throw Exception("Unable to get OS arch") - val name = System.getProperty("os.name")?.lowercase() ?: throw Exception("Unable to get OS name") - - log("System architecture: $arch") - log("Operating system name: $name") - - when { - name.contains("mac") && arch.contains("aarch64") -> - System.setProperty("uniffi.component.web5.libraryOverride", "web5_uniffi_aarch64_apple_darwin") - - name.contains("mac") && arch.contains("x86_64") -> - System.setProperty("uniffi.component.web5.libraryOverride", "web5_uniffi_x86_64_apple_darwin") - - name.contains("linux") && arch.contains("amd64") -> { - val osRelease = File("/etc/os-release") - if (osRelease.exists()) { - val osReleaseContent = osRelease.readText().lowercase() - log("OS release content: $osReleaseContent") - when { - osReleaseContent.contains("ubuntu") -> - System.setProperty("uniffi.component.web5.libraryOverride", "web5_uniffi_x86_64_unknown_linux_gnu") - - osReleaseContent.contains("alpine") -> - System.setProperty("uniffi.component.web5.libraryOverride", "web5_uniffi_x86_64_unknown_linux_musl") - - else -> throw Exception("Unsupported OS arch $osReleaseContent") - } - } else { - throw Exception("Linux /etc/os-release not found") - } - } - - else -> throw Exception("Unsupported OS arch $arch $name") - } - isSet = true + name.contains("mac") && arch.contains("x86_64") -> + return "web5_uniffi_x86_64_apple_darwin" + + name.contains("linux") && arch.contains("amd64") -> { + val osRelease = File("/etc/os-release") + if (osRelease.exists()) { + val osReleaseContent = osRelease.readText().lowercase() + log("OS release content: $osReleaseContent") + return when { + osReleaseContent.contains("ubuntu") -> + "web5_uniffi_x86_64_unknown_linux_gnu" + + osReleaseContent.contains("alpine") -> + "web5_uniffi_x86_64_unknown_linux_musl" + + else -> throw Exception("Unsupported OS arch $osReleaseContent") } + } else { + throw Exception("Linux /etc/os-release not found") } } + + else -> throw Exception("Unsupported OS arch $arch $name") } } \ No newline at end of file diff --git a/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt b/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt index 9ab044e0..ed39f91d 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/rust/UniFFI.kt @@ -367,7 +367,7 @@ private fun findLibraryName(componentName: String): String { private inline fun loadIndirect( componentName: String ): Lib { - return Native.load(findLibraryName(componentName), Lib::class.java) + return Native.load(detectSystemTarget(), Lib::class.java) } // Define FFI callback types diff --git a/bound/kt/src/main/kotlin/web5/sdk/vc/pex/PresentationDefinition.kt b/bound/kt/src/main/kotlin/web5/sdk/vc/pex/PresentationDefinition.kt index 52d66835..1236585b 100644 --- a/bound/kt/src/main/kotlin/web5/sdk/vc/pex/PresentationDefinition.kt +++ b/bound/kt/src/main/kotlin/web5/sdk/vc/pex/PresentationDefinition.kt @@ -2,7 +2,6 @@ package web5.sdk.vc.pex import com.fasterxml.jackson.annotation.JsonProperty import web5.sdk.Json -import web5.sdk.rust.SystemTarget import web5.sdk.rust.PresentationDefinition as RustCorePresentationDefinition data class PresentationDefinition( @@ -12,10 +11,6 @@ data class PresentationDefinition( @JsonProperty("input_descriptors") val inputDescriptors: List ) { - init { - SystemTarget.set() // ensure the sys arch is set for first-time loading - } - internal val rustCorePresentationDefinition = RustCorePresentationDefinition( Json.stringify(this) ) diff --git a/bound/kt/src/test/kotlin/web5/sdk/rust/SystemTargetTest.kt b/bound/kt/src/test/kotlin/web5/sdk/rust/SystemTargetTest.kt index 5aece0f4..ba7e24c4 100644 --- a/bound/kt/src/test/kotlin/web5/sdk/rust/SystemTargetTest.kt +++ b/bound/kt/src/test/kotlin/web5/sdk/rust/SystemTargetTest.kt @@ -6,7 +6,6 @@ class SystemTargetTest { @Test fun `can load shared library`() { System.setProperty("WEB5_SDK_LOG_LEVEL", "debug") - SystemTarget.set() UniffiLib.INSTANCE println("Successfully loaded shared library for ${System.getProperty("uniffi.component.web5.libraryOverride")}") }