Linux: support loading native libraries for unknown architectures (issue #966)

expected filename is same as for `x86_64` architecture but replace `x86_64` with value of system property `os.arch`
This commit is contained in:
Karl Tauber
2025-02-20 13:56:50 +01:00
parent 411a2f6d29
commit f0685d179e

View File

@@ -58,6 +58,7 @@ class FlatNativeLibrary
String classifier; String classifier;
String ext; String ext;
boolean unknownArch = false;
if( SystemInfo.isWindows_10_orLater && (SystemInfo.isX86 || SystemInfo.isX86_64 || SystemInfo.isAARCH64) ) { if( SystemInfo.isWindows_10_orLater && (SystemInfo.isX86 || SystemInfo.isX86_64 || SystemInfo.isAARCH64) ) {
// Windows: requires Windows 10/11 (x86, x86_64 or aarch64) // Windows: requires Windows 10/11 (x86, x86_64 or aarch64)
@@ -90,11 +91,14 @@ class FlatNativeLibrary
classifier = SystemInfo.isAARCH64 ? "macos-arm64" : "macos-x86_64"; classifier = SystemInfo.isAARCH64 ? "macos-arm64" : "macos-x86_64";
ext = "dylib"; ext = "dylib";
} else if( SystemInfo.isLinux && (SystemInfo.isX86_64 || SystemInfo.isAARCH64)) { } else if( SystemInfo.isLinux ) {
// Linux: requires x86_64 or aarch64 // Linux: x86_64 or aarch64 (but also supports unknown architectures)
classifier = SystemInfo.isAARCH64 ? "linux-arm64" : "linux-x86_64"; classifier = SystemInfo.isAARCH64 ? "linux-arm64"
: (SystemInfo.isX86_64 ? "linux-x86_64"
: "linux-" + sanitize( System.getProperty( "os.arch" ) ));
ext = "so"; ext = "so";
unknownArch = !SystemInfo.isX86_64 && !SystemInfo.isAARCH64;
// Load libjawt.so (part of JRE) explicitly because it is not found // Load libjawt.so (part of JRE) explicitly because it is not found
// in all Java versions/distributions. // in all Java versions/distributions.
@@ -106,7 +110,7 @@ class FlatNativeLibrary
return; // no native library available for current OS or CPU architecture return; // no native library available for current OS or CPU architecture
// load native library // load native library
NativeLibrary nativeLibrary = createNativeLibrary( classifier, ext ); NativeLibrary nativeLibrary = createNativeLibrary( classifier, ext, unknownArch );
if( !nativeLibrary.isLoaded() ) if( !nativeLibrary.isLoaded() )
return; return;
@@ -128,7 +132,7 @@ class FlatNativeLibrary
FlatNativeLibrary.nativeLibrary = nativeLibrary; FlatNativeLibrary.nativeLibrary = nativeLibrary;
} }
private static NativeLibrary createNativeLibrary( String classifier, String ext ) { private static NativeLibrary createNativeLibrary( String classifier, String ext, boolean unknownArch ) {
String libraryName = "flatlaf-" + classifier; String libraryName = "flatlaf-" + classifier;
// load from "java.library.path" or from path specified in system property "flatlaf.nativeLibraryPath" // load from "java.library.path" or from path specified in system property "flatlaf.nativeLibraryPath"
@@ -139,9 +143,11 @@ class FlatNativeLibrary
if( library.isLoaded() ) if( library.isLoaded() )
return library; return library;
if( !unknownArch ) {
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '" + System.mapLibraryName( libraryName ) LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '" + System.mapLibraryName( libraryName )
+ "' not found in java.library.path '" + System.getProperty( "java.library.path" ) + "' not found in java.library.path '" + System.getProperty( "java.library.path" )
+ "'. Using extracted native library instead.", null ); + "'. Using extracted native library instead.", null );
}
} else { } else {
// try standard library naming scheme // try standard library naming scheme
// (same as in flatlaf.jar in package 'com/formdev/flatlaf/natives') // (same as in flatlaf.jar in package 'com/formdev/flatlaf/natives')
@@ -160,6 +166,7 @@ class FlatNativeLibrary
return new NativeLibrary( libraryFile2, true ); return new NativeLibrary( libraryFile2, true );
} }
if( !unknownArch ) {
LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '" LoggingFacade.INSTANCE.logSevere( "FlatLaf: Native library '"
+ libraryFile.getName() + libraryFile.getName()
+ (libraryName2 != null ? ("' or '" + libraryName2) : "") + (libraryName2 != null ? ("' or '" + libraryName2) : "")
@@ -167,6 +174,7 @@ class FlatNativeLibrary
+ "'. Using extracted native library instead.", null ); + "'. Using extracted native library instead.", null );
} }
} }
}
// load from beside the FlatLaf jar // load from beside the FlatLaf jar
// e.g. for flatlaf-3.1.jar, load flatlaf-3.1-windows-x86_64.dll (from same directory) // e.g. for flatlaf-3.1.jar, load flatlaf-3.1-windows-x86_64.dll (from same directory)
@@ -175,7 +183,7 @@ class FlatNativeLibrary
return new NativeLibrary( libraryFile, true ); return new NativeLibrary( libraryFile, true );
// load from FlatLaf jar (extract native library to temp folder) // load from FlatLaf jar (extract native library to temp folder)
return new NativeLibrary( "com/formdev/flatlaf/natives/" + libraryName, null, true ); return new NativeLibrary( "com/formdev/flatlaf/natives/" + libraryName, null, !unknownArch );
} }
/** /**
@@ -273,6 +281,13 @@ class FlatNativeLibrary
+ '-' + classifier + '.' + ext; + '-' + classifier + '.' + ext;
} }
/**
* Allow only 'a'-'z', 'A'-'Z', '0'-'9', '_' and '-' in filenames.
*/
private static String sanitize( String s ) {
return s.replaceAll( "[^a-zA-Z0-9_-]", "_" );
}
private static void loadJAWT() { private static void loadJAWT() {
try { try {
System.loadLibrary( "jawt" ); System.loadLibrary( "jawt" );