Skip to content

Commit

Permalink
Support packaging the JDK default CDS image in hermetic deploy JAR us…
Browse files Browse the repository at this point in the history
…ing 'java_runtime.default_cds' attribute.

If a hermetic 'java_runtime' target specifies the 'default_cds' attribute, and the 'java_binary' target does not provide its own CDS archive (by specifying the 'classlist' attribute), the java_runtime's default CDS is packaged when building a hermetic deploy JAR. This CL simply bridges the current hermetic support with the earlier work (unknown commit has added the related single JAR connection for embedding CDS) for deploy JAR embedded CDS.

The 'java_runtime' change is in unknown commit, which has additional integration test cases for the hermetic default CDS.

#hermetic-java-static-linking

PiperOrigin-RevId: 510195888
Change-Id: I51e8fef364bcb7c53c42d010cd51dbc4f689247c
  • Loading branch information
Googler authored and copybara-github committed Feb 16, 2023
1 parent f39272a commit 53ff268
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
ImmutableList<String> deployManifestLines =
getDeployManifestLines(ruleContext, originalMainClass);

// Create the java_binary target specific CDS archive.
Artifact jsa = createSharedArchive(ruleContext, javaArtifacts, attributes);

if (ruleContext.isAttrDefined("hermetic", BOOLEAN)
Expand All @@ -428,6 +429,13 @@ public ConfiguredTarget create(RuleContext ruleContext)
.setLibModules(javaRuntime.libModules())
.setHermeticInputs(javaRuntime.hermeticInputs());
}

if (jsa == null) {
// Use the JDK default CDS specified by the JavaRuntime if the
// java_binary target specific CDS archive is null, when building
// a hermetic deploy JAR.
jsa = javaRuntime.defaultCDS();
}
}

deployArchiveBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public ConfiguredTarget create(RuleContext ruleContext)
ImmutableList<CcInfo> hermeticStaticLibs =
ImmutableList.copyOf(ruleContext.getPrerequisites("hermetic_static_libs", CcInfo.PROVIDER));

// If a runtime does not set default_cds in hermetic mode, it is not fatal.
// We can skip the default CDS in the check below.
Artifact defaultCDS = ruleContext.getPrerequisiteArtifact("default_cds");

if ((!hermeticInputs.isEmpty() || libModules != null || !hermeticStaticLibs.isEmpty())
&& (hermeticInputs.isEmpty() || libModules == null || hermeticStaticLibs.isEmpty())) {
ruleContext.attributeError(
Expand All @@ -124,6 +128,7 @@ public ConfiguredTarget create(RuleContext ruleContext)
javaBinaryRunfilesPath,
hermeticInputs,
libModules,
defaultCDS,
hermeticStaticLibs);

TemplateVariableInfo templateVariableInfo =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static JavaRuntimeInfo create(
PathFragment javaBinaryRunfilesPath,
NestedSet<Artifact> hermeticInputs,
@Nullable Artifact libModules,
@Nullable Artifact defaultCDS,
ImmutableList<CcInfo> hermeticStaticLibs) {
return new JavaRuntimeInfo(
javaBaseInputs,
Expand All @@ -60,6 +61,7 @@ public static JavaRuntimeInfo create(
javaBinaryRunfilesPath,
hermeticInputs,
libModules,
defaultCDS,
hermeticStaticLibs);
}

Expand Down Expand Up @@ -121,6 +123,7 @@ private static JavaRuntimeInfo from(RuleContext ruleContext, ToolchainInfo toolc
private final PathFragment javaBinaryRunfilesPath;
private final NestedSet<Artifact> hermeticInputs;
@Nullable private final Artifact libModules;
@Nullable private final Artifact defaultCDS;
private final ImmutableList<CcInfo> hermeticStaticLibs;

private JavaRuntimeInfo(
Expand All @@ -131,6 +134,7 @@ private JavaRuntimeInfo(
PathFragment javaBinaryRunfilesPath,
NestedSet<Artifact> hermeticInputs,
@Nullable Artifact libModules,
@Nullable Artifact defaultCDS,
ImmutableList<CcInfo> hermeticStaticLibs) {
this.javaBaseInputs = javaBaseInputs;
this.javaHome = javaHome;
Expand All @@ -139,6 +143,7 @@ private JavaRuntimeInfo(
this.javaBinaryRunfilesPath = javaBinaryRunfilesPath;
this.hermeticInputs = hermeticInputs;
this.libModules = libModules;
this.defaultCDS = defaultCDS;
this.hermeticStaticLibs = hermeticStaticLibs;
}

Expand Down Expand Up @@ -199,6 +204,12 @@ public Artifact libModules() {
return libModules;
}

@Override
@Nullable
public Artifact defaultCDS() {
return defaultCDS;
}

public ImmutableList<CcInfo> hermeticStaticLibs() {
return hermeticStaticLibs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
.singleArtifact()
.allowedFileTypes(FileTypeSet.ANY_FILE)
.exec())
/* <!-- #BLAZE_RULE(java_runtime).ATTRIBUTE(default_cds) -->
Default CDS archive for hermetic <code>java_runtime</code>. When hermetic
is enabled for a <code>java_binary</code> target and if the target does not
provide its own CDS archive by specifying the
<a href="${link java_binary.classlist}"><code>classlist</code></a> attribute,
the <code>java_runtime</code> default CDS is packaged in the hermetic deploy JAR.
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
.add(
attr("default_cds", LABEL)
.singleArtifact()
.allowedFileTypes(FileTypeSet.ANY_FILE)
.exec())
.add(
attr("hermetic_static_libs", LABEL_LIST)
.mandatoryProviders(StarlarkProviderIdentifier.forKey(CcInfo.PROVIDER.getKey()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ public interface JavaRuntimeInfoApi extends StructApi {
@Nullable
FileApi libModules();

/** The JDK default CDS. */
@StarlarkMethod(
name = "default_cds",
doc = "Returns the JDK default CDS archive.",
structField = true,
allowReturnNones = true)
@Nullable
FileApi defaultCDS();

/** The JDK static libraries needed for hermetic deployments. */
@StarlarkMethod(
name = "hermetic_static_libs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,6 @@ def _create_deploy_archive(
if one_version_level == "WARNING":
args.add("--succeed_on_found_violations")

if shared_archive:
input_files.append(shared_archive)
args.add("--cds_archive", shared_archive)

if multi_release:
args.add("--multi_release")

Expand All @@ -197,6 +193,13 @@ def _create_deploy_archive(
args.add_all("--resources", hermetic_files)
input_files.append(lib_modules)

if shared_archive == None:
shared_archive = runtime.default_cds

if shared_archive:
input_files.append(shared_archive)
args.add("--cds_archive", shared_archive)

args.add_all("--add_exports", add_exports)
args.add_all("--add_opens", add_opens)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public void equalityIsObjectIdentity() {
PathFragment.create(""),
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
null,
null,
ImmutableList.of());
JavaRuntimeInfo b =
JavaRuntimeInfo.create(
Expand All @@ -47,6 +48,7 @@ public void equalityIsObjectIdentity() {
PathFragment.create(""),
NestedSetBuilder.emptySet(Order.STABLE_ORDER),
null,
null,
ImmutableList.of());

new EqualsTester().addEqualityGroup(a).addEqualityGroup(b).testEquals();
Expand Down

0 comments on commit 53ff268

Please sign in to comment.