Skip to content

Commit 23d0e37

Browse files
committed
Detect the native architecture on Apple Silicon.
1 parent 11c5a84 commit 23d0e37

File tree

1 file changed

+39
-8
lines changed
  • durian-swt.os/src/main/java/com/diffplug/common/swt/os

1 file changed

+39
-8
lines changed

durian-swt.os/src/main/java/com/diffplug/common/swt/os/OS.java

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
package com.diffplug.common.swt.os;
1717

1818

19+
import java.io.ByteArrayOutputStream;
20+
import java.io.IOException;
21+
import java.io.InputStream;
22+
import java.io.OutputStream;
23+
import java.nio.charset.StandardCharsets;
1924
import java.util.Arrays;
2025
import java.util.Locale;
2126

@@ -107,9 +112,8 @@ private static OS calculateNative() {
107112
boolean isWin = os_name.contains("win");
108113
boolean isMac = os_name.contains("mac");
109114
boolean isLinux = Arrays.asList("nix", "nux", "aix").stream().anyMatch(os_name::contains);
110-
111115
if (isMac) {
112-
return MAC_x64;
116+
return exec("uname", "-a").contains("_ARM64_") ? MAC_silicon : MAC_x64;
113117
} else if (isWin) {
114118
boolean is64bit = System.getenv("ProgramFiles(x86)") != null;
115119
return is64bit ? WIN_x64 : WIN_x86;
@@ -130,15 +134,37 @@ private static OS calculateNative() {
130134
}
131135
}
132136

137+
private static String exec(String... cmd) {
138+
try {
139+
Process process = Runtime.getRuntime().exec(new String[]{"uname", "-a"});
140+
ByteArrayOutputStream output = new ByteArrayOutputStream();
141+
drain(process.getInputStream(), output);
142+
return new String(output.toByteArray(), StandardCharsets.UTF_8);
143+
} catch (IOException e) {
144+
throw new RuntimeException(e);
145+
}
146+
}
147+
148+
private static void drain(InputStream input, OutputStream output) throws IOException {
149+
byte[] buf = new byte[1024];
150+
int numRead;
151+
while ((numRead = input.read(buf)) != -1) {
152+
output.write(buf, 0, numRead);
153+
}
154+
}
155+
133156
private static final OS RUNNING_OS = calculateRunning();
134157

135158
/** Calculates the running OS. */
136159
private static OS calculateRunning() {
137160
Arch runningArch = runningJvm();
138-
return NATIVE_OS.winMacLinux(
139-
runningArch.x86x64(OS.WIN_x86, OS.WIN_x64),
140-
runningArch.x64arm64(OS.MAC_x64, OS.MAC_silicon),
141-
runningArch.x86x64(OS.LINUX_x86, OS.LINUX_x64));
161+
OS runningOs = NATIVE_OS.winMacLinux(
162+
runningArch.x86x64arm64(OS.WIN_x86, OS.WIN_x64, null),
163+
runningArch.x86x64arm64(null, OS.MAC_x64, OS.MAC_silicon),
164+
runningArch.x86x64arm64(OS.LINUX_x86, OS.LINUX_x64, null));
165+
if (runningOs == null) {
166+
throw new IllegalArgumentException("Unsupported OS/Arch combo: " + runningOs + " " + runningArch);
167+
}
142168
}
143169

144170
/** Returns the arch of the currently running JVM. */
@@ -148,14 +174,19 @@ private static Arch runningJvm() {
148174
case "32":
149175
return Arch.x86;
150176
case "64":
151-
return Arch.x64;
177+
return "aarch64".equals(System.getProperty("os.arch")) ? Arch.arm64 : Arch.x64;
152178
default:
153-
throw new IllegalArgumentException(sunArchDataModel);
179+
throw new IllegalArgumentException("Expcted 32 or 64, was " + sunArchDataModel);
154180
}
155181
}
156182

157183
/** Returns an UnsupportedOperationException for the given OS. */
158184
public static UnsupportedOperationException unsupportedException(OS os) {
159185
return new UnsupportedOperationException("Operating system '" + os + "' is not supported.");
160186
}
187+
188+
public static void main(String[] args) {
189+
System.out.println("native=" + OS.getNative());
190+
System.out.println("running=" + OS.getRunning());
191+
}
161192
}

0 commit comments

Comments
 (0)