Skip to content

Commit

Permalink
Only export modules to native-image when using GraalVM >= 22.2
Browse files Browse the repository at this point in the history
  • Loading branch information
zakkak committed Jun 15, 2022
1 parent 2e80961 commit bd4ff77
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public class NativeImageBuildStep {
private static final String MOVED_TRUST_STORE_NAME = "trustStore";
public static final String APP_SOURCES = "app-sources";

@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
@BuildStep(onlyIf = NativeOrNativeSourcesBuildGraal22_2OrLater.class)
void addExportsToNativeImage(BuildProducer<JPMSExportBuildItem> exports) {
// Needed by io.quarkus.runtime.ResourceHelper.registerResources
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.builder", "com.oracle.svm.core.jdk"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ public boolean getAsBoolean() {
return packageConfig.type.equalsIgnoreCase(PackageConfig.NATIVE)
|| packageConfig.type.equalsIgnoreCase(PackageConfig.NATIVE_SOURCES);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.quarkus.deployment.pkg.steps;

import java.util.function.BooleanSupplier;

import org.graalvm.home.Version;

import io.quarkus.deployment.pkg.PackageConfig;

/**
* Supplier that can be used to only run build steps in the
* native or native sources builds when using Graal >= 22.2.
* Most build steps that need to be run conditionally should use this instead of {@link NativeBuild}.
*/
public class NativeOrNativeSourcesBuildGraal22_2OrLater implements BooleanSupplier {

private final PackageConfig packageConfig;

NativeOrNativeSourcesBuildGraal22_2OrLater(PackageConfig packageConfig) {
this.packageConfig = packageConfig;
}

@Override
public boolean getAsBoolean() {
return (packageConfig.type.equalsIgnoreCase(PackageConfig.NATIVE)
|| packageConfig.type.equalsIgnoreCase(PackageConfig.NATIVE_SOURCES))
&& (Version.getCurrent().isSnapshot() || Version.getCurrent().compareTo(22, 2) >= 0);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.builditem.nativeimage.UnsafeAccessedFieldBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuildGraal22_2OrLater;
import io.quarkus.gizmo.AssignableResultHandle;
import io.quarkus.gizmo.CatchBlockCreator;
import io.quarkus.gizmo.ClassCreator;
Expand Down Expand Up @@ -117,7 +118,7 @@ GeneratedResourceBuildItem generateNativeResourcesList(List<NativeImageResourceB
sb.toString().getBytes(StandardCharsets.UTF_8));
}

@BuildStep
@BuildStep(onlyIf = NativeOrNativeSourcesBuildGraal22_2OrLater.class)
JPMSExportBuildItem addExportsToNativeImage(List<JniRuntimeAccessBuildItem> jniRuntimeAccessibleClasses) {
// required in order to access com.oracle.svm.core.jni.JNIRuntimeAccess
if (jniRuntimeAccessibleClasses != null && !jniRuntimeAccessibleClasses.isEmpty()) {
Expand All @@ -126,6 +127,10 @@ JPMSExportBuildItem addExportsToNativeImage(List<JniRuntimeAccessBuildItem> jniR
return null;
}

private boolean graalVM22_2OrLater() {
return Version.getCurrent().isSnapshot() || Version.getCurrent().compareTo(22, 2) >= 0;
}

@BuildStep
void generateFeature(BuildProducer<GeneratedNativeImageClassBuildItem> nativeImageClass,
BuildProducer<JPMSExportBuildItem> exports,
Expand Down Expand Up @@ -250,8 +255,10 @@ public void write(String s, byte[] bytes) {
}

if (!proxies.isEmpty()) {
// Needed to access DYNAMIC_PROXY_REGISTRY
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.builder", "com.oracle.svm.core.jdk.proxy"));
if (graalVM22_2OrLater()) {
// Needed to access DYNAMIC_PROXY_REGISTRY
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.builder", "com.oracle.svm.core.jdk.proxy"));
}

ResultHandle proxySupportClass = overallCatch.loadClassFromTCCL(DYNAMIC_PROXY_REGISTRY);
ResultHandle proxySupport = overallCatch.invokeStaticMethod(
Expand All @@ -273,8 +280,10 @@ public void write(String s, byte[] bytes) {

/* Resource includes and excludes */
if (!resourcePatterns.isEmpty()) {
// Needed to access LOOKUP_METHOD
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.base", "com.oracle.svm.util"));
if (graalVM22_2OrLater()) {
// Needed to access LOOKUP_METHOD
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.base", "com.oracle.svm.util"));
}

ResultHandle resourcesRegistrySingleton = overallCatch.invokeStaticMethod(IMAGE_SINGLETONS_LOOKUP,
overallCatch.loadClassFromTCCL("com.oracle.svm.core.configure.ResourcesRegistry"));
Expand Down Expand Up @@ -333,8 +342,11 @@ public void write(String s, byte[] bytes) {
}

if (!resourceBundles.isEmpty()) {
// Needed to access LOCALIZATION_FEATURE
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.builder", "com.oracle.svm.core.jdk.localization"));
if (graalVM22_2OrLater()) {
// Needed to access LOCALIZATION_FEATURE
exports.produce(
new JPMSExportBuildItem("org.graalvm.nativeimage.builder", "com.oracle.svm.core.jdk.localization"));
}

AssignableResultHandle registerMethod = overallCatch.createVariable(Method.class);
AssignableResultHandle locClass = overallCatch.createVariable(Class.class);
Expand Down Expand Up @@ -494,8 +506,10 @@ public void write(String s, byte[] bytes) {

if (entry.getValue().serialization) {
if (registerSerializationMethod == null) {
// Needed by createRegisterSerializationForClassMethod to access LOOKUP_METHOD
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.base", "com.oracle.svm.util"));
if (graalVM22_2OrLater()) {
// Needed by createRegisterSerializationForClassMethod to access LOOKUP_METHOD
exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.base", "com.oracle.svm.util"));
}
registerSerializationMethod = createRegisterSerializationForClassMethod(file);
}

Expand Down

0 comments on commit bd4ff77

Please sign in to comment.