Skip to content

Commit 28a3f65

Browse files
committed
Windows: use latest jextract
1 parent 9f31481 commit 28a3f65

File tree

15 files changed

+160
-16
lines changed

15 files changed

+160
-16
lines changed

java-does-usb/jextract/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ The generated code has the same problem as the Linux code for *udev*. It must be
6565

6666
## Windows
6767

68-
Most Windows SDK header files are not independent. They require that `Windows.h` is included first. So instead of specifying the target header files directly, a helper header file (`windows_headers.h` in this directory) is specified.
68+
Most Windows SDK header files are not independent. They require `Windows.h` to be included first. So instead of specifying the target header files directly, a helper header file (`windows_headers.h` in this directory) is specified.
6969

7070
Compared to Linux and macOS, the code generation on Windows is very slow (about 1 min vs 3 seconds). And jextract crashes sometimes.
7171

java-does-usb/jextract/windows/gen_win.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
set JEXTRACT=..\..\..\..\jextract-22\bin\jextract.bat
1+
set JEXTRACT=..\..\..\..\jextract\build\jextract\bin\jextract.bat
22
set SDK_DIR=C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0
33

44
call %JEXTRACT% --output ../../src/main/java ^

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/advapi32/Advapi32.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Advapi32"), LIBRARY_ARENA)
5068
.or(SymbolLookup.loaderLookup())
5169
.or(Linker.nativeLinker().defaultLookup());

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/kernel32/Kernel32.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Kernel32"), LIBRARY_ARENA)
5068
.or(SymbolLookup.loaderLookup())
5169
.or(Linker.nativeLinker().defaultLookup());

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/ntdll/NtDll.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.loaderLookup()
5068
.or(Linker.nativeLinker().defaultLookup());
5169

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/ole32/Ole32.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Ole32"), LIBRARY_ARENA)
5068
.or(SymbolLookup.loaderLookup())
5169
.or(Linker.nativeLinker().defaultLookup());

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/setupapi/SetupAPI.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("SetupAPI"), LIBRARY_ARENA)
5068
.or(SymbolLookup.loaderLookup())
5169
.or(Linker.nativeLinker().defaultLookup());

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/usbioctl/USBIoctl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.loaderLookup()
5068
.or(Linker.nativeLinker().defaultLookup());
5169

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/usbioctl/_USB_DESCRIPTOR_REQUEST.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class _USB_DESCRIPTOR_REQUEST {
3939
}
4040

4141
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
42-
USBIoctl.C_LONG.withByteAlignment(1).withName("ConnectionIndex"),
42+
USBIoctl.align(USBIoctl.C_LONG, 1).withName("ConnectionIndex"),
4343
_USB_DESCRIPTOR_REQUEST.SetupPacket.layout().withName("SetupPacket"),
4444
MemoryLayout.sequenceLayout(0, USBIoctl.C_CHAR).withName("Data")
4545
).withName("_USB_DESCRIPTOR_REQUEST");
@@ -115,9 +115,9 @@ public static class SetupPacket {
115115
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
116116
USBIoctl.C_CHAR.withName("bmRequest"),
117117
USBIoctl.C_CHAR.withName("bRequest"),
118-
USBIoctl.C_SHORT.withByteAlignment(1).withName("wValue"),
119-
USBIoctl.C_SHORT.withByteAlignment(1).withName("wIndex"),
120-
USBIoctl.C_SHORT.withByteAlignment(1).withName("wLength")
118+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wValue"),
119+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wIndex"),
120+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wLength")
121121
).withName("$anon$971:5");
122122

