Skip to content

Commit e29deed

Browse files
authored
Allow config if to auto load native libraries with -Dswift-java.auto-load-libraries=false (#364)
1 parent 91d2132 commit e29deed

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ extension FFMSwift2JavaGenerator {
194194
@SuppressWarnings("unused")
195195
private static final boolean INITIALIZED_LIBS = initializeLibs();
196196
static boolean initializeLibs() {
197-
System.loadLibrary(SwiftLibraries.STDLIB_DYLIB_NAME);
198-
System.loadLibrary("SwiftKitSwift");
197+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE);
198+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFTKITSWIFT);
199199
System.loadLibrary(LIB_NAME);
200200
return true;
201201
}
@@ -343,10 +343,11 @@ extension FFMSwift2JavaGenerator {
343343
"""
344344
static final SymbolLookup SYMBOL_LOOKUP = getSymbolLookup();
345345
private static SymbolLookup getSymbolLookup() {
346-
// Ensure Swift and our Lib are loaded during static initialization of the class.
347-
SwiftLibraries.loadLibrary("swiftCore");
348-
SwiftLibraries.loadLibrary("SwiftKitSwift");
349-
SwiftLibraries.loadLibrary(LIB_NAME);
346+
if (SwiftLibraries.AUTO_LOAD_LIBS) {
347+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFT_CORE);
348+
System.loadLibrary(SwiftLibraries.LIB_NAME_SWIFTKITSWIFT);
349+
System.loadLibrary(LIB_NAME);
350+
}
350351
351352
if (PlatformUtils.isMacOS()) {
352353
return SymbolLookup.libraryLookup(System.mapLibraryName(LIB_NAME), LIBRARY_ARENA)

SwiftKitCore/src/main/java/org/swift/swiftkit/core/SwiftLibraries.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,35 @@
2424

2525
public final class SwiftLibraries {
2626

27-
public static final String STDLIB_DYLIB_NAME = "swiftCore";
28-
public static final String SWIFTKITSWIFT_DYLIB_NAME = "SwiftKitSwift";
27+
// Library names of core Swift and SwiftKit
28+
public static final String LIB_NAME_SWIFT_CORE = "swiftCore";
29+
public static final String LIB_NAME_SWIFT_CONCURRENCY = "swift_Concurrency";
30+
public static final String LIB_NAME_SWIFTKITSWIFT = "SwiftKitSwift";
2931

30-
private static final String STDLIB_MACOS_DYLIB_PATH = "/usr/lib/swift/libswiftCore.dylib";
32+
/**
33+
* Allows for configuration if jextracted types should automatically attempt to load swiftCore and the library type is from.
34+
* <p/>
35+
* If all libraries you need to load are available in paths passed to {@code -Djava.library.path} this should work correctly,
36+
* however if attempting to load libraries from e.g. the jar as a resource, you may want to disable this.
37+
*/
38+
public static final boolean AUTO_LOAD_LIBS = System.getProperty("swift-java.auto-load-libraries") == null ?
39+
true
40+
: Boolean.getBoolean("swiftkit.auto-load-libraries");
3141

3242
@SuppressWarnings("unused")
3343
private static final boolean INITIALIZED_LIBS = loadLibraries(false);
3444

3545
public static boolean loadLibraries(boolean loadSwiftKit) {
36-
System.loadLibrary(STDLIB_DYLIB_NAME);
46+
System.loadLibrary(LIB_NAME_SWIFTKITSWIFT);
3747
if (loadSwiftKit) {
38-
System.loadLibrary(SWIFTKITSWIFT_DYLIB_NAME);
48+
System.loadLibrary(LIB_NAME_SWIFTKITSWIFT);
3949
}
4050
return true;
4151
}
4252

4353
// ==== ------------------------------------------------------------------------------------------------------------
4454
// Loading libraries
4555

46-
public static void loadLibrary(String libname) {
47-
// TODO: avoid concurrent loadResource calls; one load is enough esp since we cause File IO when we do that
48-
try {
49-
// try to load a dylib from our classpath, e.g. when we included it in our jar
50-
loadResourceLibrary(libname);
51-
} catch (UnsatisfiedLinkError | RuntimeException e) {
52-
// fallback to plain system path loading
53-
System.loadLibrary(libname);
54-
}
55-
}
56-
5756
public static void loadResourceLibrary(String libname) {
5857
String resourceName = PlatformUtils.dynamicLibraryName(libname);
5958
if (CallTraces.TRACE_DOWNCALLS) {

0 commit comments

Comments
 (0)