|
| 1 | +# jextract Maven Plugin |
| 2 | + |
| 3 | +`netty5-jextract-maven-plugin` generates `java.lang.foreign` (FFM) bindings from vendored C |
| 4 | +headers by driving a pre-installed [`jextract`](https://jdk.java.net/jextract/) binary — one |
| 5 | +invocation per configured binding. |
| 6 | + |
| 7 | +Generation is **on-demand**: the produced sources are committed to git, so ordinary builds and CI |
| 8 | +compile the committed code and never invoke jextract. Re-run the plugin only when the vendored |
| 9 | +headers or the requested symbols change. |
| 10 | + |
| 11 | +## Prerequisite: install jextract |
| 12 | + |
| 13 | +jextract is a developer prerequisite and is **never downloaded by the build**. Install it from |
| 14 | +<https://jdk.java.net/jextract/>, then make it discoverable. The plugin resolves the binary in |
| 15 | +this order: |
| 16 | + |
| 17 | +1. the `jextract` plugin parameter (`-Djextract=/path/to/jextract`); |
| 18 | +1. the `JEXTRACT` environment variable; |
| 19 | +1. `jextract` on the `PATH`. |
| 20 | + |
| 21 | +If none resolve to an executable, the goal fails with an actionable message. |
| 22 | + |
| 23 | +> **Windows note:** PATH discovery is POSIX-only — it looks for a file literally named |
| 24 | +> `jextract`, not the `jextract.bat` the Windows distribution ships. On Windows, point the plugin |
| 25 | +> at the executable explicitly with `-Djextract=` or the `JEXTRACT` environment variable (and note |
| 26 | +> that exec-ing a `.bat` may require a `cmd /c` wrapper). The FFM transports target macOS/Linux, so |
| 27 | +> this is a non-issue in practice. |
| 28 | +
|
| 29 | +## Configuration |
| 30 | + |
| 31 | +A consuming module configures the plugin with its vendored header directory, the target package |
| 32 | +for the generated code, and one `<binding>` per generated class. Each symbol list maps to the |
| 33 | +matching jextract `--include-*` flag (jextract only emits the symbols explicitly requested). |
| 34 | + |
| 35 | +```xml |
| 36 | +<plugin> |
| 37 | + <groupId>io.netty</groupId> |
| 38 | + <artifactId>netty5-jextract-maven-plugin</artifactId> |
| 39 | + <version>${project.version}</version> |
| 40 | + <configuration> |
| 41 | + <headerDirectory>${project.basedir}/headers/macos</headerDirectory> |
| 42 | + <targetPackage>io.netty.channel.unix.ffm.generated</targetPackage> |
| 43 | + <!-- outputDirectory defaults to ${project.basedir}/src/main/java (committed to git) --> |
| 44 | + <bindings> |
| 45 | + <binding> |
| 46 | + <header>socket.h</header> |
| 47 | + <className>BsdSocket</className> |
| 48 | + <functions> |
| 49 | + <function>socket</function> |
| 50 | + <function>bind</function> |
| 51 | + </functions> |
| 52 | + <structs> |
| 53 | + <struct>sockaddr_in</struct> |
| 54 | + </structs> |
| 55 | + <constants> |
| 56 | + <constant>AF_INET</constant> |
| 57 | + <constant>SOCK_STREAM</constant> |
| 58 | + </constants> |
| 59 | + <!-- Optional, for the less common symbol kinds: --> |
| 60 | + <typedefs> |
| 61 | + <typedef>socklen_t</typedef> |
| 62 | + </typedefs> |
| 63 | + <unions> |
| 64 | + <union>sockaddr_storage_u</union> |
| 65 | + </unions> |
| 66 | + <vars> |
| 67 | + <var>errno</var> |
| 68 | + </vars> |
| 69 | + </binding> |
| 70 | + </bindings> |
| 71 | + </configuration> |
| 72 | +</plugin> |
| 73 | +``` |
| 74 | + |
| 75 | +### Parameters |
| 76 | + |
| 77 | +| Parameter | Required | Default | Description | |
| 78 | +|---|---|---|---| |
| 79 | +| `headerDirectory` | yes | — | Directory holding the vendored C headers each binding's `header` resolves against. | |
| 80 | +| `targetPackage` | yes | — | Java package for the generated bindings (`--target-package`). | |
| 81 | +| `bindings` | yes | — | One `<binding>` per generated class; each `className` must be unique. | |
| 82 | +| `outputDirectory` | no | `${project.basedir}/src/main/java` | Where jextract writes sources; the default keeps them committed to git. | |
| 83 | +| `jextract` | no | — | Explicit path to the jextract executable (property `jextract`). Overrides the `JEXTRACT` env var and `PATH`. | |
| 84 | +| `timeoutSeconds` | no | `300` | Per-invocation timeout (property `jextract.timeoutSeconds`). A run that exceeds it is forcibly terminated and the build fails, so a wedged tool never hangs the build. `0` waits without a bound. | |
| 85 | + |
| 86 | +Each `<binding>` has: `header` (required), `className` (required, `--header-class-name`), and the |
| 87 | +optional symbol lists, each mapping to the matching jextract `--include-*` flag: |
| 88 | +`functions` (`--include-function`), `structs` (`--include-struct`), |
| 89 | +`constants` (`--include-constant`), `typedefs` (`--include-typedef`), |
| 90 | +`unions` (`--include-union`), and `vars` (`--include-var`). POSIX socket code in particular |
| 91 | +tends to need `typedefs` (e.g. `socklen_t`), `unions` (sockaddr storage), and `vars` |
| 92 | +(`errno`-style globals). |
| 93 | + |
| 94 | +> **One type, one binding.** jextract emits each struct/typedef/union as a **standalone file named |
| 95 | +> after the C symbol** (e.g. `--include-struct sockaddr_in` writes `sockaddr_in.java`), *not* after |
| 96 | +> the header class. So if two `<binding>`s request the same struct/typedef/union into the same |
| 97 | +> `targetPackage`, they write the same file and clobber each other — the survivor even |
| 98 | +> back-references whichever binding ran last. Request each struct/typedef/union from **at most one** |
| 99 | +> `<binding>`; if several classes need a shared type, include it in one binding and let the others |
| 100 | +> reference it. The plugin enforces this: it fails the build with an actionable message if two |
| 101 | +> bindings name the same type symbol (it also rejects duplicate `<className>`). Functions, |
| 102 | +> constants, and vars are emitted into the header-class file, so they are already namespaced by the |
| 103 | +> (unique) class name and may be repeated safely. |
| 104 | +> |
| 105 | +> **Header search path (no `-I`/`-D` passthrough).** The plugin passes no `-I` include directories |
| 106 | +> or `-D` macro definitions to jextract. It relies on jextract's bundled libclang finding system |
| 107 | +> headers (`<stdint.h>`, `<sys/socket.h>`, …) on its own and on relative `#include "dep.h"` |
| 108 | +> resolving against the header's own directory. This works on a normally-configured developer/CI |
| 109 | +> machine (verified against jextract 25). If a header fails to extract because a system include |
| 110 | +> cannot be found (unusual SDK layout, cross-SDK/CI image), there is currently **no escape hatch** — |
| 111 | +> an `includeDirs`/`defines` parameter would need to be added to the plugin. Note that the |
| 112 | +> ABI-identical-across-architectures check below does not help here: a missing include fails on both |
| 113 | +> arches identically. |
| 114 | +
|
| 115 | +## Regenerating bindings |
| 116 | + |
| 117 | +The `generate` goal is deliberately **not** bound to a lifecycle phase. Run it explicitly, then |
| 118 | +commit the regenerated sources: |
| 119 | + |
| 120 | +```bash |
| 121 | +# jextract on PATH (or via JEXTRACT / -Djextract=...). Replace the module with the one that |
| 122 | +# owns the bindings; transport-ffm-unix-common is illustrative and may not exist yet. |
| 123 | +mvn -pl transport-ffm-unix-common io.netty:netty5-jextract-maven-plugin:generate |
| 124 | +``` |
| 125 | + |
| 126 | +Because macOS bindings are ABI-identical across architectures, regenerating on x86_64 and arm64 |
| 127 | +must produce byte-identical output (a good sanity check after changing headers). |
| 128 | + |
| 129 | +> **Delete stale output first.** The plugin writes into the committed `src/main/java` and does |
| 130 | +> **not** prune. If you rename/remove a `<binding>` or shrink an `--include-*` set, previously |
| 131 | +> generated classes are left orphaned in the tree. Delete the target package directory before |
| 132 | +> regenerating so the committed output reflects exactly the current configuration. Note too that a |
| 133 | +> mid-run failure (e.g. binding 3 of 5) leaves a partially regenerated tree — there is no staging |
| 134 | +> directory or atomic move — so re-run to completion before committing. |