RFR: Add `--library-path-resolver` option for custom library path resolving
devjeonghwan
duke at openjdk.org
Thu Nov 13 08:46:50 UTC 2025
#### 1. Status Quo
Currently, `jextract` generates code using two standard loading mechanisms:
1. **Default:** `SymbolLookup.libraryLookup(System.mapLibraryName("name"), ...)`
2. **With `--use-system-load-library`:** `System.loadLibrary("name")`
Both methods depend on static paths (e.g., `java.library.path`) and do not support runtime path resolution (hooking) without manual code modification.
#### 2. Problem
Standard loading fails in dynamic deployment scenarios:
* **Bundled Libraries:** Native libraries inside JARs (extracted to temp directories) require loading from an absolute path.
* **Dynamic Paths:** Paths determined at runtime based on OS/Arch cannot be handled by static system properties.
Users currently must manually edit the generated source to inject custom loading logic.
#### 3. Fix
Introduced a new CLI option: `--library-path-resolver`.
This option injects a user-defined static method to resolve the library path (or name) at runtime.
**Usage:**
jextract ... --library-path-resolver com.example.Resolver#resolvePath
**Code Generation:**
**Case 1: Default (SymbolLookup)**
// Before
SymbolLookup.libraryLookup(System.mapLibraryName("foo"), ARENA);
// After
SymbolLookup.libraryLookup(com.example.Resolver.resolvePath("foo"), ARENA);
**Case 2: With `--use-system-load-library`**
// Before
System.loadLibrary("foo");
// After
System.load(com.example.Resolver.resolvePath("foo"));
#### 4. Additional Notes
* **Method Signature:** The resolver method must match `String <method_name>(String <parameter_name>)`.
* **Return Value:**
* For `--use-system-load-library`: Must return an **absolute path** (required by `System.load()`).
* For Default: Returns a path or name accepted by `SymbolLookup`.
#### 5. Testing
Added `TestLibraryPathResolver`.
* Dynamic compilation and loading of a custom resolver class.
* Correct code generation when the option is present.
* Verifying that the custom resolver is actually invoked and that the native library is successfully loaded and linked via the resolved path
All tests pass locally.
-------------
Commit messages:
- Remove trailing whitespace in `TestLibraryPathResolver.java`
- Refactor and rename library path resolver test
- Add test for custom library path resolver functionality
- Support custom library path resolvers in system library loading
Changes: https://git.openjdk.org/jextract/pull/295/files
Webrev: https://webrevs.openjdk.org/?repo=jextract&pr=295&range=00
Stats: 193 lines in 10 files changed: 176 ins; 0 del; 17 mod
Patch: https://git.openjdk.org/jextract/pull/295.diff
Fetch: git fetch https://git.openjdk.org/jextract.git pull/295/head:pull/295
PR: https://git.openjdk.org/jextract/pull/295
More information about the jextract-dev
mailing list