GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add jextract plugin for code generation · netty/netty@4803ab6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs
    • Jextract.md
  • jextract-maven-plugin
    • pom.xml
    • src
      • main/java/io/netty/build/jextract
        • Binding.java
        • GenerateMojo.java
        • JextractCommand.java
        • JextractLocator.java
        • JextractNotFoundException.java
        • package-info.java
      • test/java/io/netty/build/jextract
        • GenerateMojoTest.java
        • JextractCommandTest.java
        • JextractLocatorTest.java
  • pom.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/Jextract.md‎

Copy file name to clipboard
+134Lines changed: 134 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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.
Collapse file

‎jextract-maven-plugin/pom.xml‎

Copy file name to clipboard
+131Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2026 The Netty Project
4+
~
5+
~ The Netty Project licenses this file to you under the Apache License,
6+
~ version 2.0 (the "License"); you may not use this file except in compliance
7+
~ with the License. You may obtain a copy of the License at:
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
~ WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
~ License for the specific language governing permissions and limitations
15+
~ under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/maven-v4_0_0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<parent>
20+
<groupId>io.netty</groupId>
21+
<artifactId>netty5-parent</artifactId>
22+
<version>5.0.0.Final-SNAPSHOT</version>
23+
</parent>
24+
25+
<artifactId>netty5-jextract-maven-plugin</artifactId>
26+
<packaging>maven-plugin</packaging>
27+
28+
<name>Netty/Build/Jextract Maven Plugin</name>
29+
<description>
30+
Generates java.lang.foreign (FFM) bindings from vendored C headers by driving a
31+
pre-installed jextract binary. Generation is on-demand; the produced sources are
32+
committed to git so ordinary builds never require jextract.
33+
</description>
34+
35+
<properties>
36+
<!-- Build tool, not a published API: opt out of the checks that do not apply. -->
37+
<revapi.skip>true</revapi.skip>
38+
<maven.javadoc.skip>true</maven.javadoc.skip>
39+
<!--
40+
This is a Maven plugin that runs inside Maven's own JVM using only plain Java
41+
(ProcessBuilder). It needs none of the Java 25 / FFM features the transport modules do, so
42+
it targets Java 17. This also keeps its bytecode readable by maven-plugin-plugin's descriptor
43+
generator, whose bundled ASM cannot parse Java 25 (class file major version 69) bytecode.
44+
-->
45+
<maven.compiler.release>17</maven.compiler.release>
46+
<maven.compiler.source>17</maven.compiler.source>
47+
<maven.compiler.target>17</maven.compiler.target>
48+
<!-- Coordinates for the Maven APIs we compile against. -->
49+
<mavenPlugin.api.version>3.9.9</mavenPlugin.api.version>
50+
<mavenPlugin.tools.version>3.13.1</mavenPlugin.tools.version>
51+
</properties>
52+
53+
<dependencies>
54+
<dependency>
55+
<groupId>org.apache.maven</groupId>
56+
<artifactId>maven-plugin-api</artifactId>
57+
<version>${mavenPlugin.api.version}</version>
58+
<scope>provided</scope>
59+
</dependency>
60+
<dependency>
61+
<groupId>org.apache.maven</groupId>
62+
<artifactId>maven-core</artifactId>
63+
<version>${mavenPlugin.api.version}</version>
64+
<scope>provided</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.apache.maven.plugin-tools</groupId>
68+
<artifactId>maven-plugin-annotations</artifactId>
69+
<version>${mavenPlugin.tools.version}</version>
70+
<scope>provided</scope>
71+
</dependency>
72+
<dependency>
73+
<!-- @Nullable for parameters/fields that deliberately tolerate null (version managed by parent). -->
74+
<groupId>org.jetbrains</groupId>
75+
<artifactId>annotations</artifactId>
76+
<scope>provided</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter-api</artifactId>
81+
<scope>test</scope>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.junit.jupiter</groupId>
85+
<artifactId>junit-jupiter-engine</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
</dependencies>
89+
90+
<build>
91+
<plugins>
92+
<plugin>
93+
<groupId>org.apache.maven.plugins</groupId>
94+
<artifactId>maven-plugin-plugin</artifactId>
95+
<version>${mavenPlugin.tools.version}</version>
96+
<configuration>
97+
<goalPrefix>jextract</goalPrefix>
98+
</configuration>
99+
<executions>
100+
<execution>
101+
<id>default-descriptor</id>
102+
<phase>process-classes</phase>
103+
<goals>
104+
<goal>descriptor</goal>
105+
</goals>
106+
</execution>
107+
</executions>
108+
</plugin>
109+
<plugin>
110+
<!--
111+
The parent binds maven-jar-plugin to a manifestFile produced by maven-bundle-plugin.
112+
maven-bundle-plugin only supports jar/bundle packaging, so for this maven-plugin module
113+
no such manifest is generated; override the execution to let the jar plugin build its
114+
own manifest (the plugin descriptor is already written into target/classes).
115+
-->
116+
<groupId>org.apache.maven.plugins</groupId>
117+
<artifactId>maven-jar-plugin</artifactId>
118+
<executions>
119+
<execution>
120+
<id>default-jar</id>
121+
<configuration combine.self="override">
122+
<archive>
123+
<addMavenDescriptor>true</addMavenDescriptor>
124+
</archive>
125+
</configuration>
126+
</execution>
127+
</executions>
128+
</plugin>
129+
</plugins>
130+
</build>
131+
</project>

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.