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@2b9eecf

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs
    • FFM-Build-Strategy.md
    • Jextract.md
  • jextract-maven-plugin
    • pom.xml
    • src
      • main/java/io/netty/build/jextract
        • Binding.java
        • BindingValidator.java
        • GenerateMojo.java
        • JextractCommand.java
        • JextractException.java
        • JextractExceptions.java
        • JextractLocator.java
        • JextractProcessUtils.java
        • JextractVersionChecker.java
        • ProcessStarter.java
        • ProcessStarters.java
        • ProvenanceManifest.java
        • StagingDirectory.java
        • StringUtils.java
        • package-info.java
      • test/java/io/netty/build/jextract
        • BindingValidatorTest.java
        • FakeProcess.java
        • GenerateMojoTest.java
        • JextractCommandTest.java
        • JextractLocatorTest.java
        • JextractVersionCheckerTest.java
        • ProvenanceManifestTest.java
  • pom.xml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/FFM-Build-Strategy.md‎

Copy file name to clipboard
+141Lines changed: 141 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# FFM transport build strategy
2+
3+
Design notes for adding `java.lang.foreign` (FFM) transports (starting with kqueue on macOS)
4+
alongside the existing JNI transports: module layout, how the jextract-generated bindings are
5+
produced and committed, and the build-integration work required. See [Jextract.md](Jextract.md) for
6+
the generator plugin itself.
7+
8+
## Summary
9+
10+
- Each FFM transport mirrors the existing `transport-classes-*` / `transport-native-*` split, except
11+
the "native" half holds **committed jextract-generated Java** instead of a compiled library.
12+
- Bindings are **per-OS** (jextract bakes ABI constants and struct layouts into the output) and
13+
**arch-invariant within an OS**, so one plain jar per OS serves all its architectures. There are no
14+
per-arch classifier jars.
15+
- Generated sources are committed and treated as a build artifact: pinned jextract, pinned SDK, a
16+
provenance manifest, and a regeneration-diff CI check. jextract is never on a consumer's build path.
17+
- The only hand-written binding code is a small errno-capture shim, needed until jextract can emit
18+
`captureCallState` handles.
19+
20+
## Relationship to the JNI transports
21+
22+
The JNI transports split platform-neutral Java from a native module that ships a compiled library per
23+
architecture:
24+
25+
```
26+
transport-classes-kqueue platform-neutral Java (channels, IoHandler, config)
27+
transport-native-kqueue per-arch classifier jars (osx-x86_64, osx-aarch_64); .jnilib in META-INF/native
28+
```
29+
30+
FFM resolves libc symbols at runtime via `Linker.nativeLinker().defaultLookup()`, so there is no
31+
compiled library. The platform-varying artifact becomes the jextract-generated Java, which we commit.
32+
33+
## Module layout
34+
35+
| JNI module | FFM module | Contents |
36+
|---|---|---|
37+
| `transport-native-unix-common` | `transport-ffm-unix-common` | OS-agnostic hand-written support. Currently only the errno capture-state helper (see below). |
38+
| `transport-native-kqueue` | `transport-ffm-native-kqueue` | Committed jextract output for macOS, the provenance manifest, and the per-OS errno-capture call sites. No C headers. |
39+
| `transport-classes-kqueue` | `transport-ffm-classes-kqueue` | Hand-written kqueue transport: `IoHandler`, channels, configs. |
40+
41+
Linux follows the same shape (`transport-ffm-{native,classes}-epoll`, likewise `io_uring`).
42+
43+
**Dependency direction.** The JNI native jar is a runtime-only, per-arch classifier the user selects.
44+
The FFM bindings are Java types the transport calls, so `transport-ffm-classes-kqueue` has a plain
45+
compile dependency on `transport-ffm-native-kqueue` (no classifier). Consumers depend on the classes
46+
module and get the bindings transitively; there is no platform-classifier selection step and jextract
47+
is never required to build.
48+
49+
**One module per OS, not a cross-OS common module.** Generated sources bake in per-OS ABI values, and
50+
committed source has no classifier idiom the way a compiled `transport-native-unix-common` does.
51+
Per-OS modules avoid shipping other OSes' classes or profile-gating source roots. If a second same-OS
52+
FFM consumer appears (e.g. a macOS DNS resolver), split out a per-OS common bindings module then.
53+
54+
**Package naming.** Generated code lives in an OS-named package, e.g.
55+
`io.netty.channel.unix.ffm.generated.macos`, so the target OS is visible at the call site and in stack
56+
traces and a cross-OS import stands out in review.
57+
58+
## Bindings are per-OS, arch-invariant within an OS
59+
60+
jextract bakes constant values (`AF_INET`), struct layouts and offsets (`sockaddr_in`), and type
61+
sizes into the generated code, and the API itself differs (`kqueue` vs `epoll`). A binding set is
62+
therefore valid for exactly one OS. Within an OS the supported architectures share an ABI (x86_64 and
63+
aarch64 are both LP64), so the output is identical across them and one no-classifier jar per OS
64+
suffices. A CI check (below) regenerates on both arches and asserts byte-identical output; a future
65+
non-LP64 architecture would need its own binding set.
66+
67+
## Hand-written code: errno capture
68+
69+
jextract does not build its downcall handles with `Linker.Option.captureCallState("errno")`, so the
70+
generated `socket`/`bind`/etc. methods cannot read errno atomically (a separate read is racy). Until
71+
jextract can emit capture-state handles, errno-sensitive calls are hand-written:
72+
73+
- An OS-agnostic helper in `transport-ffm-unix-common` decorates a generated `FunctionDescriptor` with
74+
`captureCallState` and owns the cached errno `VarHandle`. It runs once per symbol at handle
75+
construction, not per call.
76+
- Per-OS typed call sites in the bindings module invoke the resulting `static final MethodHandle`.
77+
78+
Everything above the bindings (channels, IoHandler, config) is ordinary hand-written transport code.
79+
80+
Guidelines for the wrappers: invoke a `static final MethodHandle` via `invokeExact` (not
81+
`invoke`/`Object[]`, which box and adapt at runtime); reuse a loop-confined capture segment rather
82+
than allocating per call; return `(result, errno)` bit-packed in a `long`. `Linker.Option.critical()`
83+
is worth evaluating for short non-blocking calls, but blocking calls (`read`/`write`/`kevent`) must
84+
not use it, and its composition with `captureCallState` needs checking on the target JDK.
85+
86+
## Code generation and determinism
87+
88+
We author no C headers. jextract reads the OS's system headers directly from the platform SDK (e.g.
89+
`<sys/socket.h>` from `$(xcrun --show-sdk-path)/usr/include`), and `--include-*` flags select the
90+
symbols. It runs on-demand, never in the normal build, and the output is committed.
91+
92+
Generation is inherently per-OS: jextract parses with the host clang's predefined macros (`__APPLE__`,
93+
`__linux__`, arch macros), so macOS bindings must be generated on macOS and Linux bindings in a
94+
matching Linux toolchain (a pinned container works, including via Docker on a Mac). No `-I` flag
95+
changes that.
96+
97+
Determinism is treated as a property of the artifact:
98+
99+
- Pin jextract; the plugin verifies `jextract --version` and fails on mismatch.
100+
- Pin the SDK/toolchain and record its identity in the provenance manifest.
101+
- A `GENERATED.properties` manifest sits beside the sources (jextract version, host OS, SDK id) and is
102+
deterministic (no timestamps or paths), so it reproduces byte-for-byte.
103+
- A CI regeneration-diff job regenerates on the canonical pinned toolchain and fails on any diff. That
104+
job, not a developer's laptop, is the source of truth; it can be a manually-triggered workflow that
105+
opens the PR.
106+
107+
The POSIX socket / kqueue / epoll ABIs are frozen for binary compatibility, so cross-SDK-version drift
108+
is unlikely in practice; the diff gate catches any exception.
109+
110+
See [Jextract.md](Jextract.md) for the plugin configuration and the recommended `<bindings>` layout.
111+
112+
## Build integration
113+
114+
- **Checkstyle / forbidden-apis.** The committed generated tree under `src/main/java` is scanned at
115+
`validate` and fails it (no copyright header, no `package-info.java`, line length, and so on). Exempt
116+
it, preferably module-locally with `<checkstyle.excludes>**/generated/**/*</checkstyle.excludes>`
117+
(and the forbidden-apis equivalent), or by adding a `generated/` entry to the shared
118+
`SuppressionFilter` in `netty-build-common` (a cross-artifact change). Needed before the first FFM
119+
module builds.
120+
- **Java version.** FFM requires Java 25; gate the modules behind a JDK-25-activated profile (as the
121+
existing `java25` profile does) so older JDKs skip them. No multi-release jar needed.
122+
- **`module-info`** via the `io.github.dmlloyd.module-info` plugin. No `provides`: transport selection
123+
stays explicit (`KQueue.isAvailable() ? ... : NioIoHandler.newFactory()`), as with the other
124+
transports.
125+
- **Runtime flags** for consumers and the testsuite: `--enable-native-access=ALL-UNNAMED` plus the
126+
usual `--add-opens java.base/sun.nio.ch=ALL-UNNAMED`.
127+
- **Aggregation.** Add the modules to the root `<modules>`, the BOM, and the `all` aggregator.
128+
129+
## Open questions
130+
131+
- Module naming: `transport-ffm-native-kqueue` vs `transport-ffm-bindings-kqueue`.
132+
- JDK gating: profile-skip on JDK < 25 vs. hard-require 25 to build.
133+
- Exact jextract option for pinning the SDK sysroot (may be `SDKROOT` or a clang-arg passthrough
134+
rather than a first-class flag).
135+
- `critical()` / `captureCallState` composition; benchmark before relying on it.
136+
137+
## Prerequisites for the first FFM module (not yet done)
138+
139+
- The checkstyle / forbidden-apis exemption above.
140+
- CI: the per-OS regeneration-diff check and the arch-invariance byte-identical check.
141+
- Migrate the exploratory `ffm/` prototype into the real modules and delete it.
Collapse file

