]>
Commit | Line | Data |
---|---|---|
f67539c2 | 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
7c673cae FG |
2 | package org.rocksdb.util; |
3 | ||
f67539c2 TL |
4 | import java.io.File; |
5 | import java.io.IOException; | |
6 | ||
7c673cae FG |
7 | public class Environment { |
8 | private static String OS = System.getProperty("os.name").toLowerCase(); | |
9 | private static String ARCH = System.getProperty("os.arch").toLowerCase(); | |
f67539c2 TL |
10 | private static boolean MUSL_LIBC; |
11 | ||
12 | static { | |
13 | try { | |
14 | final Process p = new ProcessBuilder("/usr/bin/env", "sh", "-c", "ldd /usr/bin/env | grep -q musl").start(); | |
15 | MUSL_LIBC = p.waitFor() == 0; | |
16 | } catch (final IOException | InterruptedException e) { | |
17 | MUSL_LIBC = false; | |
18 | } | |
19 | } | |
20 | ||
21 | public static boolean isAarch64() { | |
22 | return ARCH.contains("aarch64"); | |
23 | } | |
7c673cae FG |
24 | |
25 | public static boolean isPowerPC() { | |
26 | return ARCH.contains("ppc"); | |
27 | } | |
28 | ||
11fdf7f2 TL |
29 | public static boolean isS390x() { |
30 | return ARCH.contains("s390x"); | |
31 | } | |
32 | ||
7c673cae FG |
33 | public static boolean isWindows() { |
34 | return (OS.contains("win")); | |
35 | } | |
36 | ||
11fdf7f2 TL |
37 | public static boolean isFreeBSD() { |
38 | return (OS.contains("freebsd")); | |
39 | } | |
40 | ||
7c673cae FG |
41 | public static boolean isMac() { |
42 | return (OS.contains("mac")); | |
43 | } | |
44 | ||
45 | public static boolean isAix() { | |
46 | return OS.contains("aix"); | |
47 | } | |
48 | ||
49 | public static boolean isUnix() { | |
50 | return OS.contains("nix") || | |
51 | OS.contains("nux"); | |
52 | } | |
53 | ||
f67539c2 TL |
54 | public static boolean isMuslLibc() { |
55 | return MUSL_LIBC; | |
56 | } | |
57 | ||
7c673cae FG |
58 | public static boolean isSolaris() { |
59 | return OS.contains("sunos"); | |
60 | } | |
61 | ||
11fdf7f2 TL |
62 | public static boolean isOpenBSD() { |
63 | return (OS.contains("openbsd")); | |
64 | } | |
65 | ||
7c673cae FG |
66 | public static boolean is64Bit() { |
67 | if (ARCH.indexOf("sparcv9") >= 0) { | |
68 | return true; | |
69 | } | |
70 | return (ARCH.indexOf("64") > 0); | |
71 | } | |
72 | ||
73 | public static String getSharedLibraryName(final String name) { | |
74 | return name + "jni"; | |
75 | } | |
76 | ||
77 | public static String getSharedLibraryFileName(final String name) { | |
78 | return appendLibOsSuffix("lib" + getSharedLibraryName(name), true); | |
79 | } | |
80 | ||
f67539c2 TL |
81 | /** |
82 | * Get the name of the libc implementation | |
83 | * | |
84 | * @return the name of the implementation, | |
85 | * or null if the default for that platform (e.g. glibc on Linux). | |
86 | */ | |
87 | public static /* @Nullable */ String getLibcName() { | |
88 | if (isMuslLibc()) { | |
89 | return "musl"; | |
90 | } else { | |
91 | return null; | |
92 | } | |
93 | } | |
94 | ||
95 | private static String getLibcPostfix() { | |
96 | final String libcName = getLibcName(); | |
97 | if (libcName == null) { | |
98 | return ""; | |
99 | } | |
100 | return "-" + libcName; | |
101 | } | |
102 | ||
7c673cae FG |
103 | public static String getJniLibraryName(final String name) { |
104 | if (isUnix()) { | |
105 | final String arch = is64Bit() ? "64" : "32"; | |
f67539c2 TL |
106 | if (isPowerPC() || isAarch64()) { |
107 | return String.format("%sjni-linux-%s%s", name, ARCH, getLibcPostfix()); | |
108 | } else if (isS390x()) { | |
11fdf7f2 | 109 | return String.format("%sjni-linux%s", name, ARCH); |
7c673cae | 110 | } else { |
f67539c2 | 111 | return String.format("%sjni-linux%s%s", name, arch, getLibcPostfix()); |
7c673cae FG |
112 | } |
113 | } else if (isMac()) { | |
114 | return String.format("%sjni-osx", name); | |
11fdf7f2 TL |
115 | } else if (isFreeBSD()) { |
116 | return String.format("%sjni-freebsd%s", name, is64Bit() ? "64" : "32"); | |
7c673cae FG |
117 | } else if (isAix() && is64Bit()) { |
118 | return String.format("%sjni-aix64", name); | |
119 | } else if (isSolaris()) { | |
120 | final String arch = is64Bit() ? "64" : "32"; | |
121 | return String.format("%sjni-solaris%s", name, arch); | |
122 | } else if (isWindows() && is64Bit()) { | |
123 | return String.format("%sjni-win64", name); | |
11fdf7f2 TL |
124 | } else if (isOpenBSD()) { |
125 | return String.format("%sjni-openbsd%s", name, is64Bit() ? "64" : "32"); | |
7c673cae FG |
126 | } |
127 | ||
128 | throw new UnsupportedOperationException(String.format("Cannot determine JNI library name for ARCH='%s' OS='%s' name='%s'", ARCH, OS, name)); | |
129 | } | |
130 | ||
131 | public static String getJniLibraryFileName(final String name) { | |
132 | return appendLibOsSuffix("lib" + getJniLibraryName(name), false); | |
133 | } | |
134 | ||
135 | private static String appendLibOsSuffix(final String libraryFileName, final boolean shared) { | |
11fdf7f2 | 136 | if (isUnix() || isAix() || isSolaris() || isFreeBSD() || isOpenBSD()) { |
7c673cae FG |
137 | return libraryFileName + ".so"; |
138 | } else if (isMac()) { | |
139 | return libraryFileName + (shared ? ".dylib" : ".jnilib"); | |
140 | } else if (isWindows()) { | |
141 | return libraryFileName + ".dll"; | |
142 | } | |
143 | throw new UnsupportedOperationException(); | |
144 | } | |
145 | ||
146 | public static String getJniLibraryExtension() { | |
147 | if (isWindows()) { | |
148 | return ".dll"; | |
149 | } | |
150 | return (isMac()) ? ".jnilib" : ".so"; | |
151 | } | |
152 | } |