forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Platform.java
475 lines (429 loc) · 14.9 KB
/
Platform.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/***** BEGIN LICENSE BLOCK *****
* Version: EPL 2.0/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Eclipse Public
* License Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.eclipse.org/legal/epl-v20.html
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2008 JRuby project
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the EPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the EPL, the GPL or the LGPL.
***** END LICENSE BLOCK *****/
package org.jruby.ext.ffi;
import java.nio.ByteOrder;
import java.util.regex.Pattern;
import org.jruby.Ruby;
import org.jruby.RubyModule;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ThreadContext;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.SafePropertyAccessor;
/**
*
*/
public class Platform {
private static final java.util.Locale LOCALE = java.util.Locale.ENGLISH;
public static final CPU_TYPE CPU = determineCPU();
public static final OS_TYPE OS = determineOS();
private static final int ARCH_DATA_MODEL = determineArchDataModel();
public static final String NAME = CPU + "-" + OS;
public static final String LIBPREFIX = OS == OS.WINDOWS ? "" : "lib";
public static final String LIBSUFFIX = determineLibExt();
public static final String LIBC = determineLibC();
public static final int BIG_ENDIAN = 4321;
public static final int LITTLE_ENDIAN = 1234;
public static final int BYTE_ORDER = ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN) ? BIG_ENDIAN : LITTLE_ENDIAN;
protected final int addressSize, longSize;
private final long addressMask;
protected final Pattern libPattern;
private int javaVersionMajor = -1;
public enum OS_TYPE {
DARWIN,
FREEBSD,
NETBSD,
OPENBSD,
DRAGONFLYBSD,
LINUX,
SOLARIS,
AIX,
WINDOWS,
UNKNOWN;
@Override
public String toString() { return name().toLowerCase(LOCALE); }
}
public enum CPU_TYPE {
I386,
X86_64,
POWERPC,
POWERPC64,
POWERPC64LE,
SPARC,
SPARCV9,
S390X,
ARM,
AARCH64,
UNKNOWN;
@Override
public String toString() { return name().toLowerCase(LOCALE); }
}
private static final class SingletonHolder {
private static final Platform PLATFORM = determinePlatform(determineOS());
}
private static final OS_TYPE determineOS() {
String osName = System.getProperty("os.name").split(" ")[0];
if (startsWithIgnoreCase(osName, "mac") || startsWithIgnoreCase(osName, "darwin")) {
return OS_TYPE.DARWIN;
} else if (startsWithIgnoreCase(osName, "sunos") || startsWithIgnoreCase(osName, "solaris")) {
return OS_TYPE.SOLARIS;
}
for (OS_TYPE os : OS_TYPE.values()) {
if (startsWithIgnoreCase(osName, os.toString())) {
return os;
}
}
return OS_TYPE.UNKNOWN;
}
private static final Platform determinePlatform(OS_TYPE os) {
switch (os) {
case DARWIN:
return new Darwin();
case LINUX:
return new Linux();
case AIX:
return new AIX();
case WINDOWS:
return new Windows();
case UNKNOWN:
return new Unsupported(os);
default:
return new Default(os);
}
}
private static final CPU_TYPE determineCPU() {
String archString = System.getProperty("os.arch").toLowerCase(LOCALE);
if ("x86".equals(archString) || "i386".equals(archString) || "i86pc".equals(archString)) {
return CPU.I386;
} else if ("x86_64".equals(archString) || "amd64".equals(archString)) {
return CPU.X86_64;
} else if ("ppc".equals(archString) || "powerpc".equals(archString)) {
return CPU.POWERPC;
} else if ("ppc64".equals(archString)) {
return CPU.POWERPC64;
} else if ("ppc64le".equals(archString)) {
return CPU.POWERPC64LE;
} else if ("sparc".equals(archString)) {
return CPU.SPARC;
} else if ("sparcv9".equals(archString)) {
return CPU.SPARCV9;
} else if ("s390x".equals(archString)) {
return CPU.S390X;
} else if ("arm".equals(archString)) {
return CPU.ARM;
} else if ("aarch64".equals(archString)) {
return CPU.AARCH64;
} else if ("universal".equals(archString)) {
// OS X OpenJDK7 builds report "universal" right now
String bits = SafePropertyAccessor.getProperty("sun.arch.data.model");
if ("32".equals(bits)) {
System.setProperty("os.arch", "i386");
return CPU.I386;
} else if ("64".equals(bits)) {
System.setProperty("os.arch", "x86_64");
return CPU.X86_64;
}
}
return CPU.UNKNOWN;
}
private static final String determineLibC() {
switch (OS) {
case WINDOWS:
return "msvcrt.dll";
case LINUX:
return "libc.so.6";
case AIX:
if (ARCH_DATA_MODEL == 32) {
return "libc.a(shr.o)";
} else {
return "libc.a(shr_64.o)";
}
default:
return LIBPREFIX + "c." + LIBSUFFIX;
}
}
private static final String determineLibExt() {
switch (OS) {
case WINDOWS:
return "dll";
case AIX:
return "a";
case DARWIN:
return "dylib";
default:
return "so";
}
}
private static int determineArchDataModel() {
int dataModel = SafePropertyAccessor.getInt("sun.arch.data.model"); // default: 0
if (dataModel != 32 && dataModel != 64) {
switch (CPU) {
case I386:
case POWERPC:
case SPARC:
dataModel = 32;
break;
case X86_64:
case POWERPC64:
case POWERPC64LE:
case SPARCV9:
case S390X:
case AARCH64:
dataModel = 64;
break;
default:
dataModel = 0;
}
}
return dataModel;
}
protected Platform(OS_TYPE os) {
addressSize = ARCH_DATA_MODEL;
addressMask = addressSize == 32 ? 0xffffffffL : 0xffffffffffffffffL;
longSize = os == OS.WINDOWS ? 32 : addressSize; // Windows is LLP64
String libpattern;
switch (os) {
case WINDOWS:
libpattern = ".*\\.dll$";
break;
case DARWIN:
libpattern = "lib.*\\.(dylib|jnilib)$";
break;
case AIX:
libpattern = "lib.*\\.a$";
break;
default:
libpattern = "lib.*\\.so.*$";
break;
}
libPattern = Pattern.compile(libpattern);
}
/**
* Gets the current <tt>Platform</tt>
*
* @return The current platform.
*/
public static final Platform getPlatform() {
return SingletonHolder.PLATFORM;
}
/**
* Gets the current Operating System.
*
* @return A <tt>OS</tt> value representing the current Operating System.
*/
public final OS_TYPE getOS() {
return OS;
}
/**
* Gets the current processor architecture the JVM is running on.
*
* @return A <tt>CPU</tt> value representing the current processor architecture.
*/
public final CPU_TYPE getCPU() {
return CPU;
}
/**
* Gets the version of the Java Virtual Machine (JVM) jffi is running on.
*
* @return A number representing the java version. e.g. 8 for java 1.8, 9 for java 9
*/
public final int getJavaMajorVersion() {
if (javaVersionMajor != -1) return javaVersionMajor;
int version = 5;
try {
String versionString = SafePropertyAccessor.getProperty("java.version");
if (versionString != null) {
// remove additional version identifiers, e.g. -ea
versionString = versionString.split("-|\\+")[0];
String[] v = versionString.split("\\.");
if (v[0].equals("1")) {
// Pre Java 9, 1.x style
version = Integer.valueOf(v[1]);
} else {
// Java 9+, x.y.z style
version = Integer.valueOf(v[0]);
}
}
} catch (Exception ex) {
version = 0;
}
return javaVersionMajor = version;
}
public final boolean isBSD() {
return OS == OS.FREEBSD || OS == OS.OPENBSD || OS == OS.NETBSD || OS == OS.DARWIN || OS == OS.DRAGONFLYBSD;
}
public final boolean isUnix() {
return OS != OS.WINDOWS;
}
public final boolean isSupported() {
return OS != OS.UNKNOWN
&& CPU != CPU.UNKNOWN
&& (addressSize == 32 || addressSize == 64);
}
public static void createPlatformModule(Ruby runtime, RubyModule ffi) {
RubyModule module = ffi.defineModuleUnder("Platform");
Platform platform = Platform.getPlatform();
OS_TYPE os = platform.getOS();
module.defineConstant("ADDRESS_SIZE", runtime.newFixnum(platform.addressSize));
module.defineConstant("LONG_SIZE", runtime.newFixnum(platform.longSize));
module.defineConstant("IS_DRAGONFLYBSD", runtime.newBoolean(os == OS.DRAGONFLYBSD));
module.defineConstant("BYTE_ORDER", runtime.newFixnum(BYTE_ORDER));
module.defineConstant("BIG_ENDIAN", runtime.newFixnum(BIG_ENDIAN));
module.defineConstant("LITTLE_ENDIAN", runtime.newFixnum(LITTLE_ENDIAN));
module.defineAnnotatedMethods(Platform.class);
}
/**
* An extension over <code>System.getProperty</code> method.
* Handles security restrictions, and returns the default
* value if the access to the property is restricted.
* @param property The system property name.
* @param defValue The default value.
* @return The value of the system property,
* or the default value.
*/
public static String getProperty(String property, String defValue) {
try {
return System.getProperty(property, defValue);
} catch (SecurityException se) {
return defValue;
}
}
/**
* Gets the size of a C 'long' on the native platform.
*
* @return the size of a long in bits
*/
public final int longSize() {
return longSize;
}
/**
* Gets the size of a C address/pointer on the native platform.
*
* @return the size of a pointer in bits
*/
public final int addressSize() {
return addressSize;
}
/**
* Gets the 32/64bit mask of a C address/pointer on the native platform.
*
* @return the size of a pointer in bits
*/
public final long addressMask() {
return addressMask;
}
/**
* Gets the name of this <tt>Platform</tt>.
*
* @return The name of this platform.
*/
public String getName() {
return CPU + "-" + OS;
}
public String mapLibraryName(String libName) {
//
// A specific version was requested - use as is for search
//
if (libPattern.matcher(libName).find()) {
return libName;
}
return System.mapLibraryName(libName);
}
private static class Supported extends Platform {
public Supported(OS_TYPE os) {
super(os);
}
}
private static class Unsupported extends Platform {
public Unsupported(OS_TYPE os) {
super(os);
}
}
private static final class Default extends Platform {
public Default(OS_TYPE os) {
super(os);
}
}
/**
* A {@link Platform} subclass representing the MacOS system.
*/
private static final class Darwin extends Supported {
public Darwin() {
super(OS.DARWIN);
}
@Override
public String mapLibraryName(String libName) {
//
// A specific version was requested - use as is for search
//
if (libPattern.matcher(libName).find()) {
return libName;
}
return "lib" + libName + ".dylib";
}
}
/**
* A {@link Platform} subclass representing the Linux operating system.
*/
private static final class Linux extends Supported {
public Linux() {
super(OS.LINUX);
}
@Override
public String mapLibraryName(String libName) {
// Older JDK on linux map 'c' to 'libc.so' which doesn't work
return "c".equals(libName) || "libc.so".equals(libName)
? "libc.so.6" : super.mapLibraryName(libName);
}
}
/**
* A {@link Platform} subclass representing the Linux operating system.
*/
private static final class AIX extends Supported {
public AIX() {
super(OS.AIX);
}
@Override
public String mapLibraryName(String libName) {
return "c".equals(libName) || "libc.so".equals(libName)
? LIBC : super.mapLibraryName(libName);
}
}
/**
* A {@link Platform} subclass representing the Windows system.
*/
private static class Windows extends Supported {
public Windows() {
super(OS.WINDOWS);
}
}
private static boolean startsWithIgnoreCase(String s1, String s2) {
return s1.startsWith(s2)
|| s1.toUpperCase(LOCALE).startsWith(s2.toUpperCase(LOCALE))
|| s1.toLowerCase(LOCALE).startsWith(s2.toLowerCase(LOCALE));
}
}