forked from java-native-access/jna
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support JNA over Static JNI on GraalVM
Implements a new optional linkage feature, called Static JNI, under GraalVM Native Image. With `com.sun.jna.SubstrateStaticJNA` enabled (opt-in), JNA is loaded eagerly at image build time, and then linked against a static copy of `libjnidispatch` at image link-time. The result is that `libjnidispatch.a` is embedded within the final image. No precursor library unpacking step is necessary before using JNA in this circumstance, because JNA's native layer is built directly into the image itself. - feat: implement static jni feature - chore: full gvm ci build - chore: add static jni sample Signed-off-by: Sam Gammon <sam@elide.ventures> Signed-off-by: Dario Valdespino <dario@elide.ventures> Co-authored-by: Sam Gammon <sam@elide.ventures> Co-authored-by: Dario Valdespino <dario@elide.ventures>
- Loading branch information
Showing
17 changed files
with
838 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/.gradle | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# JNA Sample: GraalVM Native Image (Static) | ||
|
||
This directory contains a sample Gradle project which uses JNA with [GraalVM](https://graalvm.org/). The project builds a | ||
[native image](https://www.graalvm.org/latest/reference-manual/native-image/) which uses JNA features, powered by JNA's integration library for Substrate. | ||
|
||
This sample leverages [Static JNI](https://www.blog.akhil.cc/static-jni) to build JNA and JNA-related user code | ||
directly into the native image. | ||
|
||
Using this technique can optimize startup time and other performance factors, because no dynamic library unpack-and-load | ||
step is required to use JNA. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* Copyright (c) 2015 Adam Marcionek, All Rights Reserved | ||
* | ||
* The contents of this file is dual-licensed under 2 | ||
* alternative Open Source/Free licenses: LGPL 2.1 or later and | ||
* Apache License 2.0. (starting with JNA version 4.0.0). | ||
* | ||
* You can freely decide which license you want to apply to | ||
* the project. | ||
* | ||
* You may obtain a copy of the LGPL License at: | ||
* | ||
* http://www.gnu.org/licenses/licenses.html | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "LGPL2.1". | ||
* | ||
* You may obtain a copy of the Apache License at: | ||
* | ||
* http://www.apache.org/licenses/ | ||
* | ||
* A copy is also included in the downloadable source code package | ||
* containing JNA, in file "AL2.0". | ||
*/ | ||
plugins { | ||
java | ||
application | ||
alias(libs.plugins.graalvm) | ||
} | ||
|
||
application { | ||
mainClass = "com.example.JnaNative" | ||
} | ||
|
||
java { | ||
toolchain { | ||
languageVersion = JavaLanguageVersion.of(22) | ||
vendor = JvmVendorSpec.GRAAL_VM | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation(libs.bundles.jna) | ||
implementation(libs.bundles.graalvm.api) | ||
nativeImageClasspath(libs.jna.graalvm) | ||
} | ||
|
||
val nativeImageDebug: String by properties | ||
|
||
graalvmNative { | ||
testSupport = true | ||
toolchainDetection = false | ||
|
||
binaries { | ||
named("main") { | ||
buildArgs.addAll(listOf( | ||
"--features=com.sun.jna.SubstrateStaticJNA", | ||
).plus(if (nativeImageDebug != "true") emptyList() else listOf( | ||
"--verbose", | ||
"--debug-attach", | ||
"-J-Xlog:library=info", | ||
"-H:+UnlockExperimentalVMOptions", | ||
"-H:+JNIEnhancedErrorCodes", | ||
"-H:+SourceLevelDebug", | ||
"-H:-DeleteLocalSymbols", | ||
"-H:-RemoveUnusedSymbols", | ||
"-H:+PreserveFramePointer", | ||
"-H:+ReportExceptionStackTraces", | ||
"-H:CCompilerOption=-v", | ||
"-H:NativeLinkerOption=-v", | ||
))) | ||
} | ||
} | ||
} | ||
|
||
// Allow the outer Ant build to override the version of JNA or GraalVM. | ||
// These properties are used in JNA's CI and don't need to be in projects that use JNA. | ||
|
||
val jnaVersion: String by properties | ||
val graalvmVersion: String by properties | ||
val overrides = jnaVersion.isNotBlank() || graalvmVersion.isNotBlank() | ||
|
||
if (overrides) configurations.all { | ||
resolutionStrategy.eachDependency { | ||
if (requested.group == "net.java.dev.jna") { | ||
useVersion(jnaVersion) | ||
because("overridden by ant build") | ||
} | ||
if (requested.group == "org.graalvm") { | ||
useVersion(graalvmVersion) | ||
because("overridden by ant build") | ||
} | ||
} | ||
} |
Oops, something went wrong.