• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 gRPC authors.
2#
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
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
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.
14
15"""
16Bazel macros to declare gRPC libraries automatically generated from proto files.
17
18This file declares two macros:
19- objc_proto_library
20- objc_grpc_library
21"""
22
23def _lower_underscore_to_upper_camel(str):
24    humps = []
25    for hump in str.split("_"):
26        humps += [hump[0].upper() + hump[1:]]
27    return "".join(humps)
28
29def _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])
33
34def _file_with_extension(src, ext):
35    elements = src.rpartition("/")
36    basename = elements[-1].partition(".")[0]
37    return "".join(elements[:-1] + [basename, ext])
38
39def _protoc_invocation(srcs, flags):
40    """Returns a command line to invoke protoc from a genrule, on the given
41    sources, using the given flags.
42    """
43    protoc_command = "$(location //external:protoc) -I . "
44    srcs_params = ""
45    for src in srcs:
46        srcs_params += " $(location %s)" % (src)
47    return protoc_command + flags + srcs_params
48
49def 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.
52    """
53    h_files = []
54    m_files = []
55    for src in srcs:
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")]
59
60    protoc_flags = "--objc_out=$(GENDIR)"
61
62    native.genrule(
63        name = name + "_codegen",
64        srcs = srcs + ["//external:protoc"],
65        outs = h_files + m_files,
66        cmd = _protoc_invocation(srcs, protoc_flags),
67    )
68    native.objc_library(
69        name = name,
70        hdrs = h_files,
71        includes = ["."],
72        non_arc_srcs = m_files,
73        deps = ["//external:protobuf_objc"],
74        visibility = visibility,
75    )
76
77def 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.
81    """
82    objc_proto_library(name + "_messages", services + other_messages)
83
84    h_files = []
85    m_files = []
86    for src in services:
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")]
90
91    protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" +
92                    "protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)")
93
94    native.genrule(
95        name = name + "_codegen",
96        srcs = services + [
97            "//external:grpc_protoc_plugin_objc",
98            "//external:protoc",
99        ],
100        outs = h_files + m_files,
101        cmd = _protoc_invocation(services, protoc_flags),
102    )
103    native.objc_library(
104        name = name,
105        hdrs = h_files,
106        includes = ["."],
107        srcs = m_files,
108        deps = [
109            ":" + name + "_messages",
110            "//external:proto_objc_rpc",
111        ],
112        visibility = visibility,
113    )
114