Skip to content

Commit

Permalink
use system properties if uname command is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Jan 7, 2025
1 parent 2ed343b commit 4a7740f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ nativeBuildTool := Meson.make(Nil)
|--------------------------------|---------------|
| automatic, when JniNative enabled | [JniPackage.scala](plugin/src/main/scala/com/github/sbt/jni/plugins/JniPackage.scala) |

This plugin packages native libraries produced by JniNative in a way that they can be transparently loaded with JniLoad. It uses the notion of a native "platform", defined using the `os.name` and `os.arch` properties. A native binary of a given platform is assumed to be executable on any machines of the same platform.
This plugin packages native libraries produced by JniNative in a way that they can be transparently loaded with JniLoad. It uses the notion of a native "platform", defined as the architecture-kernel values returned by `uname -sm`, or using the `os.name` and `os.arch` properties if the `uname` command is not available. A native binary of a given platform is assumed to be executable on any machines of the same platform.

## Canonical Use

Expand Down
40 changes: 29 additions & 11 deletions core/src/main/scala-2/com/github/sbt/jni/annotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,36 @@ class nativeLoaderAnnotationMacro(val c: Context) {
}

def determinePlatform(): (String, String) = {
val os = System.getProperty("os.name").toLowerCase match {
case s if s.contains("win") => "windows"
case s if s.contains("mac") => "darwin"
case _ => "linux"
}

val arch = System.getProperty("os.arch").toLowerCase match {
case "arm64" | "aarch64" => "arm64"
case _ => "x86_64"
try {
val line =
try {
scala.io.Source.fromString(scala.sys.process.Process("uname -sm").!!).getLines().next()
} catch {
case _: Exception => sys.error("Error running `uname` command")
}
val parts = line.split(" ")
if (parts.length != 2) {
sys.error("Could not determine platform: 'uname -sm' returned unexpected string: " + line)
} else {
val arch = parts(1).toLowerCase.replaceAll("\\s", "")
val kernel = parts(0).toLowerCase.replaceAll("\\s", "")
(arch, kernel)
}
} catch {
case _: Exception =>
val os = System.getProperty("os.name").toLowerCase match {
case s if s.contains("win") => "windows"
case s if s.contains("mac") => "darwin"
case _ => "linux"
}

val arch = System.getProperty("os.arch").toLowerCase match {
case "arm64" | "aarch64" => "arm64"
case _ => "x86_64"
}

(os, arch)
}

(os, arch)
}

load()
Expand Down
38 changes: 28 additions & 10 deletions core/src/main/scala/com/github/sbt/jni/syntax/NativeLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,36 @@ object NativeLoader {
}

def determinePlatform(): (String, String) = {
val os = System.getProperty("os.name").toLowerCase match {
case s if s.contains("win") => "windows"
case s if s.contains("mac") => "darwin"
case _ => "linux"
}
try {
val line =
try {
scala.io.Source.fromString(scala.sys.process.Process("uname -sm").!!).getLines().next()
} catch {
case _: Exception => sys.error("Error running `uname` command")
}
val parts = line.split(" ")
if (parts.length != 2) {
sys.error("Could not determine platform: 'uname -sm' returned unexpected string: " + line)
} else {
val arch = parts(1).toLowerCase.replaceAll("\\s", "")
val kernel = parts(0).toLowerCase.replaceAll("\\s", "")
(arch, kernel)
}
} catch {
case _: Exception =>
val os = System.getProperty("os.name").toLowerCase match {
case s if s.contains("win") => "windows"
case s if s.contains("mac") => "darwin"
case _ => "linux"
}

val arch = System.getProperty("os.arch").toLowerCase match {
case "arm64" | "aarch64" => "arm64"
case _ => "x86_64"
}
val arch = System.getProperty("os.arch").toLowerCase match {
case "arm64" | "aarch64" => "arm64"
case _ => "x86_64"
}

(os, arch)
(os, arch)
}
}

load()
Expand Down
40 changes: 29 additions & 11 deletions plugin/src/main/scala/com/github/sbt/jni/plugins/JniNative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,36 @@ object JniNative extends AutoPlugin {
)

private def determinePlatform(): (String, String) = {
val os = System.getProperty("os.name").toLowerCase match {
case s if s.contains("win") => "windows"
case s if s.contains("mac") => "darwin"
case _ => "linux"
}

val arch = System.getProperty("os.arch").toLowerCase match {
case "arm64" | "aarch64" => "arm64"
case _ => "x86_64"
try {
val line =
try {
scala.io.Source.fromString(scala.sys.process.Process("uname -sm").!!).getLines().next()
} catch {
case _: Exception => sys.error("Error running `uname` command")
}
val parts = line.split(" ")
if (parts.length != 2) {
sys.error("Could not determine platform: 'uname -sm' returned unexpected string: " + line)
} else {
val arch = parts(1).toLowerCase.replaceAll("\\s", "")
val kernel = parts(0).toLowerCase.replaceAll("\\s", "")
(arch, kernel)
}
} catch {
case _: Exception =>
val os = System.getProperty("os.name").toLowerCase match {
case s if s.contains("win") => "windows"
case s if s.contains("mac") => "darwin"
case _ => "linux"
}

val arch = System.getProperty("os.arch").toLowerCase match {
case "arm64" | "aarch64" => "arm64"
case _ => "x86_64"
}

(os, arch)
}

(os, arch)
}

override lazy val projectSettings = settings
Expand Down

0 comments on commit 4a7740f

Please sign in to comment.