Skip to content

Commit 5beedfc

Browse files
sgammondarvld
andcommitted
Add support for GraalVM
Adds a JAR publication at `jna-graalvm.jar`, with accompanying build infrastructure, which provides support for JNA within the context of the Substrate Virtual Machine (SVM). GraalVM Native Image targets use SVM instead of JVM at runtime. JNA's current strategy of unpacking libraries at runtime works under SVM, but is suboptimal; the binary is native, so it can simply include JNA object code for the current platform directly. To accomplish this, several GraalVM "feature" implementations are provided in this new publication. By default, regular JNA access is enabled through the `JavaNativeAccess` feature; this class enables reflection and runtime JNI configurations for downstream projects which use JNA. Another feature, `SubstrateStaticJNA`, is experimental because it relies on unstable GraalVM APIs, but instead of loading JNA at runtime from a dynamic library, it builds JNA into the final native image with a static object. These features are enabled through a resource within `META-INF`, called `native-image.properties`, which is picked up by the native image compiler at build time. The new artifact only needs to be present for GraalVM native targets at build time; otherwise, the classes and libraries in `jna-graalvm.jar` are inert. Includes tested support for: - macOS aarch64 - Linux amd64 - feat: add `jna-graalvm.jar` publication - feat: add base `JavaNativeAccess` feature for auto-config of JNA - feat: add initial implementation of `SubstrateStaticJNA` feature - test: sample/test gradle build for native image - chore: ci config to run native sample - chore: add readme to `lib/gvm` Signed-off-by: Sam Gammon <[email protected]> Signed-off-by: Dario Valdespino <[email protected]> Co-authored-by: Sam Gammon <[email protected]> Co-authored-by: Dario Valdespino <[email protected]>
1 parent 4f94c57 commit 5beedfc

22 files changed

+1685
-5
lines changed

.github/workflows/graalvm.yaml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# GraalVM build and native test.
2+
name: GraalVM CI
3+
4+
on:
5+
workflow_dispatch:
6+
workflow_call:
7+
pull_request:
8+
push:
9+
branches:
10+
- master
11+
12+
permissions:
13+
contents: read
14+
15+
env:
16+
ANT_OPTS: -Djava.security.manager=allow
17+
18+
jobs:
19+
build:
20+
runs-on: ubuntu-latest
21+
name: Test GVM 22, ubuntu-latest
22+
23+
steps:
24+
- uses: actions/checkout@v4
25+
- uses: graalvm/setup-graalvm@v1
26+
with:
27+
java-version: '22'
28+
distribution: 'graalvm-community'
29+
github-token: ${{ secrets.GITHUB_TOKEN }}
30+
- name: Linux requirements
31+
run: sudo apt-get -y install texinfo
32+
- uses: gradle/actions/setup-gradle@v3
33+
- name: "Build: Native Image"
34+
run: ant dist && ant install && ant nativeImage && ant nativeRun

build.xml

Lines changed: 209 additions & 4 deletions
Large diffs are not rendered by default.

common.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</or>
2020
</condition>
2121
<property name="jna.version" value="${jna.major}.${jna.minor}.${jna.revision}${version.suffix}"/>
22+
<property name="graalvm.version" value="24.0.1" />
2223
<property name="osgi.version" value="${jna.major}.${jna.minor}.${jna.revision}"/>
2324
<!-- jnidispatch library release version -->
2425
<property name="jni.major" value="7"/>