‎docs/Jextract.md‎

Copy file name to clipboard
+85Lines changed: 85 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# jextract Maven Plugin
2+
3+
`netty5-jextract-maven-plugin` generates `java.lang.foreign` (FFM) bindings by driving a pre-installed
4+
[`jextract`](https://jdk.java.net/jextract/), one invocation per `<binding>`. Output is **committed
5+
to git**; ordinary builds and CI compile it and never run jextract. Regenerate only when the requested
6+
symbols change.
7+
8+
## Install jextract
9+
10+
A developer prerequisite, **never downloaded by the build**. Install from
11+
<https://jdk.java.net/jextract/>. The plugin resolves it from the `jextract` parameter
12+
(`-Djextract=…`), then `$JEXTRACT`, then `PATH`, failing with an actionable message if none is
13+
executable. (PATH lookup is POSIX-only; on Windows point at the binary explicitly.)
14+
15+
## Configure
16+
17+
```xml
18+
<plugin>
19+
<groupId>io.netty</groupId>
20+
<artifactId>netty5-jextract-maven-plugin</artifactId>
21+
<version>${project.version}</version>
22+
<configuration>
23+
<targetPackage>io.netty.channel.unix.ffm.generated</targetPackage>
24+
<jextractVersion>22</jextractVersion>
25+
<bindings>
26+
<binding>
27+
<header>sys/socket.h</header>
28+
<className>BsdSocket</className>
29+
<functions>
30+
<function>socket</function>
31+
<function>bind</function>
32+
</functions>
33+
<constants>
34+
<constant>AF_INET</constant>
35+
</constants>
36+
</binding>
37+
</bindings>
38+
</configuration>
39+
</plugin>
40+
```
41+
42+
| Parameter | Req | Default | Notes |
43+
|---|---|---|---|
44+
| `targetPackage` | yes | none | Package for generated code (`--target-package`). |
45+
| `jextractVersion` | yes | none | Pinned version (`jextract.version`); the goal runs `jextract --version` and fails on mismatch. |
46+
| `bindings` | no | none | One `<binding>` per class; unique `className`. Empty = no-op. |
47+
| `sdk` | no | none | Toolchain id (e.g. `MacOSX14.5`, `jextract.sdk`); recorded in the provenance manifest, not verified. |
48+
| `outputDirectory` | no | `…/src/main/java/generated` | Where sources are written (committed). Must share a filesystem with the build directory (the default does), since promotion is an atomic rename. |
49+
| `jextract` | no | none | Explicit path (`jextract`); overrides `$JEXTRACT`/`PATH`. |
50+
| `timeoutSeconds` | no | `300` | Per-invocation timeout (`jextract.timeoutSeconds`); `0` = unbounded. |
51+
52+
Each `<binding>`: `header` (passed to jextract **verbatim**, libclang resolves it against the active
53+
SDK, so a system path like `sys/socket.h` works with no vendored copy and no `-I`/`-D`), `className`
54+
(`--header-class-name`), and the symbol lists mapping to `--include-*`: `functions`, `structs`,
55+
`constants`, `typedefs`, `unions`, `vars`.
56+
57+
## Binding layout: one type per binding
58+
59+
jextract writes each **struct / union / aggregate typedef** to a standalone file named after the C
60+
symbol (`sockaddr_in.java`), so two bindings requesting the same one clobber each other, the plugin
61+
fails the build if they do. Functions, constants, and vars live *inside* their header class and never
62+
clash. Therefore:
63+
64+
- Own each struct/union/aggregate typedef in **one** binding (a types-only binding per header).
65+
- Other bindings list only functions/constants/vars and use those types **by pointer** (`C_POINTER`,
66+
no `--include` needed).
67+
- Don't list **primitive** typedefs (e.g. `socklen_t`), jextract inlines them, so they emit no file.
68+
- A function taking/returning a struct/union **by value** must sit in the same binding as that type
69+
(jextract errors otherwise); rare for the pointer-based POSIX/kqueue APIs.
70+
71+
## Regenerate
72+
73+
Not bound to a lifecycle phase, run explicitly on the **matching OS**, then commit the output:
74+
75+
```bash
76+
mvn -pl <module> io.netty:netty5-jextract-maven-plugin:generate -Djextract.version=22
77+
```
78+
79+
- **Deterministic per OS/arch.** macOS bindings are ABI-identical across x86_64/arm64, regenerating
80+
on both must be byte-identical.
81+
- **Atomic + pruning.** Generates into a staging dir and, only on full success, prunes the
82+
`targetPackage` subtree and swaps the fresh output in, no orphans, and a failed run never touches
83+
the committed tree. Hand-written code must live outside `targetPackage`.
84+
- **Provenance.** Writes a deterministic `GENERATED.properties` (`jextract.version`, `os`, `sdk`) with
85+
no timestamps or machine paths, so a toolchain change surfaces as a reviewable diff.
Collapse file

‎jextract-maven-plugin/pom.xml‎

Copy file name to clipboard
+136Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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 C headers by driving a pre-installed
31+
jextract binary. Generation is on-demand; the produced sources are committed to git so
32+
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+
<dependency>
89+
<groupId>org.junit.jupiter</groupId>
90+
<artifactId>junit-jupiter-params</artifactId>
91+
<scope>test</scope>
92+
</dependency>
93+
</dependencies>
94+
95+
<build>
96+
<plugins>
97+
<plugin>
98+
<groupId>org.apache.maven.plugins</groupId>
99+
<artifactId>maven-plugin-plugin</artifactId>
100+
<version>${mavenPlugin.tools.version}</version>
101+
<configuration>
102+
<goalPrefix>jextract</goalPrefix>
103+
</configuration>
104+
<executions>
105+
<execution>
106+
<id>default-descriptor</id>
107+
<phase>process-classes</phase>
108+
<goals>
109+
<goal>descriptor</goal>
110+
</goals>
111+
</execution>
112+
</executions>
113+
</plugin>
114+
<plugin>
115+
<!--
116+
The parent binds maven-jar-plugin to a manifestFile produced by maven-bundle-plugin.
117+
maven-bundle-plugin only supports jar/bundle packaging, so for this maven-plugin module
118+
no such manifest is generated; override the execution to let the jar plugin build its
119+
own manifest (the plugin descriptor is already written into target/classes).
120+
-->
121+
<groupId>org.apache.maven.plugins</groupId>
122+
<artifactId>maven-jar-plugin</artifactId>
123+
<executions>
124+
<execution>
125+
<id>default-jar</id>
126+
<configuration combine.self="override">
127+
<archive>
128+
<addMavenDescriptor>true</addMavenDescriptor>
129+
</archive>
130+
</configuration>
131+
</execution>
132+
</executions>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
</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.