Skip to content

Commit

Permalink
Fix broken platform detection (#32)
Browse files Browse the repository at this point in the history
* Fix broken platform detection

- this also changes the platform suffix for windows from `x86_64-msys_nt-10.0-20348` to `x86_64-windows`

Fixes #31

* understand more arch options

* exception handling
  • Loading branch information
oyvindberg authored Jan 31, 2023
1 parent c95a9d7 commit cc21d7a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 2 additions & 0 deletions crossterm/src/java/tui/crossterm/CrosstermJni.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public class CrosstermJni {
static {
try {
NativeLoader.load("crossterm");
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand Down
37 changes: 19 additions & 18 deletions crossterm/src/java/tui/crossterm/NativeLoader.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package tui.crossterm;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.util.Arrays.asList;

class NativeLoader {
public static void load(String nativeLibrary) throws Exception {
try {
Expand All @@ -15,7 +12,7 @@ public static void load(String nativeLibrary) throws Exception {
}
}

static String withPlatformName(String lib) throws IOException, InterruptedException {
static String withPlatformName(String lib) {
if (System.getProperty("org.graalvm.nativeimage.imagecode") != null)
return "/" + lib;
else {
Expand Down Expand Up @@ -46,20 +43,24 @@ static void loadPackaged(String nativeLibrary) throws Exception {
System.load(extractedPath.toAbsolutePath().toString());
}

private static String getPlatform() throws IOException, InterruptedException {
var process = new ProcessBuilder(asList("uname", "-sm")).start();
var ret = process.waitFor();
if (ret != 0) {
throw new RuntimeException("Error running `uname` command: " + ret);
}
String uname = new String(process.getInputStream().readAllBytes());
String line = uname.split("\n")[0];
String[] parts = line.split(" ");
if (parts.length != 2) {
throw new RuntimeException("Could not determine platform: 'uname -sm' returned unexpected string: " + line);
private static String getPlatform() {
if (System.getenv().containsKey("TUI_SCALA_PLATFORM")) {
return System.getenv().get("TUI_SCALA_PLATFORM");
}
String arch = parts[1].toLowerCase().replaceAll("\\s", "");
String kernel = parts[0].toLowerCase().replaceAll("\\s", "");
return arch + "-" + kernel;
String arch = System.getProperty("os.arch");
String name = System.getProperty("os.name");
String nameLower = name.toLowerCase();
boolean isAmd64 = arch.equals("x86_64") || arch.equals("amd64");
boolean isArm64 = arch.equals("aarch64") || arch.equals("arm64");

if (isAmd64 && nameLower.contains("win")) return "x86_64-windows";
if (isAmd64 && nameLower.contains("lin")) return "x86_64-linux";
if (isAmd64 && nameLower.contains("mac")) return "x86_64-darwin";
if (isArm64 && nameLower.contains("mac")) return "arm64-darwin";
throw new RuntimeException(
"Platform detection does not understand os.name = " + name + " and os.arch = " + arch + ". " +
"You can set environment variable TUI_SCALA_PLATFORM to x86_64-windows, x86_64-linux, x86_64-darwin, arm64-darwin to override. " +
"Open an issue at https://github.com/oyvindberg/tui-scala/issues ."
);
}
}
2 changes: 1 addition & 1 deletion scripts/src/scala/tui/scripts/GenJniLibrary.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ object GenJniLibrary extends BleepScript("GenJniLibrary") {
override lazy val nativePlatform: String =
OsArch.current match {
case OsArch.LinuxAmd64 => "x86_64-linux"
case OsArch.WindowsAmd64 => "x86_64-msys_nt-10.0-20348"
case OsArch.WindowsAmd64 => "x86_64-windows"
case OsArch.MacosAmd64 => "x86_64-darwin"
case OsArch.MacosArm64(_) => "arm64-darwin"
case other: OsArch.Other => sys.error(s"not implemented: $other")
Expand Down

0 comments on commit cc21d7a

Please sign in to comment.