Skip to content

Sample code for running jextract over native libraries' headers for C, OpenGL, Python.

Notifications You must be signed in to change notification settings

ammbra/treasures

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

From native code gems to Java treasures

Prerequisites

You need the following components to run the samples provided here:

How to use the sample with C code

  1. Obtain Java bindings to C by running in a terminal window:
# macOS compatible command
export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
jextract --output src -t org.unix -I $C_INCLUDE_PATH $C_INCLUDE_PATH/time.h

# Linux
export C_INCLUDE_PATH=/usr/include/
jextract --output src -t org.unix -I $C_INCLUDE_PATH $C_INCLUDE_PATH/time.h
  1. Run TimeTranslator.java with the following command:
# macOS and Linux compatible command
java --enable-native-access=ALL-UNNAMED --enable-preview --source 23 src/TimeTranslator.java

How to use the sample with Python code

  1. Obtain Java bindings to Python by running in a terminal window:
# macOS and Linux compatible command
jextract --output src \                                                                  
    -l :/Library/Frameworks/Python.framework/Versions/3.11/lib/libpython3.11.dylib \
    -I $/Library/Frameworks/Python.framework/Versions/3.11/include/python3.11 \
    -t org.python \
    /Library/Frameworks/Python.framework/Versions/3.11/include/python3.11/Python.h
  1. Run PyDukerMaker.java with the following command:
# macOS and Linux compatible command
java --enable-native-access=ALL-UNNAMED --enable-preview --source 23 src/PyDukeMaker.java

How to use the sample for OpenGL

In order to run successfully, the following commands need also the presence of compile_flags.txt.

  1. Dump of all the symbols encountered in a header file:
# macOS and Linux compatible command
jextract --output src \
    -l :/System/Library/Frameworks/GLUT.framework/GLUT \
    -I /opt/homebrew/Cellar/freeglut \
    -I /opt/homebrew/Cellar/mesa \
    -I /opt/homebrew/Cellar/mesa-glu \
    -t org.freeglut \
    --dump-includes glut.symbols \
    /opt/homebrew/Cellar/freeglut/3.6.0/include/GL/freeglut.h
  1. Obtain Java bindings to OpenGL by running the filtering file:
# macOS and Linux compatible command
jextract --output src \
    -l :/System/Library/Frameworks/GLUT.framework/GLUT \
    -I /opt/homebrew/Cellar/freeglut \
    -I /opt/homebrew/Cellar/mesa \
    -I /opt/homebrew/Cellar/mesa-glu \
    -t org.freeglut @glut.symbols \
    /opt/homebrew/Cellar/freeglut/3.6.0/include/GL/freeglut.h
  1. Run Teapot.java with:
java -XstartOnFirstThread --enable-native-access=ALL-UNNAMED src/Teapot.java

When debugging an application is useful to inspect the parameters passed to a native call. Code generated by jextract supports tracing of native calls, meaning parameters passed to native calls can be printed on the standard output.

To enable the tracing support, just pass the -Djextract.trace.downcalls=true flag as a VM argument when launching your application:

# macOS and Linux compatible command
java -XstartOnFirstThread -Djextract.trace.downcalls=true --enable-native-access=ALL-UNNAMED src/Teapot.java

How to run the sample with Rust

This example was created by Jorn Vernee and you can find out all about it by reading his blog post https://jornvernee.github.io/java/panama/rust/panama-ffi/2021/09/03/rust-panama-helloworld.html.

  1. Obtain Java bindings to Rust by running in a terminal window:
jextract --output src -t org.rust --include-function hello_world \
  -l :./rust-panama-helloworld/target/debug/librust_panama_helloworld.dylib \
  rust-panama-helloworld/lib.h 
  1. Run SimpleRust.java by using the following command:
java --enable-native-access=ALL-UNNAMED --enable-preview --source 23 \
  -Djava.library.path=./rust-panama-helloworld/target/debug \
  src/SimpleRust.java

About

Sample code for running jextract over native libraries' headers for C, OpenGL, Python.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages