Skip to content

Commit

Permalink
Update patch to new built-time approach
Browse files Browse the repository at this point in the history
  • Loading branch information
jerboaa committed Feb 26, 2024
1 parent 3376933 commit d9653ec
Show file tree
Hide file tree
Showing 45 changed files with 1,389 additions and 799 deletions.
5 changes: 4 additions & 1 deletion make/CompileToolsJdk.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ $(eval $(call SetupJavaCompilation, BUILD_TOOLS_JDK, \
--add-exports java.base/sun.text=ALL-UNNAMED \
--add-exports java.base/sun.security.util=ALL-UNNAMED \
--add-exports jdk.internal.opt/jdk.internal.opt=jdk.compiler.interim \
--add-exports jdk.internal.opt/jdk.internal.opt=jdk.javadoc.interim, \
--add-exports jdk.internal.opt/jdk.internal.opt=jdk.javadoc.interim \
--add-exports java.base/jdk.internal.module=ALL-UNNAMED \
--add-exports java.base/jdk.internal.jimage=ALL-UNNAMED \
, \
))

TARGETS += $(BUILD_TOOLS_JDK)
Expand Down
53 changes: 49 additions & 4 deletions make/Images.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ include MakeBase.gmk
include Execute.gmk
include Modules.gmk
include Utils.gmk
include ToolsJdk.gmk

JDK_TARGETS :=
JRE_TARGETS :=
Expand Down Expand Up @@ -85,10 +86,22 @@ JLINK_TOOL := $(JLINK) -J-Djlink.debug=true \

JLINK_JRE_EXTRA_OPTS := --no-man-pages --no-header-files --strip-debug

ifeq ($(JLINK_PRODUCE_RUNTIME_LINK_JDK), true)
JDK_LINK_OUTPUT_DIR := $(RL_INTERMEDIATE_IMAGE_DIR)
else
JDK_LINK_OUTPUT_DIR := $(JDK_IMAGE_DIR)
endif

ifeq ($(JLINK_KEEP_PACKAGED_MODULES), true)
JLINK_JDK_EXTRA_OPTS := --keep-packaged-modules $(JDK_IMAGE_DIR)/jmods
ifeq ($(JLINK_PRODUCE_RUNTIME_LINK_JDK), true)
JLINK_JDK_EXTRA_OPTS := --keep-packaged-modules $(RL_INTERMEDIATE_IMAGE_DIR)/jmods
JLINK_RUNTIME_CREATE_EXTRA := --keep-packaged-modules $(JDK_IMAGE_DIR)/jmods
else
JLINK_JDK_EXTRA_OPTS := --keep-packaged-modules $(JDK_IMAGE_DIR)/jmods
endif
endif


JLINK_DISABLE_WARNINGS := | ( $(GREP) -v -e "WARNING: Using incubator module" || test "$$?" = "1" )

JDK_IMAGE_SUPPORT_DIR := $(SUPPORT_OUTPUTDIR)/images/jdk
Expand All @@ -98,16 +111,48 @@ $(eval $(call SetupExecute, jlink_jdk, \
WARN := Creating jdk image, \
DEPS := $(JDK_JMODS) $(BASE_RELEASE_FILE) \
$(call DependOnVariable, JDK_MODULES_LIST, $(JDK_IMAGE_SUPPORT_DIR)/_jlink_jdk.vardeps), \
OUTPUT_DIR := $(JDK_IMAGE_DIR), \
OUTPUT_DIR := $(JDK_LINK_OUTPUT_DIR), \
SUPPORT_DIR := $(JDK_IMAGE_SUPPORT_DIR), \
PRE_COMMAND := $(RM) -r $(JDK_IMAGE_DIR), \
PRE_COMMAND := $(RM) -r $(JDK_LINK_OUTPUT_DIR), \
COMMAND := $(JLINK_TOOL) --add-modules $(JDK_MODULES_LIST) \
$(JLINK_JDK_EXTRA_OPTS) --output $(JDK_IMAGE_DIR) \
$(JLINK_JDK_EXTRA_OPTS) --output $(JDK_LINK_OUTPUT_DIR) \
$(JLINK_DISABLE_WARNINGS), \
))

JLINK_JDK_TARGETS := $(jlink_jdk)

ifeq ($(JLINK_PRODUCE_RUNTIME_LINK_JDK), true)

JDK_RUN_TIME_IMAGE_SUPPORT_DIR := $(SUPPORT_OUTPUTDIR)/images/jlink-run-time
JDK_JMODS_DIFF := $(JDK_RUN_TIME_IMAGE_SUPPORT_DIR)/jimage_packaged_modules_diff.data
JLINK_RUNTIME_CREATE_EXTRA += --create-linkable-runtime=$(JDK_JMODS_DIFF)

$(eval $(call SetupExecute, generate_jimage_diff, \
WARN := Generating the jimage to packaged modules diff, \
DEPS := $(jlink_jdk), \
OUTPUT_FILE := $(JDK_JMODS_DIFF), \
SUPPORT_DIR := $(JDK_RUN_TIME_IMAGE_SUPPORT_DIR), \
COMMAND := $(TOOL_JIMAGE_DIFF_TO_JMODS) $(IMAGES_OUTPUTDIR)/jmods \
$(JDK_LINK_OUTPUT_DIR)/lib/modules $(JDK_JMODS_DIFF), \
))

