blob: c227e6c920ac50cebb7a106448e90ebaad9cda21 [file] [log] [blame]
Simon Horlick6d9e1492017-06-23 04:06:49 +08001def _path_ignoring_repository(f):
2 if (len(f.owner.workspace_root) == 0):
3 return f.short_path
4 return f.path[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 + [src.short_path 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("@grpc_java//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 "@grpc_java//core",
111 "@grpc_java//stub",
112 "@grpc_java//protobuf",
Paul Gross72b9ee22017-08-11 16:43:08 +0000113 "@com_google_guava_guava//jar",
Simon Horlick6d9e1492017-06-23 04:06:49 +0800114 ]
115 if flavor == "normal":
116 added_deps += ["@com_google_protobuf_java//:protobuf_java"]
117 elif flavor == "lite":
118 # TODO: This is currently blocked on https://github.com/google/protobuf/issues/2762
119 added_deps += ["@com_google_protobuf_java_lite//:protobuf_java_lite"]
120 else:
121 fail("Unknown flavor type", "flavor")
122
123 native.java_library(
124 name = name,
125 srcs = [gensource_name],
126 visibility = visibility,
127 deps = [
128 "@com_google_code_findbugs_jsr305//jar",
129 ] + deps + added_deps,
130 **kwargs
131 )