Skip to content

Commit

Permalink
Use SymbolLookup::libraryLookup to load libraries, remove custom LibL…
Browse files Browse the repository at this point in the history
…oad class
  • Loading branch information
jwharm committed Mar 31, 2024
1 parent 059d475 commit 768e5be
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 120 deletions.
15 changes: 2 additions & 13 deletions buildSrc/src/main/groovy/java-gi.library-conventions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import io.github.jwharm.javagi.GenerateSources
* - Set JDK version
* - Configure 'generateSources' action
* - Enable preview features
* - Set OS-specific library paths for unit tests
* - Set OS-specific JVM parameters
* - Set common POM metadata and enable signing
*/

Expand Down Expand Up @@ -84,22 +84,11 @@ tasks.named('test', Test) {
enabled = false
}

// Configure library path for MacOS (Homebrew) and set MacOS-specific JVM parameter
// Set MacOS-specific JVM parameter
if (Os.isFamily(Os.FAMILY_MAC)) {
jvmArgs += '-Djava.library.path=/opt/homebrew/lib'
jvmArgs += '-XstartOnFirstThread'
}

// Configure library path for Arch, Fedora and Debian/Ubuntu
else if (Os.isFamily(Os.FAMILY_UNIX)) {
jvmArgs += '-Djava.library.path=/usr/lib64:/lib64:/lib:/usr/lib:/lib/x86_64-linux-gnu'
}

// Configure library path for Windows (MSYS2)
else if (Os.isFamily(Os.FAMILY_WINDOWS)) {
jvmArgs += '-Djava.library.path=C:/msys64/mingw64/bin'
}

useJUnitPlatform()
jvmArgs += '--enable-preview'
jvmArgs += '--enable-native-access=ALL-UNNAMED'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public final class ClassNames {
public static final ClassName ARENA_CLOSE_ACTION = get(PKG_INTEROP, "ArenaCloseAction");
public static final ClassName MEMORY_CLEANER = get(PKG_INTEROP, "MemoryCleaner");
public static final ClassName INTEROP = get(PKG_INTEROP, "Interop");
public static final ClassName LIB_LOAD = get(PKG_INTEROP, "LibLoad");
public static final ClassName PLATFORM = get(PKG_INTEROP, "Platform");

public static final ClassName AUTO_CLOSEABLE = get(PKG_GIO, "AutoCloseable");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private CodeBlock loadLibraries() {
Platform.toString(platform));
for (String libName : library.split(","))
block.addStatement("$T.loadLibrary($S)",
ClassNames.LIB_LOAD,
ClassNames.INTEROP,
libName);
block.endControlFlow();
}
Expand All @@ -92,7 +92,7 @@ private CodeBlock loadLibraries() {
else {
block.addStatement("case $S -> $T.loadLibrary($S)",
Platform.toString(platform),
ClassNames.LIB_LOAD,
ClassNames.INTEROP,
library);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,20 @@ private record FunctionPointer(MemorySegment address,
private static final Map<NamedFunction, MethodHandle> namedFunctions = new HashMap<>();
private static final Map<FunctionPointer, MethodHandle> functionPointers = new HashMap<>();

private final static SymbolLookup symbolLookup;
private final static Linker linker = Linker.nativeLinker();

static {
SymbolLookup loaderLookup = SymbolLookup.loaderLookup();
symbolLookup = name -> loaderLookup.find(name).or(() ->
linker.defaultLookup().find(name));
public static SymbolLookup symbolLookup = SymbolLookup.loaderLookup()
.or(Linker.nativeLinker().defaultLookup());

/**
* Load the specified library using
* {@link SymbolLookup#libraryLookup(String, Arena)}.
*
* @param name the name of the library
*/
public static void loadLibrary(String name) {
Interop.symbolLookup = SymbolLookup.libraryLookup(name, Arena.global())
.or(Interop.symbolLookup);
}

/**
Expand Down

This file was deleted.

0 comments on commit 768e5be

Please sign in to comment.