Skip to content

Commit 8f0cb57

Browse files
slowhogmagicus
andcommitted
8347831: Re-examine version check when cross linking
Co-authored-by: Magnus Ihse Bursie <ihse@openjdk.org> Reviewed-by: erikj, alanb
1 parent 37cd8d6 commit 8f0cb57

File tree

5 files changed

+76
-24
lines changed

5 files changed

+76
-24
lines changed

make/modules/java.base/Gensrc.gmk

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,25 @@ $(INTPOLY_GEN_DONE): $(INTPLOY_HEADER) $(BUILD_TOOLS_JDK)
120120
TARGETS += $(INTPOLY_GEN_DONE)
121121

122122
################################################################################
123+
124+
RELEASE_FILE_TEMPLATE := $(TOPDIR)/src/java.base/share/classes/jdk/internal/misc/resources/release.txt.template
125+
RELEASE_FILE_TARGET := $(SUPPORT_OUTPUTDIR)/gensrc/$(MODULE)/jdk/internal/misc/resources/release.txt
126+
127+
RELEASE_FILE_VARDEPS := $(COMPANY_NAME) $(VERSION_STRING) $(VERSION_DATE)
128+
RELEASE_FILE_VARDEPS_FILE := $(call DependOnVariable, RELEASE_FILE_VARDEPS, \
129+
$(SUPPORT_OUTPUTDIR)/gensrc/$(MODULE)/jlink_release_txt.vardeps)
130+
131+
$(eval $(call SetupTextFileProcessing, BUILD_RELEASE_FILE, \
132+
SOURCE_FILES := $(RELEASE_FILE_TEMPLATE), \
133+
OUTPUT_FILE := $(RELEASE_FILE_TARGET), \
134+
REPLACEMENTS := \
135+
@@COMPANY_NAME@@ => $(COMPANY_NAME) ; \
136+
@@VERSION_STRING@@ => $(VERSION_STRING) ; \
137+
@@VERSION_DATE@@ => $(VERSION_DATE) , \
138+
))
139+
140+
$(BUILD_RELEASE_FILE): $(RELEASE_FILE_VARDEPS_FILE)
141+
142+
TARGETS += $(BUILD_RELEASE_FILE)
143+
144+
################################################################################

make/modules/java.base/Java.gmk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
DOCLINT += -Xdoclint:all/protected \
3535
'-Xdoclint/package:java.*,javax.*'
3636
JAVAC_FLAGS += -XDstringConcat=inline
37-
COPY += .icu .dat .spp .nrm content-types.properties \
37+
COPY += .icu .dat .spp .nrm .txt content-types.properties \
3838
hijrah-config-Hijrah-umalqura_islamic-umalqura.properties
3939
CLEAN += intrinsic.properties
4040

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@@COMPANY_NAME@@-@@VERSION_STRING@@-@@VERSION_DATE@@

src/jdk.jlink/share/classes/jdk/tools/jlink/internal/JlinkTask.java

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
import static jdk.tools.jlink.internal.TaskHelper.JLINK_BUNDLE;
2828

2929
import java.io.BufferedInputStream;
30+
import java.io.BufferedReader;
3031
import java.io.File;
3132
import java.io.IOException;
3233
import java.io.InputStream;
34+
import java.io.InputStreamReader;
3335
import java.io.PrintWriter;
3436
import java.io.UncheckedIOException;
3537
import java.lang.module.Configuration;
@@ -56,6 +58,7 @@
5658
import java.util.List;
5759
import java.util.Locale;
5860
import java.util.Map;
61+
import java.util.NoSuchElementException;
5962
import java.util.Objects;
6063
import java.util.Optional;
6164
import java.util.Set;
@@ -238,6 +241,27 @@ static class OptionsValues {
238241
}
239242

240243
public static final String OPTIONS_RESOURCE = "jdk/tools/jlink/internal/options";
244+
// Release information stored in the java.base module
245+
private static final String JDK_RELEASE_RESOURCE = "jdk/internal/misc/resources/release.txt";
246+
247+
/**
248+
* Read the release.txt from the module.
249+
*/
250+
private static Optional<String> getReleaseInfo(ModuleReference mref) {
251+
try {
252+
Optional<InputStream> release = mref.open().open(JDK_RELEASE_RESOURCE);
253+
254+
if (release.isEmpty()) {
255+
return Optional.empty();
256+
}
257+
258+
try (var r = new BufferedReader(new InputStreamReader(release.get()))) {
259+
return Optional.of(r.readLine());
260+
}
261+
} catch (IOException ioe) {
262+
throw new UncheckedIOException(ioe);
263+
}
264+
}
241265

