Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Demonstration] Working Conjure compiler graal native-image #780

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions conjure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
* limitations under the License.
*/

plugins {
id 'com.palantir.graal' version '0.7.2'
}

apply from: "$rootDir/gradle/publish-dist.gradle"

mainClassName = 'com.palantir.conjure.cli.ConjureCli'
Expand All @@ -29,4 +33,30 @@ dependencies {

annotationProcessor 'org.immutables:value'
compileOnly 'org.immutables:value::annotations'
compileOnly 'org.graalvm.sdk:graal-sdk'
}


graal {
mainClass mainClassName
javaVersion '11'
outputName 'conjure'
graalVersion '20.3.0'
// resourceconfig.json which includes all resources to match hotspot.
option ('-H:ResourceConfigurationFiles=' + file('resourceconfig.json').absolutePath)
// Fail if a native image cannot be created. Otherwise a fallback image which relies
// on a hotspot jdk is required.
option '--no-fallback'
// Missing classes result in runtime failures rather than compile time. Many libraries
// have optional dependencies that may be excluded. Take the log4j-core disruptor
// dependency for example.
option '--allow-incomplete-classpath'
// Allows optional dependencies to fail at runtime, not build time. This feature is
// equivalent to 'allow-incomplete-classpath' for unsupported java features,
// methodHandles for example.
option '--report-unsupported-elements-at-runtime'
// Easier debugging on failure
option '-H:+ReportExceptionStackTraces'
// Discover reflection info at build time
option '--features=com.palantir.conjure.cli.RuntimeReflectionRegistrationFeature'
}
7 changes: 7 additions & 0 deletions conjure/resourceconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"resources": {
"includes": [
{"pattern": "^(.(?!.*\\.class))*$"}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear if the .class extension exclusion is necessary, I was debugging other things.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In an ideal world we'd be able to do this programatically using a build Feature like reflection to reduce dependence on custom configs.

]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.palantir.conjure.cli;

import java.io.IOException;
import java.lang.reflect.AnnotatedElement;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeReflection;

@SuppressWarnings({"CatchAndPrintStackTrace", "JdkObsolete"})
@Platforms(Platform.HOSTED_ONLY.class)
final class RuntimeReflectionRegistrationFeature implements Feature {

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
// Common standard library classes
registerClassForReflection(String.class);
registerClassForReflection(HashSet.class);
registerClassForReflection(HashMap.class);
registerClassForReflection(LinkedHashMap.class);
registerClassForReflection(ArrayList.class);
registerClassForReflection(LinkedList.class);
registerClassForReflection(ConcurrentHashMap.class);
for (Path jarPath : access.getApplicationClassPath()) {
String jarFileName = jarPath.getFileName().toString();
if (!jarFileName.endsWith(".jar")) {
// Only scan jars
continue;
}
try (JarFile jar = new JarFile(jarPath.toFile())) {
Enumeration<JarEntry> entries = jar.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class")
// Exclude multirelease files, the default jvm version class name is sufficient.
&& !name.startsWith("META-INF")
// Exclude graal components
&& !name.contains("com/oracle/svm")
// Includes incorrect repackaging with malformed signatures. These break the build.
&& !name.contains("repackaged")
&& !name.contains("shaded")
&& !name.contains("glassfish")
&& !name.startsWith("javax")
&& !name.startsWith("jakarta")) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was whac-a-mole.

String className = name.replace("/", ".").substring(0, name.length() - 6);
try {
registerClassForReflection(access.findClassByName(className));
} catch (Throwable t) {
t.printStackTrace();
}
}
}
} catch (IOException | RuntimeException e) {
e.printStackTrace();
}
}
}

private static void registerClassForReflection(Class<?> clazz) {
if (clazz != null) {
try {
if (!isAnyElementGraalAnnotated(clazz)) {
RuntimeReflection.register(clazz);
RuntimeReflection.register(clazz.getDeclaredMethods());
RuntimeReflection.register(clazz.getDeclaredConstructors());
RuntimeReflection.register(clazz.getDeclaredFields());
}
} catch (NoClassDefFoundError e) {
System.err.printf("NoClassDefFoundError: %s While inspecting %s\n", e.getMessage(), clazz.getName());
} catch (Throwable t) {
t.printStackTrace();
// ignored
}
}
}

private static boolean isAnyElementGraalAnnotated(Class<?> clazz) {
try {
return isGraalAnnotated(clazz)
|| isAnyGraalAnnotated(clazz.getDeclaredMethods())
|| isAnyGraalAnnotated(clazz.getDeclaredConstructors())
|| isAnyGraalAnnotated(clazz.getDeclaredFields());
} catch (Throwable t) {
return false;
}
}

private static boolean isAnyGraalAnnotated(AnnotatedElement[] elements) {
for (AnnotatedElement element : elements) {
if (isGraalAnnotated(element)) {
return true;
}
}
return false;
}

private static boolean isGraalAnnotated(AnnotatedElement element) {
return element.isAnnotationPresent(Platforms.class);
}
}
1 change: 1 addition & 0 deletions versions.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ org.glassfish.hk2.external:aopalliance-repackaged:2.5.0-b32 (2 constraints: 0519
org.glassfish.hk2.external:javax.inject:2.5.0-b32 (2 constraints: 451f3a01)
org.glassfish.jersey.bundles.repackaged:jersey-guava:2.25.1 (1 constraints: 251120d4)
org.glassfish.jersey.core:jersey-common:2.25.1 (1 constraints: 3c05433b)
org.graalvm.sdk:graal-sdk:20.3.0 (1 constraints: 3705363b)
org.immutables:value:2.8.8 (1 constraints: 14051536)
org.javassist:javassist:3.20.0-GA (1 constraints: 4f0d1a40)
org.slf4j:slf4j-api:1.7.30 (3 constraints: 451d0579)
Expand Down
1 change: 1 addition & 0 deletions versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ org.hamcrest:hamcrest-core = 2.2
org.immutables:value = 2.8.8
org.mockito:mockito-core = 3.6.28
org.slf4j:* = 1.7.30
org.graalvm.sdk:graal-sdk = 20.3.0

# conflict resolution
javax.annotation:javax.annotation-api = 1.3.2
Expand Down