Skip to content

Commit

Permalink
Only apply debugging options if Mandrel or newer than 20.1 quarkusio#…
Browse files Browse the repository at this point in the history
…9792

* Also, check src/main/java exists
before adding it to the source search path.
  • Loading branch information
galderz committed Aug 5, 2020
1 parent 5f79f95 commit 185ed3a
Showing 1 changed file with 120 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

import org.apache.commons.lang3.SystemUtils;
Expand Down Expand Up @@ -175,7 +178,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
nativeImage = Collections.singletonList(getNativeImageExecutable(graal, java, env).getAbsolutePath());
}

final Optional<String> graalVMVersion;
final GraalVM.Version graalVMVersion;

try {
List<String> versionCommand = new ArrayList<>(nativeImage);
Expand All @@ -187,16 +190,14 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
versionProcess.waitFor();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(versionProcess.getInputStream(), StandardCharsets.UTF_8))) {
graalVMVersion = reader.lines().filter((l) -> l.startsWith("GraalVM Version")).findFirst();
graalVMVersion = GraalVM.Version.of(reader.lines());
}
} catch (Exception e) {
throw new RuntimeException("Failed to get GraalVM version", e);
}

boolean isMandrel = false;
if (graalVMVersion.isPresent()) {
checkGraalVMVersion(graalVMVersion.get());
isMandrel = graalVMVersion.get().contains("Mandrel");
if (graalVMVersion.isDetected()) {
checkGraalVMVersion(graalVMVersion);
} else {
log.error("Unable to get GraalVM version from the native-image binary.");
}
Expand Down Expand Up @@ -271,11 +272,17 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
command.add("-H:+ReportExceptionStackTraces");
}
if (nativeConfig.debug.enabled) {
command.add("-H:GenerateDebugInfo=1");
final Path sourcePath = Paths.get("..", "..", "src", "main", "java");
command.add("-H:DebugInfoSourceSearchPath=" + sourcePath.toString());
final Path sourcesCachePath = outputDir.getParent().resolve("sources-cache");
command.add("-H:DebugInfoSourceCacheRoot=" + sourcesCachePath.toString());
if (graalVMVersion.isMandrel() || graalVMVersion.isNewerThan(GraalVM.Version.VERSION_20_1)) {
command.add("-g");

final Path sourcePath = Paths.get("..", "..", "src", "main", "java");
if (sourcePath.toFile().exists()) {
command.add("-H:DebugInfoSourceSearchPath=" + sourcePath.toString());
}

final Path sourcesCachePath = outputDir.getParent().resolve("sources-cache");
command.add("-H:DebugInfoSourceCacheRoot=" + sourcesCachePath.toString());
}
}
if (nativeConfig.debugBuildProcess) {
command.add("-J-Xrunjdwp:transport=dt_socket,address=" + DEBUG_BUILD_PROCESS_PORT + ",server=y,suspend=y");
Expand Down Expand Up @@ -323,7 +330,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, NativeImageSourceJa
+ " Please consider removing this configuration key as it is ignored (JNI is always enabled) and it"
+ " will be removed in a future Quarkus version.");
}
if (!nativeConfig.enableServer && !SystemUtils.IS_OS_WINDOWS && !isMandrel) {
if (!nativeConfig.enableServer && !SystemUtils.IS_OS_WINDOWS && !graalVMVersion.isMandrel()) {
command.add("--no-server");
}
if (nativeConfig.enableVmInspection) {
Expand Down Expand Up @@ -483,13 +490,13 @@ private RuntimeException imageGenerationFailed(int exitValue, List<String> comma
}
}

private void checkGraalVMVersion(String version) {
private void checkGraalVMVersion(GraalVM.Version version) {
log.info("Running Quarkus native-image plugin on " + version);
final List<String> obsoleteGraalVmVersions = Arrays.asList("1.0.0", "19.0.", "19.1.", "19.2.", "19.3.", "20.0.");
final boolean vmVersionIsObsolete = obsoleteGraalVmVersions.stream().anyMatch(v -> version.contains(" " + v));
if (vmVersionIsObsolete) {
if (version.isObsolete()) {
final int major = GraalVM.Version.CURRENT.major;
final int minor = GraalVM.Version.CURRENT.minor;
throw new IllegalStateException("Out of date version of GraalVM detected: " + version + "."
+ " Quarkus currently supports 20.1.0. Please upgrade GraalVM to this version.");
+ " Quarkus currently supports " + major + " ." + minor + ".0. Please upgrade GraalVM to this version.");
}
}

Expand Down Expand Up @@ -639,4 +646,100 @@ private static void objcopy(String... args) {
}
}
}

private static final class GraalVM {
static final class Version implements Comparable<Version> {
private static final Pattern PATTERN = Pattern.compile(
"GraalVM Version (1.0.0|([1-9][0-9]).([0-3]).[0-9])\\s*"
);

static final Version UNVERSIONED = new Version(-1, -1, Distribution.ORACLE);
static final Version VERSION_20_1 = new Version(20, 1, Distribution.ORACLE);
static final Version CURRENT = VERSION_20_1;

final int major;
final int minor;
final Distribution distro;

Version(int major, int minor, Distribution distro) {
this.major = major;
this.minor = minor;
this.distro = distro;
}

boolean isDetected() {
return this != UNVERSIONED;
}

boolean isObsolete() {
return this.compareTo(CURRENT) < 0;
}

boolean isMandrel() {
return distro == Distribution.MANDREL;
}

boolean isNewerThan(Version version) {
return this.compareTo(version) > 0;
}

@Override
public int compareTo(Version o) {
if (major > o.major)
return 1;

if (major == o.major) {
if (minor > o.minor)
return 1;
else if (minor == o.minor)
return 0;
}

return -1;
}

static Version of(Stream<String> lines)
{
final Iterator<String> it = lines.iterator();
while (it.hasNext()) {
final String line = it.next();
final Matcher matcher = PATTERN.matcher(line);
if (matcher.find() && matcher.groupCount() == 3)
{
final Distribution distro = Distribution.of(line);
if (Objects.isNull(matcher.group(2)))
{
return new Version(1, 0, distro);
}
else
{
return new Version(
Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3)),
distro);
}
}
}

return UNVERSIONED;
}

@Override
public String toString()
{
return "Version{" +
"major=" + major +
", minor=" + minor +
'}';
}
}

enum Distribution {
ORACLE, MANDREL;

static Distribution of(String line) {
return line.contains("Mandrel") ? MANDREL : ORACLE;
}
}
}
}

0 comments on commit 185ed3a

Please sign in to comment.