1 # Copyright 2015 gRPC authors.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
16 Bazel macros to declare gRPC libraries automatically generated from proto files.
18 This file declares two macros:
23 def _lower_underscore_to_upper_camel(str):
25 for hump in str.split('_'):
26 humps += [hump[0].upper() + hump[1:]]
29 def _file_to_upper_camel(src):
30 elements = src.rpartition('/')
31 upper_camel = _lower_underscore_to_upper_camel(elements[-1])
32 return "".join(elements[:-1] + [upper_camel])
34 def _file_with_extension(src, ext):
35 elements = src.rpartition('/')
36 basename = elements[-1].partition('.')[0]
37 return "".join(elements[:-1] + [basename, ext])
39 def _protoc_invocation(srcs, flags):
40 """Returns a command line to invoke protoc from a genrule, on the given
41 sources, using the given flags.
43 protoc_command = "$(location //external:protoc) -I . "
46 srcs_params += " $(location %s)" % (src)
47 return protoc_command + flags + srcs_params
49 def objc_proto_library(name, srcs, visibility=None):
50 """Declares an objc_library for the code generated by protoc from the given
51 proto sources. This generated code doesn't include proto services.
56 src = _file_to_upper_camel(src)
57 h_files += [_file_with_extension(src, ".pbobjc.h")]
58 m_files += [_file_with_extension(src, ".pbobjc.m")]
60 protoc_flags = "--objc_out=$(GENDIR)"
63 name = name + "_codegen",
64 srcs = srcs + ["//external:protoc"],
65 outs = h_files + m_files,
66 cmd = _protoc_invocation(srcs, protoc_flags),
72 non_arc_srcs = m_files,
73 deps = ["//external:protobuf_objc"],
74 visibility = visibility,
77 def objc_grpc_library(name, services, other_messages, visibility=None):
78 """Declares an objc_library for the code generated by gRPC and protoc from the
79 given proto sources (services and other_messages). The generated code doesn't
80 include proto services of the files passed as other_messages.
82 objc_proto_library(name + "_messages", services + other_messages)
87 src = _file_to_upper_camel(src)
88 h_files += [_file_with_extension(src, ".pbrpc.h")]
89 m_files += [_file_with_extension(src, ".pbrpc.m")]
91 protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" +
92 "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)")
95 name = name + "_codegen",
97 "//external:grpc_protoc_plugin_objc",
100 outs = h_files + m_files,
101 cmd = _protoc_invocation(services, protoc_flags),
109 ":" + name + "_messages",
110 "//external:proto_objc_rpc",
112 visibility = visibility,