Skip to content

Commit

Permalink
Fix version reporting and parsing
Browse files Browse the repository at this point in the history
Quarkus currently only parses the feature and update version of JDK and
incorrectly prints message like:

```
[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildStep] Running Quarkus native-image plugin on MANDREL 22.3.3.1 JDK 17.8
```

while it should be

```
[INFO] [io.quarkus.deployment.pkg.steps.NativeImageBuildStep] Running Quarkus native-image plugin on MANDREL 22.3.3.1 JDK 17.0.8
```

i.e. 17.0.8 vs 17.8

This change fixes that issue and relies on java.lang.Runtime.Version for
parsing and comparing the JDK version.

Follow up to quarkusio#36267
  • Loading branch information
zakkak authored and hiteshkhatri97 committed Oct 24, 2023
1 parent a6d39e7 commit d07ecc6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,14 @@
* the native-image tool was bundled with.
*/
public final class NativeMinimalJavaVersionBuildItem extends MultiBuildItem {
public final int minFeature;
public final int minUpdate;
public final Runtime.Version minVersion;
public final String warning;

/**
* @param minFeature e.g. 17 for JDK 17.0.1
* @param minUpdate e.g. 1 for JDK 17.0.1
* @param minVersion e.g. 17.0.1
*/
public NativeMinimalJavaVersionBuildItem(int minFeature, int minUpdate, String warning) {
this.minFeature = minFeature;
this.minUpdate = minUpdate;
public NativeMinimalJavaVersionBuildItem(String minVersion, String warning) {
this.minVersion = Runtime.Version.parse(minVersion);
this.warning = warning;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import io.quarkus.deployment.builditem.nativeimage.NativeMinimalJavaVersionBuildItem;

public final class GraalVM {

// Implements version parsing after https://github.com/oracle/graal/pull/6302
Expand Down Expand Up @@ -52,8 +54,8 @@ static Version parse(List<String> lines) {
Matcher secondMatcher = SECOND_PATTERN.matcher(lines.get(1));
Matcher thirdMatcher = THIRD_PATTERN.matcher(lines.get(2));
if (firstMatcher.find() && secondMatcher.find() && thirdMatcher.find()) {
String javaVersion = firstMatcher.group(VNUM_GROUP);
java.lang.Runtime.Version v = null;
String javaVersion = secondMatcher.group(BUILD_INFO_GROUP);
java.lang.Runtime.Version v;
try {
v = java.lang.Runtime.Version.parse(javaVersion);
} catch (IllegalArgumentException e) {
Expand All @@ -62,8 +64,7 @@ static Version parse(List<String> lines) {

String vendorVersion = secondMatcher.group(VENDOR_VERSION_GROUP);

String buildInfo = secondMatcher.group(BUILD_INFO_GROUP);
String graalVersion = graalVersion(buildInfo, v.feature());
String graalVersion = graalVersion(javaVersion, v.feature());
if (vendorVersion.contains("-dev")) {
graalVersion = graalVersion + "-dev";
}
Expand All @@ -74,7 +75,7 @@ static Version parse(List<String> lines) {
return UNKNOWN_VERSION;
}
return new Version(lines.stream().collect(Collectors.joining("\n")),
versNum, v.feature(), v.update(), dist);
versNum, v, dist);
} else {
return UNKNOWN_VERSION;
}
Expand Down Expand Up @@ -145,36 +146,37 @@ public static final class Version implements Comparable<Version> {
*/
private static final Pattern OLD_VERS_PATTERN = Pattern.compile(
"(GraalVM|native-image)( Version)? " + VersionParseHelper.VERS_FORMAT + "(?<distro>.*?)?" +
"(\\(Java Version (?<jfeature>[0-9]+)(\\.(?<jinterim>[0-9]*)\\.(?<jupdate>[0-9]*))?.*)?$");
"(\\(Java Version (?<javaversion>(?<jfeature>[0-9]+)(\\.(?<jinterim>[0-9]*)\\.(?<jupdate>[0-9]*))?.*)\\))?$");

static final Version VERSION_21_3 = new Version("GraalVM 21.3", "21.3", Distribution.GRAALVM);
static final Version VERSION_21_3_0 = new Version("GraalVM 21.3.0", "21.3.0", Distribution.GRAALVM);
public static final Version VERSION_22_3_0 = new Version("GraalVM 22.3.0", "22.3.0", Distribution.GRAALVM);
public static final Version VERSION_22_2_0 = new Version("GraalVM 22.2.0", "22.2.0", Distribution.GRAALVM);
public static final Version VERSION_23_0_0 = new Version("GraalVM 23.0.0", "23.0.0", Distribution.GRAALVM);
public static final Version VERSION_23_1_0 = new Version("GraalVM 23.1.0", "23.1.0", Distribution.GRAALVM);
public static final Version VERSION_24_0_0 = new Version("GraalVM 24.0.0", "24.0.0", Distribution.GRAALVM);
public static final Version VERSION_22_3_0 = new Version("GraalVM 22.3.0", "22.3.0", "17", Distribution.GRAALVM);
public static final Version VERSION_22_2_0 = new Version("GraalVM 22.2.0", "22.2.0", "17", Distribution.GRAALVM);
public static final Version VERSION_23_0_0 = new Version("GraalVM 23.0.0", "23.0.0", "17", Distribution.GRAALVM);
public static final Version VERSION_23_1_0 = new Version("GraalVM 23.1.0", "23.1.0", "21", Distribution.GRAALVM);
public static final Version VERSION_24_0_0 = new Version("GraalVM 24.0.0", "24.0.0", "22", Distribution.GRAALVM);

public static final Version MINIMUM = VERSION_22_2_0;
public static final Version CURRENT = VERSION_23_1_0;
public static final int UNDEFINED = -1;

final String fullVersion;
public final int javaFeatureVersion;
public final int javaUpdateVersion;
public final Runtime.Version javaVersion;
final Distribution distribution;
private int[] versions;
private String suffix;

Version(String fullVersion, String version, Distribution distro) {
this(fullVersion, version, 11, UNDEFINED, distro);
this(fullVersion, version, "11", distro);
}

Version(String fullVersion, String version, String javaVersion, Distribution distro) {
this(fullVersion, version, Runtime.Version.parse(javaVersion), distro);
}

Version(String fullVersion, String version, int javaFeatureVersion, int javaUpdateVersion, Distribution distro) {
Version(String fullVersion, String version, Runtime.Version javaVersion, Distribution distro) {
this.fullVersion = fullVersion;
breakdownVersion(version);
this.javaFeatureVersion = javaFeatureVersion;
this.javaUpdateVersion = javaUpdateVersion;
this.javaVersion = javaVersion;
this.distribution = distro;
}

Expand Down Expand Up @@ -210,13 +212,12 @@ boolean isOlderThan(Version version) {
/**
* e.g. JDK 11.0.13 > 11.0.12, 17.0.1 > 11.0.13,
*/
public boolean jdkVersionGreaterOrEqualTo(int feature, int update) {
return this.javaFeatureVersion > feature
|| (this.javaFeatureVersion == feature && this.javaUpdateVersion >= update);
public boolean jdkVersionGreaterOrEqualTo(NativeMinimalJavaVersionBuildItem javaVersionBuildItem) {
return javaVersion.compareToIgnoreOptional(javaVersionBuildItem.minVersion) >= 0;
}

boolean is(Version version) {
return this.compareTo(version) == 0;
public boolean jdkVersionGreaterOrEqualTo(String version) {
return javaVersion.compareToIgnoreOptional(Runtime.Version.parse(version)) >= 0;
}

@Override
Expand Down Expand Up @@ -259,20 +260,20 @@ public static Version of(Stream<String> output) {
// GraalVM/Mandrel old, single line, version scheme:
final String version = oldVersMatcher.group(VersionParseHelper.VERSION_GROUP);
final String distro = oldVersMatcher.group("distro");
// JDK:
// e.g. JDK 17.0.1, feature: 17, interim: 0 (not used here), update: 1
final String jFeatureMatch = oldVersMatcher.group("jfeature");
final int jFeature = jFeatureMatch == null ? // Old GraalVM versions, like 19, didn't report the Java version.
11 : Integer.parseInt(jFeatureMatch);
final String jUpdateMatch = oldVersMatcher.group("jupdate");
final int jUpdate = jUpdateMatch == null ? // Some JDK dev builds don't report full version string.
UNDEFINED : Integer.parseInt(jUpdateMatch);
String javaVersion = oldVersMatcher.group("javaversion");
if (javaVersion == null) {
if (version.startsWith("19")) {
javaVersion = "11"; // Fallback to JDK 11 for GraalVM 19.x
} else {
throw new IllegalArgumentException(
"Cannot parse version from output: \n" + stringOutput);
}
}

return new Version(
line,
version,
jFeature,
jUpdate,
Runtime.Version.parse(javaVersion),
isMandrel(distro) ? Distribution.MANDREL : Distribution.GRAALVM);
}
}
Expand Down Expand Up @@ -303,13 +304,12 @@ public String toString() {
+ getVersionAsString() +
", fullVersion=" + fullVersion +
", distribution=" + distribution +
", javaFeatureVersion=" + javaFeatureVersion +
", javaUpdateVersion=" + javaUpdateVersion +
", javaVersion=" + javaVersion +
'}';
}

public boolean isJava17() {
return javaFeatureVersion == 17;
return javaVersion.feature() == 17;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
return new NativeImageBuildItem(finalExecutablePath,
new NativeImageBuildItem.GraalVMVersion(graalVMVersion.fullVersion,
graalVMVersion.getVersionAsString(),
graalVMVersion.javaFeatureVersion,
graalVMVersion.javaVersion.feature(),
graalVMVersion.distribution.name()));
} catch (ImageGenerationFailureException e) {
throw e;
Expand Down Expand Up @@ -477,7 +477,7 @@ private RuntimeException imageGenerationFailed(int exitValue, boolean isContaine

private void checkGraalVMVersion(GraalVM.Version version) {
log.info("Running Quarkus native-image plugin on " + version.distribution.name() + " " + version.getVersionAsString()
+ " JDK " + version.javaFeatureVersion + "." + version.javaUpdateVersion);
+ " JDK " + version.javaVersion);
if (version.isObsolete()) {
throw new IllegalStateException("Out of date version of GraalVM detected: " + version.getVersionAsString() + "."
+ " Quarkus currently supports " + GraalVM.Version.CURRENT.getVersionAsString()
Expand Down Expand Up @@ -955,17 +955,10 @@ public NativeImageInvokerInfo build() {
}

if (nativeMinimalJavaVersions != null && !nativeMinimalJavaVersions.isEmpty()) {
if (graalVMVersion.javaUpdateVersion == GraalVM.Version.UNDEFINED) {
log.warnf(
"Unable to parse used Java version from native-image version string `%s'. Java version checks will be skipped.",
graalVMVersion.fullVersion);
} else {
nativeMinimalJavaVersions.stream()
.filter(a -> !graalVMVersion.jdkVersionGreaterOrEqualTo(a.minFeature, a.minUpdate))
.forEach(a -> log.warnf("Expected: Java %d, update %d, Actual: Java %d, update %d. %s",
a.minFeature, a.minUpdate, graalVMVersion.javaFeatureVersion,
graalVMVersion.javaUpdateVersion, a.warning));
}
nativeMinimalJavaVersions.stream()
.filter(a -> !graalVMVersion.jdkVersionGreaterOrEqualTo(a))
.forEach(a -> log.warnf("Expected: Java %s, Actual: Java %s. %s",
a.minVersion, graalVMVersion.javaVersion, a.warning));
}

if (unsupportedOSes != null && !unsupportedOSes.isEmpty()) {
Expand Down
Loading

0 comments on commit d07ecc6

Please sign in to comment.