summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/sf/antcontrib/cpptasks/Platform.java
blob: 76d2cb0c17ef2c16feb82e49fb5a571b43a6b94b (plain)
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
package net.sf.antcontrib.cpptasks;

/**
 * Platform provides certain OS and architecture properties of the runtime host platform.
 */
public class Platform {
    /**
     * The architecture name, , i.e. Java's `os.arch` property.
     */
    public static final String OS_ARCH;

    /**
     * The OS name, , i.e. Java's `os.name` property.
     * <p>In case of {@link OSType#ANDROID}, see {@link #OS_TYPE}, the OS name is Linux</p>
     */
    public static final String OS_NAME;

    /**
     * The OS version string, i.e. Java's `os.version` property.
     */
    public static final String OS_VERSION;

    /**
     * The OS type, derived from {@link #OS_NAME}
     * <p>In case of {@link OSType#ANDROID} the {@link #getOSName() OS name}, is Linux</p>
     */
    public static final OSType OS_TYPE;

    static {
        OS_ARCH = System.getProperty("os.arch");
        OS_NAME =  System.getProperty("os.name");
        OS_VERSION =  System.getProperty("os.version");
        OS_TYPE = getOSTypeImpl(OS_NAME.toLowerCase(), false /* isAndroid */);
    }

    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("windows") ) {
            return OSType.WINDOWS;
        }
        if ( osLower.startsWith("kd") ) {
            return OSType.OPENKODE;
        }
        if ( osLower.startsWith("ios") ) {
            return OSType.IOS;
        }
        if ( osLower.startsWith("hp-ux") ||
             ( osLower.startsWith("hp") && osLower.indexOf("ux") >= 0 )
           )
        {
            return OSType.HPUX;
        }
        throw new RuntimeException("Please port OS detection to your platform (" + OS_NAME + ")");
    }
}