1 <!DOCTYPE html>
  2 <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
  3 <head>
  4   <meta charset="utf-8" />
  5   <meta name="generator" content="pandoc" />
  6   <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
  7   <title>panama_jextract</title>
  8   <style>
  9     code{white-space: pre-wrap;}
 10     span.smallcaps{font-variant: small-caps;}
 11     span.underline{text-decoration: underline;}
 12     div.column{display: inline-block; vertical-align: top; width: 50%;}
 13     div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
 14     ul.task-list{list-style: none;}
 15     pre > code.sourceCode { white-space: pre; position: relative; }
 16     pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
 17     pre > code.sourceCode > span:empty { height: 1.2em; }
 18     code.sourceCode > span { color: inherit; text-decoration: inherit; }
 19     div.sourceCode { margin: 1em 0; }
 20     pre.sourceCode { margin: 0; }
 21     @media screen {
 22     div.sourceCode { overflow: auto; }
 23     }
 24     @media print {
 25     pre > code.sourceCode { white-space: pre-wrap; }
 26     pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
 27     }
 28     pre.numberSource code
 29       { counter-reset: source-line 0; }
 30     pre.numberSource code > span
 31       { position: relative; left: -4em; counter-increment: source-line; }
 32     pre.numberSource code > span > a:first-child::before
 33       { content: counter(source-line);
 34         position: relative; left: -1em; text-align: right; vertical-align: baseline;
 35         border: none; display: inline-block;
 36         -webkit-touch-callout: none; -webkit-user-select: none;
 37         -khtml-user-select: none; -moz-user-select: none;
 38         -ms-user-select: none; user-select: none;
 39         padding: 0 4px; width: 4em;
 40         color: #aaaaaa;
 41       }
 42     pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
 43     div.sourceCode
 44       {   }
 45     @media screen {
 46     pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
 47     }
 48     code span.al { color: #ff0000; font-weight: bold; } /* Alert */
 49     code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
 50     code span.at { color: #7d9029; } /* Attribute */
 51     code span.bn { color: #40a070; } /* BaseN */
 52     code span.bu { } /* BuiltIn */
 53     code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
 54     code span.ch { color: #4070a0; } /* Char */
 55     code span.cn { color: #880000; } /* Constant */
 56     code span.co { color: #60a0b0; font-style: italic; } /* Comment */
 57     code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
 58     code span.do { color: #ba2121; font-style: italic; } /* Documentation */
 59     code span.dt { color: #902000; } /* DataType */
 60     code span.dv { color: #40a070; } /* DecVal */
 61     code span.er { color: #ff0000; font-weight: bold; } /* Error */
 62     code span.ex { } /* Extension */
 63     code span.fl { color: #40a070; } /* Float */
 64     code span.fu { color: #06287e; } /* Function */
 65     code span.im { } /* Import */
 66     code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
 67     code span.kw { color: #007020; font-weight: bold; } /* Keyword */
 68     code span.op { color: #666666; } /* Operator */
 69     code span.ot { color: #007020; } /* Other */
 70     code span.pp { color: #bc7a00; } /* Preprocessor */
 71     code span.sc { color: #4070a0; } /* SpecialChar */
 72     code span.ss { color: #bb6688; } /* SpecialString */
 73     code span.st { color: #4070a0; } /* String */
 74     code span.va { color: #19177c; } /* Variable */
 75     code span.vs { color: #4070a0; } /* VerbatimString */
 76     code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
 77   </style>
 78   <link rel="stylesheet" href="../make/data/docs-resources/resources/jdk-default.css" />
 79   <!--[if lt IE 9]>
 80     <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
 81   <![endif]-->
 82 </head>
 83 <body>
 84 <nav id="TOC" role="doc-toc">
 85 <ul>
 86 <li><a href="#using-the-jextract-tool">Using the <code>jextract</code> tool</a>
 87 <ul>
 88 <li><a href="#hello-world">Hello World</a>
 89 <ul>
 90 <li><a href="#hello-world-c-header-helloworld.h">Hello World C Header (helloworld.h)</a></li>
 91 <li><a href="#hello-world-c-source-helloworld.c">Hello World C Source (helloworld.c)</a></li>
 92 <li><a href="#building-hello-world">Building Hello World</a></li>
 93 <li><a href="#jextract-a-jar-file-for-helloworld.h">jextract a Jar file for helloworld.h</a></li>
 94 <li><a href="#java-program-that-uses-extracted-helloworld-interface">Java program that uses extracted helloworld interface</a></li>
 95 <li><a href="#running-the-java-code-that-invokes-helloworld">Running the Java code that invokes helloworld</a></li>
 96 </ul></li>
 97 <li><a href="#embedding-python-interpreter-in-your-java-program-mac-os">Embedding Python interpreter in your Java program (Mac OS)</a>
 98 <ul>
 99 <li><a href="#jextract-python.h">jextract Python.h</a></li>
100 <li><a href="#java-program-that-uses-extracted-python-interface">Java program that uses extracted Python interface</a></li>
101 <li><a href="#running-the-java-code-that-calls-python-interpreter">Running the Java code that calls Python interpreter</a></li>
102 </ul></li>
103 <li><a href="#using-readline-library-from-java-code-mac-os">Using readline library from Java code (Mac OS)</a>
104 <ul>
105 <li><a href="#jextract-readline.h">jextract readline.h</a></li>
106 <li><a href="#java-code-that-uses-readline">Java code that uses readline</a></li>
107 <li><a href="#running-the-java-code-that-uses-readline">Running the java code that uses readline</a></li>
108 </ul></li>
109 <li><a href="#using-libcurl-from-java-mac-os">Using libcurl from Java (Mac OS)</a>
110 <ul>
111 <li><a href="#jextract-curl.h">jextract curl.h</a></li>
112 <li><a href="#java-code-that-uses-libcurl">Java code that uses libcurl</a></li>
113 <li><a href="#running-the-java-code-that-uses-libcurl">Running the java code that uses libcurl</a></li>
114 </ul></li>
115 <li><a href="#using-blas-library">Using BLAS library</a>
116 <ul>
117 <li><a href="#installing-openblas-mac-os">Installing OpenBLAS (Mac OS)</a></li>
118 <li><a href="#jextracting-cblas.h-macos">jextracting cblas.h (MacOS)</a></li>
119 <li><a href="#java-sample-code-that-uses-cblas-library">Java sample code that uses cblas library</a></li>
120 <li><a href="#compiling-and-running-the-above-blas-sample">Compiling and running the above BLAS sample</a></li>
121 </ul></li>
122 <li><a href="#using-lapack-library-mac-os">Using LAPACK library (Mac OS)</a>
123 <ul>
124 <li><a href="#jextracting-lapacke.h">jextracting lapacke.h</a></li>
125 <li><a href="#java-sample-code-that-uses-lapack-library">Java sample code that uses LAPACK library</a></li>
126 <li><a href="#compiling-and-running-the-above-lapack-sample">Compiling and running the above LAPACK sample</a></li>
127 </ul></li>
128 <li><a href="#using-libproc-library-to-list-processes-from-java-mac-os">Using libproc library to list processes from Java (Mac OS)</a>
129 <ul>
130 <li><a href="#jextract-libproc.h">jextract libproc.h</a></li>
131 <li><a href="#java-program-that-uses-libproc-to-list-processes">Java program that uses libproc to list processes</a></li>
132 <li><a href="#compiling-and-running-the-libproc-sample">Compiling and running the libproc sample</a></li>
133 </ul></li>
134 <li><a href="#using-libgit2-from-java-mac-os">Using libgit2 from Java (Mac OS)</a>
135 <ul>
136 <li><a href="#getting-and-building-libgit2">Getting and building libgit2</a></li>
137 <li><a href="#jextract-git2.h">jextract git2.h</a></li>
138 <li><a href="#java-program-that-uses-libgit2-to-clone-github-repo">Java program that uses libgit2 to clone github repo</a></li>
139 <li><a href="#compiling-and-running-the-libgit2-sample">Compiling and running the libgit2 sample</a></li>
140 <li><a href="#cloning-a-github-repo-using-the-above-run.sh-command">Cloning a github repo using the above run.sh command</a></li>
141 </ul></li>
142 <li><a href="#using-sqlite3-library-from-java-mac-os">Using sqlite3 library from Java (Mac OS)</a>
143 <ul>
144 <li><a href="#jextract-sqlite3.h">jextract sqlite3.h</a></li>
145 <li><a href="#java-program-that-uses-sqlite3">Java program that uses sqlite3</a></li>
146 <li><a href="#compiling-and-running-the-sqlite3-sample">Compiling and running the sqlite3 sample</a></li>
147 </ul></li>
148 <li><a href="#using-opengl-library-from-java-mac-os">Using OpenGL library from Java (Mac OS)</a>
149 <ul>
150 <li><a href="#jextract-glut.h">jextract glut.h</a></li>
151 <li><a href="#java-program-that-uses-opengl">Java program that uses OpenGL</a></li>
152 <li><a href="#compiling-and-running-the-opengl-sample">Compiling and running the OpenGL sample</a></li>
153 </ul></li>
154 <li><a href="#using-tensorflow-mac-os">Using tensorflow (Mac OS)</a>
155 <ul>
156 <li><a href="#getting-libtensorflow">getting libtensorflow</a></li>
157 <li><a href="#jextract-c_api.h">jextract c_api.h</a></li>
158 <li><a href="#python-program-that-creates-and-saves-model">Python program that creates and saves model</a></li>
159 <li><a href="#java-program-that-uses-tensorflow-c-api">Java program that uses Tensorflow C API</a></li>
160 <li><a href="#compiling-and-running-the-java-tensorflow-sample">Compiling and running the Java Tensorflow sample</a></li>
161 </ul></li>
162 <li><a href="#using-time.h-mac-os">Using time.h (Mac OS)</a>
163 <ul>
164 <li><a href="#jextract-time.h">jextract time.h</a></li>
165 <li><a href="#java-program-that-uses-posix-time-library">Java program that uses POSIX time library</a></li>
166 <li><a href="#compiling-and-running-the-time-sample">Compiling and running the time sample</a></li>
167 </ul></li>
168 <li><a href="#using-libclang-library-mac-os">Using libclang library (Mac OS)</a>
169 <ul>
170 <li><a href="#jextract-index.h">jextract Index.h</a></li>
171 <li><a href="#java-program-that-uses-libclang-to-print-ast-of-a-given-c-program">Java program that uses libclang to print AST of a given C program</a></li>
172 <li><a href="#compiling-and-running-the-libclang-sample">Compiling and running the libclang sample</a></li>
173 </ul></li>
174 </ul></li>
175 </ul>
176 </nav>
177 <?xml version="1.0" encoding="utf-8"?>
178 <h1 id="using-the-jextract-tool">Using the <code>jextract</code> tool</h1>
179 <p><code>jextract</code> is a simple - but convenient - tool which generates a Java API from one or more native C headers. The tool can be obtained by building the <a href="https://github.com/openjdk/panama-foreign">foreign-jextract</a> branch of Panama foreign repository.</p>
180 <p>Interacting with the <code>jextract</code> tool usually involves two steps:</p>
181 <ol type="1">
182 <li>Use the <code>jextract</code> tool to generate a java interface for some C header files</li>
183 <li>Write a Java program which invokes the wrapper API points generated by <code>jextract</code></li>
184 </ol>
185 <p>The <code>jextract</code> tool provides some basic options in order to control how the extraction process works; these are listed below:</p>
186 <ul>
187 <li><code>-C &lt;String&gt;</code> - specify arguments to be passed to the underlying Clang parser</li>
188 <li><code>-I &lt;String&gt;</code> - specify include files path</li>
189 <li><code>-l &lt;String&gt;</code> - specify a library (name or full absolute path) which should be linked when the generated API is loaded</li>
190 <li><code>-d &lt;String&gt;</code> - specify where to place generated files</li>
191 <li><code>-t &lt;String&gt;</code> specify the target package for the generated classes</li>
192 <li>--include-function <String> - name of function to include</li>
193 <li>--include-macro <String> - name of constant macro to include</li>
194 <li>--include-struct <String> - name of struct definition to include</li>
195 <li>--include-typedef <String> - name of type definition to include</li>
196 <li>--include-union <String> - name of union definition to include</li>
197 <li>--include-var <String> - name of global variable to include</li>
198 <li><code>--source</code> - generate java sources instead of classfiles</li>
199 </ul>
200 <p>The remainder of this documents shows some basic usage examples of the <code>jextract</code> tool.</p>
201 <h2 id="hello-world">Hello World</h2>
202 <h3 id="hello-world-c-header-helloworld.h">Hello World C Header (helloworld.h)</h3>
203 <div class="sourceCode" id="cb1"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb1-1"><a href="#cb1-1"></a></span>
204 <span id="cb1-2"><a href="#cb1-2"></a><span class="pp">#ifndef helloworld_h</span></span>
205 <span id="cb1-3"><a href="#cb1-3"></a><span class="pp">#define helloworld_h</span></span>
206 <span id="cb1-4"><a href="#cb1-4"></a></span>
207 <span id="cb1-5"><a href="#cb1-5"></a><span class="kw">extern</span> <span class="dt">void</span> helloworld(<span class="dt">void</span>);</span>
208 <span id="cb1-6"><a href="#cb1-6"></a></span>
209 <span id="cb1-7"><a href="#cb1-7"></a><span class="pp">#endif </span><span class="co">/* helloworld_h */</span></span>
210 <span id="cb1-8"><a href="#cb1-8"></a></span></code></pre></div>
211 <h3 id="hello-world-c-source-helloworld.c">Hello World C Source (helloworld.c)</h3>
212 <div class="sourceCode" id="cb2"><pre class="sourceCode c"><code class="sourceCode c"><span id="cb2-1"><a href="#cb2-1"></a></span>
213 <span id="cb2-2"><a href="#cb2-2"></a><span class="pp">#include </span><span class="im">&lt;stdio.h&gt;</span></span>
214 <span id="cb2-3"><a href="#cb2-3"></a></span>
215 <span id="cb2-4"><a href="#cb2-4"></a><span class="pp">#include </span><span class="im">&quot;helloworld.h&quot;</span></span>
216 <span id="cb2-5"><a href="#cb2-5"></a></span>
217 <span id="cb2-6"><a href="#cb2-6"></a><span class="dt">void</span> helloworld(<span class="dt">void</span>) {</span>
218 <span id="cb2-7"><a href="#cb2-7"></a>    printf(<span class="st">&quot;Hello World!</span><span class="sc">\n</span><span class="st">&quot;</span>);</span>
219 <span id="cb2-8"><a href="#cb2-8"></a>}</span></code></pre></div>
220 <h3 id="building-hello-world">Building Hello World</h3>
221 <div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1"></a></span>
222 <span id="cb3-2"><a href="#cb3-2"></a><span class="fu">cc</span> -shared -o libhelloworld.dylib helloworld.c</span></code></pre></div>
223 <h3 id="jextract-a-jar-file-for-helloworld.h">jextract a Jar file for helloworld.h</h3>
224 <div class="sourceCode" id="cb4"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1"></a></span>
225 <span id="cb4-2"><a href="#cb4-2"></a><span class="ex">jextract</span> -t org.hello -lhelloworld helloworld.h</span></code></pre></div>
226 <h3 id="java-program-that-uses-extracted-helloworld-interface">Java program that uses extracted helloworld interface</h3>
227 <div class="sourceCode" id="cb5"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb5-1"><a href="#cb5-1"></a></span>
228 <span id="cb5-2"><a href="#cb5-2"></a><span class="kw">import static</span><span class="im"> org.hello.helloworld_h.*;</span></span>
229 <span id="cb5-3"><a href="#cb5-3"></a></span>
230 <span id="cb5-4"><a href="#cb5-4"></a><span class="kw">public</span> <span class="kw">class</span> HelloWorld {</span>
231 <span id="cb5-5"><a href="#cb5-5"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
232 <span id="cb5-6"><a href="#cb5-6"></a>        <span class="fu">helloworld</span>();</span>
233 <span id="cb5-7"><a href="#cb5-7"></a>    }</span>
234 <span id="cb5-8"><a href="#cb5-8"></a>}</span></code></pre></div>
235 <h3 id="running-the-java-code-that-invokes-helloworld">Running the Java code that invokes helloworld</h3>
236 <div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb6-1"><a href="#cb6-1"></a></span>
237 <span id="cb6-2"><a href="#cb6-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign HelloWorld.java</span></code></pre></div>
238 <h2 id="embedding-python-interpreter-in-your-java-program-mac-os">Embedding Python interpreter in your Java program (Mac OS)</h2>
239 <h3 id="jextract-python.h">jextract Python.h</h3>
240 <div class="sourceCode" id="cb7"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb7-1"><a href="#cb7-1"></a></span>
241 <span id="cb7-2"><a href="#cb7-2"></a><span class="ex">jextract</span> \</span>
242 <span id="cb7-3"><a href="#cb7-3"></a>  -l python2.7 \</span>
243 <span id="cb7-4"><a href="#cb7-4"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
244 <span id="cb7-5"><a href="#cb7-5"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/python2.7/ \</span>
245 <span id="cb7-6"><a href="#cb7-6"></a>  -t org.python \</span>
246 <span id="cb7-7"><a href="#cb7-7"></a>   /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/python2.7/Python.h</span></code></pre></div>
247 <h3 id="java-program-that-uses-extracted-python-interface">Java program that uses extracted Python interface</h3>
248 <div class="sourceCode" id="cb8"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb8-1"><a href="#cb8-1"></a></span>
249 <span id="cb8-2"><a href="#cb8-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
250 <span id="cb8-3"><a href="#cb8-3"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
251 <span id="cb8-4"><a href="#cb8-4"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAddress.NULL;</span></span>
252 <span id="cb8-5"><a href="#cb8-5"></a><span class="co">// import jextracted python &#39;header&#39; class</span></span>
253 <span id="cb8-6"><a href="#cb8-6"></a><span class="kw">import static</span><span class="im"> org.python.Python_h.*;</span></span>
254 <span id="cb8-7"><a href="#cb8-7"></a><span class="kw">import</span><span class="im"> org.python.*;</span></span>
255 <span id="cb8-8"><a href="#cb8-8"></a></span>
256 <span id="cb8-9"><a href="#cb8-9"></a><span class="kw">public</span> <span class="kw">class</span> PythonMain {</span>
257 <span id="cb8-10"><a href="#cb8-10"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
258 <span id="cb8-11"><a href="#cb8-11"></a>        <span class="bu">String</span> script = <span class="st">&quot;print(sum([33, 55, 66])); print(&#39;Hello from Python!&#39;)</span><span class="sc">\n</span><span class="st">&quot;</span>;</span>
259 <span id="cb8-12"><a href="#cb8-12"></a></span>
260 <span id="cb8-13"><a href="#cb8-13"></a>        <span class="fu">Py_Initialize</span>();</span>
261 <span id="cb8-14"><a href="#cb8-14"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
262 <span id="cb8-15"><a href="#cb8-15"></a>            var str = <span class="fu">toCString</span>(script, scope);</span>
263 <span id="cb8-16"><a href="#cb8-16"></a>            <span class="fu">PyRun_SimpleStringFlags</span>(str, NULL);</span>
264 <span id="cb8-17"><a href="#cb8-17"></a>            <span class="fu">Py_Finalize</span>();</span>
265 <span id="cb8-18"><a href="#cb8-18"></a>        }</span>
266 <span id="cb8-19"><a href="#cb8-19"></a>    }</span>
267 <span id="cb8-20"><a href="#cb8-20"></a>}</span></code></pre></div>
268 <h3 id="running-the-java-code-that-calls-python-interpreter">Running the Java code that calls Python interpreter</h3>
269 <div class="sourceCode" id="cb9"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb9-1"><a href="#cb9-1"></a></span>
270 <span id="cb9-2"><a href="#cb9-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign \</span>
271 <span id="cb9-3"><a href="#cb9-3"></a>    -Djava.library.path=/System/Library/Frameworks/Python.framework/Versions/2.7/lib \</span>
272 <span id="cb9-4"><a href="#cb9-4"></a>    PythonMain.java</span></code></pre></div>
273 <h2 id="using-readline-library-from-java-code-mac-os">Using readline library from Java code (Mac OS)</h2>
274 <h3 id="jextract-readline.h">jextract readline.h</h3>
275 <div class="sourceCode" id="cb10"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb10-1"><a href="#cb10-1"></a></span>
276 <span id="cb10-2"><a href="#cb10-2"></a><span class="ex">jextract</span> \</span>
277 <span id="cb10-3"><a href="#cb10-3"></a>  -l readline -t org.unix \</span>
278 <span id="cb10-4"><a href="#cb10-4"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
279 <span id="cb10-5"><a href="#cb10-5"></a>   /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/readline/readline.h</span></code></pre></div>
280 <h3 id="java-code-that-uses-readline">Java code that uses readline</h3>
281 <div class="sourceCode" id="cb11"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb11-1"><a href="#cb11-1"></a></span>
282 <span id="cb11-2"><a href="#cb11-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
283 <span id="cb11-3"><a href="#cb11-3"></a><span class="kw">import static</span><span class="im"> org.unix.readline_h.*;</span></span>
284 <span id="cb11-4"><a href="#cb11-4"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
285 <span id="cb11-5"><a href="#cb11-5"></a><span class="kw">import</span><span class="im"> org.unix.*;</span></span>
286 <span id="cb11-6"><a href="#cb11-6"></a></span>
287 <span id="cb11-7"><a href="#cb11-7"></a><span class="kw">public</span> <span class="kw">class</span> Readline {</span>
288 <span id="cb11-8"><a href="#cb11-8"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
289 <span id="cb11-9"><a href="#cb11-9"></a>       <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
290 <span id="cb11-10"><a href="#cb11-10"></a>            var url = <span class="fu">toCString</span>(<span class="st">&quot;name? &quot;</span>, scope);</span>
291 <span id="cb11-11"><a href="#cb11-11"></a></span>
292 <span id="cb11-12"><a href="#cb11-12"></a>            <span class="co">// call &quot;readline&quot; API</span></span>
293 <span id="cb11-13"><a href="#cb11-13"></a>            var p = <span class="fu">readline</span>(url);</span>
294 <span id="cb11-14"><a href="#cb11-14"></a></span>
295 <span id="cb11-15"><a href="#cb11-15"></a>            <span class="co">// print char* as is</span></span>
296 <span id="cb11-16"><a href="#cb11-16"></a>            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(p);</span>
297 <span id="cb11-17"><a href="#cb11-17"></a>            <span class="co">// convert char* ptr from readline as Java String &amp; print it</span></span>
298 <span id="cb11-18"><a href="#cb11-18"></a>            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;Hello, &quot;</span> + <span class="fu">toJavaString</span>(p));</span>
299 <span id="cb11-19"><a href="#cb11-19"></a></span>
300 <span id="cb11-20"><a href="#cb11-20"></a>            <span class="fu">freeMemory</span>(p);</span>
301 <span id="cb11-21"><a href="#cb11-21"></a>        }</span>
302 <span id="cb11-22"><a href="#cb11-22"></a>    }</span>
303 <span id="cb11-23"><a href="#cb11-23"></a>}</span></code></pre></div>
304 <h3 id="running-the-java-code-that-uses-readline">Running the java code that uses readline</h3>
305 <pre><code>java --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign \
306     -Djava.library.path=/usr/local/opt/readline/lib/ Readline.java
307 </code></pre>
308 <h2 id="using-libcurl-from-java-mac-os">Using libcurl from Java (Mac OS)</h2>
309 <h3 id="jextract-curl.h">jextract curl.h</h3>
310 <div class="sourceCode" id="cb13"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb13-1"><a href="#cb13-1"></a></span>
311 <span id="cb13-2"><a href="#cb13-2"></a><span class="ex">jextract</span> -t org.unix -lcurl \</span>
312 <span id="cb13-3"><a href="#cb13-3"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ \</span>
313 <span id="cb13-4"><a href="#cb13-4"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/curl/ \</span>
314 <span id="cb13-5"><a href="#cb13-5"></a>  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/curl/curl.h</span></code></pre></div>
315 <h3 id="java-code-that-uses-libcurl">Java code that uses libcurl</h3>
316 <div class="sourceCode" id="cb14"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb14-1"><a href="#cb14-1"></a></span>
317 <span id="cb14-2"><a href="#cb14-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
318 <span id="cb14-3"><a href="#cb14-3"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAddress.NULL;</span></span>
319 <span id="cb14-4"><a href="#cb14-4"></a><span class="kw">import static</span><span class="im"> org.jextract.curl_h.*;</span></span>
320 <span id="cb14-5"><a href="#cb14-5"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
321 <span id="cb14-6"><a href="#cb14-6"></a><span class="kw">import</span><span class="im"> org.jextract.*;</span></span>
322 <span id="cb14-7"><a href="#cb14-7"></a></span>
323 <span id="cb14-8"><a href="#cb14-8"></a><span class="kw">public</span> <span class="kw">class</span> CurlMain {</span>
324 <span id="cb14-9"><a href="#cb14-9"></a>   <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
325 <span id="cb14-10"><a href="#cb14-10"></a>       var urlStr = args[<span class="dv">0</span>];</span>
326 <span id="cb14-11"><a href="#cb14-11"></a>       <span class="fu">curl_global_init</span>(<span class="fu">CURL_GLOBAL_DEFAULT</span>());</span>
327 <span id="cb14-12"><a href="#cb14-12"></a>       var curl = <span class="fu">curl_easy_init</span>();</span>
328 <span id="cb14-13"><a href="#cb14-13"></a>       <span class="kw">if</span>(!curl.<span class="fu">equals</span>(NULL)) {</span>
329 <span id="cb14-14"><a href="#cb14-14"></a>           <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
330 <span id="cb14-15"><a href="#cb14-15"></a>               var url = <span class="fu">toCString</span>(urlStr, scope);</span>
331 <span id="cb14-16"><a href="#cb14-16"></a>               <span class="fu">curl_easy_setopt</span>(curl, <span class="fu">CURLOPT_URL</span>(), url.<span class="fu">address</span>());</span>
332 <span id="cb14-17"><a href="#cb14-17"></a>               <span class="dt">int</span> res = <span class="fu">curl_easy_perform</span>(curl);</span>
333 <span id="cb14-18"><a href="#cb14-18"></a>               <span class="kw">if</span> (res != <span class="fu">CURLE_OK</span>()) {</span>
334 <span id="cb14-19"><a href="#cb14-19"></a>                   <span class="bu">String</span> error = <span class="fu">toJavaString</span>(<span class="fu">curl_easy_strerror</span>(res));</span>
335 <span id="cb14-20"><a href="#cb14-20"></a>                   <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;Curl error: &quot;</span> + error);</span>
336 <span id="cb14-21"><a href="#cb14-21"></a>                   <span class="fu">curl_easy_cleanup</span>(curl);</span>
337 <span id="cb14-22"><a href="#cb14-22"></a>               }</span>
338 <span id="cb14-23"><a href="#cb14-23"></a>           }</span>
339 <span id="cb14-24"><a href="#cb14-24"></a>       }</span>
340 <span id="cb14-25"><a href="#cb14-25"></a>       <span class="fu">curl_global_cleanup</span>();</span>
341 <span id="cb14-26"><a href="#cb14-26"></a>   }</span>
342 <span id="cb14-27"><a href="#cb14-27"></a>}</span></code></pre></div>
343 <h3 id="running-the-java-code-that-uses-libcurl">Running the java code that uses libcurl</h3>
344 <div class="sourceCode" id="cb15"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb15-1"><a href="#cb15-1"></a></span>
345 <span id="cb15-2"><a href="#cb15-2"></a><span class="co"># run this shell script by passing a URL as first argument</span></span>
346 <span id="cb15-3"><a href="#cb15-3"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign \</span>
347 <span id="cb15-4"><a href="#cb15-4"></a>    -Djava.library.path=/usr/lib CurlMain.java <span class="va">$*</span></span></code></pre></div>
348 <h2 id="using-blas-library">Using BLAS library</h2>
349 <p>BLAS is a popular library that allows fast matrix and vector computation: <a href="http://www.netlib.org/blas/">http://www.netlib.org/blas/</a>.</p>
350 <h3 id="installing-openblas-mac-os">Installing OpenBLAS (Mac OS)</h3>
351 <p>On Mac, blas is available as part of the OpenBLAS library: <a href="https://github.com/xianyi/OpenBLAS/wiki">https://github.com/xianyi/OpenBLAS/wiki</a></p>
352 <p>OpenBLAS is an optimized BLAS library based on GotoBLAS2 1.13 BSD version.</p>
353 <p>You can install openblas using HomeBrew</p>
354 <div class="sourceCode" id="cb16"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb16-1"><a href="#cb16-1"></a></span>
355 <span id="cb16-2"><a href="#cb16-2"></a><span class="ex">brew</span> install openblas</span></code></pre></div>
356 <p>It installs include and lib directories under /usr/local/opt/openblas</p>
357 <h3 id="jextracting-cblas.h-macos">jextracting cblas.h (MacOS)</h3>
358 <p>The following command can be used to extract cblas.h on MacOs</p>
359 <div class="sourceCode" id="cb17"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb17-1"><a href="#cb17-1"></a></span>
360 <span id="cb17-2"><a href="#cb17-2"></a><span class="ex">jextract</span> -C <span class="st">&quot;-D FORCE_OPENBLAS_COMPLEX_STRUCT&quot;</span> \</span>
361 <span id="cb17-3"><a href="#cb17-3"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
362 <span id="cb17-4"><a href="#cb17-4"></a>  -l openblas -t blas /usr/local/opt/openblas/include/cblas.h</span></code></pre></div>
363 <h3 id="java-sample-code-that-uses-cblas-library">Java sample code that uses cblas library</h3>
364 <div class="sourceCode" id="cb18"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb18-1"><a href="#cb18-1"></a></span>
365 <span id="cb18-2"><a href="#cb18-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemoryAddress;</span></span>
366 <span id="cb18-3"><a href="#cb18-3"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemoryAccess;</span></span>
367 <span id="cb18-4"><a href="#cb18-4"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
368 <span id="cb18-5"><a href="#cb18-5"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.SegmentAllocator;</span></span>
369 <span id="cb18-6"><a href="#cb18-6"></a><span class="kw">import</span><span class="im"> blas.*;</span></span>
370 <span id="cb18-7"><a href="#cb18-7"></a><span class="kw">import static</span><span class="im"> blas.cblas_h.*;</span></span>
371 <span id="cb18-8"><a href="#cb18-8"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
372 <span id="cb18-9"><a href="#cb18-9"></a></span>
373 <span id="cb18-10"><a href="#cb18-10"></a><span class="kw">public</span> <span class="kw">class</span> TestBlas {</span>
374 <span id="cb18-11"><a href="#cb18-11"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
375 <span id="cb18-12"><a href="#cb18-12"></a>        <span class="dt">int</span> Layout;</span>
376 <span id="cb18-13"><a href="#cb18-13"></a>        <span class="dt">int</span> transa;</span>
377 <span id="cb18-14"><a href="#cb18-14"></a></span>
378 <span id="cb18-15"><a href="#cb18-15"></a>        <span class="dt">double</span> alpha, beta;</span>
379 <span id="cb18-16"><a href="#cb18-16"></a>        <span class="dt">int</span> m, n, lda, incx, incy, i;</span>
380 <span id="cb18-17"><a href="#cb18-17"></a></span>
381 <span id="cb18-18"><a href="#cb18-18"></a>        Layout = <span class="fu">CblasColMajor</span>();</span>
382 <span id="cb18-19"><a href="#cb18-19"></a>        transa = <span class="fu">CblasNoTrans</span>();</span>
383 <span id="cb18-20"><a href="#cb18-20"></a></span>
384 <span id="cb18-21"><a href="#cb18-21"></a>        m = <span class="dv">4</span>; <span class="co">/* Size of Column ( the number of rows ) */</span></span>
385 <span id="cb18-22"><a href="#cb18-22"></a>        n = <span class="dv">4</span>; <span class="co">/* Size of Row ( the number of columns ) */</span></span>
386 <span id="cb18-23"><a href="#cb18-23"></a>        lda = <span class="dv">4</span>; <span class="co">/* Leading dimension of 5 * 4 matrix is 5 */</span></span>
387 <span id="cb18-24"><a href="#cb18-24"></a>        incx = <span class="dv">1</span>;</span>
388 <span id="cb18-25"><a href="#cb18-25"></a>        incy = <span class="dv">1</span>;</span>
389 <span id="cb18-26"><a href="#cb18-26"></a>        alpha = <span class="dv">1</span>;</span>
390 <span id="cb18-27"><a href="#cb18-27"></a>        beta = <span class="dv">0</span>;</span>
391 <span id="cb18-28"><a href="#cb18-28"></a></span>
392 <span id="cb18-29"><a href="#cb18-29"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
393 <span id="cb18-30"><a href="#cb18-30"></a>            var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
394 <span id="cb18-31"><a href="#cb18-31"></a>            var a = allocator.<span class="fu">allocateArray</span>(C_DOUBLE, <span class="kw">new</span> <span class="dt">double</span>[] {</span>
395 <span id="cb18-32"><a href="#cb18-32"></a>                <span class="fl">1.</span><span class="dv">0</span>, <span class="fl">2.</span><span class="dv">0</span>, <span class="fl">3.</span><span class="dv">0</span>, <span class="fl">4.</span><span class="dv">0</span>,</span>
396 <span id="cb18-33"><a href="#cb18-33"></a>                <span class="fl">1.</span><span class="dv">0</span>, <span class="fl">1.</span><span class="dv">0</span>, <span class="fl">1.</span><span class="dv">0</span>, <span class="fl">1.</span><span class="dv">0</span>,</span>
397 <span id="cb18-34"><a href="#cb18-34"></a>                <span class="fl">3.</span><span class="dv">0</span>, <span class="fl">4.</span><span class="dv">0</span>, <span class="fl">5.</span><span class="dv">0</span>, <span class="fl">6.</span><span class="dv">0</span>,</span>
398 <span id="cb18-35"><a href="#cb18-35"></a>                <span class="fl">5.</span><span class="dv">0</span>, <span class="fl">6.</span><span class="dv">0</span>, <span class="fl">7.</span><span class="dv">0</span>, <span class="fl">8.</span><span class="dv">0</span></span>
399 <span id="cb18-36"><a href="#cb18-36"></a>            });</span>
400 <span id="cb18-37"><a href="#cb18-37"></a>            var x = allocator.<span class="fu">allocateArray</span>(C_DOUBLE, <span class="kw">new</span> <span class="dt">double</span>[] {</span>
401 <span id="cb18-38"><a href="#cb18-38"></a>                <span class="fl">1.</span><span class="dv">0</span>, <span class="fl">2.</span><span class="dv">0</span>, <span class="fl">1.</span><span class="dv">0</span>, <span class="fl">1.</span><span class="dv">0</span></span>
402 <span id="cb18-39"><a href="#cb18-39"></a>            });</span>
403 <span id="cb18-40"><a href="#cb18-40"></a>            var y = allocator.<span class="fu">allocateArray</span>(C_DOUBLE, n);</span>
404 <span id="cb18-41"><a href="#cb18-41"></a></span>
405 <span id="cb18-42"><a href="#cb18-42"></a>            <span class="fu">cblas_dgemv</span>(Layout, transa, m, n, alpha, a, lda, x, incx, beta, y, incy);</span>
406 <span id="cb18-43"><a href="#cb18-43"></a>            <span class="co">/* Print y */</span></span>
407 <span id="cb18-44"><a href="#cb18-44"></a>            <span class="kw">for</span> (i = <span class="dv">0</span>; i &lt; n; i++) {</span>
408 <span id="cb18-45"><a href="#cb18-45"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">print</span>(<span class="bu">String</span><span class="fu">.format</span>(<span class="st">&quot; y</span><span class="sc">%d</span><span class="st"> = </span><span class="sc">%f\n</span><span class="st">&quot;</span>, i, MemoryAccess.<span class="fu">getDoubleAtIndex</span>(y, i)));</span>
409 <span id="cb18-46"><a href="#cb18-46"></a>            }</span>
410 <span id="cb18-47"><a href="#cb18-47"></a>        }</span>
411 <span id="cb18-48"><a href="#cb18-48"></a>    }</span>
412 <span id="cb18-49"><a href="#cb18-49"></a>}</span></code></pre></div>
413 <h3 id="compiling-and-running-the-above-blas-sample">Compiling and running the above BLAS sample</h3>
414 <div class="sourceCode" id="cb19"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb19-1"><a href="#cb19-1"></a></span>
415 <span id="cb19-2"><a href="#cb19-2"></a><span class="ex">jextract</span> \</span>
416 <span id="cb19-3"><a href="#cb19-3"></a>  -C <span class="st">&quot;-D FORCE_OPENBLAS_COMPLEX_STRUCT&quot;</span> \</span>
417 <span id="cb19-4"><a href="#cb19-4"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
418 <span id="cb19-5"><a href="#cb19-5"></a>  -l openblas -t blas /usr/local/opt/openblas/include/cblas.h</span></code></pre></div>
419 <h2 id="using-lapack-library-mac-os">Using LAPACK library (Mac OS)</h2>
420 <p>On Mac OS, lapack is installed under /usr/local/opt/lapack directory.</p>
421 <h3 id="jextracting-lapacke.h">jextracting lapacke.h</h3>
422 <div class="sourceCode" id="cb20"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb20-1"><a href="#cb20-1"></a></span>
423 <span id="cb20-2"><a href="#cb20-2"></a><span class="ex">jextract</span> \</span>
424 <span id="cb20-3"><a href="#cb20-3"></a>   -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
425 <span id="cb20-4"><a href="#cb20-4"></a>   -l lapacke -t lapack \</span>
426 <span id="cb20-5"><a href="#cb20-5"></a>   /usr/local/opt/lapack/include/lapacke.h</span></code></pre></div>
427 <h3 id="java-sample-code-that-uses-lapack-library">Java sample code that uses LAPACK library</h3>
428 <div class="sourceCode" id="cb21"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb21-1"><a href="#cb21-1"></a></span>
429 <span id="cb21-2"><a href="#cb21-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemoryAccess;</span></span>
430 <span id="cb21-3"><a href="#cb21-3"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemoryAddress;</span></span>
431 <span id="cb21-4"><a href="#cb21-4"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemorySegment;</span></span>
432 <span id="cb21-5"><a href="#cb21-5"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
433 <span id="cb21-6"><a href="#cb21-6"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.SegmentAllocator;</span></span>
434 <span id="cb21-7"><a href="#cb21-7"></a><span class="kw">import</span><span class="im"> lapack.*;</span></span>
435 <span id="cb21-8"><a href="#cb21-8"></a><span class="kw">import static</span><span class="im"> lapack.lapacke_h.*;</span></span>
436 <span id="cb21-9"><a href="#cb21-9"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
437 <span id="cb21-10"><a href="#cb21-10"></a></span>
438 <span id="cb21-11"><a href="#cb21-11"></a><span class="kw">public</span> <span class="kw">class</span> TestLapack {</span>
439 <span id="cb21-12"><a href="#cb21-12"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
440 <span id="cb21-13"><a href="#cb21-13"></a></span>
441 <span id="cb21-14"><a href="#cb21-14"></a>        <span class="co">/* Locals */</span></span>
442 <span id="cb21-15"><a href="#cb21-15"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
443 <span id="cb21-16"><a href="#cb21-16"></a>            var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
444 <span id="cb21-17"><a href="#cb21-17"></a>            var A = allocator.<span class="fu">allocateArray</span>(C_DOUBLE, <span class="kw">new</span> <span class="dt">double</span>[]{</span>
445 <span id="cb21-18"><a href="#cb21-18"></a>                    <span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">4</span>, <span class="dv">5</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">5</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">1</span>, <span class="dv">4</span>, <span class="dv">2</span>, <span class="dv">5</span>, <span class="dv">3</span></span>
446 <span id="cb21-19"><a href="#cb21-19"></a>            });</span>
447 <span id="cb21-20"><a href="#cb21-20"></a>            var b = allocator.<span class="fu">allocateArray</span>(C_DOUBLE, <span class="kw">new</span> <span class="dt">double</span>[]{</span>
448 <span id="cb21-21"><a href="#cb21-21"></a>                    -<span class="dv">10</span>, <span class="dv">12</span>, <span class="dv">14</span>, <span class="dv">16</span>, <span class="dv">18</span>, -<span class="dv">3</span>, <span class="dv">14</span>, <span class="dv">12</span>, <span class="dv">16</span>, <span class="dv">16</span></span>
449 <span id="cb21-22"><a href="#cb21-22"></a>            });</span>
450 <span id="cb21-23"><a href="#cb21-23"></a>            <span class="dt">int</span> info, m, n, lda, ldb, nrhs;</span>
451 <span id="cb21-24"><a href="#cb21-24"></a></span>
452 <span id="cb21-25"><a href="#cb21-25"></a>            <span class="co">/* Initialization */</span></span>
453 <span id="cb21-26"><a href="#cb21-26"></a>            m = <span class="dv">5</span>;</span>
454 <span id="cb21-27"><a href="#cb21-27"></a>            n = <span class="dv">3</span>;</span>
455 <span id="cb21-28"><a href="#cb21-28"></a>            nrhs = <span class="dv">2</span>;</span>
456 <span id="cb21-29"><a href="#cb21-29"></a>            lda = <span class="dv">5</span>;</span>
457 <span id="cb21-30"><a href="#cb21-30"></a>            ldb = <span class="dv">5</span>;</span>
458 <span id="cb21-31"><a href="#cb21-31"></a></span>
459 <span id="cb21-32"><a href="#cb21-32"></a>            <span class="co">/* Print Entry Matrix */</span></span>
460 <span id="cb21-33"><a href="#cb21-33"></a>            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Entry Matrix A&quot;</span>, m, n, A, lda );</span>
461 <span id="cb21-34"><a href="#cb21-34"></a>            <span class="co">/* Print Right Rand Side */</span></span>
462 <span id="cb21-35"><a href="#cb21-35"></a>            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Right Hand Side b&quot;</span>, n, nrhs, b, ldb );</span>
463 <span id="cb21-36"><a href="#cb21-36"></a>            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>();</span>
464 <span id="cb21-37"><a href="#cb21-37"></a></span>
465 <span id="cb21-38"><a href="#cb21-38"></a>            <span class="co">/* Executable statements */</span></span>
466 <span id="cb21-39"><a href="#cb21-39"></a>            <span class="co">//            printf( &quot;LAPACKE_dgels (col-major, high-level) Example Program Results\n&quot; );</span></span>
467 <span id="cb21-40"><a href="#cb21-40"></a>            <span class="co">/* Solve least squares problem*/</span></span>
468 <span id="cb21-41"><a href="#cb21-41"></a>            info = <span class="fu">LAPACKE_dgels</span>(<span class="fu">LAPACK_COL_MAJOR</span>(), (<span class="dt">byte</span>)<span class="ch">&#39;N&#39;</span>, m, n, nrhs, A, lda, b, ldb);</span>
469 <span id="cb21-42"><a href="#cb21-42"></a></span>
470 <span id="cb21-43"><a href="#cb21-43"></a>            <span class="co">/* Print Solution */</span></span>
471 <span id="cb21-44"><a href="#cb21-44"></a>            <span class="fu">print_matrix_colmajor</span>(<span class="st">&quot;Solution&quot;</span>, n, nrhs, b, ldb );</span>
472 <span id="cb21-45"><a href="#cb21-45"></a>            <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>();</span>
473 <span id="cb21-46"><a href="#cb21-46"></a>            <span class="bu">System</span>.<span class="fu">exit</span>(info);</span>
474 <span id="cb21-47"><a href="#cb21-47"></a>        }</span>
475 <span id="cb21-48"><a href="#cb21-48"></a>    }</span>
476 <span id="cb21-49"><a href="#cb21-49"></a></span>
477 <span id="cb21-50"><a href="#cb21-50"></a>    <span class="dt">static</span> <span class="dt">void</span> <span class="fu">print_matrix_colmajor</span>(<span class="bu">String</span> msg, <span class="dt">int</span> m, <span class="dt">int</span> n, MemorySegment mat, <span class="dt">int</span> ldm) {</span>
478 <span id="cb21-51"><a href="#cb21-51"></a>        <span class="dt">int</span> i, j;</span>
479 <span id="cb21-52"><a href="#cb21-52"></a>        <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">\n</span><span class="st"> </span><span class="sc">%s\n</span><span class="st">&quot;</span>, msg);</span>
480 <span id="cb21-53"><a href="#cb21-53"></a></span>
481 <span id="cb21-54"><a href="#cb21-54"></a>        <span class="kw">for</span>( i = <span class="dv">0</span>; i &lt; m; i++ ) {</span>
482 <span id="cb21-55"><a href="#cb21-55"></a>            <span class="kw">for</span>( j = <span class="dv">0</span>; j &lt; n; j++ ) <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot; </span><span class="sc">%6.2f</span><span class="st">&quot;</span>, MemoryAccess.<span class="fu">getDoubleAtIndex</span>(mat, i+j*ldm));</span>
483 <span id="cb21-56"><a href="#cb21-56"></a>            <span class="bu">System</span>.<span class="fu">out.printf</span>( <span class="st">&quot;</span><span class="sc">\n</span><span class="st">&quot;</span> );</span>
484 <span id="cb21-57"><a href="#cb21-57"></a>        }</span>
485 <span id="cb21-58"><a href="#cb21-58"></a>    }</span>
486 <span id="cb21-59"><a href="#cb21-59"></a>}</span></code></pre></div>
487 <h3 id="compiling-and-running-the-above-lapack-sample">Compiling and running the above LAPACK sample</h3>
488 <div class="sourceCode" id="cb22"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb22-1"><a href="#cb22-1"></a></span>
489 <span id="cb22-2"><a href="#cb22-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED \</span>
490 <span id="cb22-3"><a href="#cb22-3"></a>    --add-modules jdk.incubator.foreign \</span>
491 <span id="cb22-4"><a href="#cb22-4"></a>    -Djava.library.path=/usr/local/opt/lapack/lib \</span>
492 <span id="cb22-5"><a href="#cb22-5"></a>    TestLapack.java</span></code></pre></div>
493 <h2 id="using-libproc-library-to-list-processes-from-java-mac-os">Using libproc library to list processes from Java (Mac OS)</h2>
494 <h3 id="jextract-libproc.h">jextract libproc.h</h3>
495 <div class="sourceCode" id="cb23"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb23-1"><a href="#cb23-1"></a></span>
496 <span id="cb23-2"><a href="#cb23-2"></a><span class="ex">jextract</span> \</span>
497 <span id="cb23-3"><a href="#cb23-3"></a>  -t org.unix \</span>
498 <span id="cb23-4"><a href="#cb23-4"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
499 <span id="cb23-5"><a href="#cb23-5"></a>  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/libproc.h</span></code></pre></div>
500 <h3 id="java-program-that-uses-libproc-to-list-processes">Java program that uses libproc to list processes</h3>
501 <div class="sourceCode" id="cb24"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb24-1"><a href="#cb24-1"></a></span>
502 <span id="cb24-2"><a href="#cb24-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.CLinker;</span></span>
503 <span id="cb24-3"><a href="#cb24-3"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemorySegment;</span></span>
504 <span id="cb24-4"><a href="#cb24-4"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
505 <span id="cb24-5"><a href="#cb24-5"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.SegmentAllocator;</span></span>
506 <span id="cb24-6"><a href="#cb24-6"></a><span class="kw">import</span><span class="im"> org.unix.*;</span></span>
507 <span id="cb24-7"><a href="#cb24-7"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAddress.NULL;</span></span>
508 <span id="cb24-8"><a href="#cb24-8"></a><span class="kw">import static</span><span class="im"> org.unix.libproc_h.*;</span></span>
509 <span id="cb24-9"><a href="#cb24-9"></a></span>
510 <span id="cb24-10"><a href="#cb24-10"></a><span class="kw">public</span> <span class="kw">class</span> LibprocMain {</span>
511 <span id="cb24-11"><a href="#cb24-11"></a>    <span class="kw">private</span> <span class="dt">static</span> <span class="dt">final</span> <span class="dt">int</span> NAME_BUF_MAX = <span class="dv">256</span>;</span>
512 <span id="cb24-12"><a href="#cb24-12"></a></span>
513 <span id="cb24-13"><a href="#cb24-13"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
514 <span id="cb24-14"><a href="#cb24-14"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
515 <span id="cb24-15"><a href="#cb24-15"></a>            var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
516 <span id="cb24-16"><a href="#cb24-16"></a>            <span class="co">// get the number of processes</span></span>
517 <span id="cb24-17"><a href="#cb24-17"></a>            <span class="dt">int</span> numPids = <span class="fu">proc_listallpids</span>(NULL, <span class="dv">0</span>);</span>
518 <span id="cb24-18"><a href="#cb24-18"></a>            <span class="co">// allocate an array</span></span>
519 <span id="cb24-19"><a href="#cb24-19"></a>            var pids = allocator.<span class="fu">allocateArray</span>(CLinker.<span class="fu">C_INT</span>, numPids);</span>
520 <span id="cb24-20"><a href="#cb24-20"></a>            <span class="co">// list all the pids into the native array</span></span>
521 <span id="cb24-21"><a href="#cb24-21"></a>            <span class="fu">proc_listallpids</span>(pids, numPids);</span>
522 <span id="cb24-22"><a href="#cb24-22"></a>            <span class="co">// convert native array to java array</span></span>
523 <span id="cb24-23"><a href="#cb24-23"></a>            <span class="dt">int</span>[] jpids = pids.<span class="fu">toIntArray</span>();</span>
524 <span id="cb24-24"><a href="#cb24-24"></a>            <span class="co">// buffer for process name</span></span>
525 <span id="cb24-25"><a href="#cb24-25"></a>            var nameBuf = allocator.<span class="fu">allocateArray</span>(CLinker.<span class="fu">C_CHAR</span>, NAME_BUF_MAX);</span>
526 <span id="cb24-26"><a href="#cb24-26"></a>            <span class="kw">for</span> (<span class="dt">int</span> i = <span class="dv">0</span>; i &lt; jpids.<span class="fu">length</span>; i++) {</span>
527 <span id="cb24-27"><a href="#cb24-27"></a>                <span class="dt">int</span> pid = jpids[i];</span>
528 <span id="cb24-28"><a href="#cb24-28"></a>                <span class="co">// get the process name</span></span>
529 <span id="cb24-29"><a href="#cb24-29"></a>                <span class="fu">proc_name</span>(pid, nameBuf, NAME_BUF_MAX);</span>
530 <span id="cb24-30"><a href="#cb24-30"></a>                <span class="bu">String</span> procName = CLinker.<span class="fu">toJavaString</span>(nameBuf);</span>
531 <span id="cb24-31"><a href="#cb24-31"></a>                <span class="co">// print pid and process name</span></span>
532 <span id="cb24-32"><a href="#cb24-32"></a>                <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">%d</span><span class="st"> </span><span class="sc">%s\n</span><span class="st">&quot;</span>, pid, procName);</span>
533 <span id="cb24-33"><a href="#cb24-33"></a>            }</span>
534 <span id="cb24-34"><a href="#cb24-34"></a>        }</span>
535 <span id="cb24-35"><a href="#cb24-35"></a>    }</span>
536 <span id="cb24-36"><a href="#cb24-36"></a>}</span></code></pre></div>
537 <h3 id="compiling-and-running-the-libproc-sample">Compiling and running the libproc sample</h3>
538 <div class="sourceCode" id="cb25"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb25-1"><a href="#cb25-1"></a></span>
539 <span id="cb25-2"><a href="#cb25-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED \</span>
540 <span id="cb25-3"><a href="#cb25-3"></a>    --add-modules jdk.incubator.foreign \</span>
541 <span id="cb25-4"><a href="#cb25-4"></a>    -Djava.library.path=/usr/lib LibprocMain.java</span></code></pre></div>
542 <h2 id="using-libgit2-from-java-mac-os">Using libgit2 from Java (Mac OS)</h2>
543 <h3 id="getting-and-building-libgit2">Getting and building libgit2</h3>
544 <ul>
545 <li>Download libgit2 v1.0.0 source from https://github.com/libgit2/libgit2/releases</li>
546 <li>Use cmake to build from libgit2</li>
547 <li>Let ${LIBGIT2_HOME} be the directory where you expanded libgit2 sources.</li>
548 <li>Let ${LIBGIT2_HOME}/build be the build directory where libgit2.dylib is built.</li>
549 </ul>
550 <h3 id="jextract-git2.h">jextract git2.h</h3>
551 <div class="sourceCode" id="cb26"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb26-1"><a href="#cb26-1"></a></span>
552 <span id="cb26-2"><a href="#cb26-2"></a><span class="ex">jextract</span> \</span>
553 <span id="cb26-3"><a href="#cb26-3"></a>  -t com.github -lgit2 \</span>
554 <span id="cb26-4"><a href="#cb26-4"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ \</span>
555 <span id="cb26-5"><a href="#cb26-5"></a>  -I <span class="va">${LIBGIT2_HOME}</span>/include/ \</span>
556 <span id="cb26-6"><a href="#cb26-6"></a>  -I <span class="va">${LIBGIT2_HOME}</span>/include/git2 \</span>
557 <span id="cb26-7"><a href="#cb26-7"></a>  <span class="va">${LIBGIT2_HOME}</span>/include/git2.h</span></code></pre></div>
558 <h3 id="java-program-that-uses-libgit2-to-clone-github-repo">Java program that uses libgit2 to clone github repo</h3>
559 <div class="sourceCode" id="cb27"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb27-1"><a href="#cb27-1"></a></span>
560 <span id="cb27-2"><a href="#cb27-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemoryAddress;</span></span>
561 <span id="cb27-3"><a href="#cb27-3"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
562 <span id="cb27-4"><a href="#cb27-4"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.SegmentAllocator;</span></span>
563 <span id="cb27-5"><a href="#cb27-5"></a><span class="kw">import static</span><span class="im"> com.github.git2_h.*;</span></span>
564 <span id="cb27-6"><a href="#cb27-6"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
565 <span id="cb27-7"><a href="#cb27-7"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAddress.NULL;</span></span>
566 <span id="cb27-8"><a href="#cb27-8"></a><span class="kw">import</span><span class="im"> com.github.*;</span></span>
567 <span id="cb27-9"><a href="#cb27-9"></a></span>
568 <span id="cb27-10"><a href="#cb27-10"></a><span class="kw">public</span> <span class="kw">class</span> GitClone {</span>
569 <span id="cb27-11"><a href="#cb27-11"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
570 <span id="cb27-12"><a href="#cb27-12"></a>          <span class="kw">if</span> (args.<span class="fu">length</span> != <span class="dv">2</span>) {</span>
571 <span id="cb27-13"><a href="#cb27-13"></a>              <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;java GitClone &lt;url&gt; &lt;path&gt;&quot;</span>);</span>
572 <span id="cb27-14"><a href="#cb27-14"></a>              <span class="bu">System</span>.<span class="fu">exit</span>(<span class="dv">1</span>);</span>
573 <span id="cb27-15"><a href="#cb27-15"></a>          }</span>
574 <span id="cb27-16"><a href="#cb27-16"></a>          <span class="fu">git_libgit2_init</span>();</span>
575 <span id="cb27-17"><a href="#cb27-17"></a>          <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
576 <span id="cb27-18"><a href="#cb27-18"></a>              var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
577 <span id="cb27-19"><a href="#cb27-19"></a>              var repo = allocator.<span class="fu">allocate</span>(C_POINTER);</span>
578 <span id="cb27-20"><a href="#cb27-20"></a>              var url = <span class="fu">toCString</span>(args[<span class="dv">0</span>], scope);</span>
579 <span id="cb27-21"><a href="#cb27-21"></a>              var path = <span class="fu">toCString</span>(args[<span class="dv">1</span>], scope);</span>
580 <span id="cb27-22"><a href="#cb27-22"></a>              <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="fu">git_clone</span>(repo, url, path, NULL));</span>
581 <span id="cb27-23"><a href="#cb27-23"></a>          }</span>
582 <span id="cb27-24"><a href="#cb27-24"></a>          <span class="fu">git_libgit2_shutdown</span>();</span>
583 <span id="cb27-25"><a href="#cb27-25"></a>    }</span>
584 <span id="cb27-26"><a href="#cb27-26"></a>}</span></code></pre></div>
585 <h3 id="compiling-and-running-the-libgit2-sample">Compiling and running the libgit2 sample</h3>
586 <div class="sourceCode" id="cb28"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb28-1"><a href="#cb28-1"></a></span>
587 <span id="cb28-2"><a href="#cb28-2"></a><span class="co"># file run.sh</span></span>
588 <span id="cb28-3"><a href="#cb28-3"></a></span>
589 <span id="cb28-4"><a href="#cb28-4"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign \</span>
590 <span id="cb28-5"><a href="#cb28-5"></a>    -Djava.library.path=<span class="va">${LIBGIT2_HOME}</span>/build/ \</span>
591 <span id="cb28-6"><a href="#cb28-6"></a>    GitClone.java <span class="va">$*</span></span></code></pre></div>
592 <h3 id="cloning-a-github-repo-using-the-above-run.sh-command">Cloning a github repo using the above run.sh command</h3>
593 <div class="sourceCode" id="cb29"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb29-1"><a href="#cb29-1"></a></span>
594 <span id="cb29-2"><a href="#cb29-2"></a><span class="fu">sh</span> run.sh https://github.com/libgit2/libgit2.git libgit2</span></code></pre></div>
595 <h2 id="using-sqlite3-library-from-java-mac-os">Using sqlite3 library from Java (Mac OS)</h2>
596 <h3 id="jextract-sqlite3.h">jextract sqlite3.h</h3>
597 <div class="sourceCode" id="cb30"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb30-1"><a href="#cb30-1"></a></span>
598 <span id="cb30-2"><a href="#cb30-2"></a><span class="ex">jextract</span> \</span>
599 <span id="cb30-3"><a href="#cb30-3"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
600 <span id="cb30-4"><a href="#cb30-4"></a>  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/sqlite3.h \</span>
601 <span id="cb30-5"><a href="#cb30-5"></a>  -t org.sqlite -lsqlite3</span></code></pre></div>
602 <h3 id="java-program-that-uses-sqlite3">Java program that uses sqlite3</h3>
603 <div class="sourceCode" id="cb31"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb31-1"><a href="#cb31-1"></a></span>
604 <span id="cb31-2"><a href="#cb31-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemoryAddress;</span></span>
605 <span id="cb31-3"><a href="#cb31-3"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.MemoryAccess;</span></span>
606 <span id="cb31-4"><a href="#cb31-4"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
607 <span id="cb31-5"><a href="#cb31-5"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.SegmentAllocator;</span></span>
608 <span id="cb31-6"><a href="#cb31-6"></a><span class="kw">import</span><span class="im"> org.sqlite.*;</span></span>
609 <span id="cb31-7"><a href="#cb31-7"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAddress.NULL;</span></span>
610 <span id="cb31-8"><a href="#cb31-8"></a><span class="kw">import static</span><span class="im"> org.sqlite.sqlite3_h.*;</span></span>
611 <span id="cb31-9"><a href="#cb31-9"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
612 <span id="cb31-10"><a href="#cb31-10"></a></span>
613 <span id="cb31-11"><a href="#cb31-11"></a><span class="kw">public</span> <span class="kw">class</span> SqliteMain {</span>
614 <span id="cb31-12"><a href="#cb31-12"></a>   <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) <span class="kw">throws</span> <span class="bu">Exception</span> {</span>
615 <span id="cb31-13"><a href="#cb31-13"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
616 <span id="cb31-14"><a href="#cb31-14"></a>            var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
617 <span id="cb31-15"><a href="#cb31-15"></a>            <span class="co">// char** errMsgPtrPtr;</span></span>
618 <span id="cb31-16"><a href="#cb31-16"></a>            var errMsgPtrPtr = allocator.<span class="fu">allocate</span>(C_POINTER);</span>
619 <span id="cb31-17"><a href="#cb31-17"></a></span>
620 <span id="cb31-18"><a href="#cb31-18"></a>            <span class="co">// sqlite3** dbPtrPtr;</span></span>
621 <span id="cb31-19"><a href="#cb31-19"></a>            var dbPtrPtr = allocator.<span class="fu">allocate</span>(C_POINTER);</span>
622 <span id="cb31-20"><a href="#cb31-20"></a></span>
623 <span id="cb31-21"><a href="#cb31-21"></a>            <span class="dt">int</span> rc = <span class="fu">sqlite3_open</span>(<span class="fu">toCString</span>(<span class="st">&quot;employee.db&quot;</span>,scope), dbPtrPtr);</span>
624 <span id="cb31-22"><a href="#cb31-22"></a>            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</span>
625 <span id="cb31-23"><a href="#cb31-23"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_open failed: &quot;</span> + rc);</span>
626 <span id="cb31-24"><a href="#cb31-24"></a>                <span class="kw">return</span>;</span>
627 <span id="cb31-25"><a href="#cb31-25"></a>            } <span class="kw">else</span> {</span>
628 <span id="cb31-26"><a href="#cb31-26"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;employee db opened&quot;</span>);</span>
629 <span id="cb31-27"><a href="#cb31-27"></a>            }</span>
630 <span id="cb31-28"><a href="#cb31-28"></a></span>
631 <span id="cb31-29"><a href="#cb31-29"></a>            <span class="co">// sqlite3* dbPtr;</span></span>
632 <span id="cb31-30"><a href="#cb31-30"></a>            var dbPtr = MemoryAccess.<span class="fu">getAddress</span>(dbPtrPtr);</span>
633 <span id="cb31-31"><a href="#cb31-31"></a></span>
634 <span id="cb31-32"><a href="#cb31-32"></a>            <span class="co">// create a new table</span></span>
635 <span id="cb31-33"><a href="#cb31-33"></a>            var sql = <span class="fu">toCString</span>(</span>
636 <span id="cb31-34"><a href="#cb31-34"></a>                <span class="st">&quot;CREATE TABLE EMPLOYEE (&quot;</span>  +</span>
637 <span id="cb31-35"><a href="#cb31-35"></a>                <span class="st">&quot;  ID INT PRIMARY KEY NOT NULL,&quot;</span> +</span>
638 <span id="cb31-36"><a href="#cb31-36"></a>                <span class="st">&quot;  NAME TEXT NOT NULL,&quot;</span>    +</span>
639 <span id="cb31-37"><a href="#cb31-37"></a>                <span class="st">&quot;  SALARY REAL NOT NULL )&quot;</span>, scope);</span>
640 <span id="cb31-38"><a href="#cb31-38"></a></span>
641 <span id="cb31-39"><a href="#cb31-39"></a>            rc = <span class="fu">sqlite3_exec</span>(dbPtr, sql, NULL, NULL, errMsgPtrPtr);</span>
642 <span id="cb31-40"><a href="#cb31-40"></a></span>
643 <span id="cb31-41"><a href="#cb31-41"></a>            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</span>
644 <span id="cb31-42"><a href="#cb31-42"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_exec failed: &quot;</span> + rc);</span>
645 <span id="cb31-43"><a href="#cb31-43"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;SQL error: &quot;</span> + <span class="fu">toJavaString</span>(MemoryAccess.<span class="fu">getAddress</span>(errMsgPtrPtr)));</span>
646 <span id="cb31-44"><a href="#cb31-44"></a>                <span class="fu">sqlite3_free</span>(MemoryAccess.<span class="fu">getAddress</span>(errMsgPtrPtr));</span>
647 <span id="cb31-45"><a href="#cb31-45"></a>            } <span class="kw">else</span> {</span>
648 <span id="cb31-46"><a href="#cb31-46"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;employee table created&quot;</span>);</span>
649 <span id="cb31-47"><a href="#cb31-47"></a>            }</span>
650 <span id="cb31-48"><a href="#cb31-48"></a></span>
651 <span id="cb31-49"><a href="#cb31-49"></a>            <span class="co">// insert two rows</span></span>
652 <span id="cb31-50"><a href="#cb31-50"></a>            sql = <span class="fu">toCString</span>(</span>
653 <span id="cb31-51"><a href="#cb31-51"></a>                <span class="st">&quot;INSERT INTO EMPLOYEE (ID,NAME,SALARY) &quot;</span> +</span>
654 <span id="cb31-52"><a href="#cb31-52"></a>                    <span class="st">&quot;VALUES (134, &#39;Xyz&#39;, 200000.0); &quot;</span> +</span>
655 <span id="cb31-53"><a href="#cb31-53"></a>                <span class="st">&quot;INSERT INTO EMPLOYEE (ID,NAME,SALARY) &quot;</span> +</span>
656 <span id="cb31-54"><a href="#cb31-54"></a>                    <span class="st">&quot;VALUES (333, &#39;Abc&#39;, 100000.0);&quot;</span>, scope</span>
657 <span id="cb31-55"><a href="#cb31-55"></a>            );</span>
658 <span id="cb31-56"><a href="#cb31-56"></a>            rc = <span class="fu">sqlite3_exec</span>(dbPtr, sql, NULL, NULL, errMsgPtrPtr);</span>
659 <span id="cb31-57"><a href="#cb31-57"></a></span>
660 <span id="cb31-58"><a href="#cb31-58"></a>            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</span>
661 <span id="cb31-59"><a href="#cb31-59"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_exec failed: &quot;</span> + rc);</span>
662 <span id="cb31-60"><a href="#cb31-60"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;SQL error: &quot;</span> + <span class="fu">toJavaString</span>(MemoryAccess.<span class="fu">getAddress</span>(errMsgPtrPtr)));</span>
663 <span id="cb31-61"><a href="#cb31-61"></a>                <span class="fu">sqlite3_free</span>(MemoryAccess.<span class="fu">getAddress</span>(errMsgPtrPtr));</span>
664 <span id="cb31-62"><a href="#cb31-62"></a>            } <span class="kw">else</span> {</span>
665 <span id="cb31-63"><a href="#cb31-63"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;rows inserted&quot;</span>);</span>
666 <span id="cb31-64"><a href="#cb31-64"></a>            }</span>
667 <span id="cb31-65"><a href="#cb31-65"></a></span>
668 <span id="cb31-66"><a href="#cb31-66"></a>            <span class="dt">int</span>[] rowNum = <span class="kw">new</span> <span class="dt">int</span>[<span class="dv">1</span>];</span>
669 <span id="cb31-67"><a href="#cb31-67"></a>            <span class="co">// callback to print rows from SELECT query</span></span>
670 <span id="cb31-68"><a href="#cb31-68"></a>            var callback = sqlite3_exec$callback.<span class="fu">allocate</span>((a, argc, argv, columnNames) -&gt; {</span>
671 <span id="cb31-69"><a href="#cb31-69"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;Row num: &quot;</span> + rowNum[<span class="dv">0</span>]++);</span>
672 <span id="cb31-70"><a href="#cb31-70"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;numColumns = &quot;</span> + argc);</span>
673 <span id="cb31-71"><a href="#cb31-71"></a>                var argv_seg = argv.<span class="fu">asSegment</span>(C_POINTER.<span class="fu">byteSize</span>() * argc, scope);</span>
674 <span id="cb31-72"><a href="#cb31-72"></a>                var columnNames_seg = columnNames.<span class="fu">asSegment</span>(C_POINTER.<span class="fu">byteSize</span>() * argc, scope);</span>
675 <span id="cb31-73"><a href="#cb31-73"></a>                <span class="kw">for</span> (<span class="dt">int</span> i = <span class="dv">0</span>; i &lt; argc; i++) {</span>
676 <span id="cb31-74"><a href="#cb31-74"></a>                     <span class="bu">String</span> name = <span class="fu">toJavaString</span>(MemoryAccess.<span class="fu">getAddressAtIndex</span>(columnNames_seg, i));</span>
677 <span id="cb31-75"><a href="#cb31-75"></a>                     <span class="bu">String</span> value = <span class="fu">toJavaString</span>(MemoryAccess.<span class="fu">getAddressAtIndex</span>(argv_seg, i));</span>
678 <span id="cb31-76"><a href="#cb31-76"></a>                     <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> = </span><span class="sc">%s\n</span><span class="st">&quot;</span>, name, value);</span>
679 <span id="cb31-77"><a href="#cb31-77"></a>                }</span>
680 <span id="cb31-78"><a href="#cb31-78"></a>                <span class="kw">return</span> <span class="dv">0</span>;</span>
681 <span id="cb31-79"><a href="#cb31-79"></a>            }, scope);</span>
682 <span id="cb31-80"><a href="#cb31-80"></a></span>
683 <span id="cb31-81"><a href="#cb31-81"></a>            <span class="co">// select query</span></span>
684 <span id="cb31-82"><a href="#cb31-82"></a>            sql = <span class="fu">toCString</span>(<span class="st">&quot;SELECT * FROM EMPLOYEE&quot;</span>, scope);</span>
685 <span id="cb31-83"><a href="#cb31-83"></a>            rc = <span class="fu">sqlite3_exec</span>(dbPtr, sql, callback, NULL, errMsgPtrPtr);</span>
686 <span id="cb31-84"><a href="#cb31-84"></a></span>
687 <span id="cb31-85"><a href="#cb31-85"></a>            <span class="kw">if</span> (rc != <span class="dv">0</span>) {</span>
688 <span id="cb31-86"><a href="#cb31-86"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;sqlite3_exec failed: &quot;</span> + rc);</span>
689 <span id="cb31-87"><a href="#cb31-87"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;SQL error: &quot;</span> + <span class="fu">toJavaString</span>(MemoryAccess.<span class="fu">getAddress</span>(errMsgPtrPtr)));</span>
690 <span id="cb31-88"><a href="#cb31-88"></a>                <span class="fu">sqlite3_free</span>(MemoryAccess.<span class="fu">getAddress</span>(errMsgPtrPtr));</span>
691 <span id="cb31-89"><a href="#cb31-89"></a>            } <span class="kw">else</span> {</span>
692 <span id="cb31-90"><a href="#cb31-90"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;done&quot;</span>);</span>
693 <span id="cb31-91"><a href="#cb31-91"></a>            }</span>
694 <span id="cb31-92"><a href="#cb31-92"></a></span>
695 <span id="cb31-93"><a href="#cb31-93"></a>            <span class="fu">sqlite3_close</span>(dbPtr);</span>
696 <span id="cb31-94"><a href="#cb31-94"></a>        }</span>
697 <span id="cb31-95"><a href="#cb31-95"></a>    }</span>
698 <span id="cb31-96"><a href="#cb31-96"></a>}</span>
699 <span id="cb31-97"><a href="#cb31-97"></a></span></code></pre></div>
700 <h3 id="compiling-and-running-the-sqlite3-sample">Compiling and running the sqlite3 sample</h3>
701 <div class="sourceCode" id="cb32"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb32-1"><a href="#cb32-1"></a></span>
702 <span id="cb32-2"><a href="#cb32-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED \</span>
703 <span id="cb32-3"><a href="#cb32-3"></a>   --add-modules jdk.incubator.foreign \</span>
704 <span id="cb32-4"><a href="#cb32-4"></a>   -Djava.library.path=/usr/lib SqliteMain.java</span></code></pre></div>
705 <h2 id="using-opengl-library-from-java-mac-os">Using OpenGL library from Java (Mac OS)</h2>
706 <h3 id="jextract-glut.h">jextract glut.h</h3>
707 <div class="sourceCode" id="cb33"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb33-1"><a href="#cb33-1"></a></span>
708 <span id="cb33-2"><a href="#cb33-2"></a><span class="ex">jextract</span> -t opengl -lGL -l/System/Library/Frameworks/GLUT.framework/Versions/Current/GLUT \</span>
709 <span id="cb33-3"><a href="#cb33-3"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ \</span>
710 <span id="cb33-4"><a href="#cb33-4"></a>  -C-F/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks \</span>
711 <span id="cb33-5"><a href="#cb33-5"></a>  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/GLUT.framework/Headers/glut.h</span></code></pre></div>
712 <h3 id="java-program-that-uses-opengl">Java program that uses OpenGL</h3>
713 <div class="sourceCode" id="cb34"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb34-1"><a href="#cb34-1"></a></span>
714 <span id="cb34-2"><a href="#cb34-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.CLinker;</span></span>
715 <span id="cb34-3"><a href="#cb34-3"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.ResourceScope;</span></span>
716 <span id="cb34-4"><a href="#cb34-4"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.SegmentAllocator;</span></span>
717 <span id="cb34-5"><a href="#cb34-5"></a><span class="kw">import</span><span class="im"> opengl.*;</span></span>
718 <span id="cb34-6"><a href="#cb34-6"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
719 <span id="cb34-7"><a href="#cb34-7"></a><span class="kw">import static</span><span class="im"> opengl.glut_h.*;</span></span>
720 <span id="cb34-8"><a href="#cb34-8"></a></span>
721 <span id="cb34-9"><a href="#cb34-9"></a><span class="kw">public</span> <span class="kw">class</span> Teapot {</span>
722 <span id="cb34-10"><a href="#cb34-10"></a>    <span class="kw">private</span> <span class="dt">float</span> rot = <span class="dv">0</span>;</span>
723 <span id="cb34-11"><a href="#cb34-11"></a></span>
724 <span id="cb34-12"><a href="#cb34-12"></a>    <span class="fu">Teapot</span>(SegmentAllocator allocator) {</span>
725 <span id="cb34-13"><a href="#cb34-13"></a>        <span class="co">// Reset Background</span></span>
726 <span id="cb34-14"><a href="#cb34-14"></a>        <span class="fu">glClearColor</span>(<span class="fl">0f</span>, <span class="fl">0f</span>, <span class="fl">0f</span>, <span class="fl">0f</span>);</span>
727 <span id="cb34-15"><a href="#cb34-15"></a>        <span class="co">// Setup Lighting</span></span>
728 <span id="cb34-16"><a href="#cb34-16"></a>        <span class="fu">glShadeModel</span>(<span class="fu">GL_SMOOTH</span>());</span>
729 <span id="cb34-17"><a href="#cb34-17"></a>        var pos = allocator.<span class="fu">allocateArray</span>(C_FLOAT, <span class="kw">new</span> <span class="dt">float</span>[] {<span class="fl">0.0f</span>, <span class="fl">15.0f</span>, -<span class="fl">15.0f</span>, <span class="dv">0</span>});</span>
730 <span id="cb34-18"><a href="#cb34-18"></a>        <span class="fu">glLightfv</span>(<span class="fu">GL_LIGHT0</span>(), <span class="fu">GL_POSITION</span>(), pos);</span>
731 <span id="cb34-19"><a href="#cb34-19"></a>        var spec = allocator.<span class="fu">allocateArray</span>(C_FLOAT, <span class="kw">new</span> <span class="dt">float</span>[] {<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">0</span>});</span>
732 <span id="cb34-20"><a href="#cb34-20"></a>        <span class="fu">glLightfv</span>(<span class="fu">GL_LIGHT0</span>(), <span class="fu">GL_AMBIENT</span>(), spec);</span>
733 <span id="cb34-21"><a href="#cb34-21"></a>        <span class="fu">glLightfv</span>(<span class="fu">GL_LIGHT0</span>(), <span class="fu">GL_DIFFUSE</span>(), spec);</span>
734 <span id="cb34-22"><a href="#cb34-22"></a>        <span class="fu">glLightfv</span>(<span class="fu">GL_LIGHT0</span>(), <span class="fu">GL_SPECULAR</span>(), spec);</span>
735 <span id="cb34-23"><a href="#cb34-23"></a>        var shini = allocator.<span class="fu">allocate</span>(C_FLOAT, <span class="dv">113</span>);</span>
736 <span id="cb34-24"><a href="#cb34-24"></a>        <span class="fu">glMaterialfv</span>(<span class="fu">GL_FRONT</span>(), <span class="fu">GL_SHININESS</span>(), shini);</span>
737 <span id="cb34-25"><a href="#cb34-25"></a>        <span class="fu">glEnable</span>(<span class="fu">GL_LIGHTING</span>());</span>
738 <span id="cb34-26"><a href="#cb34-26"></a>        <span class="fu">glEnable</span>(<span class="fu">GL_LIGHT0</span>());</span>
739 <span id="cb34-27"><a href="#cb34-27"></a>        <span class="fu">glEnable</span>(<span class="fu">GL_DEPTH_TEST</span>());</span>
740 <span id="cb34-28"><a href="#cb34-28"></a>    }</span>
741 <span id="cb34-29"><a href="#cb34-29"></a></span>
742 <span id="cb34-30"><a href="#cb34-30"></a>    <span class="dt">void</span> <span class="fu">display</span>() {</span>
743 <span id="cb34-31"><a href="#cb34-31"></a>        <span class="fu">glClear</span>(<span class="fu">GL_COLOR_BUFFER_BIT</span>() | <span class="fu">GL_DEPTH_BUFFER_BIT</span>());</span>
744 <span id="cb34-32"><a href="#cb34-32"></a>        <span class="fu">glPushMatrix</span>();</span>
745 <span id="cb34-33"><a href="#cb34-33"></a>        <span class="fu">glRotatef</span>(-<span class="fl">20f</span>, <span class="fl">1f</span>, <span class="fl">1f</span>, <span class="fl">0f</span>);</span>
746 <span id="cb34-34"><a href="#cb34-34"></a>        <span class="fu">glRotatef</span>(rot, <span class="fl">0f</span>, <span class="fl">1f</span>, <span class="fl">0f</span>);</span>
747 <span id="cb34-35"><a href="#cb34-35"></a>        <span class="fu">glutSolidTeapot</span>(<span class="fl">0.5d</span>);</span>
748 <span id="cb34-36"><a href="#cb34-36"></a>        <span class="fu">glPopMatrix</span>();</span>
749 <span id="cb34-37"><a href="#cb34-37"></a>        <span class="fu">glutSwapBuffers</span>();</span>
750 <span id="cb34-38"><a href="#cb34-38"></a>    }</span>
751 <span id="cb34-39"><a href="#cb34-39"></a></span>
752 <span id="cb34-40"><a href="#cb34-40"></a>    <span class="dt">void</span> <span class="fu">onIdle</span>() {</span>
753 <span id="cb34-41"><a href="#cb34-41"></a>        rot += <span class="fl">0.</span><span class="dv">1</span>;</span>
754 <span id="cb34-42"><a href="#cb34-42"></a>        <span class="fu">glutPostRedisplay</span>();</span>
755 <span id="cb34-43"><a href="#cb34-43"></a>    }</span>
756 <span id="cb34-44"><a href="#cb34-44"></a></span>
757 <span id="cb34-45"><a href="#cb34-45"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
758 <span id="cb34-46"><a href="#cb34-46"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
759 <span id="cb34-47"><a href="#cb34-47"></a>            var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
760 <span id="cb34-48"><a href="#cb34-48"></a>            var argc = allocator.<span class="fu">allocate</span>(C_INT, <span class="dv">0</span>);</span>
761 <span id="cb34-49"><a href="#cb34-49"></a>            <span class="fu">glutInit</span>(argc, argc);</span>
762 <span id="cb34-50"><a href="#cb34-50"></a>            <span class="fu">glutInitDisplayMode</span>(<span class="fu">GLUT_DOUBLE</span>() | <span class="fu">GLUT_RGB</span>() | <span class="fu">GLUT_DEPTH</span>());</span>
763 <span id="cb34-51"><a href="#cb34-51"></a>            <span class="fu">glutInitWindowSize</span>(<span class="dv">500</span>, <span class="dv">500</span>);</span>
764 <span id="cb34-52"><a href="#cb34-52"></a>            <span class="fu">glutCreateWindow</span>(CLinker.<span class="fu">toCString</span>(<span class="st">&quot;Hello Panama!&quot;</span>, scope));</span>
765 <span id="cb34-53"><a href="#cb34-53"></a>            var teapot = <span class="kw">new</span> <span class="fu">Teapot</span>(allocator);</span>
766 <span id="cb34-54"><a href="#cb34-54"></a>            var displayStub = glutDisplayFunc$func.<span class="fu">allocate</span>(teapot::display, scope);</span>
767 <span id="cb34-55"><a href="#cb34-55"></a>            var idleStub = glutIdleFunc$func.<span class="fu">allocate</span>(teapot::onIdle, scope);</span>
768 <span id="cb34-56"><a href="#cb34-56"></a>            <span class="fu">glutDisplayFunc</span>(displayStub);</span>
769 <span id="cb34-57"><a href="#cb34-57"></a>            <span class="fu">glutIdleFunc</span>(idleStub);</span>
770 <span id="cb34-58"><a href="#cb34-58"></a>            <span class="fu">glutMainLoop</span>();</span>
771 <span id="cb34-59"><a href="#cb34-59"></a>        }</span>
772 <span id="cb34-60"><a href="#cb34-60"></a>    }</span>
773 <span id="cb34-61"><a href="#cb34-61"></a>}</span></code></pre></div>
774 <h3 id="compiling-and-running-the-opengl-sample">Compiling and running the OpenGL sample</h3>
775 <div class="sourceCode" id="cb35"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb35-1"><a href="#cb35-1"></a></span>
776 <span id="cb35-2"><a href="#cb35-2"></a><span class="ex">java</span> -XstartOnFirstThread --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign \</span>
777 <span id="cb35-3"><a href="#cb35-3"></a>    -Djava.library.path=.:/System/Library/Frameworks/OpenGL.framework/Versions/Current/Libraries/ Teapot.java <span class="va">$*</span></span></code></pre></div>
778 <h2 id="using-tensorflow-mac-os">Using tensorflow (Mac OS)</h2>
779 <h3 id="getting-libtensorflow">getting libtensorflow</h3>
780 <ul>
781 <li><p>Download tensorflow library from</p>
782 <p>https://www.tensorflow.org/install/lang_c</p></li>
783 <li><p>extract the downloaded tar in a directory called LIBTENSORFLOW_HOME</p></li>
784 </ul>
785 <h3 id="jextract-c_api.h">jextract c_api.h</h3>
786 <div class="sourceCode" id="cb36"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb36-1"><a href="#cb36-1"></a></span>
787 <span id="cb36-2"><a href="#cb36-2"></a><span class="ex">jextract</span> --source \</span>
788 <span id="cb36-3"><a href="#cb36-3"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ \</span>
789 <span id="cb36-4"><a href="#cb36-4"></a>  -t org.tensorflow \</span>
790 <span id="cb36-5"><a href="#cb36-5"></a>  -I <span class="va">${LIBTENSORFLOW_HOME}</span>/include \</span>
791 <span id="cb36-6"><a href="#cb36-6"></a>  -l <span class="va">${LIBTENSORFLOW_HOME}</span>/lib/libtensorflow.dylib \</span>
792 <span id="cb36-7"><a href="#cb36-7"></a>  <span class="va">${LIBTENSORFLOW_HOME}</span>/include/tensorflow/c/c_api.h</span>
793 <span id="cb36-8"><a href="#cb36-8"></a></span>
794 <span id="cb36-9"><a href="#cb36-9"></a><span class="ex">javac</span> --add-modules jdk.incubator.foreign org/tensorflow/*.java</span></code></pre></div>
795 <h3 id="python-program-that-creates-and-saves-model">Python program that creates and saves model</h3>
796 <p>The following Python program should be run to create and save model which will read and printed by a Java program.</p>
797 <p>Note: you need to install tensorflow package to run this python script.</p>
798 <div class="sourceCode" id="cb37"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb37-1"><a href="#cb37-1"></a></span>
799 <span id="cb37-2"><a href="#cb37-2"></a><span class="im">import</span> tensorflow <span class="im">as</span> tf</span>
800 <span id="cb37-3"><a href="#cb37-3"></a><span class="im">from</span> tensorflow.keras <span class="im">import</span> models, layers</span>
801 <span id="cb37-4"><a href="#cb37-4"></a><span class="im">from</span> tensorflow.keras.datasets <span class="im">import</span> mnist</span>
802 <span id="cb37-5"><a href="#cb37-5"></a></span>
803 <span id="cb37-6"><a href="#cb37-6"></a>model <span class="op">=</span> tf.keras.models.Sequential([</span>
804 <span id="cb37-7"><a href="#cb37-7"></a>  tf.keras.layers.Flatten(input_shape<span class="op">=</span>(<span class="dv">28</span>, <span class="dv">28</span>)),</span>
805 <span id="cb37-8"><a href="#cb37-8"></a>  tf.keras.layers.Dense(<span class="dv">128</span>,activation<span class="op">=</span><span class="st">&#39;relu&#39;</span>),</span>
806 <span id="cb37-9"><a href="#cb37-9"></a>  tf.keras.layers.Dense(<span class="dv">10</span>, activation<span class="op">=</span><span class="st">&#39;softmax&#39;</span>)</span>
807 <span id="cb37-10"><a href="#cb37-10"></a>])</span>
808 <span id="cb37-11"><a href="#cb37-11"></a></span>
809 <span id="cb37-12"><a href="#cb37-12"></a>model.<span class="bu">compile</span>(</span>
810 <span id="cb37-13"><a href="#cb37-13"></a>    loss<span class="op">=</span><span class="st">&#39;sparse_categorical_crossentropy&#39;</span>,</span>
811 <span id="cb37-14"><a href="#cb37-14"></a>    optimizer<span class="op">=</span>tf.keras.optimizers.Adam(<span class="fl">0.001</span>),</span>
812 <span id="cb37-15"><a href="#cb37-15"></a>    metrics<span class="op">=</span>[<span class="st">&#39;accuracy&#39;</span>],</span>
813 <span id="cb37-16"><a href="#cb37-16"></a>)</span>
814 <span id="cb37-17"><a href="#cb37-17"></a></span>
815 <span id="cb37-18"><a href="#cb37-18"></a><span class="bu">print</span>(model.summary())</span>
816 <span id="cb37-19"><a href="#cb37-19"></a></span>
817 <span id="cb37-20"><a href="#cb37-20"></a>(train_images, train_labels), (test_images, test_labels) <span class="op">=</span> mnist.load_data()</span>
818 <span id="cb37-21"><a href="#cb37-21"></a></span>
819 <span id="cb37-22"><a href="#cb37-22"></a>train_images <span class="op">=</span> train_images<span class="op">/</span><span class="fl">255.0</span></span>
820 <span id="cb37-23"><a href="#cb37-23"></a>test_images <span class="op">=</span> test_images<span class="op">/</span><span class="fl">255.0</span></span>
821 <span id="cb37-24"><a href="#cb37-24"></a></span>
822 <span id="cb37-25"><a href="#cb37-25"></a>model.fit(train_images, train_labels,</span>
823 <span id="cb37-26"><a href="#cb37-26"></a>    epochs<span class="op">=</span><span class="dv">4</span>, batch_size<span class="op">=</span><span class="dv">128</span>, verbose<span class="op">=</span><span class="dv">1</span>)</span>
824 <span id="cb37-27"><a href="#cb37-27"></a></span>
825 <span id="cb37-28"><a href="#cb37-28"></a>test_loss, test_accuracy <span class="op">=</span> model.evaluate(test_images, test_labels)</span>
826 <span id="cb37-29"><a href="#cb37-29"></a></span>
827 <span id="cb37-30"><a href="#cb37-30"></a><span class="bu">print</span>(test_loss, test_accuracy)</span>
828 <span id="cb37-31"><a href="#cb37-31"></a></span>
829 <span id="cb37-32"><a href="#cb37-32"></a>model.save(<span class="st">&quot;saved_mnist_model&quot;</span>)</span></code></pre></div>
830 <h3 id="java-program-that-uses-tensorflow-c-api">Java program that uses Tensorflow C API</h3>
831 <div class="sourceCode" id="cb38"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb38-1"><a href="#cb38-1"></a></span>
832 <span id="cb38-2"><a href="#cb38-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.*;</span></span>
833 <span id="cb38-3"><a href="#cb38-3"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
834 <span id="cb38-4"><a href="#cb38-4"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAccess.*;</span></span>
835 <span id="cb38-5"><a href="#cb38-5"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAddress.*;</span></span>
836 <span id="cb38-6"><a href="#cb38-6"></a><span class="kw">import static</span><span class="im"> org.tensorflow.c_api_h.*;</span></span>
837 <span id="cb38-7"><a href="#cb38-7"></a><span class="kw">import</span><span class="im"> org.tensorflow.*;</span></span>
838 <span id="cb38-8"><a href="#cb38-8"></a></span>
839 <span id="cb38-9"><a href="#cb38-9"></a><span class="co">// simple program that loads saved model and prints basic info on operations in it</span></span>
840 <span id="cb38-10"><a href="#cb38-10"></a></span>
841 <span id="cb38-11"><a href="#cb38-11"></a><span class="kw">public</span> <span class="kw">class</span> TensorflowLoadSavedModel {</span>
842 <span id="cb38-12"><a href="#cb38-12"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span><span class="kw">... </span>args) <span class="kw">throws</span> <span class="bu">Exception</span> {</span>
843 <span id="cb38-13"><a href="#cb38-13"></a>        <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>(<span class="st">&quot;TensorFlow C library version: &quot;</span> + <span class="fu">toJavaString</span>(<span class="fu">TF_Version</span>()));</span>
844 <span id="cb38-14"><a href="#cb38-14"></a></span>
845 <span id="cb38-15"><a href="#cb38-15"></a>        <span class="kw">if</span> (args.<span class="fu">length</span> == <span class="dv">0</span>) {</span>
846 <span id="cb38-16"><a href="#cb38-16"></a>            <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;java TensorflowLoadSavedModel &lt;saved model dir&gt;&quot;</span>);</span>
847 <span id="cb38-17"><a href="#cb38-17"></a>            <span class="bu">System</span>.<span class="fu">exit</span>(<span class="dv">1</span>);</span>
848 <span id="cb38-18"><a href="#cb38-18"></a>        }</span>
849 <span id="cb38-19"><a href="#cb38-19"></a></span>
850 <span id="cb38-20"><a href="#cb38-20"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
851 <span id="cb38-21"><a href="#cb38-21"></a>            var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
852 <span id="cb38-22"><a href="#cb38-22"></a>            var graph = <span class="fu">TF_NewGraph</span>();</span>
853 <span id="cb38-23"><a href="#cb38-23"></a>            var status = <span class="fu">TF_NewStatus</span>();</span>
854 <span id="cb38-24"><a href="#cb38-24"></a>            var sessionOpts = <span class="fu">TF_NewSessionOptions</span>();</span>
855 <span id="cb38-25"><a href="#cb38-25"></a></span>
856 <span id="cb38-26"><a href="#cb38-26"></a>            var savedModelDir = <span class="fu">toCString</span>(args[<span class="dv">0</span>], scope);</span>
857 <span id="cb38-27"><a href="#cb38-27"></a>            var tags = allocator.<span class="fu">allocate</span>(C_POINTER, <span class="fu">toCString</span>(<span class="st">&quot;serve&quot;</span>, scope));</span>
858 <span id="cb38-28"><a href="#cb38-28"></a>            var session = <span class="fu">TF_LoadSessionFromSavedModel</span>(sessionOpts, NULL, savedModelDir, tags, <span class="dv">1</span>, graph, NULL, status);</span>
859 <span id="cb38-29"><a href="#cb38-29"></a></span>
860 <span id="cb38-30"><a href="#cb38-30"></a>            <span class="kw">if</span> (<span class="fu">TF_GetCode</span>(status) != <span class="fu">TF_OK</span>()) {</span>
861 <span id="cb38-31"><a href="#cb38-31"></a>                <span class="bu">System</span>.<span class="fu">err.printf</span>(<span class="st">&quot;cannot load session from saved model: </span><span class="sc">%s\n</span><span class="st">&quot;</span>,</span>
862 <span id="cb38-32"><a href="#cb38-32"></a>                    <span class="fu">toJavaString</span>(<span class="fu">TF_Message</span>(status)));</span>
863 <span id="cb38-33"><a href="#cb38-33"></a>            } <span class="kw">else</span> {</span>
864 <span id="cb38-34"><a href="#cb38-34"></a>                <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;load session from saved model works!&quot;</span>);</span>
865 <span id="cb38-35"><a href="#cb38-35"></a>            }</span>
866 <span id="cb38-36"><a href="#cb38-36"></a></span>
867 <span id="cb38-37"><a href="#cb38-37"></a>            <span class="co">// print operations</span></span>
868 <span id="cb38-38"><a href="#cb38-38"></a>            var size = allocator.<span class="fu">allocate</span>(C_LONG_LONG);</span>
869 <span id="cb38-39"><a href="#cb38-39"></a>            var operation = NULL;</span>
870 <span id="cb38-40"><a href="#cb38-40"></a>            <span class="kw">while</span> (!(operation = <span class="fu">TF_GraphNextOperation</span>(graph, size)).<span class="fu">equals</span>(NULL)) {</span>
871 <span id="cb38-41"><a href="#cb38-41"></a>                <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> : </span><span class="sc">%s\n</span><span class="st">&quot;</span>,</span>
872 <span id="cb38-42"><a href="#cb38-42"></a>                    <span class="fu">toJavaString</span>(<span class="fu">TF_OperationName</span>(operation)),</span>
873 <span id="cb38-43"><a href="#cb38-43"></a>                    <span class="fu">toJavaString</span>(<span class="fu">TF_OperationOpType</span>(operation)));</span>
874 <span id="cb38-44"><a href="#cb38-44"></a>            }</span>
875 <span id="cb38-45"><a href="#cb38-45"></a></span>
876 <span id="cb38-46"><a href="#cb38-46"></a>            <span class="fu">TF_DeleteGraph</span>(graph);</span>
877 <span id="cb38-47"><a href="#cb38-47"></a>            <span class="fu">TF_DeleteSession</span>(session, status);</span>
878 <span id="cb38-48"><a href="#cb38-48"></a>            <span class="fu">TF_DeleteSessionOptions</span>(sessionOpts);</span>
879 <span id="cb38-49"><a href="#cb38-49"></a>            <span class="fu">TF_DeleteStatus</span>(status);</span>
880 <span id="cb38-50"><a href="#cb38-50"></a>        }</span>
881 <span id="cb38-51"><a href="#cb38-51"></a>    }</span>
882 <span id="cb38-52"><a href="#cb38-52"></a>}</span></code></pre></div>
883 <h3 id="compiling-and-running-the-java-tensorflow-sample">Compiling and running the Java Tensorflow sample</h3>
884 <div class="sourceCode" id="cb39"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb39-1"><a href="#cb39-1"></a></span>
885 <span id="cb39-2"><a href="#cb39-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign \</span>
886 <span id="cb39-3"><a href="#cb39-3"></a>   TensorflowLoadSavedModel.java saved_mnist_model</span></code></pre></div>
887 <h2 id="using-time.h-mac-os">Using time.h (Mac OS)</h2>
888 <h3 id="jextract-time.h">jextract time.h</h3>
889 <div class="sourceCode" id="cb40"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb40-1"><a href="#cb40-1"></a></span>
890 <span id="cb40-2"><a href="#cb40-2"></a><span class="ex">jextract</span> -t org.unix \</span>
891 <span id="cb40-3"><a href="#cb40-3"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include \</span>
892 <span id="cb40-4"><a href="#cb40-4"></a>   /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/time.h</span></code></pre></div>
893 <h3 id="java-program-that-uses-posix-time-library">Java program that uses POSIX time library</h3>
894 <div class="sourceCode" id="cb41"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb41-1"><a href="#cb41-1"></a></span>
895 <span id="cb41-2"><a href="#cb41-2"></a><span class="kw">import static</span><span class="im"> org.unix.time_h.*;</span></span>
896 <span id="cb41-3"><a href="#cb41-3"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
897 <span id="cb41-4"><a href="#cb41-4"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.*;</span></span>
898 <span id="cb41-5"><a href="#cb41-5"></a><span class="kw">import</span><span class="im"> org.unix.*;</span></span>
899 <span id="cb41-6"><a href="#cb41-6"></a></span>
900 <span id="cb41-7"><a href="#cb41-7"></a><span class="kw">public</span> <span class="kw">class</span> PanamaTime {</span>
901 <span id="cb41-8"><a href="#cb41-8"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
902 <span id="cb41-9"><a href="#cb41-9"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
903 <span id="cb41-10"><a href="#cb41-10"></a>            var allocator = SegmentAllocator.<span class="fu">ofScope</span>(scope);</span>
904 <span id="cb41-11"><a href="#cb41-11"></a>            var now = allocator.<span class="fu">allocate</span>(C_LONG, <span class="bu">System</span>.<span class="fu">currentTimeMillis</span>() / <span class="dv">1000</span>);</span>
905 <span id="cb41-12"><a href="#cb41-12"></a>            MemorySegment time = tm.<span class="fu">allocate</span>(scope);</span>
906 <span id="cb41-13"><a href="#cb41-13"></a>            <span class="fu">localtime_r</span>(now, time);</span>
907 <span id="cb41-14"><a href="#cb41-14"></a>            <span class="bu">System</span>.<span class="fu">err.printf</span>(<span class="st">&quot;Time = </span><span class="sc">%d</span><span class="st">:</span><span class="sc">%d\n</span><span class="st">&quot;</span>, tm.<span class="fu">tm_hour</span>$<span class="fu">get</span>(time), tm.<span class="fu">tm_min</span>$<span class="fu">get</span>(time));</span>
908 <span id="cb41-15"><a href="#cb41-15"></a>        }</span>
909 <span id="cb41-16"><a href="#cb41-16"></a>    }</span>
910 <span id="cb41-17"><a href="#cb41-17"></a>}</span></code></pre></div>
911 <h3 id="compiling-and-running-the-time-sample">Compiling and running the time sample</h3>
912 <div class="sourceCode" id="cb42"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb42-1"><a href="#cb42-1"></a></span>
913 <span id="cb42-2"><a href="#cb42-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign PanamaTime.java</span></code></pre></div>
914 <h2 id="using-libclang-library-mac-os">Using libclang library (Mac OS)</h2>
915 <h3 id="jextract-index.h">jextract Index.h</h3>
916 <div class="sourceCode" id="cb43"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb43-1"><a href="#cb43-1"></a></span>
917 <span id="cb43-2"><a href="#cb43-2"></a><span class="co"># LIBCLANG_HOME is the directory where you&#39;ve installed llvm 9.x or above</span></span>
918 <span id="cb43-3"><a href="#cb43-3"></a></span>
919 <span id="cb43-4"><a href="#cb43-4"></a><span class="ex">jextract</span> --source -t org.llvm.clang -lclang \</span>
920 <span id="cb43-5"><a href="#cb43-5"></a>  -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/ \</span>
921 <span id="cb43-6"><a href="#cb43-6"></a>  -I <span class="va">${LIBCLANG_HOME}</span>/include/ \</span>
922 <span id="cb43-7"><a href="#cb43-7"></a>  -I <span class="va">${LIBCLANG_HOME}</span>/include/clang-c \</span>
923 <span id="cb43-8"><a href="#cb43-8"></a>  <span class="va">${LIBCLANG_HOME}</span>/include/clang-c/Index.h</span>
924 <span id="cb43-9"><a href="#cb43-9"></a><span class="ex">javac</span> --add-modules jdk.incubator.foreign org/llvm/clang/*.java</span></code></pre></div>
925 <h3 id="java-program-that-uses-libclang-to-print-ast-of-a-given-c-program">Java program that uses libclang to print AST of a given C program</h3>
926 <div class="sourceCode" id="cb44"><pre class="sourceCode java"><code class="sourceCode java"><span id="cb44-1"><a href="#cb44-1"></a></span>
927 <span id="cb44-2"><a href="#cb44-2"></a><span class="kw">import</span><span class="im"> jdk.incubator.foreign.*;</span></span>
928 <span id="cb44-3"><a href="#cb44-3"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.CLinker.*;</span></span>
929 <span id="cb44-4"><a href="#cb44-4"></a><span class="kw">import static</span><span class="im"> jdk.incubator.foreign.MemoryAddress.NULL;</span></span>
930 <span id="cb44-5"><a href="#cb44-5"></a><span class="kw">import static</span><span class="im"> org.llvm.clang.Index_h.*;</span></span>
931 <span id="cb44-6"><a href="#cb44-6"></a><span class="kw">import</span><span class="im"> org.llvm.clang.*;</span></span>
932 <span id="cb44-7"><a href="#cb44-7"></a></span>
933 <span id="cb44-8"><a href="#cb44-8"></a><span class="kw">public</span> <span class="kw">class</span> ASTPrinter {</span>
934 <span id="cb44-9"><a href="#cb44-9"></a>    <span class="kw">private</span> <span class="dt">static</span> <span class="bu">String</span> <span class="fu">asJavaString</span>(MemorySegment clangStr) {</span>
935 <span id="cb44-10"><a href="#cb44-10"></a>        <span class="bu">String</span> str = <span class="fu">toJavaString</span>(<span class="fu">clang_getCString</span>(clangStr));</span>
936 <span id="cb44-11"><a href="#cb44-11"></a>        <span class="fu">clang_disposeString</span>(clangStr);</span>
937 <span id="cb44-12"><a href="#cb44-12"></a>        <span class="kw">return</span> str;</span>
938 <span id="cb44-13"><a href="#cb44-13"></a>    }</span>
939 <span id="cb44-14"><a href="#cb44-14"></a></span>
940 <span id="cb44-15"><a href="#cb44-15"></a>    <span class="kw">public</span> <span class="dt">static</span> <span class="dt">void</span> <span class="fu">main</span>(<span class="bu">String</span>[] args) {</span>
941 <span id="cb44-16"><a href="#cb44-16"></a>        <span class="kw">if</span> (args.<span class="fu">length</span> == <span class="dv">0</span>) {</span>
942 <span id="cb44-17"><a href="#cb44-17"></a>            <span class="bu">System</span>.<span class="fu">err</span>.<span class="fu">println</span>(<span class="st">&quot;java ASTPrinter &lt;C source or header&gt;&quot;</span>);</span>
943 <span id="cb44-18"><a href="#cb44-18"></a>            <span class="bu">System</span>.<span class="fu">exit</span>(<span class="dv">1</span>);</span>
944 <span id="cb44-19"><a href="#cb44-19"></a>        }</span>
945 <span id="cb44-20"><a href="#cb44-20"></a></span>
946 <span id="cb44-21"><a href="#cb44-21"></a>        <span class="kw">try</span> (var scope = ResourceScope.<span class="fu">newConfinedScope</span>()) {</span>
947 <span id="cb44-22"><a href="#cb44-22"></a>            <span class="co">// parse the C header/source passed from the command line</span></span>
948 <span id="cb44-23"><a href="#cb44-23"></a>            var index = <span class="fu">clang_createIndex</span>(<span class="dv">0</span>, <span class="dv">0</span>);</span>
949 <span id="cb44-24"><a href="#cb44-24"></a>            var tu = <span class="fu">clang_parseTranslationUnit</span>(index, <span class="fu">toCString</span>(args[<span class="dv">0</span>], scope),</span>
950 <span id="cb44-25"><a href="#cb44-25"></a>                    NULL, <span class="dv">0</span>, NULL, <span class="dv">0</span>, <span class="fu">CXTranslationUnit_None</span>());</span>
951 <span id="cb44-26"><a href="#cb44-26"></a>            <span class="co">// array trick to update within lambda</span></span>
952 <span id="cb44-27"><a href="#cb44-27"></a>            var level = <span class="kw">new</span> <span class="dt">int</span>[<span class="dv">1</span>];</span>
953 <span id="cb44-28"><a href="#cb44-28"></a>            var visitor = <span class="kw">new</span> MemoryAddress[<span class="dv">1</span>];</span>
954 <span id="cb44-29"><a href="#cb44-29"></a></span>
955 <span id="cb44-30"><a href="#cb44-30"></a>            <span class="co">// clang Cursor visitor callback</span></span>
956 <span id="cb44-31"><a href="#cb44-31"></a>            visitor[<span class="dv">0</span>] = CXCursorVisitor.<span class="fu">allocate</span>((cursor, parent, data) -&gt; {</span>
957 <span id="cb44-32"><a href="#cb44-32"></a>                var kind = <span class="fu">clang_getCursorKind</span>(cursor);</span>
958 <span id="cb44-33"><a href="#cb44-33"></a>                var name = <span class="fu">asJavaString</span>(<span class="fu">clang_getCursorSpelling</span>(scope, cursor));</span>
959 <span id="cb44-34"><a href="#cb44-34"></a>                var kindName = <span class="fu">asJavaString</span>(<span class="fu">clang_getCursorKindSpelling</span>(scope, kind));</span>
960 <span id="cb44-35"><a href="#cb44-35"></a>                <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot;</span><span class="sc">%s</span><span class="st"> </span><span class="sc">%s</span><span class="st"> </span><span class="sc">%s</span><span class="st">&quot;</span>, <span class="st">&quot; &quot;</span>.<span class="fu">repeat</span>(level[<span class="dv">0</span>]), kindName, name);</span>
961 <span id="cb44-36"><a href="#cb44-36"></a>                var type = <span class="fu">clang_getCursorType</span>(scope, cursor);</span>
962 <span id="cb44-37"><a href="#cb44-37"></a>                <span class="kw">if</span> (CXType.<span class="fu">kind</span>$<span class="fu">get</span>(type) != <span class="fu">CXType_Invalid</span>()) {</span>
963 <span id="cb44-38"><a href="#cb44-38"></a>                    var typeName = <span class="fu">asJavaString</span>(<span class="fu">clang_getTypeSpelling</span>(scope, type));</span>
964 <span id="cb44-39"><a href="#cb44-39"></a>                    <span class="bu">System</span>.<span class="fu">out.printf</span>(<span class="st">&quot; &lt;</span><span class="sc">%s</span><span class="st">&gt;&quot;</span>, typeName);</span>
965 <span id="cb44-40"><a href="#cb44-40"></a>                }</span>
966 <span id="cb44-41"><a href="#cb44-41"></a>                <span class="bu">System</span>.<span class="fu">out</span>.<span class="fu">println</span>();</span>
967 <span id="cb44-42"><a href="#cb44-42"></a></span>
968 <span id="cb44-43"><a href="#cb44-43"></a>                <span class="co">// visit children</span></span>
969 <span id="cb44-44"><a href="#cb44-44"></a>                level[<span class="dv">0</span>]++;</span>
970 <span id="cb44-45"><a href="#cb44-45"></a>                <span class="fu">clang_visitChildren</span>(cursor, visitor[<span class="dv">0</span>], NULL);</span>
971 <span id="cb44-46"><a href="#cb44-46"></a>                level[<span class="dv">0</span>]--;</span>
972 <span id="cb44-47"><a href="#cb44-47"></a></span>
973 <span id="cb44-48"><a href="#cb44-48"></a>                <span class="kw">return</span> <span class="fu">CXChildVisit_Continue</span>();</span>
974 <span id="cb44-49"><a href="#cb44-49"></a>            });</span>
975 <span id="cb44-50"><a href="#cb44-50"></a></span>
976 <span id="cb44-51"><a href="#cb44-51"></a>            <span class="co">// get the AST root and visit it</span></span>
977 <span id="cb44-52"><a href="#cb44-52"></a>            var root = <span class="fu">clang_getTranslationUnitCursor</span>(scope, tu);</span>
978 <span id="cb44-53"><a href="#cb44-53"></a>            <span class="fu">clang_visitChildren</span>(root, visitor[<span class="dv">0</span>], NULL);</span>
979 <span id="cb44-54"><a href="#cb44-54"></a></span>
980 <span id="cb44-55"><a href="#cb44-55"></a>            <span class="fu">clang_disposeTranslationUnit</span>(tu);</span>
981 <span id="cb44-56"><a href="#cb44-56"></a>            <span class="fu">clang_disposeIndex</span>(index);</span>
982 <span id="cb44-57"><a href="#cb44-57"></a>        }</span>
983 <span id="cb44-58"><a href="#cb44-58"></a>    }</span>
984 <span id="cb44-59"><a href="#cb44-59"></a>}</span></code></pre></div>
985 <h3 id="compiling-and-running-the-libclang-sample">Compiling and running the libclang sample</h3>
986 <div class="sourceCode" id="cb45"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb45-1"><a href="#cb45-1"></a></span>
987 <span id="cb45-2"><a href="#cb45-2"></a><span class="ex">java</span> --enable-native-access=ALL-UNNAMED \</span>
988 <span id="cb45-3"><a href="#cb45-3"></a>    -Djava.library.path=<span class="va">${LIBCLANG_HOME}</span>/lib \</span>
989 <span id="cb45-4"><a href="#cb45-4"></a>    --add-modules jdk.incubator.foreign \</span>
990 <span id="cb45-5"><a href="#cb45-5"></a>    ASTPrinter.java <span class="va">$*</span></span></code></pre></div>
991 </body>
992 </html>