Skip to content

Commit

Permalink
Move build info computation back to java binary/test deploy jar rule
Browse files Browse the repository at this point in the history
This partially reverts an earlier change[1] and avoids adding a target to every java_binary which causes a memory regression.

PiperOrigin-RevId: 513206721
Change-Id: Ifba10792a2c01b7f9470b06168306999df4bcd26
  • Loading branch information
hvadehra authored and copybara-github committed Mar 1, 2023
1 parent 7845943 commit 3b79eeb
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.Iterables;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.RuleContext;
import com.google.devtools.build.lib.analysis.TransitiveInfoCollection;
import com.google.devtools.build.lib.analysis.configuredtargets.AbstractConfiguredTarget;
import com.google.devtools.build.lib.analysis.configuredtargets.MergedConfiguredTarget;
Expand Down Expand Up @@ -445,11 +446,16 @@ public JavaInfo toJavaBinaryInfo(JavaInfo javaInfo, StarlarkThread thread) throw
}

@Override
public Sequence<Artifact> getBuildInfo(StarlarkRuleContext ruleContext, StarlarkThread thread)
public Sequence<Artifact> getBuildInfo(
StarlarkRuleContext starlarkRuleContext, boolean isStampingEnabled, StarlarkThread thread)
throws EvalException, InterruptedException {
checkPrivateAccess(thread);
RuleContext ruleContext = starlarkRuleContext.getRuleContext();
return StarlarkList.immutableCopyOf(
ruleContext.getRuleContext().getBuildInfo(JavaBuildInfoFactory.KEY));
ruleContext
.getAnalysisEnvironment()
.getBuildInfo(
isStampingEnabled, JavaBuildInfoFactory.KEY, ruleContext.getConfiguration()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,9 +699,10 @@ String getTargetKind(Object target, boolean dereferenceAliases, StarlarkThread t
@StarlarkMethod(
name = "get_build_info",
documented = false,
parameters = {@Param(name = "ctx", doc = "The rule context")},
parameters = {@Param(name = "ctx"), @Param(name = "is_stamping_enabled")},
useStarlarkThread = true)
Sequence<FileT> getBuildInfo(StarlarkRuleContextT ruleContext, StarlarkThread thread)
Sequence<FileT> getBuildInfo(
StarlarkRuleContextT ruleContext, boolean isStampingEnabled, StarlarkThread thread)
throws EvalException, InterruptedException;

@StarlarkMethod(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _bazel_deploy_jars_impl(ctx):
info.main_class,
info.coverage_main_class,
info.strip_as_default,
info.build_info_files,
info.stamp,
str(ctx.attr.binary.label),
manifest_lines = info.manifest_lines,
)
Expand Down
4 changes: 2 additions & 2 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ InternalDeployJarInfo = provider(
"main_class",
"coverage_main_class",
"strip_as_default",
"build_info_files",
"stamp",
"hermetic",
"add_exports",
"add_opens",
Expand Down Expand Up @@ -292,7 +292,7 @@ def basic_java_binary(
main_class = main_class,
coverage_main_class = coverage_main_class,
strip_as_default = strip_as_default,
build_info_files = semantics.get_build_info(ctx, ctx.attr.stamp),
stamp = ctx.attr.stamp,
hermetic = hasattr(ctx.attr, "hermetic") and ctx.attr.hermetic,
add_exports = add_exports,
add_opens = add_opens,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def create_deploy_archives(
main_class,
coverage_main_class,
strip_as_default,
build_info_files,
stamp,
build_target,
hermetic = False,
add_exports = depset(),
Expand All @@ -55,6 +55,7 @@ def create_deploy_archives(
coverage_main_class: (String) FQN of the entry point for coverage collection
build_target: (String) Name of the build target for stamping
strip_as_default: (bool) Whether to create unstripped deploy jar
stamp: (bool) Value of stamping attribute on the rule
hermetic: (bool)
add_exports: (depset)
add_opens: (depset)
Expand All @@ -76,6 +77,8 @@ def create_deploy_archives(
)
multi_release = ctx.fragments.java.multi_release_deploy_jars

build_info_files = semantics.get_build_info(ctx, stamp)

_create_deploy_archive(
ctx,
launcher_info.launcher,
Expand Down Expand Up @@ -250,6 +253,9 @@ def make_deploy_jars_rule(implementation):
"_cc_toolchain": attr.label(default = "@" + cc_semantics.get_repo() + "//tools/cpp:current_cc_toolchain"),
"_java_toolchain_type": attr.label(default = semantics.JAVA_TOOLCHAIN_TYPE),
"_java_runtime_toolchain_type": attr.label(default = semantics.JAVA_RUNTIME_TOOLCHAIN_TYPE),
"_build_info_translator": attr.label(
default = semantics.BUILD_INFO_TRANSLATOR_LABEL,
),
},
outputs = _implicit_outputs,
fragments = ["java"],
Expand Down
10 changes: 8 additions & 2 deletions src/main/starlark/builtins_bzl/common/java/java_semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ def _find_java_toolchain(ctx):
def _find_java_runtime_toolchain(ctx):
return ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].java_runtime

def _get_build_info(ctx, _):
return java_common.get_build_info(ctx)
def _stamping_enabled(ctx, stamp):
if ctx.configuration.is_tool_configuration():
stamp = 0
return (stamp == 1) or (stamp == -1 and ctx.configuration.stamp_binaries())

def _get_build_info(ctx, stamp):
return java_common.get_build_info(ctx, _stamping_enabled(ctx, stamp))

semantics = struct(
JAVA_TOOLCHAIN_LABEL = "@bazel_tools//tools/jdk:current_java_toolchain",
Expand Down Expand Up @@ -78,4 +83,5 @@ semantics = struct(
get_coverage_runner = _get_coverage_runner,
add_constraints = _add_constraints,
JAVA_STUB_TEMPLATE_LABEL = "@bazel_tools//tools/jdk:java_stub_template.txt",
BUILD_INFO_TRANSLATOR_LABEL = None,
)
Original file line number Diff line number Diff line change
Expand Up @@ -3213,7 +3213,7 @@ public void testGetBuildInfoArtifactsIsPrivateApi() throws Exception {
scratch.file(
"foo/custom_rule.bzl",
"def _impl(ctx):",
" artifacts = java_common.get_build_info(ctx)",
" artifacts = java_common.get_build_info(ctx, True)",
" return [DefaultInfo(files = depset(artifacts))]",
"custom_rule = rule(",
" implementation = _impl,",
Expand Down Expand Up @@ -3466,7 +3466,7 @@ public void testGetBuildInfoArtifacts() throws Exception {
scratch.file(
"bazel_internal/test/custom_rule.bzl",
"def _impl(ctx):",
" artifacts = java_common.get_build_info(ctx)",
" artifacts = java_common.get_build_info(ctx, False)",
" return [DefaultInfo(files = depset(artifacts))]",
"custom_rule = rule(",
" implementation = _impl,",
Expand Down

0 comments on commit 3b79eeb

Please sign in to comment.