Skip to content

Commit

Permalink
macOS: use new jextract options to specify system library
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Feb 20, 2024
1 parent a1ad5b0 commit 7c8eeee
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 21 deletions.
14 changes: 0 additions & 14 deletions java-does-usb/jextract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions java-does-usb/jextract/macos/gen_macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 7c8eeee

Please sign in to comment.