lib/gvm/AbstractJNAFeature.java

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved
2+
*
3+
* The contents of this file is dual-licensed under 2
4+
* alternative Open Source/Free licenses: LGPL 2.1 or later and
5+
* Apache License 2.0. (starting with JNA version 4.0.0).
6+
*
7+
* You can freely decide which license you want to apply to
8+
* the project.
9+
*
10+
* You may obtain a copy of the LGPL License at:
11+
*
12+
* http://www.gnu.org/licenses/licenses.html
13+
*
14+
* A copy is also included in the downloadable source code package
15+
* containing JNA, in file "LGPL2.1".
16+
*
17+
* You may obtain a copy of the Apache License at:
18+
*
19+
* http://www.apache.org/licenses/
20+
*
21+
* A copy is also included in the downloadable source code package
22+
* containing JNA, in file "AL2.0".
23+
*/
24+
package com.sun.jna;
25+
26+
import org.graalvm.nativeimage.hosted.Feature;
27+
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;
28+
import org.graalvm.nativeimage.hosted.RuntimeJNIAccess;
29+
import org.graalvm.nativeimage.hosted.RuntimeProxyCreation;
30+
import org.graalvm.nativeimage.hosted.RuntimeReflection;
31+
import org.graalvm.nativeimage.hosted.RuntimeResourceAccess;
32+
33+
import java.lang.reflect.Field;
34+
import java.lang.reflect.Method;
35+
import java.util.Arrays;
36+
37+
/**
38+
* Provides common logic for JNA-related GraalVM feature classes.
39+
*
40+
* <p>These classes should only be included at build time for a `native-image` target.</p>
41+
*
42+
* @since 5.15.0
43+
* @author Sam Gammon ([email protected])
44+
* @author Dario Valdespino ([email protected])
45+
*/
46+
abstract class AbstractJNAFeature implements Feature {
47+
/**
48+
* Obtain a reference to a method on a class, in order to register it for reflective access
49+
*
50+
* @param clazz Class to obtain method reference from
51+
* @param methodName Name of the method to obtain a reference to
52+
* @param args Method arguments
53+
* @return Method reference
54+
*/
55+
protected static Method method(Class<?> clazz, String methodName, Class<?>... args) {
56+
try {
57+
return clazz.getDeclaredMethod(methodName, args);
58+
} catch (NoSuchMethodException e) {
59+
throw new IllegalStateException(e);
60+
}
61+
}
62+
63+
/**
64+
* Obtain a reference to one or more fields on a class, in order to register them for reflective access
65+
*
66+
* @param clazz Class to obtain field references from
67+
* @param fieldNames Names of the fields to obtain references to
68+
* @return Field references
69+
*/
70+
protected static Field[] fields(Class<?> clazz, String... fieldNames) {
71+
try {
72+
Field[] fields = new Field[fieldNames.length];
73+
for (int i = 0; i < fieldNames.length; i++) {
74+
fields[i] = clazz.getDeclaredField(fieldNames[i]);
75+
}
76+
return fields;
77+
} catch (NoSuchFieldException e) {
78+
throw new IllegalStateException(e);
79+
}
80+
}
81+
82+
/**
83+
* Register a class for reflective access at runtime
84+
*
85+
* @param clazz Class to register
86+
*/
87+
protected static void reflectiveClass(Class<?>... clazz) {
88+
RuntimeReflection.register(clazz);
89+
}
90+
91+
/**
92+
* Register a resource for use in the final image
93+
*
94+
* @param module Module which owns the resource
95+
* @param resource Path to the resource to register
96+
*/
97+
protected static void registerResource(Module module, String resource) {
98+
RuntimeResourceAccess.addResource(module, resource);
99+
}
100+
101+
/**
102+
* Register a resource for use in the final image
103+
*
104+
* @param resource Path to the resource to register
105+
*/
106+
protected static void registerResource(String resource) {
107+
registerResource(AbstractJNAFeature.class.getModule(), resource);
108+
}
109+
110+
/**
111+
* Register a class for JNI access at runtime
112+
*
113+
* @param clazz Class to register
114+
*/
115+
protected static void registerJniClass(Class<?> clazz) {
116+
RuntimeJNIAccess.register(clazz);
117+
Arrays.stream(clazz.getConstructors()).forEach(RuntimeJNIAccess::register);
118+
Arrays.stream(clazz.getMethods()).forEach(RuntimeJNIAccess::register);
119+
}
120+
121+
/**
122+
* Register a class for JNI access at runtime, potentially with reflective access as well
123+
*
124+
* @param clazz Class to register
125+
* @param reflective Whether to register the class and constructors for reflective access
126+
*/
127+
protected static void registerJniClass(Class<?> clazz, Boolean reflective) {
128+
registerJniClass(clazz);
129+
if (reflective) {
130+
RuntimeReflection.register(clazz);
131+
RuntimeReflection.registerAllConstructors(clazz);
132+
}
133+
}
134+
135+
/**
136+
* Register a suite of JNA methods for use at runtime
137+
*
138+
* @param reflective Whether to register the methods for reflective access
139+
* @param methods Methods to register
140+
*/
141+
protected static void registerJniMethods(Boolean reflective, Method... methods) {
142+
RuntimeJNIAccess.register(methods);
143+
if (reflective) {
144+
RuntimeReflection.register(methods);
145+
}
146+
}
147+
148+
/**
149+
* Register a suite of JNA methods for use at runtime
150+
*
151+
* @param methods Methods to register
152+
*/
153+
protected static void registerJniMethods(Method... methods) {
154+
registerJniMethods(false, methods);
155+
}
156+
157+
/**
158+
* Register a suite of JNA fields for use at runtime
159+
*
160+
* @param reflective Whether to register the fields for reflective access
161+
* @param fields Fields to register
162+
*/
163+
protected static void registerJniFields(Boolean reflective, Field[] fields) {
164+
RuntimeJNIAccess.register(fields);
165+
if (reflective) {
166+
RuntimeReflection.register(fields);
167+
}
168+
}
169+
170+
/**
171+
* Register a suite of JNA fields for use at runtime
172+
*
173+
* @param fields Fields to register
174+
*/
175+
protected static void registerJniFields(Field[] fields) {
176+
registerJniFields(false, fields);
177+
}
178+
179+
/**
180+
* Register a combination of interfaces used at runtime as a dynamic proxy object
181+
*
182+
* @param classes Combination of interface classes; order matters
183+
*/
184+
protected static void registerProxyInterfaces(Class<?>... classes) {
185+
RuntimeProxyCreation.register(classes);
186+
}
187+
188+
/**
189+
* Assign the specified class or classes to initialize at image build time
190+
*
191+
* @param clazz Classes to register for build-time initialization
192+
*/
193+
protected static void initializeAtBuildTime(Class<?>... clazz) {
194+
for (Class<?> c : clazz) {
195+
RuntimeClassInitialization.initializeAtBuildTime(c);
196+
}
197+
}
198+
199+
/**
200+
* Assign the specified class or classes to initialize at image run-time
201+
*
202+
* @param clazz Classes to register for run-time initialization
203+
*/
204+
protected static void initializeAtRunTime(Class<?>... clazz) {
205+
for (Class<?> c : clazz) {
206+
RuntimeClassInitialization.initializeAtRunTime(c);
207+
}
208+
}
209+
}

0 commit comments

Comments
 (0)