Skip to content

Commit d77bdac

Browse files
Move java_{lite_}proto_library from Bazel repository
Tests will be submitted in separate PRs. PiperOrigin-RevId: 674321127
1 parent 50eafe9 commit d77bdac

14 files changed

+483
-33
lines changed

.bazelrc

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
build --cxxopt=-std=c++17 --host_cxxopt=-std=c++17
22

3+
# Needed for java_lite_proto_library, that's using ProguardSpecProvider
4+
# Once the provider is ported to Starlark the flag may be removed.
5+
common --experimental_google_legacy_api
6+
37
build:dbg --compilation_mode=dbg
48

59
build:opt --compilation_mode=opt

MODULE.bazel

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ bazel_dep(
4040

4141
bazel_dep(
4242
name = "rules_java",
43-
version = "5.3.5",
43+
version = "7.11.1",
4444
)
4545

4646
bazel_dep(
4747
name = "rules_jvm_external",
48-
version = "6.0",
48+
version = "6.3",
4949
)
5050

5151
bazel_dep(
@@ -55,7 +55,7 @@ bazel_dep(
5555

5656
bazel_dep(
5757
name = "rules_license",
58-
version = "0.0.8",
58+
version = "1.0.0",
5959
)
6060

6161
bazel_dep(

bazel/java_lite_proto_library.bzl

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Copyright (c) 2009-2024, Google LLC
2+
# All rights reserved.
3+
#
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file or at
6+
# https://developers.google.com/open-source/licenses/bsd
17
"""java_lite_proto_library rule"""
28

3-
java_lite_proto_library = native.java_lite_proto_library
9+
load("@proto_bazel_features//:features.bzl", "bazel_features")
10+
load("//bazel/private:java_lite_proto_library.bzl", _java_lite_proto_library = "java_lite_proto_library") # buildifier: disable=bzl-visibility
11+
12+
def java_lite_proto_library(**kwattrs):
13+
# This condition causes Starlark rules to be used only on Bazel >=7.0.0
14+
if bazel_features.proto.starlark_proto_info:
15+
_java_lite_proto_library(**kwattrs)
16+
else:
17+
# On older Bazel versions keep using native rules, so that mismatch in ProtoInfo doesn't happen
18+
native.java_lite_proto_library(**kwattrs)

bazel/java_proto_library.bzl

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
# Copyright (c) 2009-2024, Google LLC
2+
# All rights reserved.
3+
#
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file or at
6+
# https://developers.google.com/open-source/licenses/bsd
17
"""java_proto_library rule"""
28

3-
java_proto_library = native.java_proto_library
9+
load("@proto_bazel_features//:features.bzl", "bazel_features")
10+
load("//bazel/private:bazel_java_proto_library_rule.bzl", _java_proto_library = "java_proto_library") # buildifier: disable=bzl-visibility
11+
12+
def java_proto_library(**kwattrs):
13+
# This condition causes Starlark rules to be used only on Bazel >=7.0.0
14+
if bazel_features.proto.starlark_proto_info:
15+
_java_proto_library(**kwattrs)
16+
else:
17+
# On older Bazel versions keep using native rules, so that mismatch in ProtoInfo doesn't happen
18+
native.java_proto_library(**kwattrs)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Copyright (c) 2009-2024, Google LLC
2+
# All rights reserved.
3+
#
4+
# Use of this source code is governed by a BSD-style
5+
# license that can be found in the LICENSE file or at
6+
# https://developers.google.com/open-source/licenses/bsd
7+
"""The implementation of the `java_proto_library` rule and its aspect."""
8+
9+
load("@rules_java//java/common:java_info.bzl", "JavaInfo")
10+
load("//bazel/common:proto_common.bzl", "proto_common")
11+
load("//bazel/common:proto_info.bzl", "ProtoInfo")
12+
load("//bazel/private:java_proto_support.bzl", "java_compile_for_protos", "java_info_merge_for_protos")
13+
load("//bazel/private:toolchain_helpers.bzl", "toolchains")
14+
15+
# TODO: replace with toolchain type located in protobuf
16+
_JAVA_PROTO_TOOLCHAIN = "@rules_java//java/proto:toolchain_type"
17+
18+
# The provider is used to collect source and runtime jars in the `proto_library` dependency graph.
19+
JavaProtoAspectInfo = provider("JavaProtoAspectInfo", fields = ["jars"])
20+
21+
def _filter_provider(provider, *attrs):
22+
return [dep[provider] for attr in attrs for dep in attr if provider in dep]
23+
24+
def _bazel_java_proto_aspect_impl(target, ctx):
25+
"""Generates and compiles Java code for a proto_library.
26+
27+
The function runs protobuf compiler on the `proto_library` target using
28+
`proto_lang_toolchain` specified by `--proto_toolchain_for_java` flag.
29+
This generates a source jar.
30+
31+
After that the source jar is compiled, respecting `deps` and `exports` of
32+
the `proto_library`.
33+
34+
Args:
35+
target: (Target) The `proto_library` target (any target providing `ProtoInfo`.
36+
ctx: (RuleContext) The rule context.
37+
38+
Returns:
39+
([JavaInfo, JavaProtoAspectInfo]) A JavaInfo describing compiled Java
40+
version of`proto_library` and `JavaProtoAspectInfo` with all source and
41+
runtime jars.
42+
"""
43+
44+
proto_toolchain_info = toolchains.find_toolchain(ctx, "_aspect_java_proto_toolchain", _JAVA_PROTO_TOOLCHAIN)
45+
source_jar = None
46+
if proto_common.experimental_should_generate_code(target[ProtoInfo], proto_toolchain_info, "java_proto_library", target.label):
47+
# Generate source jar using proto compiler.
48+
source_jar = ctx.actions.declare_file(ctx.label.name + "-speed-src.jar")
49+
proto_common.compile(
50+
ctx.actions,
51+
target[ProtoInfo],
52+
proto_toolchain_info,
53+
[source_jar],
54+
experimental_output_files = "single",
55+
)
56+
57+
# Compile Java sources (or just merge if there aren't any)
58+
deps = _filter_provider(JavaInfo, ctx.rule.attr.deps)
59+
exports = _filter_provider(JavaInfo, ctx.rule.attr.exports)
60+
if source_jar and proto_toolchain_info.runtime:
61+
deps.append(proto_toolchain_info.runtime[JavaInfo])
62+
java_info, jars = java_compile_for_protos(
63+
ctx,
64+
"-speed.jar",
65+
source_jar,
66+
deps,
67+
exports,
68+
)
69+
70+
transitive_jars = [dep[JavaProtoAspectInfo].jars for dep in ctx.rule.attr.deps if JavaProtoAspectInfo in dep]
71+
return [
72+
java_info,
73+
JavaProtoAspectInfo(jars = depset(jars, transitive = transitive_jars)),
74+
]
75+
76+
bazel_java_proto_aspect = aspect(
77+
implementation = _bazel_java_proto_aspect_impl,
78+
attrs = toolchains.if_legacy_toolchain({
79+
"_aspect_java_proto_toolchain": attr.label(
80+
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java"),
81+
),
82+
}),
83+
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"] + toolchains.use_toolchain(_JAVA_PROTO_TOOLCHAIN),
84+
attr_aspects = ["deps", "exports"],
85+
required_providers = [ProtoInfo],
86+
provides = [JavaInfo, JavaProtoAspectInfo],
87+
fragments = ["java"],
88+
)
89+
90+
def bazel_java_proto_library_rule(ctx):
91+
"""Merges results of `java_proto_aspect` in `deps`.
92+
93+
Args:
94+
ctx: (RuleContext) The rule context.
95+
Returns:
96+
([JavaInfo, DefaultInfo, OutputGroupInfo])
97+
"""
98+
proto_toolchain = toolchains.find_toolchain(ctx, "_aspect_java_proto_toolchain", _JAVA_PROTO_TOOLCHAIN)
99+
for dep in ctx.attr.deps:
100+
proto_common.check_collocated(ctx.label, dep[ProtoInfo], proto_toolchain)
101+
102+
java_info = java_info_merge_for_protos([dep[JavaInfo] for dep in ctx.attr.deps], merge_java_outputs = False)
103+
104+
transitive_src_and_runtime_jars = depset(transitive = [dep[JavaProtoAspectInfo].jars for dep in ctx.attr.deps])
105+
transitive_runtime_jars = depset(transitive = [java_info.transitive_runtime_jars])
106+
107+
return [
108+
java_info,
109+
DefaultInfo(
110+
files = transitive_src_and_runtime_jars,
111+
runfiles = ctx.runfiles(transitive_files = transitive_runtime_jars),
112+
),
113+
OutputGroupInfo(default = depset()),
114+
]
115+
116+
java_proto_library = rule(
117+
implementation = bazel_java_proto_library_rule,
118+
doc = """
119+
<p>
120+
<code>java_proto_library</code> generates Java code from <code>.proto</code> files.
121+
</p>
122+
123+
<p>
124+
<code>deps</code> must point to <a href="protocol-buffer.html#proto_library"><code>proto_library
125+
</code></a> rules.
126+
</p>
127+
128+
<p>
129+
Example:
130+
</p>
131+
132+
<pre class="code">
133+
<code class="lang-starlark">
134+
java_library(
135+
name = "lib",
136+
runtime_deps = [":foo_java_proto"],
137+
)
138+
139+
java_proto_library(
140+
name = "foo_java_proto",
141+
deps = [":foo_proto"],
142+
)
143+
144+
proto_library(
145+
name = "foo_proto",
146+
)
147+
</code>
148+
</pre>
149+
""",
150+
attrs = {
151+
"deps": attr.label_list(
152+
providers = [ProtoInfo],
153+
aspects = [bazel_java_proto_aspect],
154+
doc = """
155+
The list of <a href="protocol-buffer.html#proto_library"><code>proto_library</code></a>
156+
rules to generate Java code for.
157+
""",
158+
),
159+
# buildifier: disable=attr-license (calling attr.license())
160+
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
161+
"distribs": attr.string_list(),
162+
} | toolchains.if_legacy_toolchain({
163+
"_aspect_java_proto_toolchain": attr.label(
164+
default = configuration_field(fragment = "proto", name = "proto_toolchain_for_java"),
165+
),
166+
}), # buildifier: disable=attr-licenses (attribute called licenses)
167+
provides = [JavaInfo],
168+
toolchains = toolchains.use_toolchain(_JAVA_PROTO_TOOLCHAIN),
169+
)

0 commit comments

Comments
 (0)