From 7c8eeeea28eff09bdf0281d1ff8a8b3a499cb0ff Mon Sep 17 00:00:00 2001 From: Manuel Bl Date: Tue, 20 Feb 2024 23:20:03 +0100 Subject: [PATCH] macOS: use new jextract options to specify system library --- java-does-usb/jextract/README.md | 14 -------------- java-does-usb/jextract/macos/gen_macos.sh | 4 ++-- .../macos/gen/corefoundation/CoreFoundation.java | 2 -- .../net/codecrete/usb/macos/gen/iokit/IOKit.java | 3 --- 4 files changed, 2 insertions(+), 21 deletions(-) diff --git a/java-does-usb/jextract/README.md b/java-does-usb/jextract/README.md index 85ad11e..170a55e 100644 --- a/java-does-usb/jextract/README.md +++ b/java-does-usb/jextract/README.md @@ -60,21 +60,7 @@ static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup("libudev.so Most of the required native functions on macOS are part of a framework. Frameworks internally have a more complex file organization of header and binary files than appears from the outside. Thus, they require a special logic to locate framework header files. *clang* supports it with the `-F`. *jextract* allows to specify the options via `compiler_flags.txt` file. Since the file must be in the local directory and since it does not apply to Linux and Windows, separate directories must be used for the operating systems. -The generated code however is unable to locate the framework at run-time. There does not seem to be a way to instruct *jextract* to generate the required code. So it must be manually changed, from: -``` -static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("CoreFoundation.framework"), LIBRARY_ARENA) -``` - -to: - -``` -static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", LIBRARY_ARENA) -``` - -This is in the class `CoreFoundation`. The analogous change must be made in the class `IOKit`. - -These aren't actual paths. But they are the correct and future-proof path to locate the framework at run-time (see man pages of `ld` on macOS). ## Windows diff --git a/java-does-usb/jextract/macos/gen_macos.sh b/java-does-usb/jextract/macos/gen_macos.sh index 1c5db86..9e93e0e 100755 --- a/java-does-usb/jextract/macos/gen_macos.sh +++ b/java-does-usb/jextract/macos/gen_macos.sh @@ -7,7 +7,7 @@ SDK_DIR=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Dev # CoreFoundation $JEXTRACT --output ../../src/main/java \ -I $SDK_DIR/usr/include \ - -lCoreFoundation.framework \ + -l :/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation \ --header-class-name CoreFoundation \ --target-package net.codecrete.usb.macos.gen.corefoundation \ --include-struct CFRange \ @@ -32,7 +32,7 @@ $JEXTRACT --output ../../src/main/java \ # IOKit $JEXTRACT --output ../../src/main/java \ -I $SDK_DIR/usr/include \ - -lIOKit.framework \ + -l :/System/Library/Frameworks/IOKit.framework/IOKit \ --header-class-name IOKit \ --target-package net.codecrete.usb.macos.gen.iokit \ --include-var kIOMasterPortDefault \ diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/corefoundation/CoreFoundation.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/corefoundation/CoreFoundation.java index 791879c..78d44fc 100644 --- a/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/corefoundation/CoreFoundation.java +++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/corefoundation/CoreFoundation.java @@ -53,8 +53,6 @@ static MemoryLayout align(MemoryLayout layout, long align) { }; } - // Manually fix. Otherwise, system library will not be found - // static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("CoreFoundation.framework"), LIBRARY_ARENA) static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", LIBRARY_ARENA) .or(SymbolLookup.loaderLookup()) .or(Linker.nativeLinker().defaultLookup()); diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/iokit/IOKit.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/iokit/IOKit.java index 9c01023..727ae61 100644 --- a/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/iokit/IOKit.java +++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/gen/iokit/IOKit.java @@ -54,9 +54,6 @@ static MemoryLayout align(MemoryLayout layout, long align) { }; } - - // Manually fix. Otherwise, system library will not be found - // static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("IOKit.framework"), LIBRARY_ARENA) static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup("/System/Library/Frameworks/IOKit.framework/IOKit", LIBRARY_ARENA) .or(SymbolLookup.loaderLookup()) .or(Linker.nativeLinker().defaultLookup());