Skip to content

Commit

Permalink
Windows: use latest jextract
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Feb 20, 2024
1 parent 9f31481 commit 28a3f65
Show file tree
Hide file tree
Showing 15 changed files with 160 additions and 16 deletions.
2 changes: 1 addition & 1 deletion java-does-usb/jextract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The generated code has the same problem as the Linux code for *udev*. It must be

## Windows

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.
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.

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

Expand Down
2 changes: 1 addition & 1 deletion java-does-usb/jextract/windows/gen_win.cmd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
set JEXTRACT=..\..\..\..\jextract-22\bin\jextract.bat
set JEXTRACT=..\..\..\..\jextract\build\jextract\bin\jextract.bat
set SDK_DIR=C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0

call %JEXTRACT% --output ../../src/main/java ^
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Advapi32"), LIBRARY_ARENA)
.or(SymbolLookup.loaderLookup())
.or(Linker.nativeLinker().defaultLookup());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Kernel32"), LIBRARY_ARENA)
.or(SymbolLookup.loaderLookup())
.or(Linker.nativeLinker().defaultLookup());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.loaderLookup()
.or(Linker.nativeLinker().defaultLookup());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Ole32"), LIBRARY_ARENA)
.or(SymbolLookup.loaderLookup())
.or(Linker.nativeLinker().defaultLookup());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("SetupAPI"), LIBRARY_ARENA)
.or(SymbolLookup.loaderLookup())
.or(Linker.nativeLinker().defaultLookup());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.loaderLookup()
.or(Linker.nativeLinker().defaultLookup());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class _USB_DESCRIPTOR_REQUEST {
}

private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
USBIoctl.C_LONG.withByteAlignment(1).withName("ConnectionIndex"),
USBIoctl.align(USBIoctl.C_LONG, 1).withName("ConnectionIndex"),
_USB_DESCRIPTOR_REQUEST.SetupPacket.layout().withName("SetupPacket"),
MemoryLayout.sequenceLayout(0, USBIoctl.C_CHAR).withName("Data")
).withName("_USB_DESCRIPTOR_REQUEST");
Expand Down Expand Up @@ -115,9 +115,9 @@ public static class SetupPacket {
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
USBIoctl.C_CHAR.withName("bmRequest"),
USBIoctl.C_CHAR.withName("bRequest"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("wValue"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("wIndex"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("wLength")
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wValue"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wIndex"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wLength")
).withName("$anon$971:5");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ public class _USB_DEVICE_DESCRIPTOR {
private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
USBIoctl.C_CHAR.withName("bLength"),
USBIoctl.C_CHAR.withName("bDescriptorType"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("bcdUSB"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("bcdUSB"),
USBIoctl.C_CHAR.withName("bDeviceClass"),
USBIoctl.C_CHAR.withName("bDeviceSubClass"),
USBIoctl.C_CHAR.withName("bDeviceProtocol"),
USBIoctl.C_CHAR.withName("bMaxPacketSize0"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("idVendor"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("idProduct"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("bcdDevice"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("idVendor"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("idProduct"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("bcdDevice"),
USBIoctl.C_CHAR.withName("iManufacturer"),
USBIoctl.C_CHAR.withName("iProduct"),
USBIoctl.C_CHAR.withName("iSerialNumber"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class _USB_ENDPOINT_DESCRIPTOR {
USBIoctl.C_CHAR.withName("bDescriptorType"),
USBIoctl.C_CHAR.withName("bEndpointAddress"),
USBIoctl.C_CHAR.withName("bmAttributes"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("wMaxPacketSize"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("wMaxPacketSize"),
USBIoctl.C_CHAR.withName("bInterval")
).withName("_USB_ENDPOINT_DESCRIPTOR");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public class _USB_NODE_CONNECTION_INFORMATION_EX {
}

private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
USBIoctl.C_LONG.withByteAlignment(1).withName("ConnectionIndex"),
USBIoctl.align(USBIoctl.C_LONG, 1).withName("ConnectionIndex"),
_USB_DEVICE_DESCRIPTOR.layout().withName("DeviceDescriptor"),
USBIoctl.C_CHAR.withName("CurrentConfigurationValue"),
USBIoctl.C_CHAR.withName("Speed"),
USBIoctl.C_CHAR.withName("DeviceIsHub"),
USBIoctl.C_SHORT.withByteAlignment(1).withName("DeviceAddress"),
USBIoctl.C_LONG.withByteAlignment(1).withName("NumberOfOpenPipes"),
USBIoctl.C_INT.withByteAlignment(1).withName("ConnectionStatus"),
USBIoctl.align(USBIoctl.C_SHORT, 1).withName("DeviceAddress"),
USBIoctl.align(USBIoctl.C_LONG, 1).withName("NumberOfOpenPipes"),
USBIoctl.align(USBIoctl.C_INT, 1).withName("ConnectionStatus"),
MemoryLayout.sequenceLayout(0, _USB_PIPE_INFO.layout()).withName("PipeList")
).withName("_USB_NODE_CONNECTION_INFORMATION_EX");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class _USB_PIPE_INFO {

private static final GroupLayout $LAYOUT = MemoryLayout.structLayout(
_USB_ENDPOINT_DESCRIPTOR.layout().withName("EndpointDescriptor"),
USBIoctl.C_LONG.withByteAlignment(1).withName("ScheduleOffset")
USBIoctl.align(USBIoctl.C_LONG, 1).withName("ScheduleOffset")
).withName("_USB_PIPE_INFO");

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("User32"), LIBRARY_ARENA)
.or(SymbolLookup.loaderLookup())
.or(Linker.nativeLinker().defaultLookup());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
import java.lang.foreign.AddressLayout;
import java.lang.foreign.Arena;
import java.lang.foreign.FunctionDescriptor;
import java.lang.foreign.GroupLayout;
import java.lang.foreign.Linker;
import java.lang.foreign.MemoryLayout;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.PaddingLayout;
import java.lang.foreign.SequenceLayout;
import java.lang.foreign.StructLayout;
import java.lang.foreign.SymbolLookup;
import java.lang.foreign.ValueLayout;
import java.lang.invoke.MethodHandle;
Expand Down Expand Up @@ -46,6 +50,20 @@ static MethodHandle upcallHandle(Class<?> fi, String name, FunctionDescriptor fd
}
}

static MemoryLayout align(MemoryLayout layout, long align) {
return switch (layout) {
case PaddingLayout p -> p;
case ValueLayout v -> v.withByteAlignment(align);
case GroupLayout g -> {
MemoryLayout[] alignedMembers = g.memberLayouts().stream()
.map(m -> align(m, align)).toArray(MemoryLayout[]::new);
yield g instanceof StructLayout ?
MemoryLayout.structLayout(alignedMembers) : MemoryLayout.unionLayout(alignedMembers);
}
case SequenceLayout s -> MemoryLayout.sequenceLayout(s.elementCount(), align(s.elementLayout(), align));
};
}

static final SymbolLookup SYMBOL_LOOKUP = SymbolLookup.libraryLookup(System.mapLibraryName("Winusb"), LIBRARY_ARENA)
.or(SymbolLookup.loaderLookup())
.or(Linker.nativeLinker().defaultLookup());
Expand Down

0 comments on commit 28a3f65

Please sign in to comment.