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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
|
package jogamp.common.os;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.List;
import jogamp.common.Debug;
import jogamp.common.os.elf.ElfHeader;
import jogamp.common.os.elf.SectionArmAttributes;
import jogamp.common.os.elf.SectionHeader;
import com.jogamp.common.nio.Buffers;
import com.jogamp.common.os.AndroidVersion;
import com.jogamp.common.os.NativeLibrary;
import com.jogamp.common.os.Platform;
import com.jogamp.common.os.Platform.ABIType;
import com.jogamp.common.os.Platform.CPUFamily;
import com.jogamp.common.os.Platform.CPUType;
import com.jogamp.common.os.Platform.OSType;
import com.jogamp.common.util.VersionNumber;
/**
* Abstract parent class of {@link Platform} initializing and holding
* platform information, which are initialized independent
* of other classes.
* <p>
* This class is not intended to be exposed in the public namespace
* and solely exist to solve initialization interdependencies.<br>
* Please use {@link Platform} to access the public fields!
* </p>
*/
public abstract class PlatformPropsImpl {
static final boolean DEBUG = Debug.debug("Platform");
/** Selected {@link Platform.OSType#MACOS} {@link VersionNumber}s. */
public static class OSXVersion {
/** OSX Tiger, i.e. 10.4.0 */
public static final VersionNumber Tiger = new VersionNumber(10,4,0);
/** OSX Lion, i.e. 10.7.0 */
public static final VersionNumber Lion = new VersionNumber(10,7,0);
/** OSX Mavericks, i.e. 10.9.0 */
public static final VersionNumber Mavericks = new VersionNumber(10,9,0);
}
//
// static initialization order:
//
/** Version 1.6. As a JVM version, it enables certain JVM 1.6 features. */
public static final VersionNumber Version16;
/** Version 1.7. As a JVM version, it enables certain JVM 1.7 features. */
public static final VersionNumber Version17;
public static final String OS;
public static final String OS_lower;
public static final String OS_VERSION;
public static final VersionNumber OS_VERSION_NUMBER;
public static final String ARCH;
public static final String ARCH_lower;
public static final String JAVA_VENDOR;
public static final String JAVA_VENDOR_URL;
public static final String JAVA_VERSION;
public static final VersionNumber JAVA_VERSION_NUMBER;
public static final int JAVA_VERSION_UPDATE;
public static final String JAVA_VM_NAME;
public static final String JAVA_RUNTIME_NAME;
/** True if having {@link java.nio.LongBuffer} and {@link java.nio.DoubleBuffer} available. */
public static final boolean JAVA_SE;
/** True if being compatible w/ language level 6, e.g. JRE 1.6. Implies {@link #JAVA_SE}. <i>Note</i>: We claim Android is compatible. */
public static final boolean JAVA_6;
public static final String NEWLINE;
public static final boolean LITTLE_ENDIAN;
/* pp */ static final CPUType sCpuType;
public static final CPUType CPU_ARCH;
public static final ABIType ABI_TYPE;
public static final OSType OS_TYPE;
public static final String os_and_arch;
static {
Version16 = new VersionNumber(1, 6, 0);
Version17 = new VersionNumber(1, 7, 0);
// We don't seem to need an AccessController.doPrivileged() block
// here as these system properties are visible even to unsigned Applets.
final boolean isAndroid = AndroidVersion.isAvailable; // also triggers it's static initialization
JAVA_VENDOR = System.getProperty("java.vendor");
JAVA_VENDOR_URL = System.getProperty("java.vendor.url");
JAVA_VERSION = System.getProperty("java.version");
JAVA_VERSION_NUMBER = new VersionNumber(JAVA_VERSION);
{
int usIdx = JAVA_VERSION.lastIndexOf("-u"); // OpenJDK update notation
int usOff;
if( 0 < usIdx ) {
usOff = 2;
} else {
usIdx = JAVA_VERSION.lastIndexOf("_"); // Oracle update notation
usOff = 1;
}
if( 0 < usIdx ) {
final String buildS = PlatformPropsImpl.JAVA_VERSION.substring(usIdx+usOff);
final VersionNumber update = new VersionNumber(buildS);
JAVA_VERSION_UPDATE = update.getMajor();
} else {
JAVA_VERSION_UPDATE = 0;
}
}
JAVA_VM_NAME = System.getProperty("java.vm.name");
JAVA_RUNTIME_NAME = getJavaRuntimeNameImpl();
JAVA_SE = initIsJavaSE();
JAVA_6 = JAVA_SE && ( isAndroid || JAVA_VERSION_NUMBER.compareTo(Version16) >= 0 ) ;
NEWLINE = System.getProperty("line.separator");
OS = System.getProperty("os.name");
OS_lower = OS.toLowerCase();
OS_VERSION = System.getProperty("os.version");
OS_VERSION_NUMBER = new VersionNumber(OS_VERSION);
OS_TYPE = getOSTypeImpl(OS_lower, isAndroid);
LITTLE_ENDIAN = queryIsLittleEndianImpl();
// Soft values, i.e. w/o probing binaries
final String sARCH = System.getProperty("os.arch");
final String sARCH_lower = sARCH.toLowerCase();
sCpuType = getCPUTypeImpl(sARCH_lower);
if( DEBUG ) {
System.err.println("Platform.Soft: str "+sARCH+", cpuType "+sCpuType);
}
// Hard values, i.e. w/ probing binaries
//
// FIXME / HACK:
// We use sCPUType for MachineDescriptionRuntime.getStatic()
// until we have determined the final CPU_TYPE, etc.
// MachineDescriptionRuntime gets notified via MachineDescriptionRuntime.notifyPropsInitialized() below.
//
// We could use Elf Ehdr's machine value to determine the bit-size
// used for it's offset table!
// However, 'os.arch' should be a good guess for this task.
final CPUType ehCpuType;
final ABIType ehAbiType;
final boolean ehValid;
{
final CPUType[] _ehCpuType = { null };
final ABIType[] _ehAbiType = { null };
final ElfHeader eh = queryABITypeImpl(OS_TYPE, _ehCpuType, _ehAbiType);
if( null != eh && null != _ehCpuType[0] && null != _ehAbiType[0] ) {
ehCpuType = _ehCpuType[0];
ehAbiType = _ehAbiType[0];
if( isAndroid ) {
if( DEBUG ) {
System.err.println("Android: CPU_ABI1 str "+AndroidVersion.CPU_ABI+", cpu "+AndroidVersion.CPU_TYPE+", abi "+AndroidVersion.ABI_TYPE);
System.err.println("Android: CPU_ABI2 str "+AndroidVersion.CPU_ABI2+", cpu "+AndroidVersion.CPU_TYPE2+", abi "+AndroidVersion.ABI_TYPE2);
}
final CPUFamily aCpuFamily1 = null != AndroidVersion.CPU_TYPE ? AndroidVersion.CPU_TYPE.family : null;
final CPUFamily aCpuFamily2 = null != AndroidVersion.CPU_TYPE2 ? AndroidVersion.CPU_TYPE2.family : null;
if( ehCpuType.family != aCpuFamily1 && ehCpuType.family != aCpuFamily2 ) {
// Ooops !
ehValid = false;
} else {
ehValid = true;
}
} else {
if( ehCpuType.family != sCpuType.family ) {
// Ooops !
ehValid = false;
} else {
ehValid = true;
}
}
if( DEBUG ) {
System.err.println("Platform.Elf: cpuType "+ehCpuType+", abiType "+ehAbiType+", valid "+ehValid);
}
} else {
ehCpuType = null;
ehAbiType = null;
ehValid = false;
if( DEBUG ) {
System.err.println("Platform.Elf: n/a");
}
}
}
if( isAndroid ) {
if( ehValid ) {
if( ehCpuType.family == AndroidVersion.CPU_TYPE.family ) {
ARCH = AndroidVersion.CPU_ABI;
CPU_ARCH = AndroidVersion.CPU_TYPE;
} else {
ARCH = AndroidVersion.CPU_ABI2;
CPU_ARCH = AndroidVersion.CPU_TYPE2;
}
ABI_TYPE = ehAbiType;
} else {
// default
if( AndroidVersion.CPU_TYPE.family == CPUFamily.ARM || null == AndroidVersion.CPU_TYPE2 ) {
ARCH = AndroidVersion.CPU_ABI;
CPU_ARCH = AndroidVersion.CPU_TYPE;
ABI_TYPE = AndroidVersion.ABI_TYPE;
} else {
ARCH = AndroidVersion.CPU_ABI2;
CPU_ARCH = AndroidVersion.CPU_TYPE2;
ABI_TYPE = AndroidVersion.ABI_TYPE2;
}
}
ARCH_lower = ARCH;
} else {
ARCH = sARCH;
ARCH_lower = sARCH_lower;
if( ehValid && CPUFamily.ARM == ehCpuType.family ) {
// Use Elf for ARM
CPU_ARCH = ehCpuType;
ABI_TYPE = ehAbiType;
} else {
// Otherwise trust detailed os.arch (?)
CPU_ARCH = sCpuType;
ABI_TYPE = ABIType.GENERIC_ABI;
}
}
MachineDescriptionRuntime.notifyPropsInitialized();
os_and_arch = getOSAndArch(OS_TYPE, CPU_ARCH, ABI_TYPE);
}
protected PlatformPropsImpl() {}
private static final String getJavaRuntimeNameImpl() {
// the fast path, check property Java SE instead of traversing through the ClassLoader
return AccessController.doPrivileged(new PrivilegedAction<String>() {
@Override
public String run() {
return System.getProperty("java.runtime.name");
}
});
}
private static final boolean initIsJavaSE() {
if( null != JAVA_RUNTIME_NAME && JAVA_RUNTIME_NAME.indexOf("Java SE") != -1) {
return true;
}
// probe for classes we need on a SE environment
try {
Class.forName("java.nio.LongBuffer");
Class.forName("java.nio.DoubleBuffer");
return true;
} catch(final ClassNotFoundException ex) {
// continue with Java SE check
}
return false;
}
private static final boolean queryIsLittleEndianImpl() {
final ByteBuffer tst_b = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT); // 32bit in native order
final IntBuffer tst_i = tst_b.asIntBuffer();
final ShortBuffer tst_s = tst_b.asShortBuffer();
tst_i.put(0, 0x0A0B0C0D);
return 0x0C0D == tst_s.get(0);
}
private static final CPUType getCPUTypeImpl(final String archLower) {
if( archLower.equals("x86") || // jvm + android
archLower.equals("i386") ||
archLower.equals("i486") ||
archLower.equals("i586") ||
archLower.equals("i686") ) {
return CPUType.X86_32;
} else if( archLower.equals("x86_64") ||
archLower.equals("amd64") ) {
return CPUType.X86_64;
} else if( archLower.equals("ia64") ) {
return CPUType.IA64;
} else if( archLower.equals("arm") ) {
return CPUType.ARM;
} else if( archLower.equals("armv5l") ) {
return CPUType.ARMv5;
} else if( archLower.equals("armv6l") ) {
return CPUType.ARMv6;
} else if( archLower.equals("armv7l") ||
archLower.equals("armeabi") || // android
archLower.equals("armeabi-v7a") ) { // android
return CPUType.ARMv7;
} else if( archLower.equals("sparc") ) {
return CPUType.SPARC_32;
} else if( archLower.equals("sparcv9") ) {
return CPUType.SPARCV9_64;
} else if( archLower.equals("pa_risc2.0") ) {
return CPUType.PA_RISC2_0;
} else if( archLower.equals("ppc") ) {
return CPUType.PPC;
} else if( archLower.equals("mips") ) { // android
return CPUType.MIPS_32;
} else {
throw new RuntimeException("Please port CPU detection to your platform (" + OS_lower + "/" + archLower + ")");
}
}
@SuppressWarnings("unused")
private static final boolean contains(final String data, final String[] search) {
if(null != data && null != search) {
for(int i=0; i<search.length; i++) {
if(data.indexOf(search[i]) >= 0) {
return true;
}
}
}
return false;
}
/**
* Returns the {@link ABIType} of the current platform using given {@link CPUType cpuType}
* and {@link OSType osType} as a hint.
* <p>
* For Elf parsing one of the following binaries is used:
* <ul>
* <li>Linux: Current executable</li>
* <li>Android: Found gluegen-rt library</li>
* <li>Other: A found java/jvm native library.</li>
* </ul>
* </p>
* <p>
* Elf ARM Tags are read using {@link ElfHeader}, .. and {@link SectionArmAttributes#abiVFPArgsAcceptsVFPVariant(byte)}.
* </p>
*/
private static final ElfHeader queryABITypeImpl(final OSType osType, final CPUType[] cpuType, final ABIType[] abiType) {
return AccessController.doPrivileged(new PrivilegedAction<ElfHeader>() {
@Override
public ElfHeader run() {
ElfHeader res = null;
try {
File file = null;
if( OSType.ANDROID == osType ) {
file = new File(NativeLibrary.findLibrary("gluegen-rt", PlatformPropsImpl.class.getClassLoader()));
} else {
if( OSType.LINUX == osType ) {
file = new File("/proc/self/exe");
if( !checkFileReadAccess(file) ) {
file = null;
}
}
if( null == file ) {
file = findSysLib("java");
}
if( null == file ) {
file = findSysLib("jvm");
}
}
if( null != file ) {
res = queryABITypeImpl(file, cpuType, abiType);
}
} catch(final Throwable t) {
if(DEBUG) {
t.printStackTrace();
}
}
return res;
} } );
}
private static final ElfHeader queryABITypeImpl(final File file, final CPUType[] cpuType, final ABIType[] abiType) {
ElfHeader res = null;
RandomAccessFile in = null;
try {
in = new RandomAccessFile(file, "r");
final ElfHeader eh = ElfHeader.read(in);
if(DEBUG) {
System.err.println("ELF: Got HDR "+file+": "+eh);
}
if( eh.isArm() ) {
boolean abiVFPArgsAcceptsVFPVariant = false;
final SectionHeader sh = eh.getSectionHeader(SectionHeader.SHT_ARM_ATTRIBUTES);
if( null != sh ) {
if(DEBUG) {
System.err.println("ELF: Got ARM Attribs Section Header: "+sh);
}
final SectionArmAttributes sArmAttrs = (SectionArmAttributes) sh.readSection(in);
if(DEBUG) {
System.err.println("ELF: Got ARM Attribs Section Block : "+sArmAttrs);
}
final SectionArmAttributes.Attribute abiVFPArgsAttr = sArmAttrs.get(SectionArmAttributes.Tag.ABI_VFP_args);
if( null != abiVFPArgsAttr ) {
abiVFPArgsAcceptsVFPVariant = SectionArmAttributes.abiVFPArgsAcceptsVFPVariant(abiVFPArgsAttr.getULEB128());
}
}
cpuType[0] = CPUType.ARM; // lowest denominator, ok for us
abiType[0] = abiVFPArgsAcceptsVFPVariant ? ABIType.EABI_GNU_ARMHF : ABIType.EABI_GNU_ARMEL;
if(DEBUG) {
System.err.println("ELF: abiARM, abiVFPArgsAcceptsVFPVariant "+abiVFPArgsAcceptsVFPVariant);
}
} else if ( eh.isX86_64() ) {
cpuType[0] = CPUType.X86_64;
abiType[0] = ABIType.GENERIC_ABI;
} else if ( eh.isX86_32() ) {
cpuType[0] = CPUType.X86_32;
abiType[0] = ABIType.GENERIC_ABI;
} else if ( eh.isIA64() ) {
cpuType[0] = CPUType.IA64;
abiType[0] = ABIType.GENERIC_ABI;
} else if ( eh.isMips() ) {
cpuType[0] = CPUType.MIPS_32; // FIXME
abiType[0] = ABIType.GENERIC_ABI;
}
res = eh;
} catch(final Throwable t) {
if(DEBUG) {
System.err.println("Caught: "+t.getMessage());
t.printStackTrace();
}
} finally {
if(null != in) {
try {
in.close();
} catch (final IOException e) { }
}
}
if(DEBUG) {
System.err.println("ELF: res "+res+", cpuType "+cpuType[0]+", abiType "+abiType[0]);
}
return res;
}
private static boolean checkFileReadAccess(final File file) {
try {
return file.isFile() && file.canRead();
} catch (final Throwable t) { }
return false;
}
private static File findSysLib(final String libName) {
final ClassLoader cl = PlatformPropsImpl.class.getClassLoader();
final List<String> possibleLibPaths = NativeLibrary.enumerateLibraryPaths(libName, libName, libName, true, cl);
for(int i=0; i<possibleLibPaths.size(); i++) {
final String libPath = possibleLibPaths.get(i);
final File lib = new File(libPath);
if(DEBUG) {
System.err.println("findSysLib #"+i+": test "+lib);
}
if( checkFileReadAccess(lib) ) {
return lib;
}
if(DEBUG) {
System.err.println("findSysLib #"+i+": "+lib+" not readable");
}
}
return null;
}
private static final OSType getOSTypeImpl(final String osLower, final boolean isAndroid) throws RuntimeException {
if ( isAndroid ) {
return OSType.ANDROID;
}
if ( osLower.startsWith("linux") ) {
return OSType.LINUX;
}
if ( osLower.startsWith("freebsd") ) {
return OSType.FREEBSD;
}
if ( osLower.startsWith("android") ) {
return OSType.ANDROID;
}
if ( osLower.startsWith("mac os x") ||
osLower.startsWith("darwin") ) {
return OSType.MACOS;
}
if ( osLower.startsWith("sunos") ) {
return OSType.SUNOS;
}
if ( osLower.startsWith("hp-ux") ) {
return OSType.HPUX;
}
if ( osLower.startsWith("windows") ) {
return OSType.WINDOWS;
}
if ( osLower.startsWith("kd") ) {
return OSType.OPENKODE;
}
throw new RuntimeException("Please port OS detection to your platform (" + OS_lower + "/" + ARCH_lower + ")");
}
/**
* kick off static initialization of <i>platform property information</i>
*/
public static void initSingleton() { }
/**
* Returns the GlueGen common name for the given OSType and CPUType
* as implemented in the build system in 'gluegen-cpptasks-base.xml'.<br>
*
* A list of currently supported <code>os.and.arch</code> strings:
* <ul>
* <li>freebsd-i586</li>
* <li>freebsd-amd64</li>
* <li>hpux-hppa</li>
* <li>linux-amd64</li>
* <li>linux-ia64</li>
* <li>linux-i586</li>
* <li>linux-armv6</li>
* <li>linux-armv6hf</li>
* <li>android-armv6</li>
* <li>macosx-universal</li>
* <li>solaris-sparc</li>
* <li>solaris-sparcv9</li>
* <li>solaris-amd64</li>
* <li>solaris-i586</li>
* <li>windows-amd64</li>
* <li>windows-i586</li>
* </ul>
* @return
*/
public static final String getOSAndArch(final OSType osType, final CPUType cpuType, final ABIType abiType) {
String _os_and_arch;
switch( cpuType ) {
case X86_32:
_os_and_arch = "i586";
break;
case ARM:
case ARMv5:
case ARMv6:
case ARMv7:
_os_and_arch = "armv6"; // TODO: sync with gluegen-cpptasks-base.xml
break;
case SPARC_32:
_os_and_arch = "sparc";
break;
case PPC:
_os_and_arch = "ppc"; // TODO: sync with gluegen-cpptasks-base.xml
break;
case X86_64:
_os_and_arch = "amd64";
break;
case IA64:
_os_and_arch = "ia64";
break;
case SPARCV9_64:
_os_and_arch = "sparcv9";
break;
case PA_RISC2_0:
_os_and_arch = "risc2.0"; // TODO: sync with gluegen-cpptasks-base.xml
break;
default:
throw new InternalError("Complete case block");
}
if( ABIType.EABI_GNU_ARMHF == abiType ) {
_os_and_arch = _os_and_arch + "hf" ;
}
switch( osType ) {
case ANDROID:
_os_and_arch = "android-" + _os_and_arch;
break;
case MACOS:
_os_and_arch = "macosx-universal";
break;
case WINDOWS:
_os_and_arch = "windows-" + _os_and_arch;
break;
case OPENKODE:
_os_and_arch = "openkode-" + _os_and_arch; // TODO: think about that
break;
case LINUX:
_os_and_arch = "linux-" + _os_and_arch;
break;
case FREEBSD:
_os_and_arch = "freebsd-" + _os_and_arch;
break;
case SUNOS:
_os_and_arch = "solaris-" + _os_and_arch;
break;
case HPUX:
_os_and_arch = "hpux-hppa"; // TODO: really only hppa ?
break;
default:
throw new InternalError("Complete case block");
}
return _os_and_arch;
}
}
|