Skip to content

Commit 4e38b17

Browse files
hvadehrarules_java Copybara
authored and
rules_java Copybara
committed
Implement android_common.enable_implicit_sourceless_deps_exports_compatibility in rules_java
Work towards dropping native `JavaInfo` provider wrappers. PiperOrigin-RevId: 707431633 Change-Id: Iff76922764f76551ff7affea7bfbd3b9d495cf47
1 parent b3a0580 commit 4e38b17

File tree

9 files changed

+157
-1
lines changed

9 files changed

+157
-1
lines changed

.bazelci/presubmit.yml

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ test_targets: &test_targets
2525

2626
test_targets_bazel6: &test_targets_bazel6
2727
- "//java/test/..."
28+
- "-//java/test/private/..."
2829

2930
test_target_integration: &test_target_integration
3031
- "//:MyTest"

java/private/BUILD

+9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ bzl_library(
4444
deps = ["@compatibility_proxy//:proxy_bzl"],
4545
)
4646

47+
# Exposed for use by the android rules.
48+
bzl_library(
49+
name = "android_support",
50+
srcs = ["android_support.bzl"],
51+
visibility = ["//visibility:public"],
52+
deps = ["@compatibility_proxy//:proxy_bzl"],
53+
)
54+
4755
filegroup(
4856
name = "srcs",
4957
srcs = glob(["**"]),
@@ -55,6 +63,7 @@ filegroup(
5563
testonly = 1,
5664
srcs = [
5765
"BUILD",
66+
":android_support",
5867
":internals",
5968
":native_bzl",
6069
":proto_support",

java/private/android_support.bzl

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2024 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Legacy support for the android rules."""
15+
16+
load("@compatibility_proxy//:proxy.bzl", "java_info_to_implicit_exportable")
17+
18+
android_support = struct(
19+
enable_implicit_sourceless_deps_exports_compatibility = java_info_to_implicit_exportable,
20+
)

java/private/java_info.bzl

+27
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,33 @@ def to_java_binary_info(java_info, compilation_info):
248248
result.update(_is_binary = True)
249249
return _new_javainfo(**result)
250250

251+
def to_implicit_exportable(java_info, neverlink = False):
252+
result = {
253+
"transitive_runtime_jars": depset() if neverlink else java_info.transitive_runtime_jars,
254+
"transitive_compile_time_jars": java_info.transitive_compile_time_jars,
255+
"compile_jars": java_info.compile_jars,
256+
"full_compile_jars": java_info.full_compile_jars,
257+
"_transitive_full_compile_time_jars": java_info._transitive_full_compile_time_jars,
258+
"_compile_time_java_dependencies": java_info._compile_time_java_dependencies,
259+
"_neverlink": neverlink,
260+
# unset defaults
261+
"source_jars": [],
262+
"outputs": _JavaRuleOutputJarsInfo(jars = [], jdeps = None, native_headers = None),
263+
"annotation_processing": None,
264+
"runtime_output_jars": [],
265+
"transitive_source_jars": depset(),
266+
"transitive_native_libraries": depset(),
267+
"cc_link_params_info": CcInfo(),
268+
"module_flags_info": _EMPTY_MODULE_FLAGS_INFO,
269+
"plugins": _EMPTY_PLUGIN_DATA,
270+
"api_generating_plugins": _EMPTY_PLUGIN_DATA,
271+
"java_outputs": [],
272+
"compilation_info": None,
273+
"_constraints": [],
274+
"_is_binary": getattr(java_info, "_is_binary", False),
275+
}
276+
return _new_javainfo(**result)
277+
251278
def _to_mutable_dict(java_info):
252279
return {
253280
key: getattr(java_info, key)

java/rules_java_deps.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ load("@rules_java//java/common/rules:java_runtime.bzl", _java_runtime = "java_ru
3939
load("@rules_java//java/common/rules:java_toolchain.bzl", _java_toolchain = "java_toolchain")
4040
load("@rules_java//java/private:java_common.bzl", _java_common = "java_common")
4141
load("@rules_java//java/private:java_common_internal.bzl", _java_common_internal_compile = "compile")
42-
load("@rules_java//java/private:java_info.bzl", _JavaInfo = "JavaInfo", _JavaPluginInfo = "JavaPluginInfo", _java_info_internal_merge = "merge")
42+
load("@rules_java//java/private:java_info.bzl", _JavaInfo = "JavaInfo", _JavaPluginInfo = "JavaPluginInfo",
43+
_java_info_internal_merge = "merge", _java_info_to_implicit_exportable = "to_implicit_exportable")
4344
4445
java_binary = _java_binary
4546
java_import = _java_import
@@ -54,6 +55,7 @@ JavaInfo = _JavaInfo
5455
JavaPluginInfo = _JavaPluginInfo
5556
java_common_internal_compile = _java_common_internal_compile
5657
java_info_internal_merge = _java_info_internal_merge
58+
java_info_to_implicit_exportable = _java_info_to_implicit_exportable
5759
http_jar = _http_jar
5860
""",
5961
)
@@ -95,6 +97,8 @@ JavaInfo = NativeJavaInfo
9597
JavaPluginInfo = NativeJavaPluginInfo
9698
java_common_internal_compile = None
9799
java_info_internal_merge = None
100+
# Not available before Bazel 7
101+
java_info_to_implicit_exportable = getattr(android_common, "enable_implicit_sourceless_deps_exports_compatibility", None)
98102
99103
http_jar = _http_jar
100104
""",

java/test/private/BUILD

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Copyright 2021 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("//java:defs.bzl", "java_library", "java_plugin")
16+
load(":android_support_tests.bzl", "android_support_tests", "my_rule")
17+
18+
java_plugin(
19+
name = "my_plugin",
20+
srcs = ["MyPlugin.java"],
21+
)
22+
23+
java_library(
24+
name = "foo",
25+
srcs = ["Foo.java"],
26+
exported_plugins = [":my_plugin"],
27+
)
28+
29+
my_rule(
30+
name = "bar",
31+
dep = ":foo",
32+
)
33+
34+
android_support_tests(name = "android_support_tests")

java/test/private/Foo.java

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class Foo {}

java/test/private/MyPlugin.java

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public class MyPlugin {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2021 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""Tests for //java/private:android_support.bzl"""
15+
16+
load("@rules_testing//lib:analysis_test.bzl", "analysis_test", "test_suite")
17+
load("//java/common:java_info.bzl", "JavaInfo")
18+
load("//java/private:android_support.bzl", "android_support")
19+
20+
def _impl(ctx):
21+
return [
22+
android_support.enable_implicit_sourceless_deps_exports_compatibility(ctx.attr.dep[JavaInfo]),
23+
]
24+
25+
my_rule = rule(
26+
implementation = _impl,
27+
attrs = {
28+
"dep": attr.label(),
29+
},
30+
)
31+
32+
def _test_enable_implicit_sourceless_deps_exports_compatibility(name):
33+
analysis_test(
34+
name = name,
35+
impl = _test_enable_implicit_sourceless_deps_exports_compatibility_impl,
36+
targets = {
37+
"foo": Label(":foo"),
38+
"bar": Label(":bar"),
39+
},
40+
)
41+
42+
def _test_enable_implicit_sourceless_deps_exports_compatibility_impl(env, targets):
43+
# TODO(hvd): write a ProviderSubject for JavaInfo
44+
foo_javainfo = targets.foo[JavaInfo]
45+
bar_javainfo = targets.bar[JavaInfo]
46+
for attr in ["transitive_runtime_jars", "compile_jars", "transitive_compile_time_jars", "full_compile_jars", "_transitive_full_compile_time_jars", "_compile_time_java_dependencies"]:
47+
env.expect.that_bool(getattr(foo_javainfo, attr) == getattr(bar_javainfo, attr)).equals(True)
48+
env.expect.that_depset_of_files(foo_javainfo.plugins.processor_jars).contains_exactly([
49+
"java/test/private/libmy_plugin.jar",
50+
])
51+
env.expect.that_depset_of_files(bar_javainfo.plugins.processor_jars).contains_exactly([])
52+
53+
def android_support_tests(name):
54+
test_suite(
55+
name = name,
56+
tests = [
57+
_test_enable_implicit_sourceless_deps_exports_compatibility,
58+
],
59+
)

0 commit comments

Comments
 (0)