Skip to content

Commit

Permalink
Native loading refactor
Browse files Browse the repository at this point in the history
Aligning with zenoh-kotlin's native lib loading logic:
- Adding extra checks for determining the platform on top of which zenoh-java is running
- Defaulting javaClass's class loader in case the system class loader fails to find the native lib
  • Loading branch information
DariusIMP committed Dec 18, 2024
1 parent 1645dab commit 3f49774
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
2 changes: 1 addition & 1 deletion zenoh-java/src/jvmMain/kotlin/io/zenoh/Target.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

package io.zenoh

enum class Target {
internal enum class Target {
WINDOWS_X86_64_MSVC,
LINUX_X86_64,
LINUX_AARCH64,
Expand Down
38 changes: 26 additions & 12 deletions zenoh-java/src/jvmMain/kotlin/io/zenoh/Zenoh.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import java.util.zip.ZipInputStream
internal actual object ZenohLoad {
private const val ZENOH_LIB_NAME = "zenoh_jni"

init {
init {
// Try first to load the local native library for cases in which the module was built locally,
// otherwise try to load from the JAR.
if (tryLoadingLocalLibrary().isFailure) {
Expand All @@ -45,24 +45,33 @@ internal actual object ZenohLoad {
*/
private fun determineTarget(): Result<Target> = runCatching {
val osName = System.getProperty("os.name").lowercase()
val osArch = System.getProperty("os.arch")
val osArch = System.getProperty("os.arch").lowercase()

val target = when {
osName.contains("win") -> when {
osArch.contains("x86_64") || osArch.contains("amd64") -> Target.WINDOWS_X86_64_MSVC
else -> throw UnsupportedOperationException("Unsupported architecture: $osArch")
osArch.contains("x86_64") || osArch.contains("amd64") || osArch.contains("x64") ->
Target.WINDOWS_X86_64_MSVC
else -> throw UnsupportedOperationException("Unsupported architecture on Windows: $osArch")
}

osName.contains("mac") -> when {
osArch.contains("x86_64") || osArch.contains("amd64") -> Target.APPLE_X86_64
osArch.contains("aarch64") -> Target.APPLE_AARCH64
else -> throw UnsupportedOperationException("Unsupported architecture: $osArch")
osName.contains("mac") || osName.contains("darwin") || osName.contains("os x") -> when {
osArch.contains("x86_64") || osArch.contains("amd64") || osArch.contains("x64") ->
Target.APPLE_X86_64

osArch.contains("aarch64") || osArch.contains("arm64") ->
Target.APPLE_AARCH64

else -> throw UnsupportedOperationException("Unsupported architecture on macOS: $osArch")
}

osName.contains("nix") || osName.contains("nux") || osName.contains("aix") -> when {
osArch.contains("x86_64") || osArch.contains("amd64") -> Target.LINUX_X86_64
osArch.contains("aarch64") -> Target.LINUX_AARCH64
else -> throw UnsupportedOperationException("Unsupported architecture: $osArch")
osArch.contains("x86_64") || osArch.contains("amd64") || osArch.contains("x64") ->
Target.LINUX_X86_64

osArch.contains("aarch64") || osArch.contains("arm64") ->
Target.LINUX_AARCH64

else -> throw UnsupportedOperationException("Unsupported architecture on Linux/Unix: $osArch")
}

else -> throw UnsupportedOperationException("Unsupported platform: $osName")
Expand Down Expand Up @@ -108,7 +117,9 @@ internal actual object ZenohLoad {
}

private fun loadLibraryAsInputStream(target: Target): Result<InputStream> = runCatching {
val libUrl = ClassLoader.getSystemClassLoader().getResourceAsStream("$target/$target.zip")!!
val targetName = "$target/$target.zip"
val libUrl = ClassLoader.getSystemClassLoader().getResourceAsStream(targetName)
?: javaClass.classLoader.getResourceAsStream(targetName)!!
val uncompressedLibFile = unzipLibrary(libUrl)
return Result.success(FileInputStream(uncompressedLibFile.getOrThrow()))
}
Expand Down Expand Up @@ -145,6 +156,9 @@ internal actual object ZenohLoad {
*/
private fun tryLoadingLocalLibrary(): Result<Unit> = runCatching {
val lib = ClassLoader.getSystemClassLoader().findLibraryStream(ZENOH_LIB_NAME)
?: javaClass.classLoader.findLibraryStream(
ZENOH_LIB_NAME
)
if (lib != null) {
loadZenohJNI(lib)
} else {
Expand Down

0 comments on commit 3f49774

Please sign in to comment.