242266
int run(String[] args) {
243267
if (log == null) {
@@ -410,7 +434,8 @@ private JlinkConfiguration initJlinkConfig() throws BadArgs {
410434

411435
// Sanity check version if we use JMODs
412436
if (!isLinkFromRuntime) {
413-
checkJavaBaseVersion(finder);
437+
assert(finder.find("java.base").isPresent());
438+
checkJavaBaseVersion(finder.find("java.base").get());
414439
}
415440

416441
// Determine the roots set
@@ -561,32 +586,34 @@ public Set<ModuleReference> findAll() {
561586
return finder;
562587
}
563588

589+
private static String getCurrentRuntimeVersion() {
590+
ModuleReference current = ModuleLayer.boot()
591+
.configuration()
592+
.findModule("java.base")
593+
.get()
594+
.reference();
595+
// This jlink runtime should always have the release.txt
596+
return getReleaseInfo(current).get();
597+
}
598+
564599
/*
565-
* Checks the version of the module descriptor of java.base for compatibility
566-
* with the current runtime version.
600+
* Checks the release information of the java.base used for target image
601+
* for compatibility with the java.base used by jlink.
567602
*
568-
* @throws IllegalArgumentException the descriptor of java.base has no
569-
* version or the java.base version is not the same as the current runtime's
570-
* version.
603+
* @throws IllegalArgumentException If the `java.base` module reference `target`
604+
* is not compatible with this jlink.
571605
*/
572-
private static void checkJavaBaseVersion(ModuleFinder finder) {
573-
assert finder.find("java.base").isPresent();
606+
private static void checkJavaBaseVersion(ModuleReference target) {
607+
String currentRelease = getCurrentRuntimeVersion();
574608

575-
// use the version of java.base module, if present, as
576-
// the release version for multi-release JAR files
577-
ModuleDescriptor.Version v = finder.find("java.base").get()
578-
.descriptor().version().orElseThrow(() ->
579-
new IllegalArgumentException("No version in java.base descriptor")
580-
);
581-
582-
Runtime.Version version = Runtime.Version.parse(v.toString());
583-
if (Runtime.version().feature() != version.feature() ||
584-
Runtime.version().interim() != version.interim()) {
585-
// jlink version and java.base version do not match.
586-
// We do not (yet) support this mode.
609+
String targetRelease = getReleaseInfo(target).orElseThrow(() -> new IllegalArgumentException(
610+
taskHelper.getMessage("err.jlink.version.missing", currentRelease)));
611+
612+
if (!currentRelease.equals(targetRelease)) {
613+
// Current runtime image and the target runtime image are not compatible build
587614
throw new IllegalArgumentException(taskHelper.getMessage("err.jlink.version.mismatch",
588-
Runtime.version().feature(), Runtime.version().interim(),
589-
version.feature(), version.interim()));
615+
currentRelease,
616+
targetRelease));
590617
}
591618
}
592619

src/jdk.jlink/share/classes/jdk/tools/jlink/resources/jlink.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ err.runtime.link.patched.module=jlink does not support linking from the run-time
130130
err.no.module.path=--module-path option must be specified with --add-modules ALL-MODULE-PATH
131131
err.empty.module.path=No module found in module path ''{0}'' with --add-modules ALL-MODULE-PATH
132132
err.limit.modules=--limit-modules not allowed with --add-modules ALL-MODULE-PATH
133-
err.jlink.version.mismatch=jlink version {0}.{1} does not match target java.base version {2}.{3}
133+
err.jlink.version.mismatch=jlink build ''{0}'' does not match target java.base build ''{1}''
134+
err.jlink.version.missing=jlink build ''{0}'' cannot find the build signature\
135+
\ in the java.base specified on module path, likely from an earlier build.
134136
err.automatic.module:automatic module cannot be used with jlink: {0} from {1}
135137
err.unknown.byte.order:unknown byte order {0}
136138
err.launcher.main.class.empty:launcher main class name cannot be empty: {0}

0 commit comments

Comments
 (0)