Skip to content

Commit

Permalink
Make Truffle from GraalVM 23.1 work in all Quarkus modes
Browse files Browse the repository at this point in the history
Fixes: #36242
(cherry picked from commit 738a24d)
  • Loading branch information
geoand authored and gsmet committed Dec 1, 2023
1 parent 78a823c commit 83a3e51
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.quarkus.deployment.builditem;

import io.quarkus.builder.item.MultiBuildItem;

/**
* A marker build item to make Quarkus set the {@code java.class.path} system property.
* This system property is used in rare by libraries (Truffle for example) to create their own ClassLoaders.
* The value of the system property is simply best effort, as there is no way to faithfully represent
* the Quarkus ClassLoader hierarchies in a system property value.
*/
public final class SetClassPathSystemPropBuildItem extends MultiBuildItem {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package io.quarkus.deployment.steps;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.SetClassPathSystemPropBuildItem;
import io.quarkus.deployment.pkg.builditem.CurateOutcomeBuildItem;
import io.quarkus.maven.dependency.ResolvedDependency;
import io.quarkus.runtime.ClassPathSystemPropertyRecorder;

public class ClassPathSystemPropBuildStep {

@BuildStep
public void produce(BuildProducer<SetClassPathSystemPropBuildItem> producer, CurateOutcomeBuildItem curateOutcome) {
boolean truffleUsed = curateOutcome.getApplicationModel().getDependencies().stream()
.anyMatch(d -> d.getGroupId().equals("org.graalvm.polyglot"));
if (truffleUsed) {
producer.produce(new SetClassPathSystemPropBuildItem());
}
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
public void set(List<SetClassPathSystemPropBuildItem> setCPItems,
CurateOutcomeBuildItem curateOutcome,
ClassPathSystemPropertyRecorder recorder) {
if (setCPItems.isEmpty()) {
return;
}
Collection<ResolvedDependency> runtimeDependencies = curateOutcome.getApplicationModel().getRuntimeDependencies();
List<Path> parentFirst = new ArrayList<>();
List<Path> regular = new ArrayList<>();
for (ResolvedDependency dependency : runtimeDependencies) {
if (dependency.isClassLoaderParentFirst()) {
parentFirst.addAll(dependency.getContentTree().getRoots());
} else {
regular.addAll(dependency.getContentTree().getRoots());

}
}
String classPathValue = Stream.concat(parentFirst.stream(), regular.stream()).map(p -> p.toAbsolutePath().toString())
.collect(Collectors.joining(":"));
recorder.set(classPathValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package io.quarkus.runtime;

import io.quarkus.runtime.annotations.Recorder;

@Recorder
public class ClassPathSystemPropertyRecorder {

public void set(String value) {
System.setProperty("java.class.path", value);
}
}

0 comments on commit 83a3e51

Please sign in to comment.