From bd4ff77e1b34a6e3639d8f4675a26209c5486651 Mon Sep 17 00:00:00 2001 From: Foivos Zakkak Date: Wed, 15 Jun 2022 16:47:40 +0300 Subject: [PATCH] Only export modules to native-image when using GraalVM >= 22.2 --- .../pkg/steps/NativeImageBuildStep.java | 2 +- .../pkg/steps/NativeOrNativeSourcesBuild.java | 1 + ...eOrNativeSourcesBuildGraal22_2OrLater.java | 29 +++++++++++++++++ .../steps/NativeImageAutoFeatureStep.java | 32 +++++++++++++------ 4 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuildGraal22_2OrLater.java diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java index 0a49733f11667..4dcec2756db51 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeImageBuildStep.java @@ -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 exports) { // Needed by io.quarkus.runtime.ResourceHelper.registerResources exports.produce(new JPMSExportBuildItem("org.graalvm.nativeimage.builder", "com.oracle.svm.core.jdk")); diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuild.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuild.java index bdfec2efa7a96..2d2027990eb6c 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuild.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuild.java @@ -22,4 +22,5 @@ public boolean getAsBoolean() { return packageConfig.type.equalsIgnoreCase(PackageConfig.NATIVE) || packageConfig.type.equalsIgnoreCase(PackageConfig.NATIVE_SOURCES); } + } diff --git a/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuildGraal22_2OrLater.java b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuildGraal22_2OrLater.java new file mode 100644 index 0000000000000..2a79f06a28b99 --- /dev/null +++ b/core/deployment/src/main/java/io/quarkus/deployment/pkg/steps/NativeOrNativeSourcesBuildGraal22_2OrLater.java @@ -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); + } + +} diff --git a/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java b/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java index 5cb6eaa7eedd6..f5bb2cfd20c70 100644 --- a/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java +++ b/core/deployment/src/main/java/io/quarkus/deployment/steps/NativeImageAutoFeatureStep.java @@ -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; @@ -117,7 +118,7 @@ GeneratedResourceBuildItem generateNativeResourcesList(List jniRuntimeAccessibleClasses) { // required in order to access com.oracle.svm.core.jni.JNIRuntimeAccess if (jniRuntimeAccessibleClasses != null && !jniRuntimeAccessibleClasses.isEmpty()) { @@ -126,6 +127,10 @@ JPMSExportBuildItem addExportsToNativeImage(List jniR return null; } + private boolean graalVM22_2OrLater() { + return Version.getCurrent().isSnapshot() || Version.getCurrent().compareTo(22, 2) >= 0; + } + @BuildStep void generateFeature(BuildProducer nativeImageClass, BuildProducer exports, @@ -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( @@ -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")); @@ -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); @@ -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); }