16
16
package com .diffplug .common .swt .os ;
17
17
18
18
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 ;
19
24
import java .util .Arrays ;
20
25
import java .util .Locale ;
21
26
@@ -107,9 +112,8 @@ private static OS calculateNative() {
107
112
boolean isWin = os_name .contains ("win" );
108
113
boolean isMac = os_name .contains ("mac" );
109
114
boolean isLinux = Arrays .asList ("nix" , "nux" , "aix" ).stream ().anyMatch (os_name ::contains );
110
-
111
115
if (isMac ) {
112
- return MAC_x64 ;
116
+ return exec ( "uname" , "-a" ). contains ( "_ARM64_" ) ? MAC_silicon : MAC_x64 ;
113
117
} else if (isWin ) {
114
118
boolean is64bit = System .getenv ("ProgramFiles(x86)" ) != null ;
115
119
return is64bit ? WIN_x64 : WIN_x86 ;
@@ -130,15 +134,37 @@ private static OS calculateNative() {
130
134
}
131
135
}
132
136
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
+
133
156
private static final OS RUNNING_OS = calculateRunning ();
134
157
135
158
/** Calculates the running OS. */
136
159
private static OS calculateRunning () {
137
160
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
+ }
142
168
}
143
169
144
170
/** Returns the arch of the currently running JVM. */
@@ -148,14 +174,19 @@ private static Arch runningJvm() {
148
174
case "32" :
149
175
return Arch .x86 ;
150
176
case "64" :
151
- return Arch .x64 ;
177
+ return "aarch64" . equals ( System . getProperty ( "os.arch" )) ? Arch . arm64 : Arch .x64 ;
152
178
default :
153
- throw new IllegalArgumentException (sunArchDataModel );
179
+ throw new IllegalArgumentException ("Expcted 32 or 64, was " + sunArchDataModel );
154
180
}
155
181
}
156
182
157
183
/** Returns an UnsupportedOperationException for the given OS. */
158
184
public static UnsupportedOperationException unsupportedException (OS os ) {
159
185
return new UnsupportedOperationException ("Operating system '" + os + "' is not supported." );
160
186
}
187
+
188
+ public static void main (String [] args ) {
189
+ System .out .println ("native=" + OS .getNative ());
190
+ System .out .println ("running=" + OS .getRunning ());
191
+ }
161
192
}
0 commit comments