Skip to content

Commit

Permalink
8264322: Generate CDS archive when creating custom JDK image
Browse files Browse the repository at this point in the history
Reviewed-by: mchung, alanb
  • Loading branch information
calvinccheung committed Aug 24, 2021
1 parent f681d65 commit f608e81
Show file tree
Hide file tree
Showing 11 changed files with 365 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -86,15 +86,17 @@ static final class DefaultExecutableImage implements ExecutableImage {
private final Path home;
private final List<String> args;
private final Set<String> modules;
private final Platform platform;

DefaultExecutableImage(Path home, Set<String> modules) {
DefaultExecutableImage(Path home, Set<String> modules, Platform p) {
Objects.requireNonNull(home);
if (!Files.exists(home)) {
throw new IllegalArgumentException("Invalid image home");
}
this.home = home;
this.modules = Collections.unmodifiableSet(modules);
this.args = createArgs(home);
this.platform = p;
}

private static List<String> createArgs(Path home) {
Expand Down Expand Up @@ -127,13 +129,18 @@ public void storeLaunchArgs(List<String> args) {
throw new UncheckedIOException(ex);
}
}

@Override
public Platform getTargetPlatform() {
return platform;
}
}

private final Path root;
private final Map<String, String> launchers;
private final Path mdir;
private final Set<String> modules = new HashSet<>();
private Platform targetPlatform;
private Platform platform;

/**
* Default image builder constructor.
Expand All @@ -148,6 +155,11 @@ public DefaultImageBuilder(Path root, Map<String, String> launchers) throws IOEx
Files.createDirectories(mdir);
}

@Override
public Platform getTargetPlatform() {
return platform;
}

@Override
public void storeFiles(ResourcePool files) {
try {
Expand All @@ -158,7 +170,7 @@ public void storeFiles(ResourcePool files) {
if (value == null) {
throw new PluginException("ModuleTarget attribute is missing for java.base module");
}
this.targetPlatform = Platform.toPlatform(value);
this.platform = Platform.parsePlatform(value);

checkResourcePool(files);

Expand Down Expand Up @@ -474,7 +486,7 @@ private String nativeDir(String filename) {
}

private boolean isWindows() {
return targetPlatform == Platform.WINDOWS;
return platform.os() == Platform.OperatingSystem.WINDOWS;
}

/**
Expand Down Expand Up @@ -509,7 +521,7 @@ private void setReadOnly(Path file) {

@Override
public ExecutableImage getExecutableImage() {
return new DefaultExecutableImage(root, modules);
return new DefaultExecutableImage(root, modules, platform);
}

// This is experimental, we should get rid-off the scripts in a near future
Expand Down Expand Up @@ -551,7 +563,10 @@ public static ExecutableImage getExecutableImage(Path root) {
Path binDir = root.resolve(BIN_DIRNAME);
if (Files.exists(binDir.resolve("java")) ||
Files.exists(binDir.resolve("java.exe"))) {
return new DefaultExecutableImage(root, retrieveModules(root));
// It may be possible to extract the platform info from the given image.
// --post-process-path is a hidden option and pass unknown platform for now.
// --generate-cds-archive plugin cannot be used with --post-process-path option.
return new DefaultExecutableImage(root, retrieveModules(root), Platform.UNKNOWN);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -28,6 +28,7 @@
import java.util.Properties;

import jdk.tools.jlink.internal.ExecutableImage;
import jdk.tools.jlink.internal.Platform;
import jdk.tools.jlink.plugin.PluginException;
import jdk.tools.jlink.plugin.ResourcePool;

Expand Down Expand Up @@ -74,4 +75,13 @@ public default void storeFiles(ResourcePool content) {
* @throws PluginException
*/
public ExecutableImage getExecutableImage();

/**
* Gets the platform of the image.
*
* @return {@code Platform} object representing the platform of the image
*/
public default Platform getTargetPlatform() {
return Platform.UNKNOWN;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -61,4 +61,11 @@ public interface ExecutableImage {
* @param args Additional arguments
*/
public void storeLaunchArgs(List<String> args);

/**
* The Platform of the image.
*
* @return Platform
*/
public Platform getTargetPlatform();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down
116 changes: 90 additions & 26 deletions src/jdk.jlink/share/classes/jdk/tools/jlink/internal/Platform.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand All @@ -24,48 +24,112 @@
*/
package jdk.tools.jlink.internal;

import jdk.tools.jlink.plugin.ResourcePoolModule;

import java.util.Locale;

/**
* Supported platforms
*/
public enum Platform {
WINDOWS,
LINUX,
MACOS,
AIX,
UNKNOWN;
public record Platform(OperatingSystem os, Architecture arch) {

/**
* Returns the {@code Platform} derived from the target platform
* in the {@code ModuleTarget} attribute.
public enum OperatingSystem {
WINDOWS,
LINUX,
MACOS,
AIX,
UNKNOWN;
}

public enum Architecture {
X86,
x64,
ARM,
AARCH64,
UNKNOWN;
}

public static final Platform UNKNOWN = new Platform(OperatingSystem.UNKNOWN, Architecture.UNKNOWN);

/*
* Returns the {@code Platform} based on the platformString of the form <operating system>-<arch>.
*/
public static Platform toPlatform(String targetPlatform) {
public static Platform parsePlatform(String platformString) {
String osName;
int index = targetPlatform.indexOf("-");
String archName;
int index = platformString.indexOf("-");
if (index < 0) {
osName = targetPlatform;
osName = platformString;
archName = "UNKNOWN";
} else {
osName = targetPlatform.substring(0, index);
osName = platformString.substring(0, index);
archName = platformString.substring(index + 1);
}
OperatingSystem os;
try {
return Platform.valueOf(osName.toUpperCase(Locale.ENGLISH));
os = OperatingSystem.valueOf(osName.toUpperCase(Locale.ENGLISH));
} catch (IllegalArgumentException e) {
return Platform.UNKNOWN;
os = OperatingSystem.UNKNOWN;
}
Architecture arch = toArch(archName);
return new Platform(os, arch);
}

/**
* Returns the {@code Platform} to which the given module is target to.
* @return true is it's a 64-bit platform
*/
public static Platform getTargetPlatform(ResourcePoolModule module) {
String targetPlatform = module.targetPlatform();
if (targetPlatform != null) {
return toPlatform(targetPlatform);
} else {
return Platform.UNKNOWN;
}
public boolean is64Bit() {
return (arch() == Platform.Architecture.x64 ||
arch() == Platform.Architecture.AARCH64);
}

/**
* Returns the runtime {@code Platform}.
*/
public static Platform runtime() {
return new Platform(runtimeOS(), runtimeArch());
}

/**
* Returns a {@code String} representation of a {@code Platform} in the format of <os>-<arch>
*/
@Override
public String toString() {
return os.toString().toLowerCase() + "-" + arch.toString().toLowerCase();
}

/**
* Returns the runtime {@code Platform.OperatingSystem}.
*/
private static OperatingSystem runtimeOS() {
String osName = System.getProperty("os.name").substring(0, 3).toLowerCase();
OperatingSystem os = switch (osName) {
case "win" -> OperatingSystem.WINDOWS;
case "lin" -> OperatingSystem.LINUX;
case "mac" -> OperatingSystem.MACOS;
case "aix" -> OperatingSystem.AIX;
default -> OperatingSystem.UNKNOWN;
};
return os;
}

/**
* Returns the runtime {@code Platform.Architechrure}.
*/
private static Architecture runtimeArch() {
String archName = System.getProperty("os.arch");
return toArch(archName);
}

/**
* Returns the {@code Platform.Architecture} based on the archName.
*/
private static Architecture toArch(String archName) {
Architecture arch = switch (archName) {
case "x86" -> Architecture.X86;
case "amd64", "x86_64" -> Architecture.x64;
case "arm" -> Architecture.ARM;
case "aarch64" -> Architecture.AARCH64;
default -> Architecture.UNKNOWN;
};
return arch;
}
}
Loading

3 comments on commit f608e81

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MBaesken
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on f608e81 Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MBaesken the backport was successfully created on the branch backport-MBaesken-f608e81a in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit f608e81a from the openjdk/jdk repository.

The commit being backported was authored by Calvin Cheung on 24 Aug 2021 and was reviewed by Mandy Chung and Alan Bateman.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git backport-MBaesken-f608e81a:backport-MBaesken-f608e81a
$ git checkout backport-MBaesken-f608e81a
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git backport-MBaesken-f608e81a

Please sign in to comment.