<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
Yes, you're right. No ARM pre-built binaries yet. Jextract generated code is platform dependent (unless you extract really portable C code - we jextract libclang on one platform and have checked-in version of generated sources in jextract repo). But in general,
extracting on intel and running on ARM may not work <span id="π">π</span></div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);" class="elementToProof">
<span>-Sundar</span></div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> panama-dev <panama-dev-retn@openjdk.org> on behalf of Manuel Bleichenbacher <manuel.bleichenbacher@gmail.com><br>
<b>Sent:</b> 22 August 2022 12:33<br>
<b>To:</b> panama-dev@openjdk.org <panama-dev@openjdk.org><br>
<b>Subject:</b> Re: Java does USB</font>
<div> </div>
</div>
<div>
<div dir="ltr">
<div>I wasn't aware of the prebuilt binaries as I've mistaken this page <a href="https://jdk.java.net/panama/">
https://jdk.java.net/panama/</a> with that one <a href="https://jdk.java.net/jextract/">
https://jdk.java.net/jextract/</a>. The former one links to rather old JDK versions (January). It's all a bit confusing and difficult to get started. But I will give the pre-built binaries a try.</div>
<div><br>
</div>
<div>As the pre-built binaries do not include a version for macOS on ARM: Can I just run the Intel version and the result will be the same? Or how do the tool architecture and the target architecture interact?</div>
<div><br>
</div>
<div>As for the specific examples for things that didn't work as expected: I will compile a list (and post it to the jextract mainling list). It will likely take me 2 days to compile it.</div>
<div><br>
</div>
<div>And I'm aware that the APIs will likely change. No problem.<br>
</div>
</div>
<br>
<div class="x_gmail_quote">
<div dir="ltr" class="x_gmail_attr">On Mon, Aug 22, 2022 at 3:37 AM Sundararajan Athijegannathan <<a href="mailto:sundararajan.athijegannathan@oracle.com">sundararajan.athijegannathan@oracle.com</a>> wrote:<br>
</div>
<blockquote class="x_gmail_quote" style="margin:0px 0px 0px 0.8ex; border-left:1px solid rgb(204,204,204); padding-left:1ex">
<div dir="ltr">
<div>
<ul style="color:rgb(0,0,0); font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt">
<li><span><span style="color:rgb(32,31,30); font-size:15px; background-color:rgb(255,255,255); display:inline">difficult to build</span></span></li></ul>
<div><font color="#201f1e"><span style="font-size:15px">jextract prebuilt builds are available here
<a href="https://jdk.java.net/jextract/" id="x_gmail-m_-4008564258900588545LPlnkOWALinkPreview" target="_blank">
https://jdk.java.net/jextract/</a></span></font></div>
<div>
<div id="x_gmail-m_-4008564258900588545LPBorder_GTaHR0cHM6Ly9qZGsuamF2YS5uZXQvamV4dHJhY3Qv" style="width:100%; margin-top:16px; margin-bottom:16px; max-width:800px; min-width:424px">
<table id="x_gmail-m_-4008564258900588545LPContainer508545" role="presentation" style="padding:12px 36px 12px 12px; width:100%; border:1px solid rgb(200,200,200); border-radius:2px">
<tbody>
<tr valign="top" style="border-spacing:0px">
<td style="width:100%">
<div id="x_gmail-m_-4008564258900588545LPTitle508545" style="font-size:21px; font-weight:300; margin-right:8px; font-family:wf_segoe-ui_light,"Segoe UI Light","Segoe WP Light","Segoe UI","Segoe WP",Tahoma,Arial,sans-serif; margin-bottom:12px">
<a id="x_gmail-m_-4008564258900588545LPUrlAnchor508545" href="https://jdk.java.net/jextract/" target="_blank" style="text-decoration:none">Jextract Early-Access Builds - Oracle</a></div>
<div id="x_gmail-m_-4008564258900588545LPDescription508545" style="font-size:14px; max-height:100px; color:rgb(102,102,102); font-family:wf_segoe-ui_normal,"Segoe UI","Segoe WP",Tahoma,Arial,sans-serif; margin-bottom:12px; margin-right:8px; overflow:hidden">
Notes. These builds are based on an incomplete version of JDK 19.; If you are using macOS Catalina or later you may need to remove the quarantine attribute from the bits before you can use the jextract binaries.. To do this, run the following: $ sudo xattr
-r -d com.apple.quarantine path/to/jextract/folder/</div>
<div id="x_gmail-m_-4008564258900588545LPMetadata508545" style="font-size:14px; font-weight:400; color:rgb(166,166,166); font-family:wf_segoe-ui_normal,"Segoe UI","Segoe WP",Tahoma,Arial,sans-serif">
<a href="http://jdk.java.net" target="_blank">jdk.java.net</a></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div><font color="#201f1e"><span style="font-size:15px"><span><font color="#201f1e" style="font-size:14px"><span style="font-size:15px; margin:0px"><br>
</span></font></span></span></font></div>
<div>
<ul>
<li><span><font><span style="margin:0px"></span><span style="color:rgb(32,31,30); font-size:15px; margin:0px">"</span></font><span style="color:rgb(32,31,30); font-size:15px; margin:0px">mixup of system headers, excessive number of files and code"</span><span style="font-size:14px; margin:0px; color:rgb(0,0,0)"></span><br>
</span></li></ul>
</div>
<div><font color="#201f1e"><span style="font-size:15px"><span><span style="font-size:14px; margin:0px; color:rgb(0,0,0)"><span style="font-size:15px; margin:0px; color:rgb(32,31,30)"><br>
</span></span></span></span></font></div>
<div><font color="#201f1e"><span style="font-size:15px"><span><span style="font-size:14px; margin:0px; color:rgb(0,0,0)"><span style="font-size:15px; margin:0px; color:rgb(32,31,30)"> --include- options can be used to filter unwanted stuff from headers.
You may want to check clang.symbols files in the repo (which filters unwanted symbols from clang's Index.h for bootstrapping jextract)</span></span></span></span></font></div>
<div><span style="color:rgb(32,31,30); font-size:15px"><br>
</span></div>
<div>
<ul>
<li><span><span style="color:rgb(32,31,30); font-size:15px"></span><span style="color:rgb(32,31,30); font-size:15px">strange class and filenames</span><br>
</span></li></ul>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<font face="Calibri, Arial, Helvetica, sans-serif"><span>ββββββWithout those $ names, generated java identifiers would clash with C identifier names. Also there are C identifiers that are Java keywords/reserved words.</span></font></div>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<ul>
<li><span><span style="color:rgb(32,31,30); font-size:15px; background-color:rgb(255,255,255); display:inline">, missing constants/defines etc.).</span><br>
</span></li></ul>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<font face="Calibri, Arial, Helvetica, sans-serif"><span>ββββββDo you have specific constant/define that was not generated? Please note that function-like macros are not supported. But other #defines, enum constants are supported and constant getter methods
are generated. But if you found any specific case, please do send us the details.</span><br>
</font></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<font face="Calibri, Arial, Helvetica, sans-serif"><span><br>
</span></font></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<font face="Calibri, Arial, Helvetica, sans-serif"><span>As for API changes, please note that panama API is still in preview-mode (as of jdk19). It was incubator API in the previously. Some API evolution is expected till it becomes a standard API.</span></font></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<font face="Calibri, Arial, Helvetica, sans-serif"><span>-Sundar</span></font></div>
<div style="font-family:Calibri,Arial,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<font face="Calibri, Arial, Helvetica, sans-serif"><br>
</font></div>
</div>
<div id="x_gmail-m_-4008564258900588545appendonsend"></div>
<hr style="display:inline-block; width:98%">
<div id="x_gmail-m_-4008564258900588545divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> panama-dev <<a href="mailto:panama-dev-retn@openjdk.org" target="_blank">panama-dev-retn@openjdk.org</a>> on
behalf of Manuel Bleichenbacher <<a href="mailto:manuel.bleichenbacher@gmail.com" target="_blank">manuel.bleichenbacher@gmail.com</a>><br>
<b>Sent:</b> 22 August 2022 02:13<br>
<b>To:</b> <a href="mailto:panama-dev@openjdk.org" target="_blank">panama-dev@openjdk.org</a> <<a href="mailto:panama-dev@openjdk.org" target="_blank">panama-dev@openjdk.org</a>><br>
<b>Subject:</b> Java does USB</font>
<div> </div>
</div>
<div>
<div dir="ltr">When I reported the issue related to GetLastError(), I was working on a library for communicating with USB devices from Java. The library has made good progress and the current result is available on GitHub: <a href="https://github.com/manuelbl/JavaDoesUSB" target="_blank">https://github.com/manuelbl/JavaDoesUSB</a>.
It runs on Windows, macOS and Linux, both on Intel and ARM processors (I haven't tested ARM on Windows yet) and does not require any additional libraries to be installed.
<div><br>
</div>
<div>The main use case for project Panama seems to be to make third-party libraries accessible to Java. This library is different: it makes native operating system APIs accessible.</div>
<div><br>
</div>
<div>My experience with project Panama so far:</div>
<div><br>
</div>
<div>Except for the GetLastError() issue, it just works. The VM never crashed because of a bug. And in most cases where I made a mistake with native data structures or function calls, it was caught with a run-time exception. In a few cases, I managed to pass
invalid data to API calls, which then crashed the VM.<br>
<br>
</div>
<div>The USB APIs on Windows, macOS and Linux are fundamentally different. So there was hardly any synergies between them. But the code for the Intel and ARM variants on each platform is identical. I guess these two 64-bit architectures have similar concepts
for passing arguments and aligning data. But project Panama also seems to take care of some of the differences.</div>
<div><br>
</div>
<div>I've written all the code for the native function calls and native data structures manually. I've tested jextract but found it frustrating (difficult to build, mixup of system headers, excessive number of files and code, strange class and filenames, missing
constants/defines etc.). It might work better for a well-written third-party library. But for all the crust that has accumulated in a 40 year old operating system, manual work turned out to be a better option.</div>
<div><br>
</div>
<div>The documentation is clearly insufficient. I had to learn from hitting exception, guessing the causes and experimenting with solutions. And with Google you find a lot of outdated documents. The documentation doesn't seem to explain how members in a StructLayout
are aligned, why you can't pass a memory segment created with MemorySegment.ofArray() to a native function etc. And if it does, it is so distributed that I didn't find it.</div>
<div><br>
</div>
<div>Translating C code into Java, I would have expected that this C structure, which requires 4 bytes of padding between timeout and data:</div>
<div>
<div>
<pre style="background-color:rgb(43,43,43); color:rgb(169,183,198); font-family:"JetBrains Mono",monospace; font-size:9.8pt"><span style="color:rgb(128,128,128)">struct usbdevfs_ctrltransfer {<br></span><span style="color:rgb(128,128,128)"> __u8 bRequestType;<br></span><span style="color:rgb(128,128,128)"> __u8 bRequest;<br></span><span style="color:rgb(128,128,128)"> __u16 wValue;<br></span><span style="color:rgb(128,128,128)"> __u16 wIndex;<br></span><span style="color:rgb(128,128,128)"> __u16 wLength;<br></span><span style="color:rgb(128,128,128)"> __u32 timeout; /* in milliseconds */<br></span><span style="color:rgb(128,128,128)"> void *data;<br></span><span style="color:rgb(128,128,128)">};<br></span></pre>
</div>
<div>simply translates to:</div>
<div>
<pre style="background-color:rgb(43,43,43); color:rgb(169,183,198); font-family:"JetBrains Mono",monospace; font-size:9.8pt"><span style="color:rgb(204,120,50)">public static final </span>GroupLayout <span style="color:rgb(152,118,170); font-style:italic">ctrltransfer$Struct </span>= <span style="font-style:italic">structLayout</span>(<br> <span style="color:rgb(152,118,170); font-style:italic">JAVA_BYTE</span>.withName(<span style="color:rgb(106,135,89)">"bRequestType"</span>)<span style="color:rgb(204,120,50)">,<br></span><span style="color:rgb(204,120,50)"> </span><span style="color:rgb(152,118,170); font-style:italic">JAVA_BYTE</span>.withName(<span style="color:rgb(106,135,89)">"bRequest"</span>)<span style="color:rgb(204,120,50)">,<br></span><span style="color:rgb(204,120,50)"> </span><span style="color:rgb(152,118,170); font-style:italic">JAVA_SHORT</span>.withName(<span style="color:rgb(106,135,89)">"wValue"</span>)<span style="color:rgb(204,120,50)">,<br></span><span style="color:rgb(204,120,50)"> </span><span style="color:rgb(152,118,170); font-style:italic">JAVA_SHORT</span>.withName(<span style="color:rgb(106,135,89)">"wIndex"</span>)<span style="color:rgb(204,120,50)">,<br></span><span style="color:rgb(204,120,50)"> </span><span style="color:rgb(152,118,170); font-style:italic">JAVA_SHORT</span>.withName(<span style="color:rgb(106,135,89)">"wLength"</span>)<span style="color:rgb(204,120,50)">,<br></span><span style="color:rgb(204,120,50)"> </span><span style="color:rgb(152,118,170); font-style:italic">JAVA_INT</span>.withName(<span style="color:rgb(106,135,89)">"timeout"</span>)<span style="color:rgb(204,120,50)">,<br></span><span style="color:rgb(204,120,50)"> </span><span style="color:rgb(152,118,170); font-style:italic">ADDRESS</span>.withName(<span style="color:rgb(106,135,89)">"data"</span>)<br>)<span style="color:rgb(204,120,50)">;<br></span></pre>
</div>
<div>After all, ADDRESS has an alignment of 64 bits / 8 bytes. So the padding could be added automatically. But it just throws an error when used. Instead the padding must be specified manually. Very surprising. And even if it needs to be specified manually,
I'd rather specify that 'data' needs an 8 byte alignment instead of calculating the padding and adding paddingLayout(32).</div>
<div><br>
</div>
<div>A single time, I hit an implementation limitation: The macOS function CFUUIDGetConstantUUIDWithBytes cannot be implemented (<a href="https://developer.apple.com/documentation/corefoundation/1542189-cfuuidgetconstantuuidwithbytes" target="_blank">https://developer.apple.com/documentation/corefoundation/1542189-cfuuidgetconstantuuidwithbytes</a>).
I found a workaround.</div>
<div><br>
</div>
<div>Overall, I like project Panama and hope that it will leave preview phase soon (including a fix for the GetLastError() issue).</div>
</div>
<div><br>
</div>
<div>My USB library has still a way to go. The next step will be technically challenging: notification about USD devices being plugged in or removed. It will require a run loop and Mach ports on macOS, message handling with message-only windows on Windows and
non-blocking I/O with select() on Linux.</div>
<div><br>
</div>
<div>Regards</div>
<div>Manuel</div>
<div><br>
</div>
</div>
</div>
</div>
</blockquote>
</div>
</div>
</body>
</html>