Skip to content

Fix the way of looking for native libraries #1960

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
Mar 15, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.microsoft.z3.Context
import com.microsoft.z3.Global
import org.utbot.common.FileUtil
import java.io.File
import java.nio.file.Files.createTempDirectory

abstract class Z3Initializer : AutoCloseable {
protected val context: Context by lazy {
Expand All @@ -27,27 +26,30 @@ abstract class Z3Initializer : AutoCloseable {
val arch = System.getProperty("os.arch")
require(arch in supportedArchs) { "Not supported arch: $arch" }

val osProperty = System.getProperty("os.name").toLowerCase()
val osProperty = System.getProperty("os.name").lowercase()
val (ext, allLibraries) = when {
osProperty.startsWith("windows") -> ".dll" to vcWinLibrariesToLoadBefore + libraries
osProperty.startsWith("linux") -> ".so" to libraries
osProperty.startsWith("mac") -> ".dylib" to libraries
else -> error("Unknown OS: $osProperty")
}
val libZ3DllUrl = Z3Initializer::class.java

val dist = if (arch == "aarch64") "arm" else "x64"

val libZ3FilesUrl = Z3Initializer::class.java
.classLoader
.getResource("lib/x64/libz3.dll") ?: error("Can't find native library folder")
.getResource("lib/$dist/libz3$ext") ?: error("Can't find native library folder")
// can't take resource of parent folder right here because in obfuscated jar parent folder
// can be missed (e.g., in case if obfuscation was applied)

val libFolder: String?
if (libZ3DllUrl.toURI().scheme == "jar") {
if (libZ3FilesUrl.toURI().scheme == "jar") {
val tempDir = FileUtil.createTempDirectory("libs-").toFile()

allLibraries.forEach { name ->
Z3Initializer::class.java
.classLoader
.getResourceAsStream("lib/x64/$name$ext")
.getResourceAsStream("lib/$dist/$name$ext")
?.use { input ->
File(tempDir, "$name$ext")
.outputStream()
Expand All @@ -57,7 +59,7 @@ abstract class Z3Initializer : AutoCloseable {

libFolder = "$tempDir"
} else {
libFolder = File(libZ3DllUrl.file).parent
libFolder = File(libZ3FilesUrl.file).parent
}

allLibraries.forEach { System.load("$libFolder/$it$ext") }
Expand Down