Skip to content

Commit

Permalink
Expose a new java_library rule factory with sharded java compilation …
Browse files Browse the repository at this point in the history
…support

I think exporting a factory method makes more sense, to allow consumers to export the rule with whatever name they like, and set a default value for the shard size. Tucking it under the experimental export also lets us get the flag-guarding for free.

PiperOrigin-RevId: 565396979
Change-Id: I758d07a6eb9c95c6d44c5698f725b4a4a1e5185f
  • Loading branch information
hvadehra authored and copybara-github committed Sep 14, 2023
1 parent ba8ed5b commit 82883fb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 22 deletions.
11 changes: 6 additions & 5 deletions src/main/starlark/builtins_bzl/bazel/exports.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@

"""Exported builtins symbols that are specific to OSS Bazel."""

load("@_builtins//:common/java/java_library.bzl", "JAVA_LIBRARY_ATTRS", "bazel_java_library_rule", "java_library")
load("@_builtins//:common/java/java_plugin.bzl", "java_plugin")
load("@_builtins//:common/cc/cc_proto_library.bzl", "cc_proto_aspect", "cc_proto_library")
load("@_builtins//:common/java/java_import.bzl", "java_import")
load("@_builtins//:common/java/java_library.bzl", "JAVA_LIBRARY_ATTRS", "bazel_java_library_rule", "java_library", "make_sharded_java_library")
load("@_builtins//:common/java/java_plugin.bzl", "java_plugin")
load("@_builtins//:common/java/proto/java_proto_library.bzl", "java_proto_library")
load("@_builtins//:common/cc/cc_proto_library.bzl", "cc_proto_aspect", "cc_proto_library")
load(":bazel/java/bazel_java_binary_wrapper.bzl", "java_binary", "java_test")
load("@_builtins//:common/python/py_binary_macro.bzl", "py_binary")
load("@_builtins//:common/python/py_internal.bzl", "py_internal")
load("@_builtins//:common/python/py_library_macro.bzl", "py_library")
load("@_builtins//:common/python/py_test_macro.bzl", "py_test")
load("@_builtins//:common/python/py_internal.bzl", "py_internal")
load(":bazel/java/bazel_java_binary_wrapper.bzl", "java_binary", "java_test")

exported_toplevels = {
# This is an experimental export in Bazel. The interface will change in a way
Expand All @@ -32,6 +32,7 @@ exported_toplevels = {
"experimental_java_library_export_do_not_use": struct(
bazel_java_library_rule = bazel_java_library_rule,
JAVA_LIBRARY_ATTRS = JAVA_LIBRARY_ATTRS,
sharded_java_library = make_sharded_java_library,
),
"cc_proto_aspect": cc_proto_aspect,
"py_internal": py_internal,
Expand Down
44 changes: 27 additions & 17 deletions src/main/starlark/builtins_bzl/common/java/java_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
Definition of java_library rule.
"""

load(":common/java/basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "construct_defaultinfo")
load(":common/rule_util.bzl", "merge_attrs")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/cc/cc_info.bzl", "CcInfo")
load(":common/java/basic_java_library.bzl", "BASIC_JAVA_LIBRARY_IMPLICIT_ATTRS", "basic_java_library", "construct_defaultinfo")
load(":common/java/java_info.bzl", "JavaInfo", "JavaPluginInfo")
load(":common/java/java_semantics.bzl", "semantics")
load(":common/rule_util.bzl", "merge_attrs")

def bazel_java_library_rule(
ctx,
Expand Down Expand Up @@ -166,17 +166,27 @@ JAVA_LIBRARY_ATTRS = merge_attrs(
},
)

java_library = rule(
_proxy,
attrs = merge_attrs(
JAVA_LIBRARY_ATTRS,
{"_use_auto_exec_groups": attr.bool(default = True)},
),
provides = [JavaInfo],
outputs = {
"classjar": "lib%{name}.jar",
"sourcejar": "lib%{name}-src.jar",
},
fragments = ["java", "cpp"],
toolchains = [semantics.JAVA_TOOLCHAIN],
)
def _make_java_library_rule(extra_attrs = {}):
return rule(
_proxy,
attrs = merge_attrs(
JAVA_LIBRARY_ATTRS,
{"_use_auto_exec_groups": attr.bool(default = True)},
extra_attrs,
),
provides = [JavaInfo],
outputs = {
"classjar": "lib%{name}.jar",
"sourcejar": "lib%{name}-src.jar",
},
fragments = ["java", "cpp"],
toolchains = [semantics.JAVA_TOOLCHAIN],
)

java_library = _make_java_library_rule()

# for experimental_java_library_export_do_not_use
def make_sharded_java_library(default_shard_size):
return _make_java_library_rule({
"experimental_javac_shard_size": attr.int(default = default_shard_size),
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ java_library(
name = "JavaTests_lib",
srcs = glob(["*.java"]),
deps = [
"//src/main/java/com/google/devtools/build/lib/actions",
"//src/main/java/com/google/devtools/build/lib/analysis:actions/template_expansion_action",
"//src/main/java/com/google/devtools/build/lib/analysis:configured_target",
"//src/main/java/com/google/devtools/build/lib/bazel/rules/java:bazel_java_semantics",
"//src/main/java/com/google/devtools/build/lib/packages",
"//src/main/java/com/google/devtools/build/lib/rules/java:java-compilation",
"//src/main/java/com/google/devtools/build/lib/util:os",
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",
"//src/test/java/com/google/devtools/build/lib/analysis/util",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
import static com.google.devtools.build.lib.testutil.TestConstants.TOOLS_REPOSITORY;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.Action;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.actions.TemplateExpansionAction;
import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
import com.google.devtools.build.lib.rules.java.JavaCompileAction;
import com.google.devtools.build.lib.util.OS;
import java.util.Arrays;
import java.util.Objects;
Expand Down Expand Up @@ -93,4 +96,37 @@ public void javaTestSetsSecurityManagerPropertyOnVersion17() throws Exception {
assertThat(jvmFlags).contains("-Djava.security.manager=allow");
}
}

@Test
public void experimentalShardedJavaLibrary_succeeds() throws Exception {
setBuildLanguageOptions("--experimental_java_library_export");
scratch.file(
"foo/rule.bzl",
//
"java_library = experimental_java_library_export_do_not_use.sharded_java_library(",
" default_shard_size = 10",
")");
scratch.file(
"foo/BUILD",
"load(':rule.bzl', 'java_library')",
"",
"java_library(",
" name = 'lib1',",
" srcs = ['1.java', '2.java', '3.java'],",
" experimental_javac_shard_size = 1,",
")",
"java_library(",
" name = 'lib2',",
" srcs = ['1.java', '2.java', '3.java'],",
" experimental_javac_shard_size = 2,",
")");

ImmutableList<Action> compileActionsWithShardSize1 =
getActions("//foo:lib1", JavaCompileAction.class);
ImmutableList<Action> compileActionsWithShardSize2 =
getActions("//foo:lib2", JavaCompileAction.class);

assertThat(compileActionsWithShardSize1).hasSize(3);
assertThat(compileActionsWithShardSize2).hasSize(2);
}
}

0 comments on commit 82883fb

Please sign in to comment.