blob: 9c271a83ee562b563a88f995baa0809bb4480373 [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
Brendan Linn131a0ff2017-09-21 07:18:55 -07004 return f.path[f.path.find(f.owner.workspace_root)+len(f.owner.workspace_root)+1:]
Simon Horlick6d9e1492017-06-23 04:06:49 +08005
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]
Eric Anderson39365572017-11-06 17:11:34 -080031 + [_path_ignoring_repository(src) for src in srcs])
Simon Horlick6d9e1492017-06-23 04:06:49 +080032 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",
Eric Anderson39365572017-11-06 17:11:34 -080048 "lite", # Not currently supported
Simon Horlick6d9e1492017-06-23 04:06:49 +080049 ],
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(
Carmi Grushko137c74d2018-02-17 13:42:46 -050061 default = Label("//compiler:grpc_java_plugin"),
Simon Horlick6d9e1492017-06-23 04:06:49 +080062 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 = [
Eric Andersonef030ab2018-03-30 21:32:15 -0700110 "@io_grpc_grpc_java//core",
111 "@io_grpc_grpc_java//stub",
David Ostrovsky23fcedf2018-04-26 23:37:57 +0200112 "@io_grpc_grpc_java//stub:javax_annotation",
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":
Eric Andersonbace06f2018-04-03 15:22:55 -0700116 added_deps += [
117 "@com_google_protobuf//:protobuf_java",
118 "@io_grpc_grpc_java//protobuf",
119 ]
Simon Horlick6d9e1492017-06-23 04:06:49 +0800120 elif flavor == "lite":
Eric Andersonbace06f2018-04-03 15:22:55 -0700121 added_deps += ["@io_grpc_grpc_java//protobuf-lite"]
Simon Horlick6d9e1492017-06-23 04:06:49 +0800122 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 )