• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1def _path_ignoring_repository(f):
2  if (len(f.owner.workspace_root) == 0):
3    return f.short_path
4  return f.path[f.path.find(f.owner.workspace_root)+len(f.owner.workspace_root)+1:]
5
6def _gensource_impl(ctx):
7  if len(ctx.attr.srcs) > 1:
8    fail("Only one src value supported", "srcs")
9  for s in ctx.attr.srcs:
10    if s.label.package != ctx.label.package:
11      print(("in srcs attribute of {0}: Proto source with label {1} should be in "
12             + "same package as consuming rule").format(ctx.label, s.label))
13  # Use .jar since .srcjar makes protoc think output will be a directory
14  srcdotjar = ctx.new_file(ctx.label.name + "_src.jar")
15
16  srcs = [f for dep in ctx.attr.srcs for f in dep.proto.direct_sources]
17  includes = [f for dep in ctx.attr.srcs for f in dep.proto.transitive_imports]
18
19  flavor = ctx.attr.flavor
20  if flavor == "normal":
21    flavor = ""
22  ctx.action(
23      inputs = [ctx.executable._java_plugin] + srcs + includes,
24      outputs = [srcdotjar],
25      executable = ctx.executable._protoc,
26      arguments = [
27          "--plugin=protoc-gen-grpc-java=" + ctx.executable._java_plugin.path,
28          "--grpc-java_out={0},enable_deprecated={1}:{2}"
29            .format(flavor, str(ctx.attr.enable_deprecated).lower(), srcdotjar.path)]
30          + ["-I{0}={1}".format(_path_ignoring_repository(include), include.path) for include in includes]
31          + [_path_ignoring_repository(src) for src in srcs])
32  ctx.action(
33      command = "cp $1 $2",
34      inputs = [srcdotjar],
35      outputs = [ctx.outputs.srcjar],
36      arguments = [srcdotjar.path, ctx.outputs.srcjar.path])
37
38_gensource = rule(
39    attrs = {
40        "srcs": attr.label_list(
41            mandatory = True,
42            non_empty = True,
43            providers = ["proto"],
44        ),
45        "flavor": attr.string(
46            values = [
47                "normal",
48                "lite",  # Not currently supported
49            ],
50            default = "normal",
51        ),
52        "enable_deprecated": attr.bool(
53            default = False,
54        ),
55        "_protoc": attr.label(
56            default = Label("@com_google_protobuf//:protoc"),
57            executable = True,
58            cfg = "host",
59        ),
60        "_java_plugin": attr.label(
61            default = Label("//compiler:grpc_java_plugin"),
62            executable = True,
63            cfg = "host",
64        ),
65    },
66    outputs = {
67        "srcjar": "%{name}.srcjar",
68    },
69    implementation = _gensource_impl,
70)
71
72def java_grpc_library(name, srcs, deps, flavor=None,
73                      enable_deprecated=None, visibility=None,
74                      **kwargs):
75  """Generates and compiles gRPC Java sources for services defined in a proto
76  file. This rule is compatible with java_proto_library and java_lite_proto_library.
77
78  Do note that this rule only scans through the proto file for RPC services. It
79  does not generate Java classes for proto messages. You will need a separate
80  java_proto_library or java_lite_proto_library rule.
81
82  Args:
83    name: (str) A unique name for this rule. Required.
84    srcs: (list) a single proto_library target that contains the schema of the
85        service. Required.
86    deps: (list) a single java_proto_library target for the proto_library in
87        srcs.  Required.
88    flavor: (str) "normal" (default) for normal proto runtime. "lite"
89        for the lite runtime.
90    visibility: (list) the visibility list
91    **kwargs: Passed through to generated targets
92  """
93  if flavor == None:
94    flavor = "normal"
95
96  if len(deps) > 1:
97    print("Multiple values in 'deps' is deprecated in " + name)
98
99  gensource_name = name + "__do_not_reference__srcjar"
100  _gensource(
101      name = gensource_name,
102      srcs = srcs,
103      flavor = flavor,
104      enable_deprecated = enable_deprecated,
105      visibility = ["//visibility:private"],
106      **kwargs
107  )
108
109  added_deps = [
110      "@io_grpc_grpc_java//core",
111      "@io_grpc_grpc_java//stub",
112      "@io_grpc_grpc_java//stub:javax_annotation",
113      "@com_google_guava_guava//jar",
114  ]
115  if flavor == "normal":
116    added_deps += [
117        "@com_google_protobuf//:protobuf_java",
118        "@io_grpc_grpc_java//protobuf",
119    ]
120  elif flavor == "lite":
121    added_deps += ["@io_grpc_grpc_java//protobuf-lite"]
122  else:
123    fail("Unknown flavor type", "flavor")
124
125  native.java_library(
126      name = name,
127      srcs = [gensource_name],
128      visibility = visibility,
129      deps = [
130          "@com_google_code_findbugs_jsr305//jar",
131      ] + deps + added_deps,
132      **kwargs
133  )
134