$(eval $(call SetupExecute, jlink_runtime_jdk, \
WARN := Creating jdk image for runtime linking, \
DEPS := $(generate_jimage_diff), \
OUTPUT_DIR := $(JDK_IMAGE_DIR), \
SUPPORT_DIR := $(JDK_RUN_TIME_IMAGE_SUPPORT_DIR), \
PRE_COMMAND := $(RM) -r $(JDK_IMAGE_DIR), \
COMMAND := $(JLINK_TOOL) --add-modules $(JDK_MODULES_LIST) \
$(JLINK_RUNTIME_CREATE_EXTRA) \
--output $(JDK_IMAGE_DIR) \
$(JLINK_DISABLE_WARNINGS), \
))

JLINK_JDK_TARGETS += $(generate_jimage_diff)
JLINK_JDK_TARGETS += $(jlink_runtime_jdk)

endif

$(eval $(call SetupExecute, jlink_jre, \
WARN := Creating legacy jre image, \
DEPS := $(JRE_JMODS) $(BASE_RELEASE_FILE) \
Expand Down
5 changes: 5 additions & 0 deletions make/ToolsJdk.gmk
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ TOOL_GENERATEEXTRAPROPERTIES = $(JAVA_SMALL) -cp $(BUILDTOOLS_OUTPUTDIR)/jdk_too
TOOL_MAKEZIPREPRODUCIBLE = $(JAVA_SMALL) -cp $(BUILDTOOLS_OUTPUTDIR)/jdk_tools_classes \
build.tools.makezipreproducible.MakeZipReproducible

TOOL_JIMAGE_DIFF_TO_JMODS = $(BUILD_JAVA_SMALL) -cp $(BUILDTOOLS_OUTPUTDIR)/jdk_tools_classes \
--add-modules=jdk.jlink --add-exports=java.base/jdk.internal.module=ALL-UNNAMED \
--add-exports=java.base/jdk.internal.jimage=ALL-UNNAMED \
build.tools.runtimelink.JimageDiffGenerator

# TODO: There are references to the jdwpgen.jar in jdk/make/netbeans/jdwpgen/build.xml
# and nbproject/project.properties in the same dir. Needs to be looked at.
TOOL_JDWPGEN = $(JAVA_SMALL) -cp $(BUILDTOOLS_OUTPUTDIR)/jdk_tools_classes build.tools.jdwpgen.Main
Expand Down
1 change: 1 addition & 0 deletions make/autoconf/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ BASIC_SETUP_DEFAULT_LOG
# We need build & target for this.
JDKOPT_SETUP_JMOD_OPTIONS
JDKOPT_SETUP_JLINK_OPTIONS
JDKOPT_SETUP_JLINK_RUN_TIME_LINK_IMAGE
JDKVER_SETUP_JDK_VERSION_NUMBERS

###############################################################################
Expand Down
16 changes: 16 additions & 0 deletions make/autoconf/jdk-options.m4
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,22 @@ AC_DEFUN_ONCE([JDKOPT_SETUP_JLINK_OPTIONS],
AC_SUBST(JLINK_KEEP_PACKAGED_MODULES)
])

################################################################################
#
# jlink related.
#
# Determines whether or not a run-time linkable JDK image is being
# produced from the product image.
#
AC_DEFUN_ONCE([JDKOPT_SETUP_JLINK_RUN_TIME_LINK_IMAGE],
[
UTIL_ARG_ENABLE(NAME: runtime-link-image, DEFAULT: false,
RESULT: JLINK_PRODUCE_RUNTIME_LINK_JDK,
DESC: [enable producing an image suitable for runtime linking],
CHECKING_MSG: [whether or not an image suitable for runtime linking should be produced])
AC_SUBST(JLINK_PRODUCE_RUNTIME_LINK_JDK)
])

################################################################################
#
# Enable or disable generation of the classlist at build time
Expand Down
5 changes: 5 additions & 0 deletions make/autoconf/spec.gmk.template
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@ NEW_JAVADOC = $(INTERIM_LANGTOOLS_ARGS) $(JAVADOC_MAIN_CLASS)

JMOD_COMPRESS := @JMOD_COMPRESS@
JLINK_KEEP_PACKAGED_MODULES := @JLINK_KEEP_PACKAGED_MODULES@
JLINK_PRODUCE_RUNTIME_LINK_JDK := @JLINK_PRODUCE_RUNTIME_LINK_JDK@

RCFLAGS := @RCFLAGS@

Expand Down Expand Up @@ -899,6 +900,10 @@ STATIC_LIBS_GRAAL_IMAGE_DIR := $(IMAGES_OUTPUTDIR)/$(STATIC_LIBS_GRAAL_IMAGE_SUB
GRAAL_BUILDER_IMAGE_SUBDIR := graal-builder-jdk
GRAAL_BUILDER_IMAGE_DIR := $(IMAGES_OUTPUTDIR)/$(GRAAL_BUILDER_IMAGE_SUBDIR)

# Runtime jdk link image
RL_INTERMEDIATE_IMAGE_SUBDIR := runtime-link-initial-jdk
RL_INTERMEDIATE_IMAGE_DIR := $(IMAGES_OUTPUTDIR)/$(RL_INTERMEDIATE_IMAGE_SUBDIR)

# Macosx bundles directory definitions
JDK_MACOSX_BUNDLE_SUBDIR := jdk-bundle
JRE_MACOSX_BUNDLE_SUBDIR := jre-bundle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/*
* Copyright (c) 2023, Red Hat, Inc.
* Copyright (c) 2024, Red Hat, Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
Expand All @@ -22,18 +20,37 @@
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package build.tools.runtimelink;

package jdk.tools.jlink.internal;

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
*
* Plugins wishing to observe the command list that was used to
* trigger the link must implement this interface.
*
*/
public interface JlinkCLIArgsListener {
import jdk.internal.jimage.BasicImageReader;

public class ImageReader extends BasicImageReader implements JimageDiffGenerator.ImageResource {

public ImageReader(Path path) throws IOException {
super(path);
}

public static boolean isNotTreeInfoResource(String path) {
return !(path.startsWith("/packages") || path.startsWith("/modules"));
}

@Override
public List<String> getEntries() {
return Arrays.asList(getEntryNames()).stream()
.filter(ImageReader::isNotTreeInfoResource)
.sorted()
.collect(Collectors.toList());
}

@Override
public byte[] getResourceBytes(String name) {
return getResource(name);
}

public void process(List<String> cliArgs);
}
130 changes: 130 additions & 0 deletions make/jdk/src/classes/build/tools/runtimelink/JimageDiffGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright (c) 2024, Red Hat, Inc.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package build.tools.runtimelink;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public class JimageDiffGenerator {

private static final boolean DEBUG = false;

@SuppressWarnings("try")
public interface ImageResource extends AutoCloseable {
public List<String> getEntries();
public byte[] getResourceBytes(String name);
}

public List<ResourceDiff> generateDiff(ImageResource baseImg, ImageResource optImage) throws Exception {
List<String> baseResources;
Set<String> optResSet = new HashSet<>();
List<ResourceDiff> diffs = new ArrayList<>();
try (baseImg;
optImage) {
optResSet.addAll(optImage.getEntries());
baseResources = baseImg.getEntries();
for (String item: baseResources) {
byte[] baseBytes = baseImg.getResourceBytes(item);
// First check that every item in the base image exist in
// the optimized image as well. If it does not, it's a removed
// item in the optimized image.
if (!optResSet.remove(item)) {
// keep track of original bytes for removed item in the
// optimized image, since we need to restore them for the
// runtime image link
ResourceDiff.Builder builder = new ResourceDiff.Builder();
ResourceDiff diff = builder.setKind(ResourceDiff.Kind.REMOVED)
.setName(item)
.setResourceBytes(baseBytes)
.build();
diffs.add(diff);
continue;
}
// Verify resource bytes are equal if present in both images
boolean contentEquals = Arrays.equals(baseBytes, optImage.getResourceBytes(item));
if (!contentEquals) {
// keep track of original bytes (non-optimized)
ResourceDiff.Builder builder = new ResourceDiff.Builder();
ResourceDiff diff = builder.setKind(ResourceDiff.Kind.MODIFIED)
.setName(item)
.setResourceBytes(baseBytes)
.build();
diffs.add(diff);
}
}
}
// What's now left in optResSet are the resources only present in the
// optimized image (generated by some plugins; not present in jmods)
for (String e: optResSet) {
ResourceDiff.Builder builder = new ResourceDiff.Builder();
ResourceDiff diff = builder.setKind(ResourceDiff.Kind.ADDED)
.setName(e)
.build();
diffs.add(diff);
}
return diffs;
}

public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: java -cp jrt-fs.jar:. JimageDiffGenerator <packaged-modules> <image-to-compare> <output-file>");
System.exit(1);
}
ImageResource base = new JmodsReader(Path.of(args[0]));
ImageResource opt = new ImageReader(Path.of(args[1]));
JimageDiffGenerator diffGen = new JimageDiffGenerator();
List<ResourceDiff> diffs = diffGen.generateDiff(base, opt);

if (DEBUG) {
printDiffs(diffs);
}
FileOutputStream fout = new FileOutputStream(new File(args[2]));
ResourceDiff.write(diffs, fout);
}

private static void printDiffs(List<ResourceDiff> diffs) {
for (ResourceDiff diff: diffs.stream().sorted().collect(Collectors.toList())) {
switch (diff.getKind()) {
case ADDED:
System.out.println("Only added in opt: " + diff.getName());
break;
case MODIFIED:
System.out.println("Modified in opt: " + diff.getName());
break;
case REMOVED:
System.out.println("Removed in opt: " + diff.getName());
break;
default:
break;
}
}
}

}
Loading

0 comments on commit d9653ec

Please sign in to comment.