Skip to content

Commit

Permalink
Proof of concept JEP 472 support
Browse files Browse the repository at this point in the history
Relates to quarkusio#44257
  • Loading branch information
zakkak committed Nov 1, 2024
1 parent 8077229 commit 50476a7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.quarkus.deployment.builditem;

import io.quarkus.builder.item.SimpleBuildItem;

public final class NativeAccessBuildItem extends SimpleBuildItem {
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import io.quarkus.deployment.builditem.GeneratedNativeImageClassBuildItem;
import io.quarkus.deployment.builditem.GeneratedResourceBuildItem;
import io.quarkus.deployment.builditem.MainClassBuildItem;
import io.quarkus.deployment.builditem.NativeAccessBuildItem;
import io.quarkus.deployment.builditem.QuarkusBuildCloseablesBuildItem;
import io.quarkus.deployment.builditem.TransformedClassesBuildItem;
import io.quarkus.deployment.configuration.ClassLoadingConfig;
Expand Down Expand Up @@ -203,7 +204,9 @@ public JarBuildItem buildRunnerJar(CurateOutcomeBuildItem curateOutcomeBuildItem
List<LegacyJarRequiredBuildItem> legacyJarRequired,
QuarkusBuildCloseablesBuildItem closeablesBuildItem,
List<AdditionalApplicationArchiveBuildItem> additionalApplicationArchiveBuildItems,
MainClassBuildItem mainClassBuildItem, Optional<AppCDSRequestedBuildItem> appCDS) throws Exception {
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess,
Optional<AppCDSRequestedBuildItem> appCDS) throws Exception {

if (appCDS.isPresent()) {
handleAppCDSSupportFileGeneration(transformedClasses, generatedClasses, appCDS.get());
Expand All @@ -218,17 +221,17 @@ public JarBuildItem buildRunnerJar(CurateOutcomeBuildItem curateOutcomeBuildItem
|| packageConfig.jar().type() == UBER_JAR)) {
return buildUberJar(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses, applicationArchivesBuildItem,
packageConfig, applicationInfo, generatedClasses, generatedResources, uberJarMergedResourceBuildItems,
uberJarIgnoredResourceBuildItems, mainClassBuildItem, classLoadingConfig);
uberJarIgnoredResourceBuildItems, mainClassBuildItem, nativeAccess, classLoadingConfig);
} else if (!legacyJarRequired.isEmpty() || packageConfig.jar().type() == LEGACY_JAR) {
return buildLegacyThinJar(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses,
applicationArchivesBuildItem,
packageConfig, applicationInfo, generatedClasses, generatedResources, mainClassBuildItem,
classLoadingConfig);
nativeAccess, classLoadingConfig);
} else {
return buildThinJar(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses, applicationArchivesBuildItem,
packageConfig, classLoadingConfig, applicationInfo, generatedClasses,
generatedResources,
additionalApplicationArchiveBuildItems, mainClassBuildItem);
additionalApplicationArchiveBuildItems, mainClassBuildItem, nativeAccess);
}
}

Expand Down Expand Up @@ -271,6 +274,7 @@ private JarBuildItem buildUberJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
List<UberJarMergedResourceBuildItem> mergeResources,
List<UberJarIgnoredResourceBuildItem> ignoredResources,
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess,
ClassLoadingConfig classLoadingConfig) throws Exception {

//we use the -runner jar name, unless we are building both types
Expand Down Expand Up @@ -300,6 +304,7 @@ private JarBuildItem buildUberJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
mergeResources,
ignoredResources,
mainClassBuildItem,
nativeAccess,
classLoadingConfig,
tmpRunnerJar);

Expand Down Expand Up @@ -355,6 +360,7 @@ private void buildUberJar0(CurateOutcomeBuildItem curateOutcomeBuildItem,
List<UberJarMergedResourceBuildItem> mergedResources,
List<UberJarIgnoredResourceBuildItem> ignoredResources,
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess,
ClassLoadingConfig classLoadingConfig,
Path runnerJar) throws Exception {
try (FileSystem runnerZipFs = createNewZip(runnerJar, packageConfig)) {
Expand Down Expand Up @@ -387,7 +393,7 @@ public boolean test(String path) {
// the manifest needs to be the first entry in the jar, otherwise JarInputStream does not work properly
// see https://bugs.openjdk.java.net/browse/JDK-8031748
generateManifest(runnerZipFs, "", packageConfig, appArtifact, mainClassBuildItem.getClassName(),
applicationInfo);
nativeAccess, applicationInfo);

for (ResolvedDependency appDep : curateOutcomeBuildItem.getApplicationModel().getRuntimeDependencies()) {

Expand Down Expand Up @@ -548,6 +554,7 @@ private JarBuildItem buildLegacyThinJar(CurateOutcomeBuildItem curateOutcomeBuil
List<GeneratedClassBuildItem> generatedClasses,
List<GeneratedResourceBuildItem> generatedResources,
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess,
ClassLoadingConfig classLoadingConfig) throws Exception {

Path runnerJar = outputTargetBuildItem.getOutputDirectory()
Expand All @@ -563,7 +570,7 @@ private JarBuildItem buildLegacyThinJar(CurateOutcomeBuildItem curateOutcomeBuil
doLegacyThinJarGeneration(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses,
applicationArchivesBuildItem, applicationInfo,
packageConfig, generatedResources, libDir, generatedClasses, runnerZipFs, mainClassBuildItem,
classLoadingConfig);
nativeAccess, classLoadingConfig);
}
runnerJar.toFile().setReadable(true, false);

Expand All @@ -582,7 +589,8 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
List<GeneratedClassBuildItem> generatedClasses,
List<GeneratedResourceBuildItem> generatedResources,
List<AdditionalApplicationArchiveBuildItem> additionalApplicationArchiveBuildItems,
MainClassBuildItem mainClassBuildItem) throws Exception {
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess) throws Exception {

boolean rebuild = outputTargetBuildItem.isRebuild();

Expand Down Expand Up @@ -798,7 +806,7 @@ private JarBuildItem buildThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
try (FileSystem runnerZipFs = createNewZip(initJar, packageConfig)) {
ResolvedDependency appArtifact = curateOutcomeBuildItem.getApplicationModel().getAppArtifact();
generateManifest(runnerZipFs, classPath.toString(), packageConfig, appArtifact,
QuarkusEntryPoint.class.getName(),
QuarkusEntryPoint.class.getName(), nativeAccess,
applicationInfo);
}

Expand Down Expand Up @@ -1010,6 +1018,7 @@ public NativeImageSourceJarBuildItem buildNativeImageJar(CurateOutcomeBuildItem
List<GeneratedNativeImageClassBuildItem> nativeImageResources,
List<GeneratedResourceBuildItem> generatedResources,
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess,
ClassLoadingConfig classLoadingConfig) throws Exception {
Path targetDirectory = outputTargetBuildItem.getOutputDirectory()
.resolve(outputTargetBuildItem.getBaseName() + "-native-image-source-jar");
Expand All @@ -1022,8 +1031,8 @@ public NativeImageSourceJarBuildItem buildNativeImageJar(CurateOutcomeBuildItem

return buildNativeImageThinJar(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses,
applicationArchivesBuildItem,
applicationInfo, packageConfig, allClasses, generatedResources, mainClassBuildItem, targetDirectory,
classLoadingConfig);
applicationInfo, packageConfig, allClasses, generatedResources, mainClassBuildItem, nativeAccess,
targetDirectory, classLoadingConfig);
}

private NativeImageSourceJarBuildItem buildNativeImageThinJar(CurateOutcomeBuildItem curateOutcomeBuildItem,
Expand All @@ -1035,6 +1044,7 @@ private NativeImageSourceJarBuildItem buildNativeImageThinJar(CurateOutcomeBuild
List<GeneratedClassBuildItem> allClasses,
List<GeneratedResourceBuildItem> generatedResources,
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess,
Path targetDirectory,
ClassLoadingConfig classLoadingConfig) throws Exception {
copyJsonConfigFiles(applicationArchivesBuildItem, targetDirectory);
Expand Down Expand Up @@ -1069,7 +1079,7 @@ private NativeImageSourceJarBuildItem buildNativeImageThinJar(CurateOutcomeBuild

doLegacyThinJarGeneration(curateOutcomeBuildItem, outputTargetBuildItem, transformedClasses,
applicationArchivesBuildItem, applicationInfo, packageConfig, generatedResources, libDir, allClasses,
runnerZipFs, mainClassBuildItem, classLoadingConfig);
runnerZipFs, mainClassBuildItem, nativeAccess, classLoadingConfig);
}
runnerJar.toFile().setReadable(true, false);
return new NativeImageSourceJarBuildItem(runnerJar, libDir);
Expand Down Expand Up @@ -1113,6 +1123,7 @@ private void doLegacyThinJarGeneration(CurateOutcomeBuildItem curateOutcomeBuild
List<GeneratedClassBuildItem> allClasses,
FileSystem runnerZipFs,
MainClassBuildItem mainClassBuildItem,
Optional<NativeAccessBuildItem> nativeAccess,
ClassLoadingConfig classLoadingConfig)
throws IOException {
final Map<String, String> seen = new HashMap<>();
Expand All @@ -1132,7 +1143,7 @@ private void doLegacyThinJarGeneration(CurateOutcomeBuildItem curateOutcomeBuild
// the manifest needs to be the first entry in the jar, otherwise JarInputStream does not work properly
// see https://bugs.openjdk.java.net/browse/JDK-8031748
generateManifest(runnerZipFs, classPath.toString(), packageConfig, appArtifact, mainClassBuildItem.getClassName(),
applicationInfo);
nativeAccess, applicationInfo);

copyCommonContent(runnerZipFs, services, applicationArchivesBuildItem, transformedClasses, allClasses,
generatedResources, seen, ignoredEntriesPredicate);
Expand Down Expand Up @@ -1350,6 +1361,7 @@ private static boolean isSignatureFile(String entry) {
private void generateManifest(FileSystem runnerZipFs, final String classPath, PackageConfig config,
ResolvedDependency appArtifact,
String mainClassName,
Optional<NativeAccessBuildItem> nativeAccess,
ApplicationInfoBuildItem applicationInfo)
throws IOException {
final Path manifestPath = runnerZipFs.getPath("META-INF", "MANIFEST.MF");
Expand Down Expand Up @@ -1402,6 +1414,9 @@ private void generateManifest(FileSystem runnerZipFs, final String classPath, Pa
attribs.putValue(entry.getKey(), entry.getValue());
}
}
if (nativeAccess.isPresent()) {
attributes.putValue("Enable-Native-Access", "ALL-UNNAMED");
}
try (final OutputStream os = Files.newOutputStream(manifestPath)) {
manifest.write(os);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import io.quarkus.deployment.builditem.ExecutorBuildItem;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.LogCategoryBuildItem;
import io.quarkus.deployment.builditem.NativeAccessBuildItem;
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigBuilderBuildItem;
import io.quarkus.deployment.builditem.ServiceStartBuildItem;
Expand Down Expand Up @@ -563,4 +564,13 @@ NativeImageFeatureBuildItem Brotli4jFeature(HttpBuildTimeConfig httpBuildTimeCon
}
return null;
}

@BuildStep
NativeAccessBuildItem brotli4jNativeAccess(HttpBuildTimeConfig httpBuildTimeConfig) {
if (httpBuildTimeConfig.compressors.isPresent()
&& httpBuildTimeConfig.compressors.get().stream().anyMatch(s -> s.equalsIgnoreCase("br"))) {
return new NativeAccessBuildItem();
}
return null;
}
}

0 comments on commit 50476a7

Please sign in to comment.