11"""Build rule for java_grpc_library."""
22
3+ load ("@com_google_protobuf//bazel/common:proto_common.bzl" , "proto_common" )
34load ("@com_google_protobuf//bazel/common:proto_info.bzl" , "ProtoInfo" )
5+ load ("@com_google_protobuf//bazel/common:proto_lang_toolchain_info.bzl" , "ProtoLangToolchainInfo" )
46load ("@rules_java//java:defs.bzl" , "JavaInfo" , "JavaPluginInfo" , "java_common" )
57
6- _JavaRpcToolchainInfo = provider (
7- fields = [
8- "java_toolchain" ,
9- "java_plugins" ,
10- "plugin" ,
11- "plugin_arg" ,
12- "protoc" ,
13- "runtime" ,
14- ],
8+ # Building a java_grpc_library happens in two phases: protoc generates Java
9+ # sources, then those sources are compiled. `proto_lang_toolchain` configures
10+ # the first phase, but it is language-agnostic and so cannot express anything
11+ # about the second. This provider carries that Java-specific configuration.
12+ _JavaRpcJavaConfigInfo = provider (
13+ doc = "Java compilation settings for the sources protoc generates." ,
14+ fields = {
15+ "java_plugins" : "(list[Target]) JavaPluginInfo targets to run as " +
16+ "annotation processors over the generated sources." ,
17+ },
1518)
1619
17- def _java_rpc_toolchain_impl (ctx ):
18- return [
19- _JavaRpcToolchainInfo (
20- java_toolchain = ctx .attr ._java_toolchain ,
21- java_plugins = ctx .attr .java_plugins ,
22- plugin = ctx .attr .plugin ,
23- plugin_arg = ctx .attr .plugin_arg ,
24- protoc = ctx .attr ._protoc ,
25- runtime = ctx .attr .runtime ,
26- ),
27- platform_common .ToolchainInfo (), # Magic for b/78647825
28- ]
20+ def _java_rpc_java_config_impl (ctx ):
21+ return [_JavaRpcJavaConfigInfo (java_plugins = ctx .attr .java_plugins )]
2922
30- java_rpc_toolchain = rule (
23+ java_rpc_java_config = rule (
24+ doc = """Java compilation settings to accompany a `proto_lang_toolchain`.
25+
26+ A `java_grpc_library` runs protoc and then compiles the generated sources.
27+ `proto_lang_toolchain` describes the protoc invocation; this rule describes the
28+ Java compilation that follows it.""" ,
3129 attrs = {
32- # This attribute has a "magic" name recognized by the native DexArchiveAspect (b/78647825).
33- "runtime" : attr .label_list (
34- cfg = "target" ,
35- providers = [JavaInfo ],
36- ),
37- "plugin" : attr .label (
38- cfg = "exec" ,
39- executable = True ,
40- ),
41- "plugin_arg" : attr .string (),
42- "_protoc" : attr .label (
43- cfg = "exec" ,
44- default = Label ("@com_google_protobuf//:protoc" ),
45- executable = True ,
46- ),
4730 "java_plugins" : attr .label_list (
4831 default = [],
4932 providers = [JavaPluginInfo ],
50- ),
51- "_java_toolchain" : attr .label (
52- default = Label ("@bazel_tools//tools/jdk:current_java_toolchain" ),
33+ doc = "Annotation processors to run over the generated sources." ,
5334 ),
5435 },
55- provides = [
56- _JavaRpcToolchainInfo ,
57- platform_common .ToolchainInfo ,
58- ],
59- implementation = _java_rpc_toolchain_impl ,
36+ provides = [_JavaRpcJavaConfigInfo ],
37+ implementation = _java_rpc_java_config_impl ,
6038)
6139
62- # "repository" here is for Bazel builds that span multiple WORKSPACES.
63- def _path_ignoring_repository (f ):
64- # Bazel creates a _virtual_imports directory in case the .proto source files
65- # need to be accessed at a path that's different from their source path:
66- # https://github.com/bazelbuild/bazel/blob/0.27.1/src/main/java/com/google/devtools/build/lib/rules/proto/ProtoCommon.java#L289
67- #
68- # In that case, the import path of the .proto file is the path relative to
69- # the virtual imports directory of the rule in question.
70- virtual_imports = "/_virtual_imports/"
71- if virtual_imports in f .path :
72- return f .path .split (virtual_imports )[1 ].split ("/" , 1 )[1 ]
73- elif len (f .owner .workspace_root ) == 0 :
74- # |f| is in the main repository
75- return f .short_path
76- else :
77- # If |f| is a generated file, it will have "bazel-out/*/genfiles" prefix
78- # before "external/workspace", so we need to add the starting index of "external/workspace"
79- return f .path [f .path .find (f .owner .workspace_root ) + len (f .owner .workspace_root ) + 1 :]
80-
8140def _java_rpc_library_impl (ctx ):
8241 if len (ctx .attr .srcs ) != 1 :
8342 fail ("Exactly one src value supported" , "srcs" )
8443 if ctx .attr .srcs [0 ].label .package != ctx .label .package :
8544 print (("in srcs attribute of {0}: Proto source with label {1} should be in " +
8645 "same package as consuming rule" ).format (ctx .label , ctx .attr .srcs [0 ].label ))
8746
88- toolchain = ctx .attr ._toolchain [_JavaRpcToolchainInfo ]
89- srcs = ctx .attr .srcs [0 ][ProtoInfo ].direct_sources
90- descriptor_set_in = ctx .attr .srcs [0 ][ProtoInfo ].transitive_descriptor_sets
91-
47+ toolchain = ctx .attr ._toolchain [ProtoLangToolchainInfo ]
9248 srcjar = ctx .actions .declare_file ("%s-proto-gensrc.jar" % ctx .label .name )
9349
94- args = ctx .actions .args ()
95- args .add (toolchain .plugin [DefaultInfo ].files_to_run .executable , format = "--plugin=protoc-gen-rpc-plugin=%s" )
96- args .add ("--rpc-plugin_out={0}:{1}" .format (toolchain .plugin_arg , srcjar .path ))
97- args .add_joined ("--descriptor_set_in" , descriptor_set_in , join_with = ctx .configuration .host_path_separator )
98- args .add_all (srcs , map_each = _path_ignoring_repository )
99-
100- ctx .actions .run (
101- inputs = depset (srcs , transitive = [descriptor_set_in , toolchain .plugin [DefaultInfo ].files ]),
102- outputs = [srcjar ],
103- executable = toolchain .protoc [DefaultInfo ].files_to_run ,
104- arguments = [args ],
105- use_default_shell_env = True ,
106- toolchain = None ,
50+ proto_common .compile (
51+ actions = ctx .actions ,
52+ proto_info = ctx .attr .srcs [0 ][ProtoInfo ],
53+ proto_lang_toolchain_info = toolchain ,
54+ generated_files = [srcjar ],
10755 )
10856
10957 deps_java_info = java_common .merge ([dep [JavaInfo ] for dep in ctx .attr .deps ])
58+ runtime_deps = [toolchain .runtime [JavaInfo ]] if toolchain .runtime else []
11059
11160 java_info = java_common .compile (
11261 ctx ,
113- java_toolchain = toolchain . java_toolchain [ java_common . JavaToolchainInfo ] ,
62+ java_toolchain = ctx . toolchains [ "@bazel_tools//tools/jdk:toolchain_type" ]. java ,
11463 source_jars = [srcjar ],
11564 output = ctx .outputs .jar ,
11665 output_source_jar = ctx .outputs .srcjar ,
117- plugins = [plugin [JavaPluginInfo ] for plugin in toolchain .java_plugins ],
66+ plugins = [
67+ plugin [JavaPluginInfo ]
68+ for plugin in ctx .attr ._java_config [_JavaRpcJavaConfigInfo ].java_plugins
69+ ],
11870 deps = [
11971 java_common .make_non_strict (deps_java_info ),
120- ] + [ dep [ JavaInfo ] for dep in toolchain . runtime ] ,
72+ ] + runtime_deps ,
12173 )
12274
12375 return [java_info ]
@@ -136,6 +88,11 @@ _java_grpc_library = rule(
13688 ),
13789 "_toolchain" : attr .label (
13890 default = Label ("//compiler:java_grpc_library_toolchain" ),
91+ providers = [ProtoLangToolchainInfo ],
92+ ),
93+ "_java_config" : attr .label (
94+ default = Label ("//compiler:java_grpc_library_java_config" ),
95+ providers = [_JavaRpcJavaConfigInfo ],
13996 ),
14097 },
14198 toolchains = ["@bazel_tools//tools/jdk:toolchain_type" ],
@@ -163,6 +120,11 @@ INTERNAL_java_grpc_library_for_xds = rule(
163120 ),
164121 "_toolchain" : attr .label (
165122 default = Label ("//xds:java_grpc_library_toolchain" ),
123+ providers = [ProtoLangToolchainInfo ],
124+ ),
125+ "_java_config" : attr .label (
126+ default = Label ("//xds:java_grpc_library_java_config" ),
127+ providers = [_JavaRpcJavaConfigInfo ],
166128 ),
167129 },
168130 toolchains = ["@bazel_tools//tools/jdk:toolchain_type" ],
@@ -190,6 +152,11 @@ _java_lite_grpc_library = rule(
190152 # This attribute has a "magic" name recognized by the native DexArchiveAspect (b/78647825).
191153 "_toolchain" : attr .label (
192154 default = Label ("//compiler:java_lite_grpc_library_toolchain" ),
155+ providers = [ProtoLangToolchainInfo ],
156+ ),
157+ "_java_config" : attr .label (
158+ default = Label ("//compiler:java_lite_grpc_library_java_config" ),
159+ providers = [_JavaRpcJavaConfigInfo ],
193160 ),
194161 },
195162 toolchains = ["@bazel_tools//tools/jdk:toolchain_type" ],
0 commit comments