Skip to content

Commit 8978b9b

Browse files
committed
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 Signed-off-by: Sam Gammon <[email protected]>
1 parent 4f94c57 commit 8978b9b

21 files changed

+1402
-5
lines changed

.github/workflows/graalvm.yaml

+34
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

+213-4
Large diffs are not rendered by default.

common.xml

+1
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

+202
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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+
// Provides common logic for JNA-related GraalVM feature classes. These classes should only be included
38+
// at build time for a `native-image` target.
39+
abstract class AbstractJNAFeature implements Feature {
40+
/**
41+
* Obtain a reference to a method on a class, in order to register it for reflective access
42+
*
43+
* @param clazz Class to obtain method reference from
44+
* @param methodName Name of the method to obtain a reference to
45+
* @param args Method arguments
46+
* @return Method reference
47+
*/
48+
protected static Method method(Class<?> clazz, String methodName, Class<?>... args) {
49+
try {
50+
return clazz.getDeclaredMethod(methodName, args);
51+
} catch (NoSuchMethodException e) {
52+
throw new IllegalStateException(e);
53+
}
54+
}
55+
56+
/**
57+
* Obtain a reference to one or more fields on a class, in order to register them for reflective access
58+
*
59+
* @param clazz Class to obtain field references from
60+
* @param fieldNames Names of the fields to obtain references to
61+
* @return Field references
62+
*/
63+
protected static Field[] fields(Class<?> clazz, String... fieldNames) {
64+
try {
65+
Field[] fields = new Field[fieldNames.length];
66+
for (int i = 0; i < fieldNames.length; i++) {
67+
fields[i] = clazz.getDeclaredField(fieldNames[i]);
68+
}
69+
return fields;
70+
} catch (NoSuchFieldException e) {
71+
throw new IllegalStateException(e);
72+
}
73+
}
74+
75+
/**
76+
* Register a class for reflective access at runtime
77+
*
78+
* @param clazz Class to register
79+
*/
80+
protected static void reflectiveClass(Class<?>... clazz) {
81+
RuntimeReflection.register(clazz);
82+
}
83+
84+
/**
85+
* Register a resource for use in the final image
86+
*
87+
* @param module Module which owns the resource
88+
* @param resource Path to the resource to register
89+
*/
90+
protected static void registerResource(Module module, String resource) {
91+
RuntimeResourceAccess.addResource(module, resource);
92+
}
93+
94+
/**
95+
* Register a resource for use in the final image
96+
*
97+
* @param resource Path to the resource to register
98+
*/
99+
protected static void registerResource(String resource) {
100+
registerResource(AbstractJNAFeature.class.getModule(), resource);
101+
}
102+
103+
/**
104+
* Register a class for JNI access at runtime
105+
*
106+
* @param clazz Class to register
107+
*/
108+
protected static void registerJniClass(Class<?> clazz) {
109+
RuntimeJNIAccess.register(clazz);
110+
Arrays.stream(clazz.getConstructors()).forEach(RuntimeJNIAccess::register);
111+
Arrays.stream(clazz.getMethods()).forEach(RuntimeJNIAccess::register);
112+
}
113+
114+
/**
115+
* Register a class for JNI access at runtime, potentially with reflective access as well
116+
*
117+
* @param clazz Class to register
118+
* @param reflective Whether to register the class and constructors for reflective access
119+
*/
120+
protected static void registerJniClass(Class<?> clazz, Boolean reflective) {
121+
registerJniClass(clazz);
122+
if (reflective) {
123+
RuntimeReflection.register(clazz);
124+
RuntimeReflection.registerAllConstructors(clazz);
125+
}
126+
}
127+
128+
/**
129+
* Register a suite of JNA methods for use at runtime
130+
*
131+
* @param reflective Whether to register the methods for reflective access
132+
* @param methods Methods to register
133+
*/
134+
protected static void registerJniMethods(Boolean reflective, Method... methods) {
135+
RuntimeJNIAccess.register(methods);
136+
if (reflective) {
137+
RuntimeReflection.register(methods);
138+
}
139+
}
140+
141+
/**
142+
* Register a suite of JNA methods for use at runtime
143+
*
144+
* @param methods Methods to register
145+
*/
146+
protected static void registerJniMethods(Method... methods) {
147+
registerJniMethods(false, methods);
148+
}
149+
150+
/**
151+
* Register a suite of JNA fields for use at runtime
152+
*
153+
* @param reflective Whether to register the fields for reflective access
154+
* @param fields Fields to register
155+
*/
156+
protected static void registerJniFields(Boolean reflective, Field[] fields) {
157+
RuntimeJNIAccess.register(fields);
158+
if (reflective) {
159+
RuntimeReflection.register(fields);
160+
}
161+
}
162+
163+
/**
164+
* Register a suite of JNA fields for use at runtime
165+
*
166+
* @param fields Fields to register
167+
*/
168+
protected static void registerJniFields(Field[] fields) {
169+
registerJniFields(false, fields);
170+
}
171+
172+
/**
173+
* Register a combination of interfaces used at runtime as a dynamic proxy object
174+
*
175+
* @param classes Combination of interface classes; order matters
176+
*/
177+
protected static void registerProxyInterfaces(Class<?>... classes) {
178+
RuntimeProxyCreation.register(classes);
179+
}
180+
181+
/**
182+
* Assign the specified class or classes to initialize at image build time
183+
*
184+
* @param clazz Classes to register for build-time initialization
185+
*/
186+
protected static void initializeAtBuildTime(Class<?>... clazz) {
187+
for (Class<?> c : clazz) {
188+
RuntimeClassInitialization.initializeAtBuildTime(c);
189+
}
190+
}
191+
192+
/**
193+
* Assign the specified class or classes to initialize at image run-time
194+
*
195+
* @param clazz Classes to register for run-time initialization
196+
*/
197+
protected static void initializeAtRunTime(Class<?>... clazz) {
198+
for (Class<?> c : clazz) {
199+
RuntimeClassInitialization.initializeAtRunTime(c);
200+
}
201+
}
202+
}

0 commit comments

Comments
 (0)