-
-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathNativeLibrary.java
More file actions
112 lines (95 loc) · 3.59 KB
/
Copy pathNativeLibrary.java
File metadata and controls
112 lines (95 loc) · 3.59 KB
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
package processing.core;
import java.io.*;
import java.nio.file.*;
/**
* Handles loading of Processing's native Rust library (libprocessing).
*/
public class NativeLibrary {
private static boolean loaded = false;
private static Throwable loadError = null;
private static final String LIBRARY_NAME = "processing";
// Platform
private static final String OS_NAME = System.getProperty("os.name").toLowerCase();
private static final String OS_ARCH = System.getProperty("os.arch").toLowerCase();
private static final String platform;
private static final String architecture;
private static final String libraryExtension;
static {
// platform
if (OS_NAME.contains("mac") || OS_NAME.contains("darwin")) {
platform = "macos";
libraryExtension = "dylib";
} else if (OS_NAME.contains("win")) {
platform = "windows";
libraryExtension = "dll";
} else if (OS_NAME.contains("linux")) {
platform = "linux";
libraryExtension = "so";
} else {
throw new UnsupportedOperationException("Unsupported OS: " + OS_NAME);
}
// architecture
if (OS_ARCH.contains("aarch64") || OS_ARCH.contains("arm")) {
architecture = "aarch64";
} else if (OS_ARCH.contains("x86_64") || OS_ARCH.contains("amd64")) {
architecture = "x86_64";
} else {
throw new UnsupportedOperationException("Unsupported architecture: " + OS_ARCH);
}
// Load the dll
try {
loadNativeLibrary();
loaded = true;
} catch (Throwable e) {
loadError = e;
System.err.println("Warning: Failed to load Processing native library: " + e.getMessage());
}
}
/**
* Ensures the native library is loaded. Throws if loading failed.
*/
public static void ensureLoaded() {
if (!loaded) {
throw new RuntimeException("Native library failed to load", loadError);
}
}
/**
* Returns whether the native library was successfully loaded.
*/
public static boolean isLoaded() {
return loaded;
}
/**
* Returns the platform string (e.g., "macos-aarch64").
*/
public static String getPlatform() {
return platform + "-" + architecture;
}
/**
* Extracts and loads the native library from JAR resources.
*/
private static void loadNativeLibrary() throws IOException {
String platformTarget = platform + "-" + architecture;
String libraryFileName = platform.equals("windows")
? LIBRARY_NAME + "." + libraryExtension
: "lib" + LIBRARY_NAME + "." + libraryExtension;
String resourcePath = "/native/" + platformTarget + "/" + libraryFileName;
// check classloader for resource in jar
InputStream libraryStream = NativeLibrary.class.getResourceAsStream(resourcePath);
if (libraryStream == null) {
throw new FileNotFoundException(
"Native library not found in JAR: " + resourcePath +
" (platform: " + platformTarget + ")"
);
}
// extract
Path tempDir = Files.createTempDirectory("processing-native-");
tempDir.toFile().deleteOnExit();
Path libraryPath = tempDir.resolve(libraryFileName);
Files.copy(libraryStream, libraryPath, StandardCopyOption.REPLACE_EXISTING);
libraryStream.close();
libraryPath.toFile().deleteOnExit();
// load!
System.load(libraryPath.toAbsolutePath().toString());
}
}