-
Notifications
You must be signed in to change notification settings - Fork 76
Try loading Swift libraries from JAR resources #511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TheTripleV
wants to merge
2
commits into
swiftlang:main
Choose a base branch
from
TheTripleV:try-loading-libs-from-jar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−38
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,39 +41,138 @@ public final class SwiftLibraries { | |
| : Boolean.getBoolean("swiftkit.auto-load-libraries"); | ||
|
|
||
| @SuppressWarnings("unused") | ||
| private static final boolean INITIALIZED_LIBS = loadLibraries(false); | ||
| private static final boolean INITIALIZED_LIBS = AUTO_LOAD_LIBS ? loadLibraries(false) : true; | ||
|
|
||
| public static boolean loadLibraries(boolean loadSwiftRuntimeFunctions) { | ||
| System.loadLibrary(LIB_NAME_SWIFT_CORE); | ||
| System.loadLibrary(LIB_NAME_SWIFT_JAVA); | ||
| if (loadSwiftRuntimeFunctions) { | ||
| System.loadLibrary(LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); | ||
| try { | ||
| loadLibraryWithFallbacks(LIB_NAME_SWIFT_CORE); | ||
| loadLibraryWithFallbacks(LIB_NAME_SWIFT_JAVA); | ||
| if (loadSwiftRuntimeFunctions) { | ||
| loadLibraryWithFallbacks(LIB_NAME_SWIFT_RUNTIME_FUNCTIONS); | ||
| } | ||
| return true; | ||
| } catch (RuntimeException e) { | ||
| // Libraries could not be loaded | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.err.println("[swift-java] Could not load libraries: " + e.getMessage()); | ||
| System.err.println("[swift-java] Libraries will need to be loaded explicitly or from JAR resources"); | ||
| } | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| // ==== ------------------------------------------------------------------------------------------------------------ | ||
| // Loading libraries | ||
|
|
||
| public static void loadResourceLibrary(String libname) { | ||
| String resourceName = PlatformUtils.dynamicLibraryName(libname); | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.out.println("[swift-java] Loading resource library: " + resourceName); | ||
| /** | ||
| * Returns the platform-specific system path to the swiftCore library. | ||
| * | ||
| * @return Full path to swiftCore on the system, or null if platform is not macOS or Linux | ||
| */ | ||
| public static String libSwiftCorePath() { | ||
| if (PlatformUtils.isMacOS()) { | ||
| return "/usr/lib/swift/libswiftCore.dylib"; | ||
| } else if (PlatformUtils.isLinux()) { | ||
| return "/usr/lib/swift/linux/libswiftCore.so"; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Attempts to load a library using multiple fallback strategies with nice error reporting. | ||
| * Tries in order: java.library.path, JAR resources, system path (for swiftCore only). | ||
| * | ||
| * @param libname The library name to load | ||
| * @throws RuntimeException if all loading strategies fail | ||
| */ | ||
| public static void loadLibraryWithFallbacks(String libname) { | ||
| // Try 1: Load from java.library.path | ||
| try { | ||
| System.loadLibrary(libname); | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.out.println("[swift-java] Loaded " + libname + " from java.library.path"); | ||
| } | ||
| return; | ||
| } catch (Throwable e) { | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.err.println("[swift-java] Failed to load " + libname + " from java.library.path: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| try (InputStream libInputStream = SwiftLibraries.class.getResourceAsStream("/" + resourceName)) { | ||
| if (libInputStream == null) { | ||
| throw new RuntimeException("Expected library '" + libname + "' ('" + resourceName + "') was not found as resource!"); | ||
| // Try 2: Load from JAR resources | ||
| try { | ||
| loadResourceLibrary(libname); | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.out.println("[swift-java] Loaded " + libname + " from JAR resources"); | ||
| } | ||
| return; | ||
| } catch (Throwable e2) { | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.err.println("[swift-java] Failed to load " + libname + " from JAR: " + e2.getMessage()); | ||
| } | ||
|
|
||
| // TODO: we could do an in memory file system here | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The TODO is still valid, don't remove it |
||
| File tempFile = File.createTempFile(libname, ""); | ||
| tempFile.deleteOnExit(); | ||
| Files.copy(libInputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
| // Try 3: For swiftCore only, try system path | ||
| if (libname.equals(LIB_NAME_SWIFT_CORE)) { | ||
| String systemPath = libSwiftCorePath(); | ||
| if (systemPath != null) { | ||
| try { | ||
| System.load(systemPath); | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.out.println("[swift-java] Loaded " + libname + " from system path: " + systemPath); | ||
| } | ||
| return; | ||
| } catch (Throwable e3) { | ||
| throw new RuntimeException( | ||
| "Failed to load " + libname + " from java.library.path, JAR resources, and system path (" + systemPath + ")", | ||
| e3 | ||
| ); | ||
| } | ||
| } else { | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.err.println("[swift-java] System path not available on this platform"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| System.load(tempFile.getAbsolutePath()); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to load dynamic library '" + libname + "' ('" + resourceName + "') as resource!", e); | ||
| throw new RuntimeException("Failed to load " + libname + " from java.library.path and JAR resources", e2); | ||
| } | ||
| } | ||
|
|
||
| // Cache of already-loaded libraries to prevent duplicate extraction | ||
| private static final java.util.concurrent.ConcurrentHashMap<String, File> loadedLibraries = new java.util.concurrent.ConcurrentHashMap<>(); | ||
|
|
||
| public static void loadResourceLibrary(String libname) { | ||
| loadedLibraries.computeIfAbsent(libname, key -> { | ||
| String resourceName = PlatformUtils.dynamicLibraryName(key); | ||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.out.println("[swift-java] Loading resource library: " + resourceName); | ||
| } | ||
|
|
||
| try (InputStream libInputStream = SwiftLibraries.class.getResourceAsStream("/" + resourceName)) { | ||
| if (libInputStream == null) { | ||
| throw new RuntimeException("Expected library '" + key + "' ('" + resourceName + "') was not found as resource!"); | ||
| } | ||
|
|
||
| // TODO: we could do an in memory file system here | ||
| // Extract to temp file | ||
| File tempFile = File.createTempFile(key, ""); | ||
| tempFile.deleteOnExit(); | ||
| Files.copy(libInputStream, tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); | ||
|
|
||
| System.load(tempFile.getAbsolutePath()); | ||
|
|
||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.out.println("[swift-java] Loaded and cached library: " + key + " from " + tempFile.getAbsolutePath()); | ||
| } | ||
|
|
||
| return tempFile; | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to load dynamic library '" + key + "' ('" + resourceName + "') as resource!", e); | ||
| } | ||
| }); | ||
|
|
||
| if (CallTraces.TRACE_DOWNCALLS) { | ||
| System.out.println("[swift-java] Library already loaded from cache: " + libname); | ||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that seems right, thanks