| 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 |
""" |
| 16 |
Bazel macros to declare gRPC libraries automatically generated from proto files. |
| 17 |
|
| 18 |
This file declares two macros: |
| 19 |
- objc_proto_library |
| 20 |
- objc_grpc_library |
| 21 |
""" |
| 22 |
|
| 23 |
def _lower_underscore_to_upper_camel(str): |
| 24 |
humps = [] |
| 25 |
for hump in str.split("_"): |
| 26 |
humps.append(hump[0].upper() + hump[1:]) |
| 27 |
return "".join(humps) |
| 28 |
|
| 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]) |
| 33 |
|
| 34 |
def _file_with_extension(src, ext): |
| 35 |
elements = src.rpartition("/") |
| 36 |
basename = elements[-1].partition(".")[0] |
| 37 |
return "".join(elements[:-1] + [basename, ext]) |
| 38 |
|
| 39 |
def _protoc_invocation(srcs, flags): |
| 40 |
"""Returns a CLI command to invoke protoc. |
| 41 |
|
| 42 |
Uses the given sources and flags. Suitable for use in a genrule. |
| 43 |
""" |
| 44 |
protoc_command = "$(location //external:protoc) -I . " |
| 45 |
srcs_params = "" |
| 46 |
for src in srcs: |
| 47 |
srcs_params += " $(location %s)" % (src) |
| 48 |
return protoc_command + flags + srcs_params |
| 49 |
|
| 50 |
def objc_proto_library(name, srcs, visibility = None): |
| 51 |
"""Declares an objc_library for the code generated by protoc. |
| 52 |
|
| 53 |
Uses the given proto sources. This generated code doesn't include proto |
| 54 |
services. |
| 55 |
|
| 56 |
Args: |
| 57 |
name: The name of the library. |
| 58 |
srcs: A list of .proto file sources. |
| 59 |
visibility: The visibility label to apply to the target. |
| 60 |
""" |
| 61 |
h_files = [] |
| 62 |
m_files = [] |
| 63 |
for src in srcs: |
| 64 |
src = _file_to_upper_camel(src) |
| 65 |
h_files.append(_file_with_extension(src, ".pbobjc.h")) |
| 66 |
m_files.append(_file_with_extension(src, ".pbobjc.m")) |
| 67 |
|
| 68 |
protoc_flags = "--objc_out=$(GENDIR)" |
| 69 |
|
| 70 |
native.genrule( |
| 71 |
name = name + "_codegen", |
| 72 |
srcs = srcs + ["//external:protoc"], |
| 73 |
outs = h_files + m_files, |
| 74 |
cmd = _protoc_invocation(srcs, protoc_flags), |
| 75 |
) |
| 76 |
native.objc_library( |
| 77 |
name = name, |
| 78 |
hdrs = h_files, |
| 79 |
includes = ["."], |
| 80 |
non_arc_srcs = m_files, |
| 81 |
deps = ["//external:protobuf_objc"], |
| 82 |
visibility = visibility, |
| 83 |
) |
| 84 |
|
| 85 |
def objc_grpc_library(name, services, other_messages, visibility = None): |
| 86 |
"""Declares an objc_library for the code generated by the gRPC ObjC plugin. |
| 87 |
|
| 88 |
The generated code does not include the services of the files in other_messages. |
| 89 |
|
| 90 |
Args: |
| 91 |
name: The name of the library. |
| 92 |
services: The .proto files from which to generate the library. |
| 93 |
other_messages: A list of .proto files containing messages needed for the library. |
| 94 |
visibility: The visibility label to apply to the library. |
| 95 |
""" |
| 96 |
objc_proto_library(name + "_messages", services + other_messages) |
| 97 |
|
| 98 |
h_files = [] |
| 99 |
m_files = [] |
| 100 |
for src in services: |
| 101 |
src = _file_to_upper_camel(src) |
| 102 |
h_files.append(_file_with_extension(src, ".pbrpc.h")) |
| 103 |
m_files.append(_file_with_extension(src, ".pbrpc.m")) |
| 104 |
|
| 105 |
protoc_flags = ("--grpc_out=$(GENDIR) --plugin=" + |
| 106 |
"protoc-gen-grpc=$(location //external:grpc_protoc_plugin_objc)") |
| 107 |
|
| 108 |
native.genrule( |
| 109 |
name = name + "_codegen", |
| 110 |
srcs = services + [ |
| 111 |
"//external:grpc_protoc_plugin_objc", |
| 112 |
"//external:protoc", |
| 113 |
], |
| 114 |
outs = h_files + m_files, |
| 115 |
cmd = _protoc_invocation(services, protoc_flags), |
| 116 |
) |
| 117 |
native.objc_library( |
| 118 |
name = name, |
| 119 |
hdrs = h_files, |
| 120 |
includes = ["."], |
| 121 |
srcs = m_files, |
| 122 |
deps = [ |
| 123 |
":" + name + "_messages", |
| 124 |
"//external:proto_objc_rpc", |
| 125 |
], |
| 126 |
visibility = visibility, |
| 127 |
) |