123123
/**

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/usbioctl/_USB_DEVICE_DESCRIPTOR.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ public class _USB_DEVICE_DESCRIPTOR {
4242
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
4343
USBIoctl.C_CHAR.withName("bLength"),
4444
USBIoctl.C_CHAR.withName("bDescriptorType"),
45-
USBIoctl.C_SHORT.withByteAlignment(1).withName("bcdUSB"),
45+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("bcdUSB"),
4646
USBIoctl.C_CHAR.withName("bDeviceClass"),
4747
USBIoctl.C_CHAR.withName("bDeviceSubClass"),
4848
USBIoctl.C_CHAR.withName("bDeviceProtocol"),
4949
USBIoctl.C_CHAR.withName("bMaxPacketSize0"),
50-
USBIoctl.C_SHORT.withByteAlignment(1).withName("idVendor"),
51-
USBIoctl.C_SHORT.withByteAlignment(1).withName("idProduct"),
52-
USBIoctl.C_SHORT.withByteAlignment(1).withName("bcdDevice"),
50+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("idVendor"),
51+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("idProduct"),
52+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("bcdDevice"),
5353
USBIoctl.C_CHAR.withName("iManufacturer"),
5454
USBIoctl.C_CHAR.withName("iProduct"),
5555
USBIoctl.C_CHAR.withName("iSerialNumber"),

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/usbioctl/_USB_ENDPOINT_DESCRIPTOR.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class _USB_ENDPOINT_DESCRIPTOR {
3636
USBIoctl.C_CHAR.withName("bDescriptorType"),
3737
USBIoctl.C_CHAR.withName("bEndpointAddress"),
3838
USBIoctl.C_CHAR.withName("bmAttributes"),
39-
USBIoctl.C_SHORT.withByteAlignment(1).withName("wMaxPacketSize"),
39+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wMaxPacketSize"),
4040
USBIoctl.C_CHAR.withName("bInterval")
4141
).withName("_USB_ENDPOINT_DESCRIPTOR");
4242

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/usbioctl/_USB_NODE_CONNECTION_INFORMATION_EX.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ public class _USB_NODE_CONNECTION_INFORMATION_EX {
3939
}
4040

4141
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
42-
USBIoctl.C_LONG.withByteAlignment(1).withName("ConnectionIndex"),
42+
USBIoctl.align(USBIoctl.C_LONG, 1).withName("ConnectionIndex"),
4343
_USB_DEVICE_DESCRIPTOR.layout().withName("DeviceDescriptor"),
4444
USBIoctl.C_CHAR.withName("CurrentConfigurationValue"),
4545
USBIoctl.C_CHAR.withName("Speed"),
4646
USBIoctl.C_CHAR.withName("DeviceIsHub"),
47-
USBIoctl.C_SHORT.withByteAlignment(1).withName("DeviceAddress"),
48-
USBIoctl.C_LONG.withByteAlignment(1).withName("NumberOfOpenPipes"),
49-
USBIoctl.C_INT.withByteAlignment(1).withName("ConnectionStatus"),
47+
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("DeviceAddress"),
48+
USBIoctl.align(USBIoctl.C_LONG, 1).withName("NumberOfOpenPipes"),
49+
USBIoctl.align(USBIoctl.C_INT, 1).withName("ConnectionStatus"),
5050
MemoryLayout.sequenceLayout(0, _USB_PIPE_INFO.layout()).withName("PipeList")
5151
).withName("_USB_NODE_CONNECTION_INFORMATION_EX");
5252

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/usbioctl/_USB_PIPE_INFO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class _USB_PIPE_INFO {
2828

2929
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
3030
_USB_ENDPOINT_DESCRIPTOR.layout().withName("EndpointDescriptor"),
31-
USBIoctl.C_LONG.withByteAlignment(1).withName("ScheduleOffset")
31+
USBIoctl.align(USBIoctl.C_LONG, 1).withName("ScheduleOffset")
3232
).withName("_USB_PIPE_INFO");
3333

3434
/**

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/user32/User32.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("User32"), LIBRARY_ARENA)
5068
.or(SymbolLookup.loaderLookup())
5169
.or(Linker.nativeLinker().defaultLookup());

java-does-usb/src/main/java/net/codecrete/usb/windows/gen/winusb/WinUSB.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
import java.lang.foreign.AddressLayout;
66
import java.lang.foreign.Arena;
77
import java.lang.foreign.FunctionDescriptor;
8+
import java.lang.foreign.GroupLayout;
89
import java.lang.foreign.Linker;
910
import java.lang.foreign.MemoryLayout;
1011
import java.lang.foreign.MemorySegment;
12+
import java.lang.foreign.PaddingLayout;
13+
import java.lang.foreign.SequenceLayout;
14+
import java.lang.foreign.StructLayout;
1115
import java.lang.foreign.SymbolLookup;
1216
import java.lang.foreign.ValueLayout;
1317
import java.lang.invoke.MethodHandle;
@@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
4650
}
4751
}
4852

53+
static MemoryLayout align(MemoryLayout layout, long align) {
54+
return switch (layout) {
55+
case PaddingLayout p -> p;
56+
case ValueLayout v -> v.withByteAlignment(align);
57+
case GroupLayout g -> {
58+
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
59+
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
60+
yield g instanceof StructLayout ?
61+
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
62+
}
63+
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
64+
};
65+
}
66+
4967
static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Winusb"), LIBRARY_ARENA)
5068
.or(SymbolLookup.loaderLookup())
5169
.or(Linker.nativeLinker().defaultLookup());

0 commit comments

Comments
 (0)