Skip to content

Commit

Permalink
Merge pull request #32432 from Karm/issue-31596
Browse files Browse the repository at this point in the history
Adapts AWT extension to the new dynamic .so loading [JDK 17, 19, 20]
  • Loading branch information
gastaldi authored Apr 12, 2023
2 parents f52e7f0 + a354621 commit 32d6e66
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 167 deletions.
7 changes: 0 additions & 7 deletions core/builder/src/main/java/io/quarkus/builder/Json.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ public static JsonArrayBuilder array() {
}

/**
*
* @param ignoreEmptyBuilders
* @return the new JSON array builder
* @see JsonBuilder#ignoreEmptyBuilders
Expand All @@ -62,15 +61,13 @@ static JsonArrayBuilder array(boolean ignoreEmptyBuilders) {
}

/**
*
* @return the new JSON object builder, empty builders are not ignored
*/
public static JsonObjectBuilder object() {
return new JsonObjectBuilder(false);
}

/**
*
* @param ignoreEmptyBuilders
* @return the new JSON object builder
* @see JsonBuilder#ignoreEmptyBuilders
Expand All @@ -84,7 +81,6 @@ abstract static class JsonBuilder<T> {
protected boolean ignoreEmptyBuilders = false;

/**
*
* @param ignoreEmptyBuilders If set to true all empty builders added to this builder will be ignored during
* {@link #build()}
*/
Expand All @@ -93,13 +89,11 @@ abstract static class JsonBuilder<T> {
}

/**
*
* @return <code>true</code> if there are no elements/properties, <code>false</code> otherwise
*/
abstract boolean isEmpty();

/**
*
* @return a string representation
* @throws IOException
*/
Expand All @@ -108,7 +102,6 @@ abstract static class JsonBuilder<T> {
abstract void appendTo(Appendable appendable) throws IOException;

/**
*
* @param value
* @return <code>true</code> if the value is null or an empty builder and {@link #ignoreEmptyBuilders} is set to
* <code>true</code>, <code>false</code>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.quarkus.deployment.builditem.nativeimage;

import java.lang.reflect.Field;

import org.jboss.jandex.FieldInfo;

import io.quarkus.builder.item.MultiBuildItem;

/**
* JNI access registration fine-grained to single fields
* for a given class.
*/
public final class JniRuntimeAccessFieldBuildItem extends MultiBuildItem {

final String declaringClass;
final String name;

public JniRuntimeAccessFieldBuildItem(FieldInfo field) {
this.name = field.name();
this.declaringClass = field.declaringClass().name().toString();
}

public JniRuntimeAccessFieldBuildItem(Field field) {
this.name = field.getName();
this.declaringClass = field.getDeclaringClass().getName();
}

public JniRuntimeAccessFieldBuildItem(String declaringClass, String name) {
this.name = name;
this.declaringClass = declaringClass;
}

public String getDeclaringClass() {
return declaringClass;
}

public String getName() {
return name;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.quarkus.deployment.builditem.nativeimage;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;

import org.jboss.jandex.MethodInfo;

import io.quarkus.builder.item.MultiBuildItem;

/**
* JNI access registration fine-grained to single methods
* for a given class.
*/
public final class JniRuntimeAccessMethodBuildItem extends MultiBuildItem {

final String declaringClass;
final String name;
final String[] params;

public JniRuntimeAccessMethodBuildItem(MethodInfo methodInfo) {
String[] params = new String[methodInfo.parametersCount()];
for (int i = 0; i < params.length; ++i) {
params[i] = methodInfo.parameterType(i).name().toString();
}
this.name = methodInfo.name();
this.params = params;
this.declaringClass = methodInfo.declaringClass().name().toString();
}

public JniRuntimeAccessMethodBuildItem(Method method) {
this.params = new String[method.getParameterCount()];
if (method.getParameterCount() > 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < params.length; ++i) {
params[i] = parameterTypes[i].getName();
}
}
this.name = method.getName();
this.declaringClass = method.getDeclaringClass().getName();
}

public JniRuntimeAccessMethodBuildItem(String declaringClass, String name, String... params) {
this.declaringClass = declaringClass;
this.name = name;
this.params = params;
}

public JniRuntimeAccessMethodBuildItem(String declaringClass, String name, Class<?>... params) {
this.declaringClass = declaringClass;
this.name = name;
this.params = new String[params.length];
for (int i = 0; i < params.length; ++i) {
this.params[i] = params[i].getName();
}
}

public JniRuntimeAccessMethodBuildItem(String declaringClass, String name) {
this.declaringClass = declaringClass;
this.name = name;
this.params = new String[0];
}

public String getName() {
return name;
}

public String[] getParams() {
return params;
}

public String getDeclaringClass() {
return declaringClass;
}

@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
JniRuntimeAccessMethodBuildItem that = (JniRuntimeAccessMethodBuildItem) o;
return Objects.equals(declaringClass, that.declaringClass) &&
Objects.equals(name, that.name) &&
Arrays.equals(params, that.params);
}

@Override
public int hashCode() {
int result = Objects.hash(declaringClass, name);
result = 31 * result + Arrays.hashCode(params);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,24 @@ public abstract class NativeImageBuildRunner {

private static final Logger log = Logger.getLogger(NativeImageBuildRunner.class);

private static GraalVM.Version graalVMVersion = null;

public GraalVM.Version getGraalVMVersion() {
final GraalVM.Version graalVMVersion;
try {
String[] versionCommand = getGraalVMVersionCommand(Collections.singletonList("--version"));
log.debugf(String.join(" ", versionCommand).replace("$", "\\$"));
Process versionProcess = new ProcessBuilder(versionCommand)
.redirectErrorStream(true)
.start();
versionProcess.waitFor();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(versionProcess.getInputStream(), StandardCharsets.UTF_8))) {
graalVMVersion = GraalVM.Version.of(reader.lines());
if (graalVMVersion == null) {
try {
final String[] versionCommand = getGraalVMVersionCommand(Collections.singletonList("--version"));
log.debugf(String.join(" ", versionCommand).replace("$", "\\$"));
final Process versionProcess = new ProcessBuilder(versionCommand)
.redirectErrorStream(true)
.start();
versionProcess.waitFor();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(versionProcess.getInputStream(), StandardCharsets.UTF_8))) {
graalVMVersion = GraalVM.Version.of(reader.lines());
}
} catch (Exception e) {
throw new RuntimeException("Failed to get GraalVM version", e);
}
} catch (Exception e) {
throw new RuntimeException("Failed to get GraalVM version", e);
}
return graalVMVersion;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -273,6 +274,26 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
Files.delete(generatedSymbols);
}
}

if (graalVMVersion.compareTo(GraalVM.Version.VERSION_23_0_0) >= 0) {
// See https://github.com/oracle/graal/issues/4921
try (DirectoryStream<Path> sharedLibs = Files.newDirectoryStream(outputDir, "*.{so,dll}")) {
sharedLibs.forEach(src -> {
Path dst = null;
try {
dst = Path.of(outputTargetBuildItem.getOutputDirectory().toAbsolutePath().toString(),
src.getFileName().toString());
log.debugf("Copying a shared lib from %s to %s.", src, dst);
Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
log.errorf("Could not copy shared lib from %s to %s. Continuing. Error: %s", src, dst, e);
}
});
} catch (IOException e) {
log.errorf("Could not list files in directory %s. Continuing. Error: %s", outputDir, e);
}
}

System.setProperty("native.image.path", finalExecutablePath.toAbsolutePath().toString());

return new NativeImageBuildItem(finalExecutablePath,
Expand Down
Loading

0 comments on commit 32d6e66

Please sign in